blob: 4474458f1f9fe4c66240301dd64933c5258a0bfc [file] [log] [blame]
[email protected]ce5c4502009-05-06 16:46:111// Copyright (c) 2009 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]f3ec7742009-01-15 00:59:165#include "chrome/browser/tab_contents/tab_contents.h"
initial.commit09911bf2008-07-26 23:55:296
[email protected]beb440c2009-11-06 04:08:547#include "app/gfx/text_elider.h"
[email protected]a92b8642009-05-05 23:38:568#include "app/l10n_util.h"
[email protected]9929da92009-05-05 02:05:119#include "app/resource_bundle.h"
[email protected]420ae012009-04-24 05:16:3210#include "base/file_version_info.h"
11#include "base/process_util.h"
[email protected]96d185d2009-04-24 03:28:5412#include "base/string16.h"
[email protected]996fd702009-09-04 19:12:3713#include "base/string_util.h"
[email protected]96d185d2009-04-24 03:28:5414#include "base/time.h"
[email protected]21674df2009-10-28 19:51:2115#include "chrome/browser/autofill/autofill_manager.h"
[email protected]66ba4932009-06-04 19:22:1316#include "chrome/browser/blocked_popup_container.h"
[email protected]420ae012009-04-24 05:16:3217#include "chrome/browser/bookmarks/bookmark_model.h"
[email protected]96d185d2009-04-24 03:28:5418#include "chrome/browser/browser_process.h"
[email protected]cf68b092010-01-15 20:05:5519#include "chrome/browser/browser_shutdown.h"
initial.commit09911bf2008-07-26 23:55:2920#include "chrome/browser/cert_store.h"
[email protected]aacd7cc2009-09-02 14:51:1021#include "chrome/browser/character_encoding.h"
[email protected]96d185d2009-04-24 03:28:5422#include "chrome/browser/debugger/devtools_manager.h"
[email protected]420ae012009-04-24 05:16:3223#include "chrome/browser/dom_operation_notification_details.h"
[email protected]96d185d2009-04-24 03:28:5424#include "chrome/browser/dom_ui/dom_ui.h"
25#include "chrome/browser/dom_ui/dom_ui_factory.h"
[email protected]bcef0dc02009-02-28 00:35:0226#include "chrome/browser/download/download_item_model.h"
[email protected]420ae012009-04-24 05:16:3227#include "chrome/browser/download/download_manager.h"
28#include "chrome/browser/download/download_request_manager.h"
[email protected]e7eaedde2009-09-25 20:09:4929#include "chrome/browser/external_protocol_handler.h"
[email protected]92c6f9b92009-10-24 04:35:0830#include "chrome/browser/extensions/extensions_service.h"
[email protected]0189bc722009-08-28 21:56:4831#include "chrome/browser/favicon_service.h"
[email protected]19289d02009-10-21 21:47:0032#include "chrome/browser/form_field_history_manager.h"
[email protected]420ae012009-04-24 05:16:3233#include "chrome/browser/google_util.h"
[email protected]8897c382009-05-06 17:53:2634#include "chrome/browser/hung_renderer_dialog.h"
[email protected]420ae012009-04-24 05:16:3235#include "chrome/browser/jsmessage_box_handler.h"
36#include "chrome/browser/load_from_memory_cache_details.h"
37#include "chrome/browser/load_notification_details.h"
[email protected]84abba62009-10-07 17:01:4438#include "chrome/browser/metrics/metric_event_duration_details.h"
[email protected]ea0e80892009-08-22 00:36:0539#include "chrome/browser/modal_html_dialog_delegate.h"
[email protected]c8865482009-07-23 20:40:1040#include "chrome/browser/omnibox_search_hint.h"
[email protected]fdd61c62009-04-22 19:22:5741#include "chrome/browser/password_manager/password_manager.h"
42#include "chrome/browser/plugin_installer.h"
[email protected]ce560f82009-06-03 09:39:4443#include "chrome/browser/profile.h"
[email protected]1db6ff152009-10-12 15:32:0744#include "chrome/browser/renderer_host/render_process_host.h"
[email protected]96d185d2009-04-24 03:28:5445#include "chrome/browser/renderer_host/render_widget_host_view.h"
[email protected]fd694982009-07-03 17:32:3746#include "chrome/browser/renderer_host/resource_request_details.h"
[email protected]1db6ff152009-10-12 15:32:0747#include "chrome/browser/renderer_host/site_instance.h"
[email protected]96d185d2009-04-24 03:28:5448#include "chrome/browser/renderer_host/web_cache_manager.h"
[email protected]93623c5d2009-12-10 21:40:3249#include "chrome/browser/renderer_preferences_util.h"
[email protected]bd1ad682009-05-15 22:19:1750#include "chrome/browser/tab_contents/infobar_delegate.h"
[email protected]7e383692009-06-12 19:14:5451#include "chrome/browser/tab_contents/interstitial_page.h"
[email protected]f3ec7742009-01-15 00:59:1652#include "chrome/browser/tab_contents/navigation_entry.h"
[email protected]de34a962009-06-26 23:22:4353#include "chrome/browser/tab_contents/provisional_load_details.h"
[email protected]f3ec7742009-01-15 00:59:1654#include "chrome/browser/tab_contents/tab_contents_delegate.h"
[email protected]fdd61c62009-04-22 19:22:5755#include "chrome/browser/tab_contents/tab_contents_view.h"
[email protected]58dca552009-06-17 00:35:0256#include "chrome/browser/tab_contents/thumbnail_generator.h"
[email protected]449478302009-06-09 20:04:2857#include "chrome/browser/thumbnail_store.h"
[email protected]420ae012009-04-24 05:16:3258#include "chrome/browser/search_engines/template_url_fetcher.h"
59#include "chrome/browser/search_engines/template_url_model.h"
[email protected]35f7d212009-04-29 21:19:2760#include "chrome/common/chrome_switches.h"
[email protected]5d246db22009-10-27 06:17:5761#include "chrome/common/extensions/extension_action.h"
[email protected]bfd04a62009-02-01 18:16:5662#include "chrome/common/notification_service.h"
[email protected]89d6e6e2009-12-03 23:06:5163#include "chrome/common/platform_util.h"
initial.commit09911bf2008-07-26 23:55:2964#include "chrome/common/pref_names.h"
[email protected]1eb89e82008-08-15 12:27:0365#include "chrome/common/pref_service.h"
[email protected]420ae012009-04-24 05:16:3266#include "chrome/common/render_messages.h"
[email protected]1ee614862009-07-06 20:29:1867#include "chrome/common/renderer_preferences.h"
[email protected]96d185d2009-04-24 03:28:5468#include "chrome/common/url_constants.h"
[email protected]8897c382009-05-06 17:53:2669#include "grit/generated_resources.h"
[email protected]420ae012009-04-24 05:16:3270#include "grit/locale_settings.h"
71#include "net/base/mime_util.h"
72#include "net/base/net_errors.h"
[email protected]d686e812009-06-03 19:10:2973#include "net/base/net_util.h"
[email protected]420ae012009-04-24 05:16:3274#include "net/base/registry_controlled_domain.h"
[email protected]1ee614862009-07-06 20:29:1875#include "webkit/glue/password_form.h"
76#include "webkit/glue/webpreferences.h"
[email protected]3e45ba92009-02-20 21:09:0077
[email protected]304f8792009-08-19 21:28:0978#if defined(OS_CHROMEOS)
79// For GdkScreen
80#include <gdk/gdk.h>
[email protected]0ba5c5e2009-08-20 16:47:1381#endif // defined(OS_CHROMEOS)
82
[email protected]420ae012009-04-24 05:16:3283// Cross-Site Navigations
84//
85// If a TabContents is told to navigate to a different web site (as determined
86// by SiteInstance), it will replace its current RenderViewHost with a new
87// RenderViewHost dedicated to the new SiteInstance. This works as follows:
88//
89// - Navigate determines whether the destination is cross-site, and if so,
90// it creates a pending_render_view_host_ and moves into the PENDING
91// RendererState.
92// - The pending RVH is "suspended," so that no navigation messages are sent to
93// its renderer until the onbeforeunload JavaScript handler has a chance to
94// run in the current RVH.
95// - The pending RVH tells CrossSiteRequestManager (a thread-safe singleton)
96// that it has a pending cross-site request. ResourceDispatcherHost will
97// check for this when the response arrives.
98// - The current RVH runs its onbeforeunload handler. If it returns false, we
99// cancel all the pending logic and go back to NORMAL. Otherwise we allow
100// the pending RVH to send the navigation request to its renderer.
101// - ResourceDispatcherHost receives a ResourceRequest on the IO thread. It
102// checks CrossSiteRequestManager to see that the RVH responsible has a
103// pending cross-site request, and then installs a CrossSiteEventHandler.
104// - When RDH receives a response, the BufferedEventHandler determines whether
105// it is a download. If so, it sends a message to the new renderer causing
106// it to cancel the request, and the download proceeds in the download
107// thread. For now, we stay in a PENDING state (with a pending RVH) until
[email protected]57c6a652009-05-04 07:58:34108// the next DidNavigate event for this TabContents. This isn't ideal, but it
[email protected]420ae012009-04-24 05:16:32109// doesn't affect any functionality.
110// - After RDH receives a response and determines that it is safe and not a
111// download, it pauses the response to first run the old page's onunload
112// handler. It does this by asynchronously calling the OnCrossSiteResponse
[email protected]57c6a652009-05-04 07:58:34113// method of TabContents on the UI thread, which sends a ClosePage message
[email protected]420ae012009-04-24 05:16:32114// to the current RVH.
115// - Once the onunload handler is finished, a ClosePage_ACK message is sent to
116// the ResourceDispatcherHost, who unpauses the response. Data is then sent
117// to the pending RVH.
118// - The pending renderer sends a FrameNavigate message that invokes the
119// DidNavigate method. This replaces the current RVH with the
120// pending RVH and goes back to the NORMAL RendererState.
121
122namespace {
123
124// Amount of time we wait between when a key event is received and the renderer
125// is queried for its state and pushed to the NavigationEntry.
126const int kQueryStateDelay = 5000;
127
128const int kSyncWaitDelay = 40;
129
[email protected]304f8792009-08-19 21:28:09130#if defined(OS_CHROMEOS)
131// If a popup window is bigger than this fraction of the screen on chrome os,
132// turn it into a tab
133const float kMaxWidthFactor = 0.5;
134const float kMaxHeightFactor = 0.6;
135#endif
136
[email protected]420ae012009-04-24 05:16:32137// If another javascript message box is displayed within
138// kJavascriptMessageExpectedDelay of a previous javascript message box being
139// dismissed, display an option to suppress future message boxes from this
140// contents.
141const int kJavascriptMessageExpectedDelay = 1000;
142
[email protected]420ae012009-04-24 05:16:32143// The list of prefs we want to observe.
144const wchar_t* kPrefsToObserve[] = {
145 prefs::kAlternateErrorPagesEnabled,
146 prefs::kWebKitJavaEnabled,
147 prefs::kWebKitJavascriptEnabled,
148 prefs::kWebKitLoadsImagesAutomatically,
149 prefs::kWebKitPluginsEnabled,
150 prefs::kWebKitUsesUniversalDetector,
151 prefs::kWebKitSerifFontFamily,
152 prefs::kWebKitSansSerifFontFamily,
153 prefs::kWebKitFixedFontFamily,
154 prefs::kWebKitDefaultFontSize,
155 prefs::kWebKitDefaultFixedFontSize,
156 prefs::kDefaultCharset
157 // kWebKitStandardFontIsSerif needs to be added
158 // if we let users pick which font to use, serif or sans-serif when
159 // no font is specified or a CSS generic family (serif or sans-serif)
160 // is not specified.
161};
162
163const int kPrefsToObserveLength = arraysize(kPrefsToObserve);
164
[email protected]420ae012009-04-24 05:16:32165// Returns true if the entry's transition type is FORM_SUBMIT.
166bool IsFormSubmit(const NavigationEntry* entry) {
167 return (PageTransition::StripQualifier(entry->transition_type()) ==
168 PageTransition::FORM_SUBMIT);
169}
170
171#if defined(OS_WIN)
172
173BOOL CALLBACK InvalidateWindow(HWND hwnd, LPARAM lparam) {
174 // Note: erase is required to properly paint some widgets borders. This can
175 // be seen with textfields.
176 InvalidateRect(hwnd, NULL, TRUE);
177 return TRUE;
178}
179#endif
180
[email protected]5e369672009-11-03 23:48:30181ViewMsg_Navigate_Params::NavigationType GetNavigationType(
182 Profile* profile, const NavigationEntry& entry, bool reload) {
183 if (reload)
184 return ViewMsg_Navigate_Params::RELOAD;
185
186 if (entry.restore_type() == NavigationEntry::RESTORE_LAST_SESSION &&
187 profile->DidLastSessionExitCleanly())
188 return ViewMsg_Navigate_Params::RESTORE;
189
190 return ViewMsg_Navigate_Params::NORMAL;
191}
192
193void MakeNavigateParams(Profile* profile, const NavigationEntry& entry,
194 bool reload, ViewMsg_Navigate_Params* params) {
[email protected]056de2d2009-06-26 16:41:34195 params->page_id = entry.page_id();
196 params->url = entry.url();
197 params->referrer = entry.referrer();
198 params->transition = entry.transition_type();
199 params->state = entry.content_state();
[email protected]5e369672009-11-03 23:48:30200 params->navigation_type = GetNavigationType(profile, entry, reload);
[email protected]056de2d2009-06-26 16:41:34201 params->request_time = base::Time::Now();
202}
203
[email protected]420ae012009-04-24 05:16:32204} // namespace
205
206// -----------------------------------------------------------------------------
207
[email protected]fdd61c62009-04-22 19:22:57208// static
209int TabContents::find_request_id_counter_ = -1;
210
[email protected]420ae012009-04-24 05:16:32211
[email protected]420ae012009-04-24 05:16:32212TabContents::TabContents(Profile* profile,
213 SiteInstance* site_instance,
214 int routing_id,
[email protected]34ac70502009-09-25 17:07:23215 const TabContents* base_tab_contents)
[email protected]b680ad22009-04-15 23:19:42216 : delegate_(NULL),
[email protected]66ba4932009-06-04 19:22:13217 ALLOW_THIS_IN_INITIALIZER_LIST(controller_(this, profile)),
218 ALLOW_THIS_IN_INITIALIZER_LIST(view_(
[email protected]d82ed61e2009-06-16 02:46:22219 TabContentsView::Create(this))),
220 ALLOW_THIS_IN_INITIALIZER_LIST(render_manager_(this, this)),
[email protected]fdd61c62009-04-22 19:22:57221 property_bag_(),
222 registrar_(),
[email protected]d82ed61e2009-06-16 02:46:22223 ALLOW_THIS_IN_INITIALIZER_LIST(printing_(*this)),
[email protected]fdd61c62009-04-22 19:22:57224 save_package_(),
[email protected]19289d02009-10-21 21:47:00225 form_field_history_manager_(),
[email protected]21674df2009-10-28 19:51:21226 autofill_manager_(),
[email protected]fdd61c62009-04-22 19:22:57227 password_manager_(),
228 plugin_installer_(),
[email protected]d82ed61e2009-06-16 02:46:22229 ALLOW_THIS_IN_INITIALIZER_LIST(fav_icon_helper_(this)),
[email protected]fdd61c62009-04-22 19:22:57230 select_file_dialog_(),
[email protected]d5f942ba2008-09-26 19:30:34231 is_loading_(false),
initial.commit09911bf2008-07-26 23:55:29232 is_crashed_(false),
[email protected]d5f942ba2008-09-26 19:30:34233 waiting_for_response_(false),
[email protected]d5f942ba2008-09-26 19:30:34234 max_page_id_(-1),
[email protected]fdd61c62009-04-22 19:22:57235 current_load_start_(),
236 load_state_(net::LOAD_STATE_IDLE),
237 load_state_host_(),
[email protected]094e5b22009-09-25 04:23:56238 upload_size_(0),
239 upload_position_(0),
[email protected]fdd61c62009-04-22 19:22:57240 received_page_title_(false),
241 is_starred_(false),
242 contents_mime_type_(),
243 encoding_(),
[email protected]332af7732009-03-11 13:21:35244 blocked_popups_(NULL),
[email protected]fdd61c62009-04-22 19:22:57245 infobar_delegates_(),
[email protected]fdd61c62009-04-22 19:22:57246 find_ui_active_(false),
247 find_op_aborted_(false),
248 current_find_request_id_(find_request_id_counter_++),
[email protected]e491f1c2009-05-22 20:28:12249 last_search_case_sensitive_(false),
250 last_search_prepopulate_text_(NULL),
251 last_search_result_(),
[email protected]fdd61c62009-04-22 19:22:57252 capturing_contents_(false),
253 is_being_destroyed_(false),
254 notify_disconnection_(false),
255 history_requests_(),
256#if defined(OS_WIN)
257 message_box_active_(CreateEvent(NULL, TRUE, FALSE, NULL)),
258#endif
259 last_javascript_message_dismissal_(),
[email protected]c2e74fe82009-09-03 17:57:44260 suppress_javascript_messages_(false),
[email protected]7ab1e7d62009-10-14 23:32:01261 is_showing_before_unload_dialog_(false),
[email protected]7ff431e2010-01-07 18:18:54262 renderer_preferences_(),
[email protected]c2e74fe82009-09-03 17:57:44263 opener_dom_ui_type_(DOMUIFactory::kNoDOMUI) {
[email protected]7ff431e2010-01-07 18:18:54264 renderer_preferences_util::UpdateFromSystemSettings(
265 &renderer_preferences_, profile);
266
[email protected]16779842009-07-08 23:45:29267#if defined(OS_CHROMEOS)
[email protected]58dca552009-06-17 00:35:02268 // Make sure the thumbnailer is started before starting the render manager.
269 // The thumbnailer will want to listen for RVH creations, one of which will
270 // happen in RVHManager::Init.
271 ThumbnailGenerator* generator = g_browser_process->GetThumbnailGenerator();
272 if (generator)
273 generator->StartThumbnailing();
274#endif
275
[email protected]12636df2009-09-28 22:32:21276 render_manager_.Init(profile, site_instance, routing_id);
[email protected]420ae012009-04-24 05:16:32277
[email protected]34ac70502009-09-25 17:07:23278 // We have the initial size of the view be based on the size of the passed in
279 // tab contents (normally a tab from the same window).
280 view_->CreateView(base_tab_contents ?
281 base_tab_contents->view()->GetContainerSize() : gfx::Size());
[email protected]420ae012009-04-24 05:16:32282
283 // Register for notifications about all interested prefs change.
284 PrefService* prefs = profile->GetPrefs();
285 if (prefs) {
286 for (int i = 0; i < kPrefsToObserveLength; ++i)
287 prefs->AddPrefObserver(kPrefsToObserve[i], this);
288 }
289
290 // Register for notifications about URL starredness changing on any profile.
291 registrar_.Add(this, NotificationType::URLS_STARRED,
292 NotificationService::AllSources());
293 registrar_.Add(this, NotificationType::BOOKMARK_MODEL_LOADED,
294 NotificationService::AllSources());
295 registrar_.Add(this, NotificationType::RENDER_WIDGET_HOST_DESTROYED,
296 NotificationService::AllSources());
[email protected]d3ba77272009-09-03 00:06:09297#if defined(OS_LINUX)
298 registrar_.Add(this, NotificationType::BROWSER_THEME_CHANGED,
299 NotificationService::AllSources());
300#endif
[email protected]420ae012009-04-24 05:16:32301
302 // Keep a global copy of the previous search string (if any).
303 static string16 global_last_search = string16();
[email protected]e491f1c2009-05-22 20:28:12304 last_search_prepopulate_text_ = &global_last_search;
[email protected]840b1512009-07-21 16:53:46305
[email protected]c8865482009-07-23 20:40:10306 // Set-up the showing of the omnibox search infobar if applicable.
307 if (OmniboxSearchHint::IsEnabled(profile))
308 omnibox_search_hint_.reset(new OmniboxSearchHint(this));
[email protected]332af7732009-03-11 13:21:35309}
initial.commit09911bf2008-07-26 23:55:29310
311TabContents::~TabContents() {
[email protected]420ae012009-04-24 05:16:32312 is_being_destroyed_ = true;
313
[email protected]bfe4c1582009-10-02 18:11:09314 // We don't want any notifications while we're running our destructor.
[email protected]420ae012009-04-24 05:16:32315 registrar_.RemoveAll();
316
317 // Unregister the notifications of all observed prefs change.
318 PrefService* prefs = profile()->GetPrefs();
319 if (prefs) {
320 for (int i = 0; i < kPrefsToObserveLength; ++i)
321 prefs->RemovePrefObserver(kPrefsToObserve[i], this);
322 }
323
[email protected]420ae012009-04-24 05:16:32324 NotifyDisconnected();
[email protected]e7b8d832009-11-20 22:41:00325 hung_renderer_dialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:32326
[email protected]420ae012009-04-24 05:16:32327 // First cleanly close all child windows.
328 // TODO(mpcomplete): handle case if MaybeCloseChildWindows() already asked
329 // some of these to close. CloseWindows is async, so it might get called
330 // twice before it runs.
331 int size = static_cast<int>(child_windows_.size());
332 for (int i = size - 1; i >= 0; --i) {
333 ConstrainedWindow* window = child_windows_[i];
[email protected]fa1cf0b82010-01-15 21:49:44334 if (window) {
[email protected]420ae012009-04-24 05:16:32335 window->CloseConstrainedWindow();
[email protected]fa1cf0b82010-01-15 21:49:44336 BlockTabContent(false);
337 }
[email protected]420ae012009-04-24 05:16:32338 }
339
[email protected]66ba4932009-06-04 19:22:13340 if (blocked_popups_)
341 blocked_popups_->Destroy();
342
[email protected]420ae012009-04-24 05:16:32343 // Notify any observer that have a reference on this tab contents.
344 NotificationService::current()->Notify(
345 NotificationType::TAB_CONTENTS_DESTROYED,
346 Source<TabContents>(this),
347 NotificationService::NoDetails());
348
[email protected]1e5e74f2009-05-27 20:55:12349 // Notify any lasting InfobarDelegates that have not yet been removed that
350 // whatever infobar they were handling in this TabContents has closed,
351 // because the TabContents is going away entirely.
352 // This must happen after the TAB_CONTENTS_DESTROYED notification as the
353 // notification may trigger infobars calls that access their delegate. (and
354 // some implementations of InfoBarDelegate do delete themselves on
355 // InfoBarClosed()).
356 for (int i = 0; i < infobar_delegate_count(); ++i) {
357 InfoBarDelegate* delegate = GetInfoBarDelegateAt(i);
358 delegate->InfoBarClosed();
359 }
360 infobar_delegates_.clear();
361
[email protected]420ae012009-04-24 05:16:32362 // TODO(brettw) this should be moved to the view.
363#if defined(OS_WIN)
364 // If we still have a window handle, destroy it. GetNativeView can return
365 // NULL if this contents was part of a window that closed.
366 if (GetNativeView())
367 ::DestroyWindow(GetNativeView());
368#endif
[email protected]7ab1e7d62009-10-14 23:32:01369
370 // OnCloseStarted isn't called in unit tests.
371 if (!tab_close_start_time_.is_null()) {
372 UMA_HISTOGRAM_TIMES("Tab.Close",
373 base::TimeTicks::Now() - tab_close_start_time_);
374 }
initial.commit09911bf2008-07-26 23:55:29375}
376
[email protected]d5f942ba2008-09-26 19:30:34377// static
378void TabContents::RegisterUserPrefs(PrefService* prefs) {
[email protected]420ae012009-04-24 05:16:32379 prefs->RegisterBooleanPref(prefs::kAlternateErrorPagesEnabled, true);
380
381 WebPreferences pref_defaults;
382 prefs->RegisterBooleanPref(prefs::kWebKitJavascriptEnabled,
383 pref_defaults.javascript_enabled);
384 prefs->RegisterBooleanPref(prefs::kWebKitWebSecurityEnabled,
385 pref_defaults.web_security_enabled);
386 prefs->RegisterBooleanPref(
387 prefs::kWebKitJavascriptCanOpenWindowsAutomatically, true);
388 prefs->RegisterBooleanPref(prefs::kWebKitLoadsImagesAutomatically,
389 pref_defaults.loads_images_automatically);
390 prefs->RegisterBooleanPref(prefs::kWebKitPluginsEnabled,
391 pref_defaults.plugins_enabled);
392 prefs->RegisterBooleanPref(prefs::kWebKitDomPasteEnabled,
393 pref_defaults.dom_paste_enabled);
394 prefs->RegisterBooleanPref(prefs::kWebKitShrinksStandaloneImagesToFit,
395 pref_defaults.shrinks_standalone_images_to_fit);
[email protected]0df30122009-06-03 12:13:08396 prefs->RegisterStringPref(prefs::kWebKitInspectorSettings,
[email protected]8922e1f2009-10-03 05:01:26397 UTF8ToWide(pref_defaults.inspector_settings));
[email protected]420ae012009-04-24 05:16:32398 prefs->RegisterBooleanPref(prefs::kWebKitTextAreasAreResizable,
399 pref_defaults.text_areas_are_resizable);
400 prefs->RegisterBooleanPref(prefs::kWebKitJavaEnabled,
401 pref_defaults.java_enabled);
402
403 prefs->RegisterLocalizedStringPref(prefs::kAcceptLanguages,
404 IDS_ACCEPT_LANGUAGES);
405 prefs->RegisterLocalizedStringPref(prefs::kDefaultCharset,
406 IDS_DEFAULT_ENCODING);
407 prefs->RegisterLocalizedBooleanPref(prefs::kWebKitStandardFontIsSerif,
408 IDS_STANDARD_FONT_IS_SERIF);
409 prefs->RegisterLocalizedStringPref(prefs::kWebKitFixedFontFamily,
410 IDS_FIXED_FONT_FAMILY);
411 prefs->RegisterLocalizedStringPref(prefs::kWebKitSerifFontFamily,
412 IDS_SERIF_FONT_FAMILY);
413 prefs->RegisterLocalizedStringPref(prefs::kWebKitSansSerifFontFamily,
414 IDS_SANS_SERIF_FONT_FAMILY);
415 prefs->RegisterLocalizedStringPref(prefs::kWebKitCursiveFontFamily,
416 IDS_CURSIVE_FONT_FAMILY);
417 prefs->RegisterLocalizedStringPref(prefs::kWebKitFantasyFontFamily,
418 IDS_FANTASY_FONT_FAMILY);
419 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitDefaultFontSize,
420 IDS_DEFAULT_FONT_SIZE);
421 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitDefaultFixedFontSize,
422 IDS_DEFAULT_FIXED_FONT_SIZE);
423 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitMinimumFontSize,
424 IDS_MINIMUM_FONT_SIZE);
425 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitMinimumLogicalFontSize,
426 IDS_MINIMUM_LOGICAL_FONT_SIZE);
427 prefs->RegisterLocalizedBooleanPref(prefs::kWebKitUsesUniversalDetector,
428 IDS_USES_UNIVERSAL_DETECTOR);
429 prefs->RegisterLocalizedStringPref(prefs::kStaticEncodings,
430 IDS_STATIC_ENCODING_LIST);
initial.commit09911bf2008-07-26 23:55:29431}
432
[email protected]13367f72009-07-08 16:39:02433// Returns true if contains content rendered by an extension.
434bool TabContents::HostsExtension() const {
435 return GetURL().SchemeIs(chrome::kExtensionScheme);
[email protected]d5f942ba2008-09-26 19:30:34436}
437
[email protected]6e95706d2009-04-23 22:55:01438PasswordManager* TabContents::GetPasswordManager() {
439 if (password_manager_.get() == NULL)
440 password_manager_.reset(new PasswordManager(this));
441 return password_manager_.get();
442}
443
444PluginInstaller* TabContents::GetPluginInstaller() {
445 if (plugin_installer_.get() == NULL)
446 plugin_installer_.reset(new PluginInstaller(this));
447 return plugin_installer_.get();
448}
449
[email protected]d5f942ba2008-09-26 19:30:34450const GURL& TabContents::GetURL() const {
451 // We may not have a navigation entry yet
[email protected]ce3fa3c2009-04-20 19:55:57452 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]ebe89e062009-08-13 23:16:54453 return entry ? entry->virtual_url() : GURL::EmptyGURL();
[email protected]d5f942ba2008-09-26 19:30:34454}
455
[email protected]96d185d2009-04-24 03:28:54456const string16& TabContents::GetTitle() const {
[email protected]4c6092c5b2009-06-06 00:23:55457 // Transient entries take precedence. They are used for interstitial pages
458 // that are shown on top of existing pages.
459 NavigationEntry* entry = controller_.GetTransientEntry();
460 if (entry)
461 return entry->GetTitleForDisplay(&controller_);
462
[email protected]96d185d2009-04-24 03:28:54463 DOMUI* our_dom_ui = render_manager_.pending_dom_ui() ?
464 render_manager_.pending_dom_ui() : render_manager_.dom_ui();
465 if (our_dom_ui) {
466 // Don't override the title in view source mode.
[email protected]4c6092c5b2009-06-06 00:23:55467 entry = controller_.GetActiveEntry();
[email protected]96d185d2009-04-24 03:28:54468 if (!(entry && entry->IsViewSourceMode())) {
469 // Give the DOM UI the chance to override our title.
470 const string16& title = our_dom_ui->overridden_title();
471 if (!title.empty())
472 return title;
473 }
474 }
475
476 // We use the title for the last committed entry rather than a pending
477 // navigation entry. For example, when the user types in a URL, we want to
478 // keep the old page's title until the new load has committed and we get a new
479 // title.
[email protected]96d185d2009-04-24 03:28:54480 entry = controller_.GetLastCommittedEntry();
481 if (entry)
482 return entry->GetTitleForDisplay(&controller_);
[email protected]96d185d2009-04-24 03:28:54483 return EmptyString16();
484}
485
[email protected]d5f942ba2008-09-26 19:30:34486int32 TabContents::GetMaxPageID() {
487 if (GetSiteInstance())
488 return GetSiteInstance()->max_page_id();
489 else
490 return max_page_id_;
491}
492
493void TabContents::UpdateMaxPageID(int32 page_id) {
494 // Ensure both the SiteInstance and RenderProcessHost update their max page
[email protected]57c6a652009-05-04 07:58:34495 // IDs in sync. Only TabContents will also have site instances, except during
[email protected]d5f942ba2008-09-26 19:30:34496 // testing.
497 if (GetSiteInstance())
498 GetSiteInstance()->UpdateMaxPageID(page_id);
[email protected]57c6a652009-05-04 07:58:34499 process()->UpdateMaxPageID(page_id);
[email protected]d5f942ba2008-09-26 19:30:34500}
501
[email protected]96d185d2009-04-24 03:28:54502SiteInstance* TabContents::GetSiteInstance() const {
503 return render_manager_.current_host()->site_instance();
504}
505
[email protected]d5f942ba2008-09-26 19:30:34506const std::wstring TabContents::GetDefaultTitle() const {
507 return l10n_util::GetString(IDS_DEFAULT_TAB_TITLE);
508}
509
[email protected]96d185d2009-04-24 03:28:54510bool TabContents::ShouldDisplayURL() {
[email protected]8ab24cc2009-11-13 20:56:15511 // Don't hide the url in view source mode and with interstitials.
[email protected]96d185d2009-04-24 03:28:54512 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]8ab24cc2009-11-13 20:56:15513 if (entry && (entry->IsViewSourceMode() ||
514 entry->page_type() == NavigationEntry::INTERSTITIAL_PAGE)) {
[email protected]96d185d2009-04-24 03:28:54515 return true;
[email protected]8ab24cc2009-11-13 20:56:15516 }
[email protected]83e3895b2009-06-11 00:07:16517
[email protected]96d185d2009-04-24 03:28:54518 DOMUI* dom_ui = GetDOMUIForCurrentState();
519 if (dom_ui)
520 return !dom_ui->should_hide_url();
521 return true;
522}
523
[email protected]d5f942ba2008-09-26 19:30:34524SkBitmap TabContents::GetFavIcon() const {
525 // Like GetTitle(), we also want to use the favicon for the last committed
526 // entry rather than a pending navigation entry.
[email protected]ce3fa3c2009-04-20 19:55:57527 NavigationEntry* entry = controller_.GetTransientEntry();
[email protected]cbab76d2008-10-13 22:42:47528 if (entry)
529 return entry->favicon().bitmap();
530
[email protected]ce3fa3c2009-04-20 19:55:57531 entry = controller_.GetLastCommittedEntry();
[email protected]d5f942ba2008-09-26 19:30:34532 if (entry)
533 return entry->favicon().bitmap();
[email protected]d5f942ba2008-09-26 19:30:34534 return SkBitmap();
535}
536
[email protected]0b303cc2009-09-28 22:35:15537bool TabContents::FavIconIsValid() const {
538 NavigationEntry* entry = controller_.GetTransientEntry();
539 if (entry)
540 return entry->favicon().is_valid();
541
542 entry = controller_.GetLastCommittedEntry();
543 if (entry)
544 return entry->favicon().is_valid();
545
546 return false;
547}
548
[email protected]96d185d2009-04-24 03:28:54549bool TabContents::ShouldDisplayFavIcon() {
550 // Always display a throbber during pending loads.
551 if (controller_.GetLastCommittedEntry() && controller_.pending_entry())
552 return true;
553
554 DOMUI* dom_ui = GetDOMUIForCurrentState();
555 if (dom_ui)
556 return !dom_ui->hide_favicon();
557 return true;
558}
559
[email protected]96d185d2009-04-24 03:28:54560std::wstring TabContents::GetStatusText() const {
561 if (!is_loading() || load_state_ == net::LOAD_STATE_IDLE)
562 return std::wstring();
563
564 switch (load_state_) {
565 case net::LOAD_STATE_WAITING_FOR_CACHE:
566 return l10n_util::GetString(IDS_LOAD_STATE_WAITING_FOR_CACHE);
567 case net::LOAD_STATE_RESOLVING_PROXY_FOR_URL:
568 return l10n_util::GetString(IDS_LOAD_STATE_RESOLVING_PROXY_FOR_URL);
569 case net::LOAD_STATE_RESOLVING_HOST:
570 return l10n_util::GetString(IDS_LOAD_STATE_RESOLVING_HOST);
571 case net::LOAD_STATE_CONNECTING:
572 return l10n_util::GetString(IDS_LOAD_STATE_CONNECTING);
573 case net::LOAD_STATE_SENDING_REQUEST:
[email protected]094e5b22009-09-25 04:23:56574 if (upload_size_)
575 return l10n_util::GetStringF(
576 IDS_LOAD_STATE_SENDING_REQUEST_WITH_PROGRESS,
577 static_cast<int>((100 * upload_position_) / upload_size_));
578 else
579 return l10n_util::GetString(IDS_LOAD_STATE_SENDING_REQUEST);
[email protected]96d185d2009-04-24 03:28:54580 case net::LOAD_STATE_WAITING_FOR_RESPONSE:
581 return l10n_util::GetStringF(IDS_LOAD_STATE_WAITING_FOR_RESPONSE,
582 load_state_host_);
583 // Ignore net::LOAD_STATE_READING_RESPONSE and net::LOAD_STATE_IDLE
584 case net::LOAD_STATE_IDLE:
585 case net::LOAD_STATE_READING_RESPONSE:
586 break;
587 }
588
589 return std::wstring();
590}
591
[email protected]d5f942ba2008-09-26 19:30:34592void TabContents::SetIsCrashed(bool state) {
593 if (state == is_crashed_)
594 return;
595
596 is_crashed_ = state;
[email protected]c9cd2222009-05-06 05:16:50597 NotifyNavigationStateChanged(INVALIDATE_TAB);
[email protected]d5f942ba2008-09-26 19:30:34598}
599
[email protected]744ef172009-10-16 21:53:46600void TabContents::PageActionStateChanged() {
601 NotifyNavigationStateChanged(TabContents::INVALIDATE_PAGE_ACTIONS);
[email protected]d7eaf572009-07-01 21:57:00602}
[email protected]ce5c4502009-05-06 16:46:11603
[email protected]d5f942ba2008-09-26 19:30:34604void TabContents::NotifyNavigationStateChanged(unsigned changed_flags) {
605 if (delegate_)
606 delegate_->NavigationStateChanged(this, changed_flags);
607}
608
[email protected]96d185d2009-04-24 03:28:54609void TabContents::DidBecomeSelected() {
610 controller_.SetActive(true);
[email protected]789e9152009-08-04 21:59:43611 if (render_widget_host_view()) {
[email protected]96d185d2009-04-24 03:28:54612 render_widget_host_view()->DidBecomeSelected();
[email protected]789e9152009-08-04 21:59:43613#if defined(OS_MACOSX)
614 render_widget_host_view()->SetActive(true);
615#endif
616 }
[email protected]96d185d2009-04-24 03:28:54617
[email protected]76543b92009-08-31 17:27:45618 WebCacheManager::GetInstance()->ObserveActivity(process()->id());
[email protected]96d185d2009-04-24 03:28:54619}
620
621void TabContents::WasHidden() {
622 if (!capturing_contents()) {
623 // |render_view_host()| can be NULL if the user middle clicks a link to open
624 // a tab in then background, then closes the tab before selecting it. This
625 // is because closing the tab calls TabContents::Destroy(), which removes
626 // the |render_view_host()|; then when we actually destroy the window,
627 // OnWindowPosChanged() notices and calls HideContents() (which calls us).
628 if (render_widget_host_view())
629 render_widget_host_view()->WasHidden();
[email protected]96d185d2009-04-24 03:28:54630 }
631
632 NotificationService::current()->Notify(
633 NotificationType::TAB_CONTENTS_HIDDEN,
634 Source<TabContents>(this),
635 NotificationService::NoDetails());
636}
637
[email protected]d5f942ba2008-09-26 19:30:34638void TabContents::Activate() {
639 if (delegate_)
640 delegate_->ActivateContents(this);
641}
642
[email protected]96d185d2009-04-24 03:28:54643void TabContents::ShowContents() {
644 if (render_widget_host_view())
645 render_widget_host_view()->DidBecomeSelected();
[email protected]96d185d2009-04-24 03:28:54646}
647
648void TabContents::HideContents() {
649 // TODO(pkasting): http://b/1239839 Right now we purposefully don't call
650 // our superclass HideContents(), because some callers want to be very picky
651 // about the order in which these get called. In addition to making the code
652 // here practically impossible to understand, this also means we end up
653 // calling TabContents::WasHidden() twice if callers call both versions of
[email protected]57c6a652009-05-04 07:58:34654 // HideContents() on a TabContents.
[email protected]96d185d2009-04-24 03:28:54655 WasHidden();
656}
657
[email protected]c0588052008-10-27 23:01:50658void TabContents::OpenURL(const GURL& url, const GURL& referrer,
[email protected]d5f942ba2008-09-26 19:30:34659 WindowOpenDisposition disposition,
660 PageTransition::Type transition) {
661 if (delegate_)
[email protected]c0588052008-10-27 23:01:50662 delegate_->OpenURLFromTab(this, url, referrer, disposition, transition);
[email protected]d5f942ba2008-09-26 19:30:34663}
664
665bool TabContents::NavigateToPendingEntry(bool reload) {
[email protected]96d185d2009-04-24 03:28:54666 const NavigationEntry& entry = *controller_.pending_entry();
667
668 RenderViewHost* dest_render_view_host = render_manager_.Navigate(entry);
669 if (!dest_render_view_host)
670 return false; // Unable to create the desired render view host.
671
672 // Tell DevTools agent that it is attached prior to the navigation.
[email protected]d9f9b792009-06-24 13:17:12673 DevToolsManager* devtools_manager = DevToolsManager::GetInstance();
[email protected]7e8e6b62009-05-08 11:28:32674 if (devtools_manager) { // NULL in unit tests.
[email protected]684b4ba22009-05-07 06:27:41675 devtools_manager->OnNavigatingToPendingEntry(
676 render_view_host(),
677 dest_render_view_host,
678 controller_.pending_entry()->url());
[email protected]7e8e6b62009-05-08 11:28:32679 }
[email protected]96d185d2009-04-24 03:28:54680
681 // Used for page load time metrics.
682 current_load_start_ = base::TimeTicks::Now();
683
684 // Navigate in the desired RenderViewHost.
[email protected]056de2d2009-06-26 16:41:34685 ViewMsg_Navigate_Params navigate_params;
[email protected]5e369672009-11-03 23:48:30686 MakeNavigateParams(profile(), entry, reload, &navigate_params);
[email protected]056de2d2009-06-26 16:41:34687 dest_render_view_host->Navigate(navigate_params);
[email protected]96d185d2009-04-24 03:28:54688
689 if (entry.page_id() == -1) {
690 // HACK!! This code suppresses javascript: URLs from being added to
691 // session history, which is what we want to do for javascript: URLs that
692 // do not generate content. What we really need is a message from the
693 // renderer telling us that a new page was not created. The same message
694 // could be used for mailto: URLs and the like.
695 if (entry.url().SchemeIs(chrome::kJavaScriptScheme))
696 return false;
697 }
698
699 // Clear any provisional password saves - this stops password infobars
700 // showing up on pages the user navigates to while the right page is
701 // loading.
702 GetPasswordManager()->ClearProvisionalSave();
703
704 if (reload && !profile()->IsOffTheRecord()) {
[email protected]0189bc722009-08-28 21:56:48705 FaviconService* favicon_service =
706 profile()->GetFaviconService(Profile::IMPLICIT_ACCESS);
707 if (favicon_service)
708 favicon_service->SetFaviconOutOfDateForPage(entry.url());
[email protected]96d185d2009-04-24 03:28:54709 }
710
[email protected]d5f942ba2008-09-26 19:30:34711 return true;
712}
713
[email protected]96d185d2009-04-24 03:28:54714void TabContents::Stop() {
715 render_manager_.Stop();
716 printing_.Stop();
717}
718
719void TabContents::Cut() {
720 render_view_host()->Cut();
721}
722
723void TabContents::Copy() {
724 render_view_host()->Copy();
725}
726
[email protected]c0cc3092009-09-12 08:27:27727#if defined(OS_MACOSX)
[email protected]a954bf72009-09-12 07:30:35728void TabContents::CopyToFindPboard() {
729 render_view_host()->CopyToFindPboard();
730}
[email protected]c0cc3092009-09-12 08:27:27731#endif
[email protected]a954bf72009-09-12 07:30:35732
[email protected]96d185d2009-04-24 03:28:54733void TabContents::Paste() {
734 render_view_host()->Paste();
735}
736
[email protected]7e786822009-11-07 00:00:27737#if defined(OS_MACOSX)
738void TabContents::PasteAndMatchStyle() {
739 render_view_host()->ForwardEditCommand("PasteAndMatchStyle", "");
740}
741#endif
742
[email protected]96d185d2009-04-24 03:28:54743void TabContents::DisassociateFromPopupCount() {
744 render_view_host()->DisassociateFromPopupCount();
745}
746
747TabContents* TabContents::Clone() {
748 // We create a new SiteInstance so that the new tab won't share processes
749 // with the old one. This can be changed in the future if we need it to share
750 // processes for some reason.
[email protected]420ae012009-04-24 05:16:32751 TabContents* tc = new TabContents(profile(),
[email protected]96d185d2009-04-24 03:28:54752 SiteInstance::CreateSiteInstance(profile()),
[email protected]12636df2009-09-28 22:32:21753 MSG_ROUTING_NONE, this);
[email protected]96d185d2009-04-24 03:28:54754 tc->controller().CopyStateFrom(controller_);
755 return tc;
756}
757
[email protected]4d677202009-07-19 07:37:12758void TabContents::ShowPageInfo(const GURL& url,
759 const NavigationEntry::SSLStatus& ssl,
760 bool show_history) {
761 if (!delegate_)
762 return;
763
[email protected]bb678332009-07-21 00:15:50764 delegate_->ShowPageInfo(profile(), url, ssl, show_history);
[email protected]4d677202009-07-19 07:37:12765}
766
initial.commit09911bf2008-07-26 23:55:29767ConstrainedWindow* TabContents::CreateConstrainedDialog(
[email protected]e8382172009-06-19 22:16:28768 ConstrainedWindowDelegate* delegate) {
initial.commit09911bf2008-07-26 23:55:29769 ConstrainedWindow* window =
[email protected]e8382172009-06-19 22:16:28770 ConstrainedWindow::CreateConstrainedDialog(this, delegate);
initial.commit09911bf2008-07-26 23:55:29771 child_windows_.push_back(window);
[email protected]fa1cf0b82010-01-15 21:49:44772
773 if (child_windows_.size() == 1) {
774 window->ShowConstrainedWindow();
775 BlockTabContent(true);
776 }
777
initial.commit09911bf2008-07-26 23:55:29778 return window;
779}
780
[email protected]fa1cf0b82010-01-15 21:49:44781void TabContents::BlockTabContent(bool blocked) {
782 render_view_host()->set_ignore_input_events(blocked);
783 if (delegate_)
784 delegate_->SetTabContentBlocked(this, blocked);
785}
786
initial.commit09911bf2008-07-26 23:55:29787void TabContents::AddNewContents(TabContents* new_contents,
788 WindowOpenDisposition disposition,
789 const gfx::Rect& initial_pos,
[email protected]c88a70fe2009-05-05 20:00:22790 bool user_gesture,
791 const GURL& creator_url) {
initial.commit09911bf2008-07-26 23:55:29792 if (!delegate_)
793 return;
794
[email protected]35f7d212009-04-29 21:19:27795 if ((disposition == NEW_POPUP) && !user_gesture &&
796 !CommandLine::ForCurrentProcess()->HasSwitch(
797 switches::kDisablePopupBlocking)) {
[email protected]2c4410d2009-05-06 23:46:22798 // Unrequested popups from normal pages are constrained unless they're in
[email protected]b6c874582009-05-08 19:38:31799 // the whitelist. The popup owner will handle checking this.
800 delegate_->GetConstrainingContents(this)->AddPopup(new_contents,
801 initial_pos,
802 creator_url.is_valid() ? creator_url.host() : std::string());
803 } else {
[email protected]304f8792009-08-19 21:28:09804#if defined(OS_CHROMEOS)
805 if (disposition == NEW_POPUP) {
806 // If the popup is bigger than a given factor of the screen, then
807 // turn it into a foreground tab (on chrome os only)
808 GdkScreen* screen = gdk_screen_get_default();
809 int max_width = gdk_screen_get_width(screen) * kMaxWidthFactor;
810 int max_height = gdk_screen_get_height(screen) * kMaxHeightFactor;
811 if (initial_pos.width() > max_width ||
812 initial_pos.height() > max_height) {
813 disposition = NEW_FOREGROUND_TAB;
814 }
815 }
816#endif
817
[email protected]0aa55312008-10-17 21:53:08818 new_contents->DisassociateFromPopupCount();
initial.commit09911bf2008-07-26 23:55:29819 delegate_->AddNewContents(this, new_contents, disposition, initial_pos,
820 user_gesture);
[email protected]7f37dd92009-09-15 16:35:49821 NotificationService::current()->Notify(
822 NotificationType::TAB_ADDED,
823 Source<TabContentsDelegate>(delegate_),
824 Details<TabContents>(this));
initial.commit09911bf2008-07-26 23:55:29825 }
[email protected]b6c874582009-05-08 19:38:31826 PopupNotificationVisibilityChanged(ShowingBlockedPopupNotification());
initial.commit09911bf2008-07-26 23:55:29827}
828
initial.commit09911bf2008-07-26 23:55:29829void TabContents::CloseAllSuppressedPopups() {
[email protected]d6598c052008-11-05 19:03:25830 if (blocked_popups_)
[email protected]2c4410d2009-05-06 23:46:22831 blocked_popups_->CloseAll();
initial.commit09911bf2008-07-26 23:55:29832}
833
[email protected]912256b32009-09-18 09:47:35834void TabContents::ExecuteCode(int request_id, const std::string& extension_id,
[email protected]61f5a7b2009-12-22 22:21:20835 const std::vector<URLPattern>& host_permissions,
[email protected]20ad2692009-11-20 18:27:20836 bool is_js_code, const std::string& code_string,
837 bool all_frames) {
[email protected]912256b32009-09-18 09:47:35838 render_view_host()->Send(
[email protected]61f5a7b2009-12-22 22:21:20839 new ViewMsg_ExecuteCode(render_view_host()->routing_id(),
840 ViewMsg_ExecuteCode_Params(request_id, extension_id, host_permissions,
841 is_js_code, code_string, all_frames)));
[email protected]912256b32009-09-18 09:47:35842}
843
[email protected]96d185d2009-04-24 03:28:54844void TabContents::PopupNotificationVisibilityChanged(bool visible) {
845 render_view_host()->PopupNotificationVisibilityChanged(visible);
846}
847
[email protected]5c9e97a2009-09-09 23:48:30848gfx::NativeView TabContents::GetContentNativeView() const {
[email protected]96d185d2009-04-24 03:28:54849 return view_->GetContentNativeView();
850}
851
852gfx::NativeView TabContents::GetNativeView() const {
853 return view_->GetNativeView();
854}
855
856void TabContents::GetContainerBounds(gfx::Rect *out) const {
857 view_->GetContainerBounds(out);
858}
859
860void TabContents::Focus() {
861 view_->Focus();
862}
863
[email protected]90daadb42009-06-08 21:27:28864void TabContents::FocusThroughTabTraversal(bool reverse) {
[email protected]7e383692009-06-12 19:14:54865 if (showing_interstitial_page()) {
866 render_manager_.interstitial_page()->FocusThroughTabTraversal(reverse);
867 return;
868 }
[email protected]96d185d2009-04-24 03:28:54869 render_view_host()->SetInitialFocus(reverse);
870}
871
872bool TabContents::FocusLocationBarByDefault() {
873 DOMUI* dom_ui = GetDOMUIForCurrentState();
874 if (dom_ui)
875 return dom_ui->focus_location_bar_by_default();
[email protected]3e3f0eb2009-06-22 18:33:43876 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]e0d481582009-09-15 21:06:25877 if (entry && entry->url() == GURL(chrome::kAboutBlankURL))
[email protected]3e3f0eb2009-06-22 18:33:43878 return true;
[email protected]96d185d2009-04-24 03:28:54879 return false;
880}
881
[email protected]616ed5a2008-11-21 22:27:24882void TabContents::AddInfoBar(InfoBarDelegate* delegate) {
883 // Look through the existing InfoBarDelegates we have for a match. If we've
884 // already got one that matches, then we don't add the new one.
[email protected]f86a07022008-11-25 01:06:05885 for (int i = 0; i < infobar_delegate_count(); ++i) {
[email protected]6d27a7b2009-12-18 23:25:45886 if (GetInfoBarDelegateAt(i)->EqualsDelegate(delegate)) {
887 // Tell the new infobar to close so that it can clean itself up.
888 delegate->InfoBarClosed();
[email protected]616ed5a2008-11-21 22:27:24889 return;
[email protected]6d27a7b2009-12-18 23:25:45890 }
[email protected]616ed5a2008-11-21 22:27:24891 }
892
893 infobar_delegates_.push_back(delegate);
[email protected]bfd04a62009-02-01 18:16:56894 NotificationService::current()->Notify(
895 NotificationType::TAB_CONTENTS_INFOBAR_ADDED,
896 Source<TabContents>(this),
897 Details<InfoBarDelegate>(delegate));
[email protected]616ed5a2008-11-21 22:27:24898
899 // Add ourselves as an observer for navigations the first time a delegate is
900 // added. We use this notification to expire InfoBars that need to expire on
901 // page transitions.
902 if (infobar_delegates_.size() == 1) {
[email protected]bfd04a62009-02-01 18:16:56903 registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED,
[email protected]ce3fa3c2009-04-20 19:55:57904 Source<NavigationController>(&controller_));
[email protected]616ed5a2008-11-21 22:27:24905 }
906}
907
908void TabContents::RemoveInfoBar(InfoBarDelegate* delegate) {
909 std::vector<InfoBarDelegate*>::iterator it =
910 find(infobar_delegates_.begin(), infobar_delegates_.end(), delegate);
911 if (it != infobar_delegates_.end()) {
912 InfoBarDelegate* delegate = *it;
[email protected]bfd04a62009-02-01 18:16:56913 NotificationService::current()->Notify(
914 NotificationType::TAB_CONTENTS_INFOBAR_REMOVED,
915 Source<TabContents>(this),
916 Details<InfoBarDelegate>(delegate));
[email protected]f86a07022008-11-25 01:06:05917 infobar_delegates_.erase(it);
[email protected]616ed5a2008-11-21 22:27:24918
[email protected]6a02963e2009-01-06 16:58:03919 // Remove ourselves as an observer if we are tracking no more InfoBars.
920 if (infobar_delegates_.empty()) {
[email protected]bfd04a62009-02-01 18:16:56921 registrar_.Remove(this, NotificationType::NAV_ENTRY_COMMITTED,
[email protected]ce3fa3c2009-04-20 19:55:57922 Source<NavigationController>(&controller_));
[email protected]6a02963e2009-01-06 16:58:03923 }
[email protected]616ed5a2008-11-21 22:27:24924 }
925}
[email protected]3edd9522009-03-04 22:19:41926
[email protected]f81821c2009-08-07 01:29:47927void TabContents::ReplaceInfoBar(InfoBarDelegate* old_delegate,
928 InfoBarDelegate* new_delegate) {
929 std::vector<InfoBarDelegate*>::iterator it =
930 find(infobar_delegates_.begin(), infobar_delegates_.end(), old_delegate);
931 DCHECK(it != infobar_delegates_.end());
932
933 // Notify the container about the change of plans.
934 scoped_ptr<std::pair<InfoBarDelegate*, InfoBarDelegate*> > details(
935 new std::pair<InfoBarDelegate*, InfoBarDelegate*>(
936 old_delegate, new_delegate));
937 NotificationService::current()->Notify(
938 NotificationType::TAB_CONTENTS_INFOBAR_REPLACED,
939 Source<TabContents>(this),
940 Details<std::pair<InfoBarDelegate*, InfoBarDelegate*> >(details.get()));
941
942 // Remove the old one.
943 infobar_delegates_.erase(it);
944
945 // Add the new one.
946 infobar_delegates_.push_back(new_delegate);
947}
948
[email protected]20ede072009-10-06 00:28:35949bool TabContents::ShouldShowBookmarkBar() {
[email protected]aa9196f2009-10-22 16:20:27950 if (showing_interstitial_page())
951 return false;
952
[email protected]96d185d2009-04-24 03:28:54953 // See GetDOMUIForCurrentState() comment for more info. This case is very
954 // similar, but for non-first loads, we want to use the committed entry. This
955 // is so the bookmarks bar disappears at the same time the page does.
956 if (controller_.GetLastCommittedEntry()) {
957 // Not the first load, always use the committed DOM UI.
958 if (render_manager_.dom_ui())
959 return render_manager_.dom_ui()->force_bookmark_bar_visible();
960 return false; // Default.
961 }
962
963 // When it's the first load, we know either the pending one or the committed
964 // one will have the DOM UI in it (see GetDOMUIForCurrentState), and only one
965 // of them will be valid, so we can just check both.
966 if (render_manager_.pending_dom_ui())
967 return render_manager_.pending_dom_ui()->force_bookmark_bar_visible();
968 if (render_manager_.dom_ui())
969 return render_manager_.dom_ui()->force_bookmark_bar_visible();
970 return false; // Default.
971}
972
[email protected]d8f16ae2009-08-30 03:47:26973bool TabContents::IsExtensionShelfAlwaysVisible() {
974 // See GetDOMUIForCurrentState() comment for more info. This case is very
975 // similar, but for non-first loads, we want to use the committed entry. This
976 // is so the bookmarks bar disappears at the same time the page does.
977 if (controller_.GetLastCommittedEntry()) {
978 // Not the first load, always use the committed DOM UI.
979 if (render_manager_.dom_ui())
980 return render_manager_.dom_ui()->force_extension_shelf_visible();
981 return false; // Default.
982 }
983
984 // When it's the first load, we know either the pending one or the committed
985 // one will have the DOM UI in it (see GetDOMUIForCurrentState), and only one
986 // of them will be valid, so we can just check both.
987 if (render_manager_.pending_dom_ui())
988 return render_manager_.pending_dom_ui()->force_extension_shelf_visible();
989 if (render_manager_.dom_ui())
990 return render_manager_.dom_ui()->force_extension_shelf_visible();
991 return false; // Default.
992}
993
[email protected]3edd9522009-03-04 22:19:41994void TabContents::ToolbarSizeChanged(bool is_animating) {
995 TabContentsDelegate* d = delegate();
996 if (d)
997 d->ToolbarSizeChanged(this, is_animating);
998}
[email protected]616ed5a2008-11-21 22:27:24999
[email protected]fc6fb7fb2009-11-07 02:35:041000bool TabContents::CanDownload(int request_id) {
1001 TabContentsDelegate* d = delegate();
1002 if (d)
1003 return d->CanDownload(request_id);
1004 return true;
1005}
1006
[email protected]d5f942ba2008-09-26 19:30:341007void TabContents::OnStartDownload(DownloadItem* download) {
1008 DCHECK(download);
[email protected]d5f942ba2008-09-26 19:30:341009
1010 // Download in a constrained popup is shown in the tab that opened it.
[email protected]b6c874582009-05-08 19:38:311011 TabContents* tab_contents = delegate()->GetConstrainingContents(this);
[email protected]d5f942ba2008-09-26 19:30:341012
[email protected]59560e0b2009-06-04 03:30:221013 if (tab_contents && tab_contents->delegate())
1014 tab_contents->delegate()->OnStartDownload(download);
[email protected]3edd9522009-03-04 22:19:411015}
[email protected]3edd9522009-03-04 22:19:411016
[email protected]d5f942ba2008-09-26 19:30:341017void TabContents::WillClose(ConstrainedWindow* window) {
1018 ConstrainedWindowList::iterator it =
1019 find(child_windows_.begin(), child_windows_.end(), window);
[email protected]fa1cf0b82010-01-15 21:49:441020 bool removed_topmost_window = it == child_windows_.begin();
[email protected]d5f942ba2008-09-26 19:30:341021 if (it != child_windows_.end())
1022 child_windows_.erase(it);
[email protected]fa1cf0b82010-01-15 21:49:441023 if (removed_topmost_window && child_windows_.size() > 0) {
1024 child_windows_[0]->ShowConstrainedWindow();
1025 BlockTabContent(true);
1026 } else {
1027 BlockTabContent(false);
1028 }
[email protected]66ba4932009-06-04 19:22:131029}
[email protected]d5f942ba2008-09-26 19:30:341030
[email protected]66ba4932009-06-04 19:22:131031void TabContents::WillCloseBlockedPopupContainer(
1032 BlockedPopupContainer* container) {
1033 DCHECK(blocked_popups_ == container);
1034 blocked_popups_ = NULL;
[email protected]d5f942ba2008-09-26 19:30:341035}
1036
[email protected]d5f942ba2008-09-26 19:30:341037void TabContents::DidMoveOrResize(ConstrainedWindow* window) {
[email protected]332af7732009-03-11 13:21:351038#if defined(OS_WIN)
[email protected]92edc472009-02-10 20:32:061039 UpdateWindow(GetNativeView());
[email protected]332af7732009-03-11 13:21:351040#endif
[email protected]d5f942ba2008-09-26 19:30:341041}
1042
[email protected]c1dc9f132009-11-20 23:13:211043void TabContents::StartFinding(string16 search_string,
[email protected]e491f1c2009-05-22 20:28:121044 bool forward_direction,
1045 bool case_sensitive) {
[email protected]c1dc9f132009-11-20 23:13:211046 // If search_string is empty, it means FindNext was pressed with a keyboard
[email protected]6e95706d2009-04-23 22:55:011047 // shortcut so unless we have something to search for we return early.
[email protected]c1dc9f132009-11-20 23:13:211048 if (search_string.empty() && find_text_.empty()) {
[email protected]87fae512009-12-11 02:20:211049 // Try the last thing we searched for on this tab, then the last thing
1050 // searched for on any tab.
1051 if (!previous_find_text_.empty())
1052 search_string = previous_find_text_;
1053 else if (!last_search_prepopulate_text_->empty())
1054 search_string = *last_search_prepopulate_text_;
1055 else
[email protected]e491f1c2009-05-22 20:28:121056 return;
[email protected]e491f1c2009-05-22 20:28:121057 }
[email protected]6e95706d2009-04-23 22:55:011058
[email protected]101c90e2009-10-26 21:14:441059 // Keep track of the previous search.
1060 previous_find_text_ = find_text_;
1061
[email protected]6e95706d2009-04-23 22:55:011062 // This is a FindNext operation if we are searching for the same text again,
1063 // or if the passed in search text is empty (FindNext keyboard shortcut). The
1064 // exception to this is if the Find was aborted (then we don't want FindNext
1065 // because the highlighting has been cleared and we need it to reappear). We
1066 // therefore treat FindNext after an aborted Find operation as a full fledged
1067 // Find.
[email protected]c1dc9f132009-11-20 23:13:211068 bool find_next = (find_text_ == search_string || search_string.empty()) &&
[email protected]e491f1c2009-05-22 20:28:121069 (last_search_case_sensitive_ == case_sensitive) &&
[email protected]6e95706d2009-04-23 22:55:011070 !find_op_aborted_;
1071 if (!find_next)
1072 current_find_request_id_ = find_request_id_counter_++;
1073
[email protected]c1dc9f132009-11-20 23:13:211074 if (!search_string.empty())
1075 find_text_ = search_string;
[email protected]e491f1c2009-05-22 20:28:121076 last_search_case_sensitive_ = case_sensitive;
[email protected]6e95706d2009-04-23 22:55:011077
1078 find_op_aborted_ = false;
1079
1080 // Keep track of what the last search was across the tabs.
[email protected]c1dc9f132009-11-20 23:13:211081 *last_search_prepopulate_text_ = find_text_;
[email protected]6e95706d2009-04-23 22:55:011082 render_view_host()->StartFinding(current_find_request_id_,
1083 find_text_,
1084 forward_direction,
[email protected]e491f1c2009-05-22 20:28:121085 case_sensitive,
[email protected]6e95706d2009-04-23 22:55:011086 find_next);
1087}
1088
1089void TabContents::StopFinding(bool clear_selection) {
[email protected]2d2dffc2009-06-09 21:41:001090 // When |clear_selection| is true, it means the find string has been cleared
1091 // by the user, but the UI has not been dismissed.
1092 if (!clear_selection)
1093 find_ui_active_ = false;
[email protected]87fae512009-12-11 02:20:211094 previous_find_text_ = find_text_;
[email protected]101c90e2009-10-26 21:14:441095 find_text_.clear();
[email protected]6e95706d2009-04-23 22:55:011096 find_op_aborted_ = true;
[email protected]e491f1c2009-05-22 20:28:121097 last_search_result_ = FindNotificationDetails();
[email protected]6e95706d2009-04-23 22:55:011098 render_view_host()->StopFinding(clear_selection);
1099}
1100
[email protected]89f0e74932010-01-16 01:59:201101void TabContents::GetPageLanguage() {
1102 render_view_host()->GetPageLanguage();
1103}
1104
[email protected]420ae012009-04-24 05:16:321105void TabContents::OnSavePage() {
1106 // If we can not save the page, try to download it.
1107 if (!SavePackage::IsSavableContents(contents_mime_type())) {
1108 DownloadManager* dlm = profile()->GetDownloadManager();
1109 const GURL& current_page_url = GetURL();
1110 if (dlm && current_page_url.is_valid())
[email protected]d82ed61e2009-06-16 02:46:221111 dlm->DownloadUrl(current_page_url, GURL(), "", this);
[email protected]420ae012009-04-24 05:16:321112 return;
1113 }
1114
1115 Stop();
1116
1117 // Create the save package and possibly prompt the user for the name to save
1118 // the page as. The user prompt is an asynchronous operation that runs on
1119 // another thread.
[email protected]d82ed61e2009-06-16 02:46:221120 save_package_ = new SavePackage(this);
[email protected]420ae012009-04-24 05:16:321121 save_package_->GetSaveInfo();
1122}
1123
1124// Used in automated testing to bypass prompting the user for file names.
1125// Instead, the names and paths are hard coded rather than running them through
1126// file name sanitation and extension / mime checking.
[email protected]828cabe2009-09-26 22:47:111127bool TabContents::SavePage(const FilePath& main_file, const FilePath& dir_path,
[email protected]420ae012009-04-24 05:16:321128 SavePackage::SavePackageType save_type) {
1129 // Stop the page from navigating.
1130 Stop();
1131
[email protected]828cabe2009-09-26 22:47:111132 save_package_ = new SavePackage(this, save_type, main_file, dir_path);
[email protected]cdf6cc82009-08-04 21:48:591133 return save_package_->Init();
[email protected]420ae012009-04-24 05:16:321134}
1135
[email protected]89d6e6e2009-12-03 23:06:511136void TabContents::EmailPageLocation() {
1137 std::string title = EscapeQueryParamValue(UTF16ToUTF8(GetTitle()), false);
1138 std::string page_url = EscapeQueryParamValue(GetURL().spec(), false);
1139 std::string mailto = std::string("mailto:?subject=Fwd:%20") +
1140 title + "&body=%0A%0A" + page_url;
1141 platform_util::OpenExternal(GURL(mailto));
1142}
1143
[email protected]420ae012009-04-24 05:16:321144void TabContents::PrintPreview() {
1145 // We don't show the print preview yet, only the print dialog.
1146 PrintNow();
1147}
1148
1149bool TabContents::PrintNow() {
1150 // We can't print interstitial page for now.
1151 if (showing_interstitial_page())
1152 return false;
1153
1154 return render_view_host()->PrintPages();
1155}
1156
[email protected]82270452009-06-19 15:58:011157void TabContents::PrintingDone(int document_cookie, bool success) {
1158 render_view_host()->PrintingDone(document_cookie, success);
1159}
1160
[email protected]420ae012009-04-24 05:16:321161bool TabContents::IsActiveEntry(int32 page_id) {
1162 NavigationEntry* active_entry = controller_.GetActiveEntry();
1163 return (active_entry != NULL &&
1164 active_entry->site_instance() == GetSiteInstance() &&
1165 active_entry->page_id() == page_id);
1166}
1167
[email protected]3f8eb7f2009-10-02 23:58:051168void TabContents::LogNewTabTime(const std::string& event_name) {
1169 // Not all new tab pages get timed. In those cases, we don't have a
1170 // new_tab_start_time_.
1171 if (new_tab_start_time_.is_null())
1172 return;
1173
1174 base::TimeDelta duration = base::TimeTicks::Now() - new_tab_start_time_;
[email protected]84abba62009-10-07 17:01:441175 MetricEventDurationDetails details(event_name,
1176 static_cast<int>(duration.InMilliseconds()));
1177
[email protected]7ab1e7d62009-10-14 23:32:011178 if (event_name == "Tab.NewTabScriptStart") {
1179 UMA_HISTOGRAM_TIMES("Tab.NewTabScriptStart", duration);
1180 } else if (event_name == "Tab.NewTabDOMContentLoaded") {
1181 UMA_HISTOGRAM_TIMES("Tab.NewTabDOMContentLoaded", duration);
1182 } else if (event_name == "Tab.NewTabOnload") {
1183 UMA_HISTOGRAM_TIMES("Tab.NewTabOnload", duration);
[email protected]3f8eb7f2009-10-02 23:58:051184 // The new tab page has finished loading; reset it.
1185 new_tab_start_time_ = base::TimeTicks();
[email protected]84abba62009-10-07 17:01:441186 } else {
1187 NOTREACHED();
[email protected]3f8eb7f2009-10-02 23:58:051188 }
[email protected]84abba62009-10-07 17:01:441189 NotificationService::current()->Notify(
1190 NotificationType::METRIC_EVENT_DURATION,
1191 Source<TabContents>(this),
1192 Details<MetricEventDurationDetails>(&details));
[email protected]3f8eb7f2009-10-02 23:58:051193}
1194
[email protected]7ab1e7d62009-10-14 23:32:011195void TabContents::OnCloseStarted() {
1196 if (tab_close_start_time_.is_null())
1197 tab_close_start_time_ = base::TimeTicks::Now();
1198}
1199
[email protected]96d185d2009-04-24 03:28:541200// Notifies the RenderWidgetHost instance about the fact that the page is
1201// loading, or done loading and calls the base implementation.
1202void TabContents::SetIsLoading(bool is_loading,
1203 LoadNotificationDetails* details) {
1204 if (is_loading == is_loading_)
1205 return;
1206
1207 if (!is_loading) {
1208 load_state_ = net::LOAD_STATE_IDLE;
1209 load_state_host_.clear();
[email protected]094e5b22009-09-25 04:23:561210 upload_size_ = 0;
1211 upload_position_ = 0;
[email protected]96d185d2009-04-24 03:28:541212 }
1213
1214 render_manager_.SetIsLoading(is_loading);
1215
1216 is_loading_ = is_loading;
1217 waiting_for_response_ = is_loading;
1218
1219 if (delegate_)
1220 delegate_->LoadingStateChanged(this);
[email protected]329581b2009-04-28 06:52:351221 NotifyNavigationStateChanged(INVALIDATE_LOAD);
[email protected]96d185d2009-04-24 03:28:541222
1223 NotificationType type = is_loading ? NotificationType::LOAD_START :
1224 NotificationType::LOAD_STOP;
[email protected]ce5c4502009-05-06 16:46:111225 NotificationDetails det = NotificationService::NoDetails();
[email protected]96d185d2009-04-24 03:28:541226 if (details)
1227 det = Details<LoadNotificationDetails>(details);
1228 NotificationService::current()->Notify(type,
1229 Source<NavigationController>(&controller_),
1230 det);
1231}
1232
[email protected]2c4410d2009-05-06 23:46:221233void TabContents::CreateBlockedPopupContainerIfNecessary() {
1234 if (blocked_popups_)
1235 return;
1236
[email protected]4d4c0b92009-06-08 23:13:011237 blocked_popups_ = BlockedPopupContainer::Create(this, profile());
[email protected]2c4410d2009-05-06 23:46:221238}
1239
[email protected]b6c874582009-05-08 19:38:311240void TabContents::AddPopup(TabContents* new_contents,
1241 const gfx::Rect& initial_pos,
1242 const std::string& host) {
[email protected]2c4410d2009-05-06 23:46:221243 CreateBlockedPopupContainerIfNecessary();
1244 blocked_popups_->AddTabContents(new_contents, initial_pos, host);
[email protected]2c4410d2009-05-06 23:46:221245}
1246
[email protected]e69bce3dd2009-04-20 22:05:121247// TODO(brettw) This should be on the TabContentsView.
[email protected]66ba4932009-06-04 19:22:131248void TabContents::RepositionSupressedPopupsToFit() {
[email protected]d6598c052008-11-05 19:03:251249 if (blocked_popups_)
[email protected]4d4c0b92009-06-08 23:13:011250 blocked_popups_->RepositionBlockedPopupContainer();
[email protected]d5f942ba2008-09-26 19:30:341251}
1252
[email protected]b9681312008-11-07 00:08:261253bool TabContents::ShowingBlockedPopupNotification() const {
1254 return blocked_popups_ != NULL &&
[email protected]2c4410d2009-05-06 23:46:221255 blocked_popups_->GetBlockedPopupCount() != 0;
[email protected]b9681312008-11-07 00:08:261256}
[email protected]616ed5a2008-11-21 22:27:241257
1258namespace {
1259bool TransitionIsReload(PageTransition::Type transition) {
1260 return PageTransition::StripQualifier(transition) == PageTransition::RELOAD;
1261}
1262}
1263
1264void TabContents::ExpireInfoBars(
1265 const NavigationController::LoadCommittedDetails& details) {
1266 // Only hide InfoBars when the user has done something that makes the main
1267 // frame load. We don't want various automatic or subframe navigations making
1268 // it disappear.
1269 if (!details.is_user_initiated_main_frame_load())
1270 return;
1271
[email protected]f86a07022008-11-25 01:06:051272 for (int i = infobar_delegate_count() - 1; i >= 0; --i) {
[email protected]616ed5a2008-11-21 22:27:241273 InfoBarDelegate* delegate = GetInfoBarDelegateAt(i);
[email protected]f86a07022008-11-25 01:06:051274 if (delegate->ShouldExpire(details))
[email protected]616ed5a2008-11-21 22:27:241275 RemoveInfoBar(delegate);
[email protected]616ed5a2008-11-21 22:27:241276 }
1277}
[email protected]fdd61c62009-04-22 19:22:571278
[email protected]96d185d2009-04-24 03:28:541279DOMUI* TabContents::GetDOMUIForCurrentState() {
1280 // When there is a pending navigation entry, we want to use the pending DOMUI
1281 // that goes along with it to control the basic flags. For example, we want to
1282 // show the pending URL in the URL bar, so we want the display_url flag to
1283 // be from the pending entry.
1284 //
1285 // The confusion comes because there are multiple possibilities for the
1286 // initial load in a tab as a side effect of the way the RenderViewHostManager
1287 // works.
1288 //
1289 // - For the very first tab the load looks "normal". The new tab DOM UI is
1290 // the pending one, and we want it to apply here.
1291 //
1292 // - For subsequent new tabs, they'll get a new SiteInstance which will then
1293 // get switched to the one previously associated with the new tab pages.
1294 // This switching will cause the manager to commit the RVH/DOMUI. So we'll
1295 // have a committed DOM UI in this case.
1296 //
1297 // This condition handles all of these cases:
1298 //
1299 // - First load in first tab: no committed nav entry + pending nav entry +
1300 // pending dom ui:
1301 // -> Use pending DOM UI if any.
1302 //
1303 // - First load in second tab: no committed nav entry + pending nav entry +
1304 // no pending DOM UI:
1305 // -> Use the committed DOM UI if any.
1306 //
1307 // - Second navigation in any tab: committed nav entry + pending nav entry:
1308 // -> Use pending DOM UI if any.
1309 //
1310 // - Normal state with no load: committed nav entry + no pending nav entry:
1311 // -> Use committed DOM UI.
1312 if (controller_.pending_entry() &&
1313 (controller_.GetLastCommittedEntry() ||
1314 render_manager_.pending_dom_ui()))
1315 return render_manager_.pending_dom_ui();
1316 return render_manager_.dom_ui();
1317}
[email protected]420ae012009-04-24 05:16:321318
1319void TabContents::DidNavigateMainFramePostCommit(
1320 const NavigationController::LoadCommittedDetails& details,
1321 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]c2e74fe82009-09-03 17:57:441322 if (opener_dom_ui_type_ != DOMUIFactory::kNoDOMUI) {
1323 // If this is a window.open navigation, use the same DOMUI as the renderer
1324 // that opened the window, as long as both renderers have the same
1325 // privileges.
1326 if (opener_dom_ui_type_ == DOMUIFactory::GetDOMUIType(GetURL())) {
1327 DOMUI* dom_ui = DOMUIFactory::CreateDOMUIForURL(this, GetURL());
1328 // dom_ui might be NULL if the URL refers to a non-existent extension.
1329 if (dom_ui) {
1330 render_manager_.SetDOMUIPostCommit(dom_ui);
1331 dom_ui->RenderViewCreated(render_view_host());
1332 }
1333 }
1334 opener_dom_ui_type_ = DOMUIFactory::kNoDOMUI;
1335 }
1336
[email protected]420ae012009-04-24 05:16:321337 if (details.is_user_initiated_main_frame_load()) {
1338 // Clear the status bubble. This is a workaround for a bug where WebKit
1339 // doesn't let us know that the cursor left an element during a
1340 // transition (this is also why the mouse cursor remains as a hand after
1341 // clicking on a link); see bugs 1184641 and 980803. We don't want to
1342 // clear the bubble when a user navigates to a named anchor in the same
1343 // page.
1344 UpdateTargetURL(details.entry->page_id(), GURL());
1345
1346 // UpdateHelpersForDidNavigate will handle the case where the password_form
1347 // origin is valid.
1348 // TODO(brettw) bug 1343111: Password manager stuff in here needs to be
1349 // cleaned up and covered by tests.
1350 if (!params.password_form.origin.is_valid())
1351 GetPasswordManager()->DidNavigate();
1352 }
1353
1354 // The keyword generator uses the navigation entries, so must be called after
1355 // the commit.
1356 GenerateKeywordIfNecessary(params);
1357
1358 // Allow the new page to set the title again.
1359 received_page_title_ = false;
1360
1361 // Get the favicon, either from history or request it from the net.
1362 fav_icon_helper_.FetchFavIcon(details.entry->url());
1363
[email protected]56ce6e52009-10-27 00:10:521364 // Clear all page and browser action state for this tab, unless this is an
1365 // in-page navigation.
[email protected]92c6f9b92009-10-24 04:35:081366 url_canon::Replacements<char> replacements;
1367 replacements.ClearRef();
1368 if (params.url.ReplaceComponents(replacements) !=
1369 params.referrer.ReplaceComponents(replacements)) {
[email protected]92c6f9b92009-10-24 04:35:081370 ExtensionsService* service = profile()->GetExtensionsService();
1371 if (service) {
1372 for (size_t i = 0; i < service->extensions()->size(); ++i) {
[email protected]5d246db22009-10-27 06:17:571373 ExtensionAction* browser_action =
[email protected]92c6f9b92009-10-24 04:35:081374 service->extensions()->at(i)->browser_action();
[email protected]56ce6e52009-10-27 00:10:521375 if (browser_action) {
1376 browser_action->ClearAllValuesForTab(controller().session_id().id());
1377 NotificationService::current()->Notify(
1378 NotificationType::EXTENSION_BROWSER_ACTION_UPDATED,
[email protected]5d246db22009-10-27 06:17:571379 Source<ExtensionAction>(browser_action),
[email protected]56ce6e52009-10-27 00:10:521380 NotificationService::NoDetails());
1381 }
[email protected]92c6f9b92009-10-24 04:35:081382
[email protected]5d246db22009-10-27 06:17:571383 ExtensionAction* page_action =
[email protected]56ce6e52009-10-27 00:10:521384 service->extensions()->at(i)->page_action();
1385 if (page_action) {
1386 page_action->ClearAllValuesForTab(controller().session_id().id());
1387 PageActionStateChanged();
1388 }
[email protected]92c6f9b92009-10-24 04:35:081389 }
[email protected]aece2c7f2009-08-27 20:43:171390 }
1391 }
[email protected]ce5c4502009-05-06 16:46:111392
[email protected]420ae012009-04-24 05:16:321393 // Close constrained popups if necessary.
1394 MaybeCloseChildWindows(details.previous_url, details.entry->url());
1395
1396 // Update the starred state.
1397 UpdateStarredStateForCurrentURL();
1398}
1399
1400void TabContents::DidNavigateAnyFramePostCommit(
1401 RenderViewHost* render_view_host,
1402 const NavigationController::LoadCommittedDetails& details,
1403 const ViewHostMsg_FrameNavigate_Params& params) {
1404 // If we navigate, start showing messages again. This does nothing to prevent
1405 // a malicious script from spamming messages, since the script could just
1406 // reload the page to stop blocking.
1407 suppress_javascript_messages_ = false;
1408
[email protected]420ae012009-04-24 05:16:321409 // Notify the password manager of the navigation or form submit.
1410 // TODO(brettw) bug 1343111: Password manager stuff in here needs to be
1411 // cleaned up and covered by tests.
1412 if (params.password_form.origin.is_valid())
1413 GetPasswordManager()->ProvisionallySavePassword(params.password_form);
1414}
1415
1416void TabContents::MaybeCloseChildWindows(const GURL& previous_url,
1417 const GURL& current_url) {
1418 if (net::RegistryControlledDomainService::SameDomainOrHost(
1419 previous_url, current_url))
1420 return;
1421
1422 // Clear out any child windows since we are leaving this page entirely.
1423 // We use indices instead of iterators in case CloseWindow does something
1424 // that may invalidate an iterator.
1425 int size = static_cast<int>(child_windows_.size());
1426 for (int i = size - 1; i >= 0; --i) {
1427 ConstrainedWindow* window = child_windows_[i];
[email protected]fa1cf0b82010-01-15 21:49:441428 if (window) {
1429 DCHECK(delegate_);
[email protected]420ae012009-04-24 05:16:321430 window->CloseConstrainedWindow();
[email protected]fa1cf0b82010-01-15 21:49:441431 BlockTabContent(false);
1432 }
[email protected]420ae012009-04-24 05:16:321433 }
[email protected]e29300cd2009-07-23 21:14:591434
1435 // Close the popup container.
1436 if (blocked_popups_) {
1437 blocked_popups_->Destroy();
1438 blocked_popups_ = NULL;
1439 }
[email protected]420ae012009-04-24 05:16:321440}
1441
1442void TabContents::UpdateStarredStateForCurrentURL() {
1443 BookmarkModel* model = profile()->GetBookmarkModel();
1444 const bool old_state = is_starred_;
1445 is_starred_ = (model && model->IsBookmarked(GetURL()));
1446
1447 if (is_starred_ != old_state && delegate())
1448 delegate()->URLStarredChanged(this, is_starred_);
1449}
1450
1451void TabContents::UpdateAlternateErrorPageURL() {
1452 GURL url = GetAlternateErrorPageURL();
1453 render_view_host()->SetAlternateErrorPageURL(url);
1454}
1455
1456void TabContents::UpdateWebPreferences() {
1457 render_view_host()->UpdateWebPreferences(GetWebkitPrefs());
1458}
1459
1460void TabContents::UpdateMaxPageIDIfNecessary(SiteInstance* site_instance,
1461 RenderViewHost* rvh) {
1462 // If we are creating a RVH for a restored controller, then we might
1463 // have more page IDs than the SiteInstance's current max page ID. We must
1464 // make sure that the max page ID is larger than any restored page ID.
1465 // Note that it is ok for conflicting page IDs to exist in another tab
1466 // (i.e., NavigationController), but if any page ID is larger than the max,
1467 // the back/forward list will get confused.
1468 int max_restored_page_id = controller_.max_restored_page_id();
1469 if (max_restored_page_id > 0) {
1470 int curr_max_page_id = site_instance->max_page_id();
1471 if (max_restored_page_id > curr_max_page_id) {
1472 // Need to update the site instance immediately.
1473 site_instance->UpdateMaxPageID(max_restored_page_id);
1474
1475 // Also tell the renderer to update its internal representation. We
1476 // need to reserve enough IDs to make all restored page IDs less than
1477 // the max.
1478 if (curr_max_page_id < 0)
1479 curr_max_page_id = 0;
1480 rvh->ReservePageIDRange(max_restored_page_id - curr_max_page_id);
1481 }
1482 }
1483}
1484
1485void TabContents::UpdateHistoryForNavigation(
[email protected]ebe89e062009-08-13 23:16:541486 const GURL& virtual_url,
[email protected]befd8d822009-07-01 04:51:471487 const NavigationController::LoadCommittedDetails& details,
[email protected]420ae012009-04-24 05:16:321488 const ViewHostMsg_FrameNavigate_Params& params) {
1489 if (profile()->IsOffTheRecord())
1490 return;
1491
1492 // Add to history service.
1493 HistoryService* hs = profile()->GetHistoryService(Profile::IMPLICIT_ACCESS);
1494 if (hs) {
1495 if (PageTransition::IsMainFrame(params.transition) &&
[email protected]ebe89e062009-08-13 23:16:541496 virtual_url != params.url) {
1497 // Hack on the "virtual" URL so that it will appear in history. For some
[email protected]420ae012009-04-24 05:16:321498 // types of URLs, we will display a magic URL that is different from where
1499 // the page is actually navigated. We want the user to see in history
[email protected]ebe89e062009-08-13 23:16:541500 // what they saw in the URL bar, so we add the virtual URL as a redirect.
1501 // This only applies to the main frame, as the virtual URL doesn't apply
[email protected]420ae012009-04-24 05:16:321502 // to sub-frames.
1503 std::vector<GURL> redirects = params.redirects;
1504 if (!redirects.empty())
[email protected]ebe89e062009-08-13 23:16:541505 redirects.back() = virtual_url;
1506 hs->AddPage(virtual_url, this, params.page_id, params.referrer,
[email protected]befd8d822009-07-01 04:51:471507 params.transition, redirects, details.did_replace_entry);
[email protected]420ae012009-04-24 05:16:321508 } else {
1509 hs->AddPage(params.url, this, params.page_id, params.referrer,
[email protected]befd8d822009-07-01 04:51:471510 params.transition, params.redirects,
1511 details.did_replace_entry);
[email protected]420ae012009-04-24 05:16:321512 }
1513 }
1514}
1515
1516bool TabContents::UpdateTitleForEntry(NavigationEntry* entry,
1517 const std::wstring& title) {
1518 // For file URLs without a title, use the pathname instead. In the case of a
1519 // synthesized title, we don't want the update to count toward the "one set
1520 // per page of the title to history."
1521 std::wstring final_title;
1522 bool explicit_set;
1523 if (entry->url().SchemeIsFile() && title.empty()) {
1524 final_title = UTF8ToWide(entry->url().ExtractFileName());
1525 explicit_set = false; // Don't count synthetic titles toward the set limit.
1526 } else {
1527 TrimWhitespace(title, TRIM_ALL, &final_title);
1528 explicit_set = true;
1529 }
1530
1531 if (final_title == UTF16ToWideHack(entry->title()))
1532 return false; // Nothing changed, don't bother.
1533
1534 entry->set_title(WideToUTF16Hack(final_title));
1535
1536 // Update the history system for this page.
1537 if (!profile()->IsOffTheRecord() && !received_page_title_) {
1538 HistoryService* hs =
1539 profile()->GetHistoryService(Profile::IMPLICIT_ACCESS);
1540 if (hs)
[email protected]ebe89e062009-08-13 23:16:541541 hs->SetPageTitle(entry->virtual_url(), final_title);
[email protected]420ae012009-04-24 05:16:321542
1543 // Don't allow the title to be saved again for explicitly set ones.
1544 received_page_title_ = explicit_set;
1545 }
1546
1547 // Lastly, set the title for the view.
1548 view_->SetPageTitle(final_title);
1549
1550 return true;
1551}
1552
1553void TabContents::NotifySwapped() {
1554 // After sending out a swap notification, we need to send a disconnect
1555 // notification so that clients that pick up a pointer to |this| can NULL the
1556 // pointer. See Bug 1230284.
1557 notify_disconnection_ = true;
1558 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341559 NotificationType::TAB_CONTENTS_SWAPPED,
[email protected]d82ed61e2009-06-16 02:46:221560 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321561 NotificationService::NoDetails());
1562}
1563
1564void TabContents::NotifyConnected() {
1565 notify_disconnection_ = true;
1566 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341567 NotificationType::TAB_CONTENTS_CONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221568 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321569 NotificationService::NoDetails());
1570}
1571
1572void TabContents::NotifyDisconnected() {
1573 if (!notify_disconnection_)
1574 return;
1575
1576 notify_disconnection_ = false;
1577 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341578 NotificationType::TAB_CONTENTS_DISCONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221579 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321580 NotificationService::NoDetails());
1581}
1582
1583void TabContents::GenerateKeywordIfNecessary(
1584 const ViewHostMsg_FrameNavigate_Params& params) {
1585 if (!params.searchable_form_url.is_valid())
1586 return;
1587
1588 if (profile()->IsOffTheRecord())
1589 return;
1590
1591 int last_index = controller_.last_committed_entry_index();
1592 // When there was no previous page, the last index will be 0. This is
1593 // normally due to a form submit that opened in a new tab.
1594 // TODO(brettw) bug 916126: we should support keywords when form submits
1595 // happen in new tabs.
1596 if (last_index <= 0)
1597 return;
1598 const NavigationEntry* previous_entry =
1599 controller_.GetEntryAtIndex(last_index - 1);
1600 if (IsFormSubmit(previous_entry)) {
1601 // Only generate a keyword if the previous page wasn't itself a form
1602 // submit.
1603 return;
1604 }
1605
1606 GURL keyword_url = previous_entry->user_typed_url().is_valid() ?
1607 previous_entry->user_typed_url() : previous_entry->url();
1608 std::wstring keyword =
1609 TemplateURLModel::GenerateKeyword(keyword_url, true); // autodetected
1610 if (keyword.empty())
1611 return;
1612
1613 TemplateURLModel* url_model = profile()->GetTemplateURLModel();
1614 if (!url_model)
1615 return;
1616
1617 if (!url_model->loaded()) {
1618 url_model->Load();
1619 return;
1620 }
1621
1622 const TemplateURL* current_url;
1623 std::wstring url = UTF8ToWide(params.searchable_form_url.spec());
1624 if (!url_model->CanReplaceKeyword(keyword, url, &current_url))
1625 return;
1626
1627 if (current_url) {
1628 if (current_url->originating_url().is_valid()) {
1629 // The existing keyword was generated from an OpenSearch description
1630 // document, don't regenerate.
1631 return;
1632 }
1633 url_model->Remove(current_url);
1634 }
1635 TemplateURL* new_url = new TemplateURL();
1636 new_url->set_keyword(keyword);
1637 new_url->set_short_name(keyword);
1638 new_url->SetURL(url, 0, 0);
1639 new_url->add_input_encoding(params.searchable_form_encoding);
1640 DCHECK(controller_.GetLastCommittedEntry());
1641 const GURL& favicon_url =
1642 controller_.GetLastCommittedEntry()->favicon().url();
1643 if (favicon_url.is_valid()) {
1644 new_url->SetFavIconURL(favicon_url);
1645 } else {
1646 // The favicon url isn't valid. This means there really isn't a favicon,
1647 // or the favicon url wasn't obtained before the load started. This assumes
1648 // the later.
1649 // TODO(sky): Need a way to set the favicon that doesn't involve generating
1650 // its url.
1651 new_url->SetFavIconURL(TemplateURL::GenerateFaviconURL(params.referrer));
1652 }
1653 new_url->set_safe_for_autoreplace(true);
1654 url_model->Add(new_url);
1655}
1656
[email protected]bf5c2ff392009-07-08 16:24:331657void TabContents::OnUserGesture() {
1658 // See comment in RenderViewHostDelegate::OnUserGesture as to why we do this.
1659 DownloadRequestManager* drm = g_browser_process->download_request_manager();
1660 if (drm)
1661 drm->OnUserGesture(this);
[email protected]e7eaedde2009-09-25 20:09:491662 ExternalProtocolHandler::OnUserGesture();
[email protected]bf5c2ff392009-07-08 16:24:331663 controller_.OnUserGesture();
1664}
1665
1666void TabContents::OnFindReply(int request_id,
1667 int number_of_matches,
1668 const gfx::Rect& selection_rect,
1669 int active_match_ordinal,
1670 bool final_update) {
[email protected]3ce7ced2009-07-24 18:52:421671 // Ignore responses for requests that have been aborted.
1672 if (find_op_aborted_)
1673 return;
1674
[email protected]bf5c2ff392009-07-08 16:24:331675 // Ignore responses for requests other than the one we have most recently
1676 // issued. That way we won't act on stale results when the user has
1677 // already typed in another query.
1678 if (request_id != current_find_request_id_)
1679 return;
1680
1681 if (number_of_matches == -1)
1682 number_of_matches = last_search_result_.number_of_matches();
1683 if (active_match_ordinal == -1)
1684 active_match_ordinal = last_search_result_.active_match_ordinal();
1685
1686 gfx::Rect selection = selection_rect;
1687 if (selection.IsEmpty())
1688 selection = last_search_result_.selection_rect();
1689
1690 // Notify the UI, automation and any other observers that a find result was
1691 // found.
1692 last_search_result_ = FindNotificationDetails(request_id, number_of_matches,
1693 selection, active_match_ordinal,
1694 final_update);
1695 NotificationService::current()->Notify(
1696 NotificationType::FIND_RESULT_AVAILABLE,
1697 Source<TabContents>(this),
1698 Details<FindNotificationDetails>(&last_search_result_));
1699}
1700
1701void TabContents::GoToEntryAtOffset(int offset) {
[email protected]f9cc4c452009-10-13 14:56:381702 if (!delegate_ || delegate_->OnGoToEntryOffset(offset))
1703 controller_.GoToOffset(offset);
[email protected]bf5c2ff392009-07-08 16:24:331704}
1705
1706void TabContents::GetHistoryListCount(int* back_list_count,
1707 int* forward_list_count) {
1708 int current_index = controller_.last_committed_entry_index();
1709 *back_list_count = current_index;
1710 *forward_list_count = controller_.entry_count() - current_index - 1;
1711}
1712
1713void TabContents::OnMissingPluginStatus(int status) {
1714#if defined(OS_WIN)
1715// TODO(PORT): pull in when plug-ins work
1716 GetPluginInstaller()->OnMissingPluginStatus(status);
1717#endif
1718}
1719
1720void TabContents::OnCrashedPlugin(const FilePath& plugin_path) {
[email protected]bf5c2ff392009-07-08 16:24:331721 DCHECK(!plugin_path.value().empty());
1722
1723 std::wstring plugin_name = plugin_path.ToWStringHack();
[email protected]d32d46182009-07-23 00:09:351724#if defined(OS_WIN) || defined(OS_MACOSX)
[email protected]bf5c2ff392009-07-08 16:24:331725 scoped_ptr<FileVersionInfo> version_info(
1726 FileVersionInfo::CreateFileVersionInfo(plugin_path));
1727 if (version_info.get()) {
1728 const std::wstring& product_name = version_info->product_name();
[email protected]996fd702009-09-04 19:12:371729 if (!product_name.empty()) {
[email protected]bf5c2ff392009-07-08 16:24:331730 plugin_name = product_name;
[email protected]996fd702009-09-04 19:12:371731#if defined(OS_MACOSX)
1732 // Many plugins on the Mac have .plugin in the actual name, which looks
1733 // terrible, so look for that and strip it off if present.
1734 const std::wstring plugin_extension(L".plugin");
1735 if (EndsWith(plugin_name, plugin_extension, true))
1736 plugin_name.erase(plugin_name.length() - plugin_extension.length());
1737#endif // OS_MACOSX
1738 }
[email protected]bf5c2ff392009-07-08 16:24:331739 }
[email protected]4a560ed2009-07-13 23:35:261740#else
1741 NOTIMPLEMENTED() << " convert plugin path to plugin name";
1742#endif
[email protected]bf5c2ff392009-07-08 16:24:331743 AddInfoBar(new SimpleAlertInfoBarDelegate(
1744 this, l10n_util::GetStringF(IDS_PLUGIN_CRASHED_PROMPT, plugin_name),
1745 NULL));
[email protected]bf5c2ff392009-07-08 16:24:331746}
1747
1748void TabContents::OnCrashedWorker() {
1749 AddInfoBar(new SimpleAlertInfoBarDelegate(
1750 this, l10n_util::GetString(IDS_WEBWORKER_CRASHED_PROMPT),
1751 NULL));
1752}
1753
1754void TabContents::OnDidGetApplicationInfo(
1755 int32 page_id,
1756 const webkit_glue::WebApplicationInfo& info) {
[email protected]ed5431872009-11-17 08:39:511757 web_app_info_ = info;
[email protected]bf5c2ff392009-07-08 16:24:331758
[email protected]ed5431872009-11-17 08:39:511759 if (delegate())
1760 delegate()->OnDidGetApplicationInfo(this, page_id);
[email protected]bf5c2ff392009-07-08 16:24:331761}
1762
[email protected]a2cf65eb2010-01-18 08:20:381763void TabContents::OnPageContents(const GURL& url,
1764 int renderer_process_id,
1765 int32 page_id,
1766 const std::wstring& contents) {
1767 // Don't index any https pages. People generally don't want their bank
1768 // accounts, etc. indexed on their computer, especially since some of these
1769 // things are not marked cachable.
1770 // TODO(brettw) we may want to consider more elaborate heuristics such as
1771 // the cachability of the page. We may also want to consider subframes (this
1772 // test will still index subframes if the subframe is SSL).
1773 if (!url.SchemeIsSecure()) {
1774 Profile* p = profile();
1775 if (p && !p->IsOffTheRecord()) {
1776 HistoryService* hs = p->GetHistoryService(Profile::IMPLICIT_ACCESS);
1777 if (hs)
1778 hs->SetPageContents(url, contents);
1779 }
1780 }
1781}
1782
[email protected]bf5c2ff392009-07-08 16:24:331783void TabContents::DidStartProvisionalLoadForFrame(
1784 RenderViewHost* render_view_host,
1785 bool is_main_frame,
1786 const GURL& url) {
1787 ProvisionalLoadDetails details(is_main_frame,
1788 controller_.IsURLInPageNavigation(url),
1789 url, std::string(), false);
1790 NotificationService::current()->Notify(
1791 NotificationType::FRAME_PROVISIONAL_LOAD_START,
1792 Source<NavigationController>(&controller_),
1793 Details<ProvisionalLoadDetails>(&details));
1794}
1795
1796void TabContents::DidStartReceivingResourceResponse(
1797 ResourceRequestDetails* details) {
1798 NotificationService::current()->Notify(
1799 NotificationType::RESOURCE_RESPONSE_STARTED,
1800 Source<NavigationController>(&controller()),
1801 Details<ResourceRequestDetails>(details));
1802}
1803
1804void TabContents::DidRedirectResource(ResourceRequestDetails* details) {
1805 NotificationService::current()->Notify(
1806 NotificationType::RESOURCE_RECEIVED_REDIRECT,
1807 Source<NavigationController>(&controller()),
1808 Details<ResourceRequestDetails>(details));
1809}
1810
1811void TabContents::DidLoadResourceFromMemoryCache(
1812 const GURL& url,
1813 const std::string& frame_origin,
1814 const std::string& main_frame_origin,
1815 const std::string& security_info) {
1816 // Send out a notification that we loaded a resource from our memory cache.
1817 int cert_id = 0, cert_status = 0, security_bits = 0;
1818 SSLManager::DeserializeSecurityInfo(security_info,
1819 &cert_id, &cert_status,
1820 &security_bits);
1821 LoadFromMemoryCacheDetails details(url, frame_origin, main_frame_origin,
[email protected]76543b92009-08-31 17:27:451822 process()->id(), cert_id, cert_status);
[email protected]bf5c2ff392009-07-08 16:24:331823
1824 NotificationService::current()->Notify(
1825 NotificationType::LOAD_FROM_MEMORY_CACHE,
1826 Source<NavigationController>(&controller_),
1827 Details<LoadFromMemoryCacheDetails>(&details));
1828}
1829
[email protected]e3d60e5d2009-09-25 21:08:291830void TabContents::DidDisplayInsecureContent() {
[email protected]09843b5f2009-09-26 08:30:591831 controller_.ssl_manager()->DidDisplayInsecureContent();
[email protected]e3d60e5d2009-09-25 21:08:291832}
1833
1834void TabContents::DidRunInsecureContent(const std::string& security_origin) {
[email protected]09843b5f2009-09-26 08:30:591835 controller_.ssl_manager()->DidRunInsecureContent(security_origin);
[email protected]e3d60e5d2009-09-25 21:08:291836}
1837
[email protected]bf5c2ff392009-07-08 16:24:331838void TabContents::DidFailProvisionalLoadWithError(
1839 RenderViewHost* render_view_host,
1840 bool is_main_frame,
1841 int error_code,
1842 const GURL& url,
1843 bool showing_repost_interstitial) {
1844 if (net::ERR_ABORTED == error_code) {
1845 // EVIL HACK ALERT! Ignore failed loads when we're showing interstitials.
1846 // This means that the interstitial won't be torn down properly, which is
1847 // bad. But if we have an interstitial, go back to another tab type, and
1848 // then load the same interstitial again, we could end up getting the first
1849 // interstitial's "failed" message (as a result of the cancel) when we're on
1850 // the second one.
1851 //
1852 // We can't tell this apart, so we think we're tearing down the current page
1853 // which will cause a crash later one. There is also some code in
1854 // RenderViewHostManager::RendererAbortedProvisionalLoad that is commented
1855 // out because of this problem.
1856 //
1857 // http://code.google.com/p/chromium/issues/detail?id=2855
1858 // Because this will not tear down the interstitial properly, if "back" is
1859 // back to another tab type, the interstitial will still be somewhat alive
1860 // in the previous tab type. If you navigate somewhere that activates the
1861 // tab with the interstitial again, you'll see a flash before the new load
1862 // commits of the interstitial page.
1863 if (showing_interstitial_page()) {
1864 LOG(WARNING) << "Discarding message during interstitial.";
1865 return;
1866 }
1867
1868 // This will discard our pending entry if we cancelled the load (e.g., if we
1869 // decided to download the file instead of load it). Only discard the
1870 // pending entry if the URLs match, otherwise the user initiated a navigate
1871 // before the page loaded so that the discard would discard the wrong entry.
1872 NavigationEntry* pending_entry = controller_.pending_entry();
1873 if (pending_entry && pending_entry->url() == url) {
1874 controller_.DiscardNonCommittedEntries();
1875 // Update the URL display.
1876 NotifyNavigationStateChanged(TabContents::INVALIDATE_URL);
1877 }
1878
1879 render_manager_.RendererAbortedProvisionalLoad(render_view_host);
1880 }
1881
1882 // Send out a notification that we failed a provisional load with an error.
1883 ProvisionalLoadDetails details(is_main_frame,
1884 controller_.IsURLInPageNavigation(url),
1885 url, std::string(), false);
1886 details.set_error_code(error_code);
1887
1888 NotificationService::current()->Notify(
1889 NotificationType::FAIL_PROVISIONAL_LOAD_WITH_ERROR,
1890 Source<NavigationController>(&controller_),
1891 Details<ProvisionalLoadDetails>(&details));
1892}
1893
1894void TabContents::DocumentLoadedInFrame() {
1895 controller_.DocumentLoadedInFrame();
1896}
1897
[email protected]8d3347f2009-07-09 22:00:211898RenderViewHostDelegate::View* TabContents::GetViewDelegate() {
[email protected]420ae012009-04-24 05:16:321899 return view_.get();
1900}
1901
[email protected]8d3347f2009-07-09 22:00:211902RenderViewHostDelegate::RendererManagement*
1903TabContents::GetRendererManagementDelegate() {
1904 return &render_manager_;
1905}
1906
[email protected]bf5c2ff392009-07-08 16:24:331907RenderViewHostDelegate::BrowserIntegration*
[email protected]8d3347f2009-07-09 22:00:211908 TabContents::GetBrowserIntegrationDelegate() {
1909 return this;
[email protected]bf5c2ff392009-07-08 16:24:331910}
1911
[email protected]8d3347f2009-07-09 22:00:211912RenderViewHostDelegate::Resource* TabContents::GetResourceDelegate() {
1913 return this;
[email protected]bf5c2ff392009-07-08 16:24:331914}
1915
[email protected]8d3347f2009-07-09 22:00:211916RenderViewHostDelegate::Save* TabContents::GetSaveDelegate() {
[email protected]420ae012009-04-24 05:16:321917 return save_package_.get(); // May be NULL, but we can return NULL.
1918}
1919
[email protected]8d3347f2009-07-09 22:00:211920RenderViewHostDelegate::Printing* TabContents::GetPrintingDelegate() {
1921 return &printing_;
1922}
1923
1924RenderViewHostDelegate::FavIcon* TabContents::GetFavIconDelegate() {
1925 return &fav_icon_helper_;
1926}
1927
[email protected]c54d269e2009-10-21 18:07:381928RenderViewHostDelegate::FormFieldHistory*
1929TabContents::GetFormFieldHistoryDelegate() {
[email protected]19289d02009-10-21 21:47:001930 if (form_field_history_manager_.get() == NULL)
1931 form_field_history_manager_.reset(new FormFieldHistoryManager(this));
1932 return form_field_history_manager_.get();
[email protected]bf5c2ff392009-07-08 16:24:331933}
1934
[email protected]21674df2009-10-28 19:51:211935RenderViewHostDelegate::AutoFill* TabContents::GetAutoFillDelegate() {
1936 if (autofill_manager_.get() == NULL)
1937 autofill_manager_.reset(new AutoFillManager(this));
1938 return autofill_manager_.get();
1939}
1940
[email protected]93623c5d2009-12-10 21:40:321941RendererPreferences TabContents::GetRendererPrefs(Profile* profile) const {
[email protected]840b1512009-07-21 16:53:461942 return renderer_preferences_;
[email protected]80d96fa2009-06-10 22:34:511943}
1944
[email protected]57c6a652009-05-04 07:58:341945TabContents* TabContents::GetAsTabContents() {
1946 return this;
1947}
1948
[email protected]3e7cf1362009-08-21 17:47:081949void TabContents::AddBlockedNotice(const GURL& url, const string16& reason) {
1950 CreateBlockedPopupContainerIfNecessary();
1951 blocked_popups_->AddBlockedNotice(url, reason);
1952}
1953
[email protected]7b291f92009-08-14 05:43:531954ViewType::Type TabContents::GetRenderViewType() const {
1955 return ViewType::TAB_CONTENTS;
1956}
1957
1958int TabContents::GetBrowserWindowID() const {
1959 return controller().window_id().id();
1960}
1961
[email protected]420ae012009-04-24 05:16:321962void TabContents::RenderViewCreated(RenderViewHost* render_view_host) {
[email protected]2e4633c2009-07-09 16:58:061963 NotificationService::current()->Notify(
1964 NotificationType::RENDER_VIEW_HOST_CREATED_FOR_TAB,
1965 Source<TabContents>(this),
1966 Details<RenderViewHost>(render_view_host));
[email protected]420ae012009-04-24 05:16:321967 NavigationEntry* entry = controller_.GetActiveEntry();
1968 if (!entry)
1969 return;
1970
1971 // When we're creating views, we're still doing initial setup, so we always
1972 // use the pending DOM UI rather than any possibly existing committed one.
1973 if (render_manager_.pending_dom_ui()) {
1974 render_manager_.pending_dom_ui()->RenderViewCreated(render_view_host);
1975 }
1976
1977 if (entry->IsViewSourceMode()) {
1978 // Put the renderer in view source mode.
1979 render_view_host->Send(
1980 new ViewMsg_EnableViewSourceMode(render_view_host->routing_id()));
1981 }
[email protected]0666aef2009-05-13 19:48:081982
1983 view()->RenderViewCreated(render_view_host);
[email protected]420ae012009-04-24 05:16:321984}
1985
1986void TabContents::RenderViewReady(RenderViewHost* rvh) {
1987 if (rvh != render_view_host()) {
1988 // Don't notify the world, since this came from a renderer in the
1989 // background.
1990 return;
1991 }
1992
1993 NotifyConnected();
[email protected]9d8a4642009-07-29 17:25:301994 bool was_crashed = is_crashed();
[email protected]420ae012009-04-24 05:16:321995 SetIsCrashed(false);
[email protected]9d8a4642009-07-29 17:25:301996
1997 // Restore the focus to the tab (otherwise the focus will be on the top
1998 // window).
1999 if (was_crashed && !FocusLocationBarByDefault())
2000 Focus();
[email protected]420ae012009-04-24 05:16:322001}
2002
2003void TabContents::RenderViewGone(RenderViewHost* rvh) {
2004 // Ask the print preview if this renderer was valuable.
2005 if (!printing_.OnRenderViewGone(rvh))
2006 return;
2007 if (rvh != render_view_host()) {
2008 // The pending page's RenderViewHost is gone.
2009 return;
2010 }
2011
2012 SetIsLoading(false, NULL);
2013 NotifyDisconnected();
2014 SetIsCrashed(true);
2015
[email protected]8e537302009-06-10 21:19:582016 // Tell the view that we've crashed so it can prepare the sad tab page.
[email protected]cf68b092010-01-15 20:05:552017 // Only do this if we're not in browser shutdown, so that TabContents
2018 // objects that are not in a browser (e.g., HTML dialogs) and thus are
2019 // visible do not flash a sad tab page.
2020 if (browser_shutdown::GetShutdownType() == browser_shutdown::NOT_VALID)
2021 view_->OnTabCrashed();
[email protected]420ae012009-04-24 05:16:322022
2023 // Hide any visible hung renderer warning for this web contents' process.
[email protected]e7b8d832009-11-20 22:41:002024 hung_renderer_dialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:322025}
2026
[email protected]2e4633c2009-07-09 16:58:062027void TabContents::RenderViewDeleted(RenderViewHost* rvh) {
2028 NotificationService::current()->Notify(
2029 NotificationType::RENDER_VIEW_HOST_DELETED,
2030 Source<TabContents>(this),
2031 Details<RenderViewHost>(rvh));
[email protected]fb7b79f72009-11-06 18:00:482032 render_manager_.RenderViewDeleted(rvh);
[email protected]2e4633c2009-07-09 16:58:062033}
2034
[email protected]420ae012009-04-24 05:16:322035void TabContents::DidNavigate(RenderViewHost* rvh,
2036 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]8030f012009-09-25 18:09:372037 int extra_invalidate_flags = 0;
2038
2039 if (PageTransition::IsMainFrame(params.transition)) {
[email protected]20ede072009-10-06 00:28:352040 bool was_bookmark_bar_visible = ShouldShowBookmarkBar();
[email protected]8030f012009-09-25 18:09:372041 bool was_extension_shelf_visible = IsExtensionShelfAlwaysVisible();
2042
[email protected]420ae012009-04-24 05:16:322043 render_manager_.DidNavigateMainFrame(rvh);
2044
[email protected]20ede072009-10-06 00:28:352045 if (was_bookmark_bar_visible != ShouldShowBookmarkBar())
[email protected]8030f012009-09-25 18:09:372046 extra_invalidate_flags |= INVALIDATE_BOOKMARK_BAR;
2047 if (was_extension_shelf_visible != IsExtensionShelfAlwaysVisible())
2048 extra_invalidate_flags |= INVALIDATE_EXTENSION_SHELF;
2049 }
2050
[email protected]420ae012009-04-24 05:16:322051 // Update the site of the SiteInstance if it doesn't have one yet.
2052 if (!GetSiteInstance()->has_site())
2053 GetSiteInstance()->SetSite(params.url);
2054
2055 // Need to update MIME type here because it's referred to in
2056 // UpdateNavigationCommands() called by RendererDidNavigate() to
2057 // determine whether or not to enable the encoding menu.
2058 // It's updated only for the main frame. For a subframe,
2059 // RenderView::UpdateURL does not set params.contents_mime_type.
2060 // (see http://code.google.com/p/chromium/issues/detail?id=2929 )
2061 // TODO(jungshik): Add a test for the encoding menu to avoid
2062 // regressing it again.
2063 if (PageTransition::IsMainFrame(params.transition))
2064 contents_mime_type_ = params.contents_mime_type;
2065
2066 NavigationController::LoadCommittedDetails details;
[email protected]8030f012009-09-25 18:09:372067 bool did_navigate = controller_.RendererDidNavigate(
2068 params, extra_invalidate_flags, &details);
[email protected]3e90d4a2009-07-03 17:38:392069
2070 // Update history. Note that this needs to happen after the entry is complete,
2071 // which WillNavigate[Main,Sub]Frame will do before this function is called.
[email protected]2e29d922009-10-22 22:13:352072 if (params.should_update_history &&
2073 (!delegate() || delegate()->ShouldAddNavigationsToHistory())) {
[email protected]3e90d4a2009-07-03 17:38:392074 // Most of the time, the displayURL matches the loaded URL, but for about:
2075 // URLs, we use a data: URL as the real value. We actually want to save
2076 // the about: URL to the history db and keep the data: URL hidden. This is
2077 // what the TabContents' URL getter does.
2078 UpdateHistoryForNavigation(GetURL(), details, params);
2079 }
2080
[email protected]76543b92009-08-31 17:27:452081 if (!did_navigate)
2082 return; // No navigation happened.
[email protected]420ae012009-04-24 05:16:322083
2084 // DO NOT ADD MORE STUFF TO THIS FUNCTION! Your component should either listen
2085 // for the appropriate notification (best) or you can add it to
2086 // DidNavigateMainFramePostCommit / DidNavigateAnyFramePostCommit (only if
2087 // necessary, please).
2088
2089 // Run post-commit tasks.
2090 if (details.is_main_frame)
2091 DidNavigateMainFramePostCommit(details, params);
2092 DidNavigateAnyFramePostCommit(rvh, details, params);
2093}
2094
2095void TabContents::UpdateState(RenderViewHost* rvh,
2096 int32 page_id,
2097 const std::string& state) {
2098 DCHECK(rvh == render_view_host());
2099
2100 // We must be prepared to handle state updates for any page, these occur
2101 // when the user is scrolling and entering form data, as well as when we're
2102 // leaving a page, in which case our state may have already been moved to
2103 // the next page. The navigation controller will look up the appropriate
2104 // NavigationEntry and update it when it is notified via the delegate.
2105
2106 int entry_index = controller_.GetEntryIndexWithPageID(
2107 GetSiteInstance(), page_id);
2108 if (entry_index < 0)
2109 return;
2110 NavigationEntry* entry = controller_.GetEntryAtIndex(entry_index);
2111
2112 if (state == entry->content_state())
2113 return; // Nothing to update.
2114 entry->set_content_state(state);
2115 controller_.NotifyEntryChanged(entry, entry_index);
2116}
2117
2118void TabContents::UpdateTitle(RenderViewHost* rvh,
2119 int32 page_id, const std::wstring& title) {
2120 // If we have a title, that's a pretty good indication that we've started
2121 // getting useful data.
2122 SetNotWaitingForResponse();
2123
2124 DCHECK(rvh == render_view_host());
2125 NavigationEntry* entry = controller_.GetEntryWithPageID(GetSiteInstance(),
2126 page_id);
2127 if (!entry || !UpdateTitleForEntry(entry, title))
2128 return;
2129
2130 // Broadcast notifications when the UI should be updated.
2131 if (entry == controller_.GetEntryAtOffset(0))
[email protected]f1cd5e82009-10-23 17:19:032132 NotifyNavigationStateChanged(INVALIDATE_TITLE);
[email protected]420ae012009-04-24 05:16:322133}
2134
[email protected]420ae012009-04-24 05:16:322135void TabContents::UpdateEncoding(RenderViewHost* render_view_host,
[email protected]41fc0322009-09-04 22:23:402136 const std::string& encoding) {
[email protected]420ae012009-04-24 05:16:322137 set_encoding(encoding);
2138}
2139
2140void TabContents::UpdateTargetURL(int32 page_id, const GURL& url) {
2141 if (delegate())
2142 delegate()->UpdateTargetURL(this, url);
2143}
2144
2145void TabContents::UpdateThumbnail(const GURL& url,
2146 const SkBitmap& bitmap,
2147 const ThumbnailScore& score) {
2148 // Tell History about this thumbnail
[email protected]449478302009-06-09 20:04:282149 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kThumbnailStore)) {
[email protected]72d0fc72009-07-09 17:30:492150 if (!profile()->IsOffTheRecord())
[email protected]42339402009-07-17 17:27:192151 profile()->GetThumbnailStore()->SetPageThumbnail(url, bitmap,
2152 score, true);
[email protected]449478302009-06-09 20:04:282153 } else {
2154 HistoryService* hs;
2155 if (!profile()->IsOffTheRecord() &&
2156 (hs = profile()->GetHistoryService(Profile::IMPLICIT_ACCESS))) {
2157 hs->SetPageThumbnail(url, bitmap, score);
2158 }
[email protected]420ae012009-04-24 05:16:322159 }
2160}
2161
[email protected]8922e1f2009-10-03 05:01:262162void TabContents::UpdateInspectorSettings(const std::string& raw_settings) {
[email protected]0df30122009-06-03 12:13:082163 profile()->GetPrefs()->SetString(prefs::kWebKitInspectorSettings,
[email protected]8922e1f2009-10-03 05:01:262164 UTF8ToWide(raw_settings));
[email protected]0df30122009-06-03 12:13:082165}
2166
[email protected]420ae012009-04-24 05:16:322167void TabContents::Close(RenderViewHost* rvh) {
[email protected]07707302009-11-06 00:50:292168 // The UI may be in an event-tracking loop, such as between the
2169 // mouse-down and mouse-up in text selection or a button click.
2170 // Defer the close until after tracking is complete, so that we
2171 // don't free objects out from under the UI.
2172 // TODO(shess): This could probably be integrated with the
2173 // IsDoingDrag() test below. Punting for now because I need more
2174 // research to understand how this impacts platforms other than Mac.
2175 // TODO(shess): This could get more fine-grained. For instance,
2176 // closing a tab in another window while selecting text in the
2177 // current window's Omnibox should be just fine.
2178 if (view()->IsEventTracking()) {
2179 view()->CloseTabAfterEventTracking();
2180 return;
2181 }
2182
[email protected]24a4d1062009-07-10 23:10:422183 // If we close the tab while we're in the middle of a drag, we'll crash.
2184 // Instead, cancel the drag and close it as soon as the drag ends.
2185 if (view()->IsDoingDrag()) {
2186 view()->CancelDragAndCloseTab();
2187 return;
2188 }
2189
[email protected]420ae012009-04-24 05:16:322190 // Ignore this if it comes from a RenderViewHost that we aren't showing.
2191 if (delegate() && rvh == render_view_host())
2192 delegate()->CloseContents(this);
2193}
2194
2195void TabContents::RequestMove(const gfx::Rect& new_bounds) {
2196 if (delegate() && delegate()->IsPopup(this))
2197 delegate()->MoveContents(this, new_bounds);
2198}
2199
[email protected]7ab1e7d62009-10-14 23:32:012200void TabContents::DidStartLoading() {
[email protected]420ae012009-04-24 05:16:322201 SetIsLoading(true, NULL);
2202}
2203
[email protected]7ab1e7d62009-10-14 23:32:012204void TabContents::DidStopLoading() {
[email protected]420ae012009-04-24 05:16:322205 scoped_ptr<LoadNotificationDetails> details;
2206
2207 NavigationEntry* entry = controller_.GetActiveEntry();
2208 // An entry may not exist for a stop when loading an initial blank page or
2209 // if an iframe injected by script into a blank page finishes loading.
2210 if (entry) {
[email protected]420ae012009-04-24 05:16:322211 base::TimeDelta elapsed = base::TimeTicks::Now() - current_load_start_;
2212
2213 details.reset(new LoadNotificationDetails(
[email protected]ebe89e062009-08-13 23:16:542214 entry->virtual_url(),
[email protected]420ae012009-04-24 05:16:322215 entry->transition_type(),
2216 elapsed,
2217 &controller_,
2218 controller_.GetCurrentEntryIndex()));
2219 }
2220
2221 // Tell PasswordManager we've finished a page load, which serves as a
2222 // green light to save pending passwords and reset itself.
2223 GetPasswordManager()->DidStopLoading();
2224
2225 SetIsLoading(false, details.get());
2226}
2227
[email protected]420ae012009-04-24 05:16:322228void TabContents::DidRedirectProvisionalLoad(int32 page_id,
2229 const GURL& source_url,
2230 const GURL& target_url) {
2231 NavigationEntry* entry;
2232 if (page_id == -1)
2233 entry = controller_.pending_entry();
2234 else
2235 entry = controller_.GetEntryWithPageID(GetSiteInstance(), page_id);
2236 if (!entry || entry->url() != source_url)
2237 return;
2238 entry->set_url(target_url);
2239}
2240
[email protected]420ae012009-04-24 05:16:322241void TabContents::RequestOpenURL(const GURL& url, const GURL& referrer,
2242 WindowOpenDisposition disposition) {
2243 if (render_manager_.dom_ui()) {
2244 // When we're a DOM UI, it will provide a page transition type for us (this
2245 // is so the new tab page can specify AUTO_BOOKMARK for automatically
2246 // generated suggestions).
2247 //
2248 // Note also that we hide the referrer for DOM UI pages. We don't really
[email protected]60e448982009-05-06 04:21:162249 // want web sites to see a referrer of "chrome://blah" (and some
2250 // chrome: URLs might have search terms or other stuff we don't want to
[email protected]420ae012009-04-24 05:16:322251 // send to the site), so we send no referrer.
2252 OpenURL(url, GURL(), disposition,
2253 render_manager_.dom_ui()->link_transition_type());
2254 } else {
2255 OpenURL(url, referrer, disposition, PageTransition::LINK);
2256 }
2257}
2258
[email protected]1ee614862009-07-06 20:29:182259void TabContents::DomOperationResponse(const std::string& json_string,
2260 int automation_id) {
2261}
2262
[email protected]420ae012009-04-24 05:16:322263void TabContents::ProcessDOMUIMessage(const std::string& message,
[email protected]e4dad9fb2009-10-06 18:15:582264 const Value* content,
[email protected]811bfe32009-07-01 08:46:252265 int request_id,
2266 bool has_callback) {
[email protected]420ae012009-04-24 05:16:322267 if (!render_manager_.dom_ui()) {
[email protected]c2e74fe82009-09-03 17:57:442268 // This can happen if someone uses window.open() to open an extension URL
2269 // from a non-extension context.
2270 render_view_host()->BlockExtensionRequest(request_id);
[email protected]420ae012009-04-24 05:16:322271 return;
2272 }
[email protected]811bfe32009-07-01 08:46:252273 render_manager_.dom_ui()->ProcessDOMUIMessage(message, content, request_id,
2274 has_callback);
[email protected]420ae012009-04-24 05:16:322275}
2276
2277void TabContents::ProcessExternalHostMessage(const std::string& message,
2278 const std::string& origin,
2279 const std::string& target) {
2280 if (delegate())
2281 delegate()->ForwardMessageToExternalHost(message, origin, target);
2282}
2283
[email protected]420ae012009-04-24 05:16:322284void TabContents::RunFileChooser(bool multiple_files,
2285 const string16& title,
2286 const FilePath& default_file) {
2287 if (!select_file_dialog_.get())
2288 select_file_dialog_ = SelectFileDialog::Create(this);
2289 SelectFileDialog::Type dialog_type =
2290 multiple_files ? SelectFileDialog::SELECT_OPEN_MULTI_FILE :
2291 SelectFileDialog::SELECT_OPEN_FILE;
2292 select_file_dialog_->SelectFile(dialog_type, title, default_file,
2293 NULL, 0, FILE_PATH_LITERAL(""),
2294 view_->GetTopLevelNativeWindow(), NULL);
2295}
2296
2297void TabContents::RunJavaScriptMessage(
2298 const std::wstring& message,
2299 const std::wstring& default_prompt,
2300 const GURL& frame_url,
2301 const int flags,
2302 IPC::Message* reply_msg,
2303 bool* did_suppress_message) {
2304 // Suppress javascript messages when requested and when inside a constrained
2305 // popup window (because that activates them and breaks them out of the
2306 // constrained window jail).
[email protected]91ed49a2009-07-07 21:29:552307 // Also suppress messages when showing an interstitial. The interstitial is
2308 // shown over the previous page, we don't want the hidden page dialogs to
2309 // interfere with the interstitial.
2310 bool suppress_this_message = suppress_javascript_messages_ ||
2311 showing_interstitial_page();
[email protected]420ae012009-04-24 05:16:322312 if (delegate())
2313 suppress_this_message |=
[email protected]b6c874582009-05-08 19:38:312314 (delegate()->GetConstrainingContents(this) != this);
[email protected]420ae012009-04-24 05:16:322315
2316 *did_suppress_message = suppress_this_message;
2317
2318 if (!suppress_this_message) {
2319 base::TimeDelta time_since_last_message(
2320 base::TimeTicks::Now() - last_javascript_message_dismissal_);
2321 bool show_suppress_checkbox = false;
2322 // Show a checkbox offering to suppress further messages if this message is
2323 // being displayed within kJavascriptMessageExpectedDelay of the last one.
2324 if (time_since_last_message <
2325 base::TimeDelta::FromMilliseconds(kJavascriptMessageExpectedDelay))
2326 show_suppress_checkbox = true;
2327
[email protected]d82ed61e2009-06-16 02:46:222328 RunJavascriptMessageBox(this, frame_url, flags, message, default_prompt,
[email protected]420ae012009-04-24 05:16:322329 show_suppress_checkbox, reply_msg);
2330 } else {
2331 // If we are suppressing messages, just reply as is if the user immediately
2332 // pressed "Cancel".
[email protected]beb440c2009-11-06 04:08:542333 OnMessageBoxClosed(reply_msg, false, std::wstring());
[email protected]420ae012009-04-24 05:16:322334 }
2335}
2336
2337void TabContents::RunBeforeUnloadConfirm(const std::wstring& message,
2338 IPC::Message* reply_msg) {
[email protected]7ab1e7d62009-10-14 23:32:012339 is_showing_before_unload_dialog_ = true;
[email protected]d82ed61e2009-06-16 02:46:222340 RunBeforeUnloadDialog(this, message, reply_msg);
[email protected]420ae012009-04-24 05:16:322341}
2342
2343void TabContents::ShowModalHTMLDialog(const GURL& url, int width, int height,
2344 const std::string& json_arguments,
2345 IPC::Message* reply_msg) {
2346 if (delegate()) {
2347 HtmlDialogUIDelegate* dialog_delegate =
2348 new ModalHtmlDialogDelegate(url, width, height, json_arguments,
[email protected]d82ed61e2009-06-16 02:46:222349 reply_msg, this);
[email protected]420ae012009-04-24 05:16:322350 delegate()->ShowHtmlDialog(dialog_delegate, NULL);
2351 }
2352}
2353
2354void TabContents::PasswordFormsSeen(
[email protected]ed3fb032009-06-16 19:50:562355 const std::vector<webkit_glue::PasswordForm>& forms) {
[email protected]420ae012009-04-24 05:16:322356 GetPasswordManager()->PasswordFormsSeen(forms);
2357}
2358
[email protected]420ae012009-04-24 05:16:322359// Checks to see if we should generate a keyword based on the OSDD, and if
2360// necessary uses TemplateURLFetcher to download the OSDD and create a keyword.
2361void TabContents::PageHasOSDD(RenderViewHost* render_view_host,
2362 int32 page_id, const GURL& url,
2363 bool autodetected) {
2364 // Make sure page_id is the current page, and the TemplateURLModel is loaded.
2365 DCHECK(url.is_valid());
2366 if (!IsActiveEntry(page_id))
2367 return;
2368 TemplateURLModel* url_model = profile()->GetTemplateURLModel();
2369 if (!url_model)
2370 return;
2371 if (!url_model->loaded()) {
2372 url_model->Load();
2373 return;
2374 }
2375 if (!profile()->GetTemplateURLFetcher())
2376 return;
2377
2378 if (profile()->IsOffTheRecord())
2379 return;
2380
2381 const NavigationEntry* entry = controller_.GetLastCommittedEntry();
2382 DCHECK(entry);
2383
2384 const NavigationEntry* base_entry = entry;
2385 if (IsFormSubmit(base_entry)) {
2386 // If the current page is a form submit, find the last page that was not
2387 // a form submit and use its url to generate the keyword from.
2388 int index = controller_.last_committed_entry_index() - 1;
2389 while (index >= 0 && IsFormSubmit(controller_.GetEntryAtIndex(index)))
2390 index--;
2391 if (index >= 0)
2392 base_entry = controller_.GetEntryAtIndex(index);
2393 else
2394 base_entry = NULL;
2395 }
2396
2397 // We want to use the user typed URL if available since that represents what
2398 // the user typed to get here, and fall back on the regular URL if not.
2399 if (!base_entry)
2400 return;
2401 GURL keyword_url = base_entry->user_typed_url().is_valid() ?
2402 base_entry->user_typed_url() : base_entry->url();
2403 if (!keyword_url.is_valid())
2404 return;
2405 std::wstring keyword = TemplateURLModel::GenerateKeyword(keyword_url,
2406 autodetected);
[email protected]a88dbdd52010-01-15 19:25:532407
2408 // For JS added OSDD empty keyword is OK because we will generate keyword
2409 // later from OSDD content.
2410 if (autodetected && keyword.empty())
[email protected]420ae012009-04-24 05:16:322411 return;
2412 const TemplateURL* template_url =
2413 url_model->GetTemplateURLForKeyword(keyword);
2414 if (template_url && (!template_url->safe_for_autoreplace() ||
2415 template_url->originating_url() == url)) {
2416 // Either there is a user created TemplateURL for this keyword, or the
2417 // keyword has the same OSDD url and we've parsed it.
2418 return;
2419 }
2420
2421 // Download the OpenSearch description document. If this is successful a
2422 // new keyword will be created when done.
[email protected]420ae012009-04-24 05:16:322423 profile()->GetTemplateURLFetcher()->ScheduleDownload(
2424 keyword,
2425 url,
2426 base_entry->favicon().url(),
[email protected]d0cc9fb52009-06-23 02:26:162427 this,
[email protected]420ae012009-04-24 05:16:322428 autodetected);
2429}
2430
[email protected]420ae012009-04-24 05:16:322431GURL TabContents::GetAlternateErrorPageURL() const {
2432 GURL url;
2433 // Disable alternate error pages when in OffTheRecord/Incognito mode.
2434 if (profile()->IsOffTheRecord())
2435 return url;
2436
2437 PrefService* prefs = profile()->GetPrefs();
2438 DCHECK(prefs);
2439 if (prefs->GetBoolean(prefs::kAlternateErrorPagesEnabled)) {
[email protected]a33457b82009-09-21 23:07:152440 url = google_util::AppendGoogleLocaleParam(
2441 GURL(google_util::kLinkDoctorBaseURL));
[email protected]420ae012009-04-24 05:16:322442 url = google_util::AppendGoogleTLDParam(url);
2443 }
2444 return url;
2445}
2446
2447WebPreferences TabContents::GetWebkitPrefs() {
2448 PrefService* prefs = render_view_host()->process()->profile()->GetPrefs();
[email protected]7ccddb8c2009-08-04 17:36:552449 bool is_dom_ui = false;
2450 return RenderViewHostDelegateHelper::GetWebkitPrefs(prefs, is_dom_ui);
[email protected]420ae012009-04-24 05:16:322451}
2452
[email protected]fa1cf0b82010-01-15 21:49:442453void TabContents::OnIgnoredUIEvent() {
2454 if (constrained_window_count()) {
2455 ConstrainedWindow* window = *constrained_window_begin();
2456 window->FocusConstrainedWindow();
2457 }
2458}
2459
[email protected]420ae012009-04-24 05:16:322460void TabContents::OnJSOutOfMemory() {
2461 AddInfoBar(new SimpleAlertInfoBarDelegate(
2462 this, l10n_util::GetString(IDS_JS_OUT_OF_MEMORY_PROMPT), NULL));
2463}
2464
[email protected]420ae012009-04-24 05:16:322465void TabContents::OnCrossSiteResponse(int new_render_process_host_id,
2466 int new_request_id) {
[email protected]57c6a652009-05-04 07:58:342467 // Allows the TabContents to react when a cross-site response is ready to be
[email protected]420ae012009-04-24 05:16:322468 // delivered to a pending RenderViewHost. We must first run the onunload
2469 // handler of the old RenderViewHost before we can allow it to proceed.
2470 render_manager_.OnCrossSiteResponse(new_render_process_host_id,
2471 new_request_id);
2472}
2473
2474bool TabContents::CanBlur() const {
2475 return delegate() ? delegate()->CanBlur() : true;
2476}
2477
2478gfx::Rect TabContents::GetRootWindowResizerRect() const {
2479 if (delegate())
2480 return delegate()->GetRootWindowResizerRect();
2481 return gfx::Rect();
2482}
2483
2484void TabContents::RendererUnresponsive(RenderViewHost* rvh,
2485 bool is_during_unload) {
2486 if (is_during_unload) {
2487 // Hang occurred while firing the beforeunload/unload handler.
2488 // Pretend the handler fired so tab closing continues as if it had.
[email protected]829e7612009-04-25 01:15:112489 rvh->set_sudden_termination_allowed(true);
[email protected]420ae012009-04-24 05:16:322490
2491 if (!render_manager_.ShouldCloseTabOnUnresponsiveRenderer())
2492 return;
2493
2494 // If the tab hangs in the beforeunload/unload handler there's really
2495 // nothing we can do to recover. Pretend the unload listeners have
2496 // all fired and close the tab. If the hang is in the beforeunload handler
2497 // then the user will not have the option of cancelling the close.
2498 Close(rvh);
2499 return;
2500 }
2501
2502 if (render_view_host() && render_view_host()->IsRenderViewLive())
[email protected]e7b8d832009-11-20 22:41:002503 hung_renderer_dialog::ShowForTabContents(this);
[email protected]420ae012009-04-24 05:16:322504}
2505
2506void TabContents::RendererResponsive(RenderViewHost* render_view_host) {
[email protected]e7b8d832009-11-20 22:41:002507 hung_renderer_dialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:322508}
2509
2510void TabContents::LoadStateChanged(const GURL& url,
[email protected]094e5b22009-09-25 04:23:562511 net::LoadState load_state,
2512 uint64 upload_position,
2513 uint64 upload_size) {
[email protected]420ae012009-04-24 05:16:322514 load_state_ = load_state;
[email protected]094e5b22009-09-25 04:23:562515 upload_position_ = upload_position;
2516 upload_size_ = upload_size;
[email protected]d686e812009-06-03 19:10:292517 std::wstring languages =
2518 profile()->GetPrefs()->GetString(prefs::kAcceptLanguages);
[email protected]d686e812009-06-03 19:10:292519 std::string host = url.host();
[email protected]ce85f602009-11-07 01:34:532520 load_state_host_ =
2521 net::IDNToUnicode(host.c_str(), host.size(), languages, NULL);
[email protected]420ae012009-04-24 05:16:322522 if (load_state_ == net::LOAD_STATE_READING_RESPONSE)
2523 SetNotWaitingForResponse();
2524 if (is_loading())
[email protected]c9cd2222009-05-06 05:16:502525 NotifyNavigationStateChanged(INVALIDATE_LOAD | INVALIDATE_TAB);
[email protected]420ae012009-04-24 05:16:322526}
2527
[email protected]1ee614862009-07-06 20:29:182528bool TabContents::IsExternalTabContainer() const {
2529 if (!delegate())
2530 return false;
2531
2532 return delegate()->IsExternalTabContainer();
2533}
2534
[email protected]1ee614862009-07-06 20:29:182535void TabContents::DidInsertCSS() {
2536 // This RVHDelegate function is used for extensions and not us.
[email protected]420ae012009-04-24 05:16:322537}
2538
[email protected]130efb02009-09-18 18:54:352539void TabContents::FocusedNodeChanged() {
2540 NotificationService::current()->Notify(
2541 NotificationType::FOCUS_CHANGED_IN_PAGE,
2542 Source<RenderViewHost>(render_view_host()),
2543 NotificationService::NoDetails());
2544}
2545
[email protected]420ae012009-04-24 05:16:322546void TabContents::FileSelected(const FilePath& path,
2547 int index, void* params) {
2548 render_view_host()->FileSelected(path);
2549}
2550
2551void TabContents::MultiFilesSelected(const std::vector<FilePath>& files,
2552 void* params) {
2553 render_view_host()->MultiFilesSelected(files);
2554}
2555
2556void TabContents::FileSelectionCanceled(void* params) {
2557 // If the user cancels choosing a file to upload we pass back an
2558 // empty vector.
2559 render_view_host()->MultiFilesSelected(std::vector<FilePath>());
2560}
2561
2562void TabContents::BeforeUnloadFiredFromRenderManager(
2563 bool proceed,
2564 bool* proceed_to_fire_unload) {
2565 if (delegate())
2566 delegate()->BeforeUnloadFired(this, proceed, proceed_to_fire_unload);
2567}
2568
2569void TabContents::UpdateRenderViewSizeForRenderManager() {
2570 // TODO(brettw) this is a hack. See TabContentsView::SizeContents.
2571 view_->SizeContents(view_->GetContainerSize());
2572}
2573
2574DOMUI* TabContents::CreateDOMUIForRenderManager(const GURL& url) {
[email protected]d82ed61e2009-06-16 02:46:222575 return DOMUIFactory::CreateDOMUIForURL(this, url);
[email protected]420ae012009-04-24 05:16:322576}
2577
2578NavigationEntry*
2579TabContents::GetLastCommittedNavigationEntryForRenderManager() {
2580 return controller_.GetLastCommittedEntry();
2581}
2582
2583bool TabContents::CreateRenderViewForRenderManager(
2584 RenderViewHost* render_view_host) {
[email protected]811bfe32009-07-01 08:46:252585 // If the pending navigation is to a DOMUI, tell the RenderView about any
2586 // bindings it will need enabled.
[email protected]420ae012009-04-24 05:16:322587 if (render_manager_.pending_dom_ui())
[email protected]811bfe32009-07-01 08:46:252588 render_view_host->AllowBindings(
2589 render_manager_.pending_dom_ui()->bindings());
[email protected]1adff062009-06-02 18:39:552590
[email protected]420ae012009-04-24 05:16:322591 RenderWidgetHostView* rwh_view = view_->CreateViewForWidget(render_view_host);
[email protected]1a98a932009-11-17 00:12:522592
2593 scoped_refptr<URLRequestContextGetter> request_context = request_context_;
2594 if (!request_context.get())
2595 request_context = profile()->GetRequestContext();
2596
2597 if (!render_view_host->CreateRenderView(request_context))
[email protected]420ae012009-04-24 05:16:322598 return false;
2599
2600 // Now that the RenderView has been created, we need to tell it its size.
2601 rwh_view->SetSize(view_->GetContainerSize());
2602
2603 UpdateMaxPageIDIfNecessary(render_view_host->site_instance(),
2604 render_view_host);
2605 return true;
2606}
2607
2608void TabContents::Observe(NotificationType type,
2609 const NotificationSource& source,
2610 const NotificationDetails& details) {
2611 switch (type.value) {
2612 case NotificationType::BOOKMARK_MODEL_LOADED:
2613 // BookmarkModel finished loading, fall through to update starred state.
2614 case NotificationType::URLS_STARRED: {
2615 // Somewhere, a URL has been starred.
2616 // Ignore notifications for profiles other than our current one.
2617 Profile* source_profile = Source<Profile>(source).ptr();
2618 if (!source_profile || !source_profile->IsSameProfile(profile()))
2619 return;
2620
2621 UpdateStarredStateForCurrentURL();
2622 break;
2623 }
2624 case NotificationType::PREF_CHANGED: {
2625 std::wstring* pref_name_in = Details<std::wstring>(details).ptr();
2626 DCHECK(Source<PrefService>(source).ptr() == profile()->GetPrefs());
2627 if (*pref_name_in == prefs::kAlternateErrorPagesEnabled) {
2628 UpdateAlternateErrorPageURL();
2629 } else if (*pref_name_in == prefs::kDefaultCharset ||
2630 StartsWithASCII(WideToUTF8(*pref_name_in), "webkit.webprefs.", true)
2631 ) {
2632 UpdateWebPreferences();
2633 } else {
2634 NOTREACHED() << "unexpected pref change notification" << *pref_name_in;
2635 }
2636 break;
2637 }
2638 case NotificationType::RENDER_WIDGET_HOST_DESTROYED:
2639 view_->RenderWidgetHostDestroyed(Source<RenderWidgetHost>(source).ptr());
2640 break;
2641
2642 case NotificationType::NAV_ENTRY_COMMITTED: {
2643 DCHECK(&controller_ == Source<NavigationController>(source).ptr());
2644
2645 NavigationController::LoadCommittedDetails& committed_details =
2646 *(Details<NavigationController::LoadCommittedDetails>(details).ptr());
2647 ExpireInfoBars(committed_details);
2648 break;
2649 }
2650
[email protected]d3ba77272009-09-03 00:06:092651#if defined(OS_LINUX)
2652 case NotificationType::BROWSER_THEME_CHANGED: {
[email protected]7ff431e2010-01-07 18:18:542653 renderer_preferences_util::UpdateFromSystemSettings(
2654 &renderer_preferences_, profile());
[email protected]d3ba77272009-09-03 00:06:092655 render_view_host()->SyncRendererPrefs();
2656 break;
2657 }
2658#endif
2659
[email protected]420ae012009-04-24 05:16:322660 default:
2661 NOTREACHED();
2662 }
2663}
[email protected]aacd7cc2009-09-02 14:51:102664
[email protected]beb440c2009-11-06 04:08:542665std::wstring TabContents::GetMessageBoxTitle(const GURL& frame_url,
2666 bool is_alert) {
2667 if (!frame_url.has_host())
2668 return l10n_util::GetString(
2669 is_alert ? IDS_JAVASCRIPT_ALERT_DEFAULT_TITLE
2670 : IDS_JAVASCRIPT_MESSAGEBOX_DEFAULT_TITLE);
2671
2672 // We really only want the scheme, hostname, and port.
2673 GURL::Replacements replacements;
2674 replacements.ClearUsername();
2675 replacements.ClearPassword();
2676 replacements.ClearPath();
2677 replacements.ClearQuery();
2678 replacements.ClearRef();
2679 GURL clean_url = frame_url.ReplaceComponents(replacements);
2680
2681 // TODO(brettw) it should be easier than this to do the correct language
2682 // handling without getting the accept language from the profile.
2683 std::wstring base_address = gfx::ElideUrl(clean_url, gfx::Font(), 0,
2684 profile()->GetPrefs()->GetString(prefs::kAcceptLanguages));
2685 // Force URL to have LTR directionality.
2686 if (l10n_util::GetTextDirection() == l10n_util::RIGHT_TO_LEFT)
2687 l10n_util::WrapStringWithLTRFormatting(&base_address);
2688
2689 return l10n_util::GetStringF(
2690 is_alert ? IDS_JAVASCRIPT_ALERT_TITLE : IDS_JAVASCRIPT_MESSAGEBOX_TITLE,
2691 base_address);
2692}
2693
2694gfx::NativeWindow TabContents::GetMessageBoxRootWindow() {
2695 return view_->GetTopLevelNativeWindow();
2696}
2697
2698void TabContents::OnMessageBoxClosed(IPC::Message* reply_msg,
2699 bool success,
2700 const std::wstring& prompt) {
2701 last_javascript_message_dismissal_ = base::TimeTicks::Now();
2702 if (is_showing_before_unload_dialog_ && !success) {
2703 // If a beforeunload dialog is canceled, we need to stop the throbber from
2704 // spinning, since we forced it to start spinning in Navigate.
2705 DidStopLoading();
2706
2707 tab_close_start_time_ = base::TimeTicks();
2708 }
2709 is_showing_before_unload_dialog_ = false;
2710 render_view_host()->JavaScriptMessageBoxClosed(reply_msg, success, prompt);
2711}
2712
2713void TabContents::SetSuppressMessageBoxes(bool suppress_message_boxes) {
2714 set_suppress_javascript_messages(suppress_message_boxes);
2715}
2716
[email protected]41fc0322009-09-04 22:23:402717void TabContents::set_encoding(const std::string& encoding) {
[email protected]aacd7cc2009-09-02 14:51:102718 encoding_ = CharacterEncoding::GetCanonicalEncodingNameByAliasName(encoding);
2719}