blob: 097cb3768286865c6ea5d896e1c833058541061f [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];
334 if (window)
335 window->CloseConstrainedWindow();
336 }
337
[email protected]66ba4932009-06-04 19:22:13338 if (blocked_popups_)
339 blocked_popups_->Destroy();
340
[email protected]420ae012009-04-24 05:16:32341 // Notify any observer that have a reference on this tab contents.
342 NotificationService::current()->Notify(
343 NotificationType::TAB_CONTENTS_DESTROYED,
344 Source<TabContents>(this),
345 NotificationService::NoDetails());
346
[email protected]1e5e74f2009-05-27 20:55:12347 // Notify any lasting InfobarDelegates that have not yet been removed that
348 // whatever infobar they were handling in this TabContents has closed,
349 // because the TabContents is going away entirely.
350 // This must happen after the TAB_CONTENTS_DESTROYED notification as the
351 // notification may trigger infobars calls that access their delegate. (and
352 // some implementations of InfoBarDelegate do delete themselves on
353 // InfoBarClosed()).
354 for (int i = 0; i < infobar_delegate_count(); ++i) {
355 InfoBarDelegate* delegate = GetInfoBarDelegateAt(i);
356 delegate->InfoBarClosed();
357 }
358 infobar_delegates_.clear();
359
[email protected]420ae012009-04-24 05:16:32360 // TODO(brettw) this should be moved to the view.
361#if defined(OS_WIN)
362 // If we still have a window handle, destroy it. GetNativeView can return
363 // NULL if this contents was part of a window that closed.
364 if (GetNativeView())
365 ::DestroyWindow(GetNativeView());
366#endif
[email protected]7ab1e7d62009-10-14 23:32:01367
368 // OnCloseStarted isn't called in unit tests.
369 if (!tab_close_start_time_.is_null()) {
370 UMA_HISTOGRAM_TIMES("Tab.Close",
371 base::TimeTicks::Now() - tab_close_start_time_);
372 }
initial.commit09911bf2008-07-26 23:55:29373}
374
[email protected]d5f942ba2008-09-26 19:30:34375// static
376void TabContents::RegisterUserPrefs(PrefService* prefs) {
[email protected]420ae012009-04-24 05:16:32377 prefs->RegisterBooleanPref(prefs::kAlternateErrorPagesEnabled, true);
378
379 WebPreferences pref_defaults;
380 prefs->RegisterBooleanPref(prefs::kWebKitJavascriptEnabled,
381 pref_defaults.javascript_enabled);
382 prefs->RegisterBooleanPref(prefs::kWebKitWebSecurityEnabled,
383 pref_defaults.web_security_enabled);
384 prefs->RegisterBooleanPref(
385 prefs::kWebKitJavascriptCanOpenWindowsAutomatically, true);
386 prefs->RegisterBooleanPref(prefs::kWebKitLoadsImagesAutomatically,
387 pref_defaults.loads_images_automatically);
388 prefs->RegisterBooleanPref(prefs::kWebKitPluginsEnabled,
389 pref_defaults.plugins_enabled);
390 prefs->RegisterBooleanPref(prefs::kWebKitDomPasteEnabled,
391 pref_defaults.dom_paste_enabled);
392 prefs->RegisterBooleanPref(prefs::kWebKitShrinksStandaloneImagesToFit,
393 pref_defaults.shrinks_standalone_images_to_fit);
[email protected]0df30122009-06-03 12:13:08394 prefs->RegisterStringPref(prefs::kWebKitInspectorSettings,
[email protected]8922e1f2009-10-03 05:01:26395 UTF8ToWide(pref_defaults.inspector_settings));
[email protected]420ae012009-04-24 05:16:32396 prefs->RegisterBooleanPref(prefs::kWebKitTextAreasAreResizable,
397 pref_defaults.text_areas_are_resizable);
398 prefs->RegisterBooleanPref(prefs::kWebKitJavaEnabled,
399 pref_defaults.java_enabled);
400
401 prefs->RegisterLocalizedStringPref(prefs::kAcceptLanguages,
402 IDS_ACCEPT_LANGUAGES);
403 prefs->RegisterLocalizedStringPref(prefs::kDefaultCharset,
404 IDS_DEFAULT_ENCODING);
405 prefs->RegisterLocalizedBooleanPref(prefs::kWebKitStandardFontIsSerif,
406 IDS_STANDARD_FONT_IS_SERIF);
407 prefs->RegisterLocalizedStringPref(prefs::kWebKitFixedFontFamily,
408 IDS_FIXED_FONT_FAMILY);
409 prefs->RegisterLocalizedStringPref(prefs::kWebKitSerifFontFamily,
410 IDS_SERIF_FONT_FAMILY);
411 prefs->RegisterLocalizedStringPref(prefs::kWebKitSansSerifFontFamily,
412 IDS_SANS_SERIF_FONT_FAMILY);
413 prefs->RegisterLocalizedStringPref(prefs::kWebKitCursiveFontFamily,
414 IDS_CURSIVE_FONT_FAMILY);
415 prefs->RegisterLocalizedStringPref(prefs::kWebKitFantasyFontFamily,
416 IDS_FANTASY_FONT_FAMILY);
417 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitDefaultFontSize,
418 IDS_DEFAULT_FONT_SIZE);
419 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitDefaultFixedFontSize,
420 IDS_DEFAULT_FIXED_FONT_SIZE);
421 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitMinimumFontSize,
422 IDS_MINIMUM_FONT_SIZE);
423 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitMinimumLogicalFontSize,
424 IDS_MINIMUM_LOGICAL_FONT_SIZE);
425 prefs->RegisterLocalizedBooleanPref(prefs::kWebKitUsesUniversalDetector,
426 IDS_USES_UNIVERSAL_DETECTOR);
427 prefs->RegisterLocalizedStringPref(prefs::kStaticEncodings,
428 IDS_STATIC_ENCODING_LIST);
initial.commit09911bf2008-07-26 23:55:29429}
430
[email protected]13367f72009-07-08 16:39:02431// Returns true if contains content rendered by an extension.
432bool TabContents::HostsExtension() const {
433 return GetURL().SchemeIs(chrome::kExtensionScheme);
[email protected]d5f942ba2008-09-26 19:30:34434}
435
[email protected]6e95706d2009-04-23 22:55:01436PasswordManager* TabContents::GetPasswordManager() {
437 if (password_manager_.get() == NULL)
438 password_manager_.reset(new PasswordManager(this));
439 return password_manager_.get();
440}
441
442PluginInstaller* TabContents::GetPluginInstaller() {
443 if (plugin_installer_.get() == NULL)
444 plugin_installer_.reset(new PluginInstaller(this));
445 return plugin_installer_.get();
446}
447
[email protected]d5f942ba2008-09-26 19:30:34448const GURL& TabContents::GetURL() const {
449 // We may not have a navigation entry yet
[email protected]ce3fa3c2009-04-20 19:55:57450 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]ebe89e062009-08-13 23:16:54451 return entry ? entry->virtual_url() : GURL::EmptyGURL();
[email protected]d5f942ba2008-09-26 19:30:34452}
453
[email protected]96d185d2009-04-24 03:28:54454const string16& TabContents::GetTitle() const {
[email protected]4c6092c5b2009-06-06 00:23:55455 // Transient entries take precedence. They are used for interstitial pages
456 // that are shown on top of existing pages.
457 NavigationEntry* entry = controller_.GetTransientEntry();
458 if (entry)
459 return entry->GetTitleForDisplay(&controller_);
460
[email protected]96d185d2009-04-24 03:28:54461 DOMUI* our_dom_ui = render_manager_.pending_dom_ui() ?
462 render_manager_.pending_dom_ui() : render_manager_.dom_ui();
463 if (our_dom_ui) {
464 // Don't override the title in view source mode.
[email protected]4c6092c5b2009-06-06 00:23:55465 entry = controller_.GetActiveEntry();
[email protected]96d185d2009-04-24 03:28:54466 if (!(entry && entry->IsViewSourceMode())) {
467 // Give the DOM UI the chance to override our title.
468 const string16& title = our_dom_ui->overridden_title();
469 if (!title.empty())
470 return title;
471 }
472 }
473
474 // We use the title for the last committed entry rather than a pending
475 // navigation entry. For example, when the user types in a URL, we want to
476 // keep the old page's title until the new load has committed and we get a new
477 // title.
[email protected]96d185d2009-04-24 03:28:54478 entry = controller_.GetLastCommittedEntry();
479 if (entry)
480 return entry->GetTitleForDisplay(&controller_);
[email protected]96d185d2009-04-24 03:28:54481 return EmptyString16();
482}
483
[email protected]d5f942ba2008-09-26 19:30:34484int32 TabContents::GetMaxPageID() {
485 if (GetSiteInstance())
486 return GetSiteInstance()->max_page_id();
487 else
488 return max_page_id_;
489}
490
491void TabContents::UpdateMaxPageID(int32 page_id) {
492 // Ensure both the SiteInstance and RenderProcessHost update their max page
[email protected]57c6a652009-05-04 07:58:34493 // IDs in sync. Only TabContents will also have site instances, except during
[email protected]d5f942ba2008-09-26 19:30:34494 // testing.
495 if (GetSiteInstance())
496 GetSiteInstance()->UpdateMaxPageID(page_id);
[email protected]57c6a652009-05-04 07:58:34497 process()->UpdateMaxPageID(page_id);
[email protected]d5f942ba2008-09-26 19:30:34498}
499
[email protected]96d185d2009-04-24 03:28:54500SiteInstance* TabContents::GetSiteInstance() const {
501 return render_manager_.current_host()->site_instance();
502}
503
[email protected]d5f942ba2008-09-26 19:30:34504const std::wstring TabContents::GetDefaultTitle() const {
505 return l10n_util::GetString(IDS_DEFAULT_TAB_TITLE);
506}
507
[email protected]96d185d2009-04-24 03:28:54508bool TabContents::ShouldDisplayURL() {
[email protected]8ab24cc2009-11-13 20:56:15509 // Don't hide the url in view source mode and with interstitials.
[email protected]96d185d2009-04-24 03:28:54510 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]8ab24cc2009-11-13 20:56:15511 if (entry && (entry->IsViewSourceMode() ||
512 entry->page_type() == NavigationEntry::INTERSTITIAL_PAGE)) {
[email protected]96d185d2009-04-24 03:28:54513 return true;
[email protected]8ab24cc2009-11-13 20:56:15514 }
[email protected]83e3895b2009-06-11 00:07:16515
[email protected]96d185d2009-04-24 03:28:54516 DOMUI* dom_ui = GetDOMUIForCurrentState();
517 if (dom_ui)
518 return !dom_ui->should_hide_url();
519 return true;
520}
521
[email protected]d5f942ba2008-09-26 19:30:34522SkBitmap TabContents::GetFavIcon() const {
523 // Like GetTitle(), we also want to use the favicon for the last committed
524 // entry rather than a pending navigation entry.
[email protected]ce3fa3c2009-04-20 19:55:57525 NavigationEntry* entry = controller_.GetTransientEntry();
[email protected]cbab76d2008-10-13 22:42:47526 if (entry)
527 return entry->favicon().bitmap();
528
[email protected]ce3fa3c2009-04-20 19:55:57529 entry = controller_.GetLastCommittedEntry();
[email protected]d5f942ba2008-09-26 19:30:34530 if (entry)
531 return entry->favicon().bitmap();
[email protected]d5f942ba2008-09-26 19:30:34532 return SkBitmap();
533}
534
[email protected]0b303cc2009-09-28 22:35:15535bool TabContents::FavIconIsValid() const {
536 NavigationEntry* entry = controller_.GetTransientEntry();
537 if (entry)
538 return entry->favicon().is_valid();
539
540 entry = controller_.GetLastCommittedEntry();
541 if (entry)
542 return entry->favicon().is_valid();
543
544 return false;
545}
546
[email protected]96d185d2009-04-24 03:28:54547bool TabContents::ShouldDisplayFavIcon() {
548 // Always display a throbber during pending loads.
549 if (controller_.GetLastCommittedEntry() && controller_.pending_entry())
550 return true;
551
552 DOMUI* dom_ui = GetDOMUIForCurrentState();
553 if (dom_ui)
554 return !dom_ui->hide_favicon();
555 return true;
556}
557
[email protected]96d185d2009-04-24 03:28:54558std::wstring TabContents::GetStatusText() const {
559 if (!is_loading() || load_state_ == net::LOAD_STATE_IDLE)
560 return std::wstring();
561
562 switch (load_state_) {
563 case net::LOAD_STATE_WAITING_FOR_CACHE:
564 return l10n_util::GetString(IDS_LOAD_STATE_WAITING_FOR_CACHE);
565 case net::LOAD_STATE_RESOLVING_PROXY_FOR_URL:
566 return l10n_util::GetString(IDS_LOAD_STATE_RESOLVING_PROXY_FOR_URL);
567 case net::LOAD_STATE_RESOLVING_HOST:
568 return l10n_util::GetString(IDS_LOAD_STATE_RESOLVING_HOST);
569 case net::LOAD_STATE_CONNECTING:
570 return l10n_util::GetString(IDS_LOAD_STATE_CONNECTING);
571 case net::LOAD_STATE_SENDING_REQUEST:
[email protected]094e5b22009-09-25 04:23:56572 if (upload_size_)
573 return l10n_util::GetStringF(
574 IDS_LOAD_STATE_SENDING_REQUEST_WITH_PROGRESS,
575 static_cast<int>((100 * upload_position_) / upload_size_));
576 else
577 return l10n_util::GetString(IDS_LOAD_STATE_SENDING_REQUEST);
[email protected]96d185d2009-04-24 03:28:54578 case net::LOAD_STATE_WAITING_FOR_RESPONSE:
579 return l10n_util::GetStringF(IDS_LOAD_STATE_WAITING_FOR_RESPONSE,
580 load_state_host_);
581 // Ignore net::LOAD_STATE_READING_RESPONSE and net::LOAD_STATE_IDLE
582 case net::LOAD_STATE_IDLE:
583 case net::LOAD_STATE_READING_RESPONSE:
584 break;
585 }
586
587 return std::wstring();
588}
589
[email protected]d5f942ba2008-09-26 19:30:34590void TabContents::SetIsCrashed(bool state) {
591 if (state == is_crashed_)
592 return;
593
594 is_crashed_ = state;
[email protected]c9cd2222009-05-06 05:16:50595 NotifyNavigationStateChanged(INVALIDATE_TAB);
[email protected]d5f942ba2008-09-26 19:30:34596}
597
[email protected]744ef172009-10-16 21:53:46598void TabContents::PageActionStateChanged() {
599 NotifyNavigationStateChanged(TabContents::INVALIDATE_PAGE_ACTIONS);
[email protected]d7eaf572009-07-01 21:57:00600}
[email protected]ce5c4502009-05-06 16:46:11601
[email protected]d5f942ba2008-09-26 19:30:34602void TabContents::NotifyNavigationStateChanged(unsigned changed_flags) {
603 if (delegate_)
604 delegate_->NavigationStateChanged(this, changed_flags);
605}
606
[email protected]96d185d2009-04-24 03:28:54607void TabContents::DidBecomeSelected() {
608 controller_.SetActive(true);
[email protected]789e9152009-08-04 21:59:43609 if (render_widget_host_view()) {
[email protected]96d185d2009-04-24 03:28:54610 render_widget_host_view()->DidBecomeSelected();
[email protected]789e9152009-08-04 21:59:43611#if defined(OS_MACOSX)
612 render_widget_host_view()->SetActive(true);
613#endif
614 }
[email protected]96d185d2009-04-24 03:28:54615
[email protected]76543b92009-08-31 17:27:45616 WebCacheManager::GetInstance()->ObserveActivity(process()->id());
[email protected]96d185d2009-04-24 03:28:54617}
618
619void TabContents::WasHidden() {
620 if (!capturing_contents()) {
621 // |render_view_host()| can be NULL if the user middle clicks a link to open
622 // a tab in then background, then closes the tab before selecting it. This
623 // is because closing the tab calls TabContents::Destroy(), which removes
624 // the |render_view_host()|; then when we actually destroy the window,
625 // OnWindowPosChanged() notices and calls HideContents() (which calls us).
626 if (render_widget_host_view())
627 render_widget_host_view()->WasHidden();
[email protected]96d185d2009-04-24 03:28:54628 }
629
630 NotificationService::current()->Notify(
631 NotificationType::TAB_CONTENTS_HIDDEN,
632 Source<TabContents>(this),
633 NotificationService::NoDetails());
634}
635
[email protected]d5f942ba2008-09-26 19:30:34636void TabContents::Activate() {
637 if (delegate_)
638 delegate_->ActivateContents(this);
639}
640
[email protected]96d185d2009-04-24 03:28:54641void TabContents::ShowContents() {
642 if (render_widget_host_view())
643 render_widget_host_view()->DidBecomeSelected();
[email protected]96d185d2009-04-24 03:28:54644}
645
646void TabContents::HideContents() {
647 // TODO(pkasting): http://b/1239839 Right now we purposefully don't call
648 // our superclass HideContents(), because some callers want to be very picky
649 // about the order in which these get called. In addition to making the code
650 // here practically impossible to understand, this also means we end up
651 // calling TabContents::WasHidden() twice if callers call both versions of
[email protected]57c6a652009-05-04 07:58:34652 // HideContents() on a TabContents.
[email protected]96d185d2009-04-24 03:28:54653 WasHidden();
654}
655
[email protected]c0588052008-10-27 23:01:50656void TabContents::OpenURL(const GURL& url, const GURL& referrer,
[email protected]d5f942ba2008-09-26 19:30:34657 WindowOpenDisposition disposition,
658 PageTransition::Type transition) {
659 if (delegate_)
[email protected]c0588052008-10-27 23:01:50660 delegate_->OpenURLFromTab(this, url, referrer, disposition, transition);
[email protected]d5f942ba2008-09-26 19:30:34661}
662
663bool TabContents::NavigateToPendingEntry(bool reload) {
[email protected]96d185d2009-04-24 03:28:54664 const NavigationEntry& entry = *controller_.pending_entry();
665
666 RenderViewHost* dest_render_view_host = render_manager_.Navigate(entry);
667 if (!dest_render_view_host)
668 return false; // Unable to create the desired render view host.
669
670 // Tell DevTools agent that it is attached prior to the navigation.
[email protected]d9f9b792009-06-24 13:17:12671 DevToolsManager* devtools_manager = DevToolsManager::GetInstance();
[email protected]7e8e6b62009-05-08 11:28:32672 if (devtools_manager) { // NULL in unit tests.
[email protected]684b4ba22009-05-07 06:27:41673 devtools_manager->OnNavigatingToPendingEntry(
674 render_view_host(),
675 dest_render_view_host,
676 controller_.pending_entry()->url());
[email protected]7e8e6b62009-05-08 11:28:32677 }
[email protected]96d185d2009-04-24 03:28:54678
679 // Used for page load time metrics.
680 current_load_start_ = base::TimeTicks::Now();
681
682 // Navigate in the desired RenderViewHost.
[email protected]056de2d2009-06-26 16:41:34683 ViewMsg_Navigate_Params navigate_params;
[email protected]5e369672009-11-03 23:48:30684 MakeNavigateParams(profile(), entry, reload, &navigate_params);
[email protected]056de2d2009-06-26 16:41:34685 dest_render_view_host->Navigate(navigate_params);
[email protected]96d185d2009-04-24 03:28:54686
687 if (entry.page_id() == -1) {
688 // HACK!! This code suppresses javascript: URLs from being added to
689 // session history, which is what we want to do for javascript: URLs that
690 // do not generate content. What we really need is a message from the
691 // renderer telling us that a new page was not created. The same message
692 // could be used for mailto: URLs and the like.
693 if (entry.url().SchemeIs(chrome::kJavaScriptScheme))
694 return false;
695 }
696
697 // Clear any provisional password saves - this stops password infobars
698 // showing up on pages the user navigates to while the right page is
699 // loading.
700 GetPasswordManager()->ClearProvisionalSave();
701
702 if (reload && !profile()->IsOffTheRecord()) {
[email protected]0189bc722009-08-28 21:56:48703 FaviconService* favicon_service =
704 profile()->GetFaviconService(Profile::IMPLICIT_ACCESS);
705 if (favicon_service)
706 favicon_service->SetFaviconOutOfDateForPage(entry.url());
[email protected]96d185d2009-04-24 03:28:54707 }
708
[email protected]d5f942ba2008-09-26 19:30:34709 return true;
710}
711
[email protected]96d185d2009-04-24 03:28:54712void TabContents::Stop() {
713 render_manager_.Stop();
714 printing_.Stop();
715}
716
717void TabContents::Cut() {
718 render_view_host()->Cut();
719}
720
721void TabContents::Copy() {
722 render_view_host()->Copy();
723}
724
[email protected]c0cc3092009-09-12 08:27:27725#if defined(OS_MACOSX)
[email protected]a954bf72009-09-12 07:30:35726void TabContents::CopyToFindPboard() {
727 render_view_host()->CopyToFindPboard();
728}
[email protected]c0cc3092009-09-12 08:27:27729#endif
[email protected]a954bf72009-09-12 07:30:35730
[email protected]96d185d2009-04-24 03:28:54731void TabContents::Paste() {
732 render_view_host()->Paste();
733}
734
[email protected]7e786822009-11-07 00:00:27735#if defined(OS_MACOSX)
736void TabContents::PasteAndMatchStyle() {
737 render_view_host()->ForwardEditCommand("PasteAndMatchStyle", "");
738}
739#endif
740
[email protected]96d185d2009-04-24 03:28:54741void TabContents::DisassociateFromPopupCount() {
742 render_view_host()->DisassociateFromPopupCount();
743}
744
745TabContents* TabContents::Clone() {
746 // We create a new SiteInstance so that the new tab won't share processes
747 // with the old one. This can be changed in the future if we need it to share
748 // processes for some reason.
[email protected]420ae012009-04-24 05:16:32749 TabContents* tc = new TabContents(profile(),
[email protected]96d185d2009-04-24 03:28:54750 SiteInstance::CreateSiteInstance(profile()),
[email protected]12636df2009-09-28 22:32:21751 MSG_ROUTING_NONE, this);
[email protected]96d185d2009-04-24 03:28:54752 tc->controller().CopyStateFrom(controller_);
753 return tc;
754}
755
[email protected]4d677202009-07-19 07:37:12756void TabContents::ShowPageInfo(const GURL& url,
757 const NavigationEntry::SSLStatus& ssl,
758 bool show_history) {
759 if (!delegate_)
760 return;
761
[email protected]bb678332009-07-21 00:15:50762 delegate_->ShowPageInfo(profile(), url, ssl, show_history);
[email protected]4d677202009-07-19 07:37:12763}
764
initial.commit09911bf2008-07-26 23:55:29765ConstrainedWindow* TabContents::CreateConstrainedDialog(
[email protected]e8382172009-06-19 22:16:28766 ConstrainedWindowDelegate* delegate) {
initial.commit09911bf2008-07-26 23:55:29767 ConstrainedWindow* window =
[email protected]e8382172009-06-19 22:16:28768 ConstrainedWindow::CreateConstrainedDialog(this, delegate);
initial.commit09911bf2008-07-26 23:55:29769 child_windows_.push_back(window);
770 return window;
771}
772
773void TabContents::AddNewContents(TabContents* new_contents,
774 WindowOpenDisposition disposition,
775 const gfx::Rect& initial_pos,
[email protected]c88a70fe2009-05-05 20:00:22776 bool user_gesture,
777 const GURL& creator_url) {
initial.commit09911bf2008-07-26 23:55:29778 if (!delegate_)
779 return;
780
[email protected]35f7d212009-04-29 21:19:27781 if ((disposition == NEW_POPUP) && !user_gesture &&
782 !CommandLine::ForCurrentProcess()->HasSwitch(
783 switches::kDisablePopupBlocking)) {
[email protected]2c4410d2009-05-06 23:46:22784 // Unrequested popups from normal pages are constrained unless they're in
[email protected]b6c874582009-05-08 19:38:31785 // the whitelist. The popup owner will handle checking this.
786 delegate_->GetConstrainingContents(this)->AddPopup(new_contents,
787 initial_pos,
788 creator_url.is_valid() ? creator_url.host() : std::string());
789 } else {
[email protected]304f8792009-08-19 21:28:09790#if defined(OS_CHROMEOS)
791 if (disposition == NEW_POPUP) {
792 // If the popup is bigger than a given factor of the screen, then
793 // turn it into a foreground tab (on chrome os only)
794 GdkScreen* screen = gdk_screen_get_default();
795 int max_width = gdk_screen_get_width(screen) * kMaxWidthFactor;
796 int max_height = gdk_screen_get_height(screen) * kMaxHeightFactor;
797 if (initial_pos.width() > max_width ||
798 initial_pos.height() > max_height) {
799 disposition = NEW_FOREGROUND_TAB;
800 }
801 }
802#endif
803
[email protected]0aa55312008-10-17 21:53:08804 new_contents->DisassociateFromPopupCount();
initial.commit09911bf2008-07-26 23:55:29805 delegate_->AddNewContents(this, new_contents, disposition, initial_pos,
806 user_gesture);
[email protected]7f37dd92009-09-15 16:35:49807 NotificationService::current()->Notify(
808 NotificationType::TAB_ADDED,
809 Source<TabContentsDelegate>(delegate_),
810 Details<TabContents>(this));
initial.commit09911bf2008-07-26 23:55:29811 }
[email protected]b6c874582009-05-08 19:38:31812 PopupNotificationVisibilityChanged(ShowingBlockedPopupNotification());
initial.commit09911bf2008-07-26 23:55:29813}
814
initial.commit09911bf2008-07-26 23:55:29815void TabContents::CloseAllSuppressedPopups() {
[email protected]d6598c052008-11-05 19:03:25816 if (blocked_popups_)
[email protected]2c4410d2009-05-06 23:46:22817 blocked_popups_->CloseAll();
initial.commit09911bf2008-07-26 23:55:29818}
819
[email protected]912256b32009-09-18 09:47:35820void TabContents::ExecuteCode(int request_id, const std::string& extension_id,
[email protected]61f5a7b2009-12-22 22:21:20821 const std::vector<URLPattern>& host_permissions,
[email protected]20ad2692009-11-20 18:27:20822 bool is_js_code, const std::string& code_string,
823 bool all_frames) {
[email protected]912256b32009-09-18 09:47:35824 render_view_host()->Send(
[email protected]61f5a7b2009-12-22 22:21:20825 new ViewMsg_ExecuteCode(render_view_host()->routing_id(),
826 ViewMsg_ExecuteCode_Params(request_id, extension_id, host_permissions,
827 is_js_code, code_string, all_frames)));
[email protected]912256b32009-09-18 09:47:35828}
829
[email protected]96d185d2009-04-24 03:28:54830void TabContents::PopupNotificationVisibilityChanged(bool visible) {
831 render_view_host()->PopupNotificationVisibilityChanged(visible);
832}
833
[email protected]5c9e97a2009-09-09 23:48:30834gfx::NativeView TabContents::GetContentNativeView() const {
[email protected]96d185d2009-04-24 03:28:54835 return view_->GetContentNativeView();
836}
837
838gfx::NativeView TabContents::GetNativeView() const {
839 return view_->GetNativeView();
840}
841
842void TabContents::GetContainerBounds(gfx::Rect *out) const {
843 view_->GetContainerBounds(out);
844}
845
846void TabContents::Focus() {
847 view_->Focus();
848}
849
[email protected]90daadb42009-06-08 21:27:28850void TabContents::FocusThroughTabTraversal(bool reverse) {
[email protected]7e383692009-06-12 19:14:54851 if (showing_interstitial_page()) {
852 render_manager_.interstitial_page()->FocusThroughTabTraversal(reverse);
853 return;
854 }
[email protected]96d185d2009-04-24 03:28:54855 render_view_host()->SetInitialFocus(reverse);
856}
857
858bool TabContents::FocusLocationBarByDefault() {
859 DOMUI* dom_ui = GetDOMUIForCurrentState();
860 if (dom_ui)
861 return dom_ui->focus_location_bar_by_default();
[email protected]3e3f0eb2009-06-22 18:33:43862 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]e0d481582009-09-15 21:06:25863 if (entry && entry->url() == GURL(chrome::kAboutBlankURL))
[email protected]3e3f0eb2009-06-22 18:33:43864 return true;
[email protected]96d185d2009-04-24 03:28:54865 return false;
866}
867
[email protected]616ed5a2008-11-21 22:27:24868void TabContents::AddInfoBar(InfoBarDelegate* delegate) {
869 // Look through the existing InfoBarDelegates we have for a match. If we've
870 // already got one that matches, then we don't add the new one.
[email protected]f86a07022008-11-25 01:06:05871 for (int i = 0; i < infobar_delegate_count(); ++i) {
[email protected]6d27a7b2009-12-18 23:25:45872 if (GetInfoBarDelegateAt(i)->EqualsDelegate(delegate)) {
873 // Tell the new infobar to close so that it can clean itself up.
874 delegate->InfoBarClosed();
[email protected]616ed5a2008-11-21 22:27:24875 return;
[email protected]6d27a7b2009-12-18 23:25:45876 }
[email protected]616ed5a2008-11-21 22:27:24877 }
878
879 infobar_delegates_.push_back(delegate);
[email protected]bfd04a62009-02-01 18:16:56880 NotificationService::current()->Notify(
881 NotificationType::TAB_CONTENTS_INFOBAR_ADDED,
882 Source<TabContents>(this),
883 Details<InfoBarDelegate>(delegate));
[email protected]616ed5a2008-11-21 22:27:24884
885 // Add ourselves as an observer for navigations the first time a delegate is
886 // added. We use this notification to expire InfoBars that need to expire on
887 // page transitions.
888 if (infobar_delegates_.size() == 1) {
[email protected]bfd04a62009-02-01 18:16:56889 registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED,
[email protected]ce3fa3c2009-04-20 19:55:57890 Source<NavigationController>(&controller_));
[email protected]616ed5a2008-11-21 22:27:24891 }
892}
893
894void TabContents::RemoveInfoBar(InfoBarDelegate* delegate) {
895 std::vector<InfoBarDelegate*>::iterator it =
896 find(infobar_delegates_.begin(), infobar_delegates_.end(), delegate);
897 if (it != infobar_delegates_.end()) {
898 InfoBarDelegate* delegate = *it;
[email protected]bfd04a62009-02-01 18:16:56899 NotificationService::current()->Notify(
900 NotificationType::TAB_CONTENTS_INFOBAR_REMOVED,
901 Source<TabContents>(this),
902 Details<InfoBarDelegate>(delegate));
[email protected]f86a07022008-11-25 01:06:05903 infobar_delegates_.erase(it);
[email protected]616ed5a2008-11-21 22:27:24904
[email protected]6a02963e2009-01-06 16:58:03905 // Remove ourselves as an observer if we are tracking no more InfoBars.
906 if (infobar_delegates_.empty()) {
[email protected]bfd04a62009-02-01 18:16:56907 registrar_.Remove(this, NotificationType::NAV_ENTRY_COMMITTED,
[email protected]ce3fa3c2009-04-20 19:55:57908 Source<NavigationController>(&controller_));
[email protected]6a02963e2009-01-06 16:58:03909 }
[email protected]616ed5a2008-11-21 22:27:24910 }
911}
[email protected]3edd9522009-03-04 22:19:41912
[email protected]f81821c2009-08-07 01:29:47913void TabContents::ReplaceInfoBar(InfoBarDelegate* old_delegate,
914 InfoBarDelegate* new_delegate) {
915 std::vector<InfoBarDelegate*>::iterator it =
916 find(infobar_delegates_.begin(), infobar_delegates_.end(), old_delegate);
917 DCHECK(it != infobar_delegates_.end());
918
919 // Notify the container about the change of plans.
920 scoped_ptr<std::pair<InfoBarDelegate*, InfoBarDelegate*> > details(
921 new std::pair<InfoBarDelegate*, InfoBarDelegate*>(
922 old_delegate, new_delegate));
923 NotificationService::current()->Notify(
924 NotificationType::TAB_CONTENTS_INFOBAR_REPLACED,
925 Source<TabContents>(this),
926 Details<std::pair<InfoBarDelegate*, InfoBarDelegate*> >(details.get()));
927
928 // Remove the old one.
929 infobar_delegates_.erase(it);
930
931 // Add the new one.
932 infobar_delegates_.push_back(new_delegate);
933}
934
[email protected]20ede072009-10-06 00:28:35935bool TabContents::ShouldShowBookmarkBar() {
[email protected]aa9196f2009-10-22 16:20:27936 if (showing_interstitial_page())
937 return false;
938
[email protected]96d185d2009-04-24 03:28:54939 // See GetDOMUIForCurrentState() comment for more info. This case is very
940 // similar, but for non-first loads, we want to use the committed entry. This
941 // is so the bookmarks bar disappears at the same time the page does.
942 if (controller_.GetLastCommittedEntry()) {
943 // Not the first load, always use the committed DOM UI.
944 if (render_manager_.dom_ui())
945 return render_manager_.dom_ui()->force_bookmark_bar_visible();
946 return false; // Default.
947 }
948
949 // When it's the first load, we know either the pending one or the committed
950 // one will have the DOM UI in it (see GetDOMUIForCurrentState), and only one
951 // of them will be valid, so we can just check both.
952 if (render_manager_.pending_dom_ui())
953 return render_manager_.pending_dom_ui()->force_bookmark_bar_visible();
954 if (render_manager_.dom_ui())
955 return render_manager_.dom_ui()->force_bookmark_bar_visible();
956 return false; // Default.
957}
958
[email protected]d8f16ae2009-08-30 03:47:26959bool TabContents::IsExtensionShelfAlwaysVisible() {
960 // See GetDOMUIForCurrentState() comment for more info. This case is very
961 // similar, but for non-first loads, we want to use the committed entry. This
962 // is so the bookmarks bar disappears at the same time the page does.
963 if (controller_.GetLastCommittedEntry()) {
964 // Not the first load, always use the committed DOM UI.
965 if (render_manager_.dom_ui())
966 return render_manager_.dom_ui()->force_extension_shelf_visible();
967 return false; // Default.
968 }
969
970 // When it's the first load, we know either the pending one or the committed
971 // one will have the DOM UI in it (see GetDOMUIForCurrentState), and only one
972 // of them will be valid, so we can just check both.
973 if (render_manager_.pending_dom_ui())
974 return render_manager_.pending_dom_ui()->force_extension_shelf_visible();
975 if (render_manager_.dom_ui())
976 return render_manager_.dom_ui()->force_extension_shelf_visible();
977 return false; // Default.
978}
979
[email protected]3edd9522009-03-04 22:19:41980void TabContents::ToolbarSizeChanged(bool is_animating) {
981 TabContentsDelegate* d = delegate();
982 if (d)
983 d->ToolbarSizeChanged(this, is_animating);
984}
[email protected]616ed5a2008-11-21 22:27:24985
[email protected]fc6fb7fb2009-11-07 02:35:04986bool TabContents::CanDownload(int request_id) {
987 TabContentsDelegate* d = delegate();
988 if (d)
989 return d->CanDownload(request_id);
990 return true;
991}
992
[email protected]d5f942ba2008-09-26 19:30:34993void TabContents::OnStartDownload(DownloadItem* download) {
994 DCHECK(download);
[email protected]d5f942ba2008-09-26 19:30:34995
996 // Download in a constrained popup is shown in the tab that opened it.
[email protected]b6c874582009-05-08 19:38:31997 TabContents* tab_contents = delegate()->GetConstrainingContents(this);
[email protected]d5f942ba2008-09-26 19:30:34998
[email protected]59560e0b2009-06-04 03:30:22999 if (tab_contents && tab_contents->delegate())
1000 tab_contents->delegate()->OnStartDownload(download);
[email protected]3edd9522009-03-04 22:19:411001}
[email protected]3edd9522009-03-04 22:19:411002
[email protected]d5f942ba2008-09-26 19:30:341003void TabContents::WillClose(ConstrainedWindow* window) {
1004 ConstrainedWindowList::iterator it =
1005 find(child_windows_.begin(), child_windows_.end(), window);
1006 if (it != child_windows_.end())
1007 child_windows_.erase(it);
[email protected]66ba4932009-06-04 19:22:131008}
[email protected]d5f942ba2008-09-26 19:30:341009
[email protected]66ba4932009-06-04 19:22:131010void TabContents::WillCloseBlockedPopupContainer(
1011 BlockedPopupContainer* container) {
1012 DCHECK(blocked_popups_ == container);
1013 blocked_popups_ = NULL;
[email protected]d5f942ba2008-09-26 19:30:341014}
1015
[email protected]d5f942ba2008-09-26 19:30:341016void TabContents::DidMoveOrResize(ConstrainedWindow* window) {
[email protected]332af7732009-03-11 13:21:351017#if defined(OS_WIN)
[email protected]92edc472009-02-10 20:32:061018 UpdateWindow(GetNativeView());
[email protected]332af7732009-03-11 13:21:351019#endif
[email protected]d5f942ba2008-09-26 19:30:341020}
1021
[email protected]c1dc9f132009-11-20 23:13:211022void TabContents::StartFinding(string16 search_string,
[email protected]e491f1c2009-05-22 20:28:121023 bool forward_direction,
1024 bool case_sensitive) {
[email protected]c1dc9f132009-11-20 23:13:211025 // If search_string is empty, it means FindNext was pressed with a keyboard
[email protected]6e95706d2009-04-23 22:55:011026 // shortcut so unless we have something to search for we return early.
[email protected]c1dc9f132009-11-20 23:13:211027 if (search_string.empty() && find_text_.empty()) {
[email protected]87fae512009-12-11 02:20:211028 // Try the last thing we searched for on this tab, then the last thing
1029 // searched for on any tab.
1030 if (!previous_find_text_.empty())
1031 search_string = previous_find_text_;
1032 else if (!last_search_prepopulate_text_->empty())
1033 search_string = *last_search_prepopulate_text_;
1034 else
[email protected]e491f1c2009-05-22 20:28:121035 return;
[email protected]e491f1c2009-05-22 20:28:121036 }
[email protected]6e95706d2009-04-23 22:55:011037
[email protected]101c90e2009-10-26 21:14:441038 // Keep track of the previous search.
1039 previous_find_text_ = find_text_;
1040
[email protected]6e95706d2009-04-23 22:55:011041 // This is a FindNext operation if we are searching for the same text again,
1042 // or if the passed in search text is empty (FindNext keyboard shortcut). The
1043 // exception to this is if the Find was aborted (then we don't want FindNext
1044 // because the highlighting has been cleared and we need it to reappear). We
1045 // therefore treat FindNext after an aborted Find operation as a full fledged
1046 // Find.
[email protected]c1dc9f132009-11-20 23:13:211047 bool find_next = (find_text_ == search_string || search_string.empty()) &&
[email protected]e491f1c2009-05-22 20:28:121048 (last_search_case_sensitive_ == case_sensitive) &&
[email protected]6e95706d2009-04-23 22:55:011049 !find_op_aborted_;
1050 if (!find_next)
1051 current_find_request_id_ = find_request_id_counter_++;
1052
[email protected]c1dc9f132009-11-20 23:13:211053 if (!search_string.empty())
1054 find_text_ = search_string;
[email protected]e491f1c2009-05-22 20:28:121055 last_search_case_sensitive_ = case_sensitive;
[email protected]6e95706d2009-04-23 22:55:011056
1057 find_op_aborted_ = false;
1058
1059 // Keep track of what the last search was across the tabs.
[email protected]c1dc9f132009-11-20 23:13:211060 *last_search_prepopulate_text_ = find_text_;
[email protected]6e95706d2009-04-23 22:55:011061 render_view_host()->StartFinding(current_find_request_id_,
1062 find_text_,
1063 forward_direction,
[email protected]e491f1c2009-05-22 20:28:121064 case_sensitive,
[email protected]6e95706d2009-04-23 22:55:011065 find_next);
1066}
1067
1068void TabContents::StopFinding(bool clear_selection) {
[email protected]2d2dffc2009-06-09 21:41:001069 // When |clear_selection| is true, it means the find string has been cleared
1070 // by the user, but the UI has not been dismissed.
1071 if (!clear_selection)
1072 find_ui_active_ = false;
[email protected]87fae512009-12-11 02:20:211073 previous_find_text_ = find_text_;
[email protected]101c90e2009-10-26 21:14:441074 find_text_.clear();
[email protected]6e95706d2009-04-23 22:55:011075 find_op_aborted_ = true;
[email protected]e491f1c2009-05-22 20:28:121076 last_search_result_ = FindNotificationDetails();
[email protected]6e95706d2009-04-23 22:55:011077 render_view_host()->StopFinding(clear_selection);
1078}
1079
[email protected]cf3f3872010-01-15 17:38:191080void TabContents::GetPageLanguage() {
1081 render_view_host()->GetPageLanguage();
1082}
1083
[email protected]420ae012009-04-24 05:16:321084void TabContents::OnSavePage() {
1085 // If we can not save the page, try to download it.
1086 if (!SavePackage::IsSavableContents(contents_mime_type())) {
1087 DownloadManager* dlm = profile()->GetDownloadManager();
1088 const GURL& current_page_url = GetURL();
1089 if (dlm && current_page_url.is_valid())
[email protected]d82ed61e2009-06-16 02:46:221090 dlm->DownloadUrl(current_page_url, GURL(), "", this);
[email protected]420ae012009-04-24 05:16:321091 return;
1092 }
1093
1094 Stop();
1095
1096 // Create the save package and possibly prompt the user for the name to save
1097 // the page as. The user prompt is an asynchronous operation that runs on
1098 // another thread.
[email protected]d82ed61e2009-06-16 02:46:221099 save_package_ = new SavePackage(this);
[email protected]420ae012009-04-24 05:16:321100 save_package_->GetSaveInfo();
1101}
1102
1103// Used in automated testing to bypass prompting the user for file names.
1104// Instead, the names and paths are hard coded rather than running them through
1105// file name sanitation and extension / mime checking.
[email protected]828cabe2009-09-26 22:47:111106bool TabContents::SavePage(const FilePath& main_file, const FilePath& dir_path,
[email protected]420ae012009-04-24 05:16:321107 SavePackage::SavePackageType save_type) {
1108 // Stop the page from navigating.
1109 Stop();
1110
[email protected]828cabe2009-09-26 22:47:111111 save_package_ = new SavePackage(this, save_type, main_file, dir_path);
[email protected]cdf6cc82009-08-04 21:48:591112 return save_package_->Init();
[email protected]420ae012009-04-24 05:16:321113}
1114
[email protected]89d6e6e2009-12-03 23:06:511115void TabContents::EmailPageLocation() {
1116 std::string title = EscapeQueryParamValue(UTF16ToUTF8(GetTitle()), false);
1117 std::string page_url = EscapeQueryParamValue(GetURL().spec(), false);
1118 std::string mailto = std::string("mailto:?subject=Fwd:%20") +
1119 title + "&body=%0A%0A" + page_url;
1120 platform_util::OpenExternal(GURL(mailto));
1121}
1122
[email protected]420ae012009-04-24 05:16:321123void TabContents::PrintPreview() {
1124 // We don't show the print preview yet, only the print dialog.
1125 PrintNow();
1126}
1127
1128bool TabContents::PrintNow() {
1129 // We can't print interstitial page for now.
1130 if (showing_interstitial_page())
1131 return false;
1132
1133 return render_view_host()->PrintPages();
1134}
1135
[email protected]82270452009-06-19 15:58:011136void TabContents::PrintingDone(int document_cookie, bool success) {
1137 render_view_host()->PrintingDone(document_cookie, success);
1138}
1139
[email protected]420ae012009-04-24 05:16:321140bool TabContents::IsActiveEntry(int32 page_id) {
1141 NavigationEntry* active_entry = controller_.GetActiveEntry();
1142 return (active_entry != NULL &&
1143 active_entry->site_instance() == GetSiteInstance() &&
1144 active_entry->page_id() == page_id);
1145}
1146
[email protected]3f8eb7f2009-10-02 23:58:051147void TabContents::LogNewTabTime(const std::string& event_name) {
1148 // Not all new tab pages get timed. In those cases, we don't have a
1149 // new_tab_start_time_.
1150 if (new_tab_start_time_.is_null())
1151 return;
1152
1153 base::TimeDelta duration = base::TimeTicks::Now() - new_tab_start_time_;
[email protected]84abba62009-10-07 17:01:441154 MetricEventDurationDetails details(event_name,
1155 static_cast<int>(duration.InMilliseconds()));
1156
[email protected]7ab1e7d62009-10-14 23:32:011157 if (event_name == "Tab.NewTabScriptStart") {
1158 UMA_HISTOGRAM_TIMES("Tab.NewTabScriptStart", duration);
1159 } else if (event_name == "Tab.NewTabDOMContentLoaded") {
1160 UMA_HISTOGRAM_TIMES("Tab.NewTabDOMContentLoaded", duration);
1161 } else if (event_name == "Tab.NewTabOnload") {
1162 UMA_HISTOGRAM_TIMES("Tab.NewTabOnload", duration);
[email protected]3f8eb7f2009-10-02 23:58:051163 // The new tab page has finished loading; reset it.
1164 new_tab_start_time_ = base::TimeTicks();
[email protected]84abba62009-10-07 17:01:441165 } else {
1166 NOTREACHED();
[email protected]3f8eb7f2009-10-02 23:58:051167 }
[email protected]84abba62009-10-07 17:01:441168 NotificationService::current()->Notify(
1169 NotificationType::METRIC_EVENT_DURATION,
1170 Source<TabContents>(this),
1171 Details<MetricEventDurationDetails>(&details));
[email protected]3f8eb7f2009-10-02 23:58:051172}
1173
[email protected]7ab1e7d62009-10-14 23:32:011174void TabContents::OnCloseStarted() {
1175 if (tab_close_start_time_.is_null())
1176 tab_close_start_time_ = base::TimeTicks::Now();
1177}
1178
[email protected]96d185d2009-04-24 03:28:541179// Notifies the RenderWidgetHost instance about the fact that the page is
1180// loading, or done loading and calls the base implementation.
1181void TabContents::SetIsLoading(bool is_loading,
1182 LoadNotificationDetails* details) {
1183 if (is_loading == is_loading_)
1184 return;
1185
1186 if (!is_loading) {
1187 load_state_ = net::LOAD_STATE_IDLE;
1188 load_state_host_.clear();
[email protected]094e5b22009-09-25 04:23:561189 upload_size_ = 0;
1190 upload_position_ = 0;
[email protected]96d185d2009-04-24 03:28:541191 }
1192
1193 render_manager_.SetIsLoading(is_loading);
1194
1195 is_loading_ = is_loading;
1196 waiting_for_response_ = is_loading;
1197
1198 if (delegate_)
1199 delegate_->LoadingStateChanged(this);
[email protected]329581b2009-04-28 06:52:351200 NotifyNavigationStateChanged(INVALIDATE_LOAD);
[email protected]96d185d2009-04-24 03:28:541201
1202 NotificationType type = is_loading ? NotificationType::LOAD_START :
1203 NotificationType::LOAD_STOP;
[email protected]ce5c4502009-05-06 16:46:111204 NotificationDetails det = NotificationService::NoDetails();
[email protected]96d185d2009-04-24 03:28:541205 if (details)
1206 det = Details<LoadNotificationDetails>(details);
1207 NotificationService::current()->Notify(type,
1208 Source<NavigationController>(&controller_),
1209 det);
1210}
1211
[email protected]2c4410d2009-05-06 23:46:221212void TabContents::CreateBlockedPopupContainerIfNecessary() {
1213 if (blocked_popups_)
1214 return;
1215
[email protected]4d4c0b92009-06-08 23:13:011216 blocked_popups_ = BlockedPopupContainer::Create(this, profile());
[email protected]2c4410d2009-05-06 23:46:221217}
1218
[email protected]b6c874582009-05-08 19:38:311219void TabContents::AddPopup(TabContents* new_contents,
1220 const gfx::Rect& initial_pos,
1221 const std::string& host) {
[email protected]2c4410d2009-05-06 23:46:221222 CreateBlockedPopupContainerIfNecessary();
1223 blocked_popups_->AddTabContents(new_contents, initial_pos, host);
[email protected]2c4410d2009-05-06 23:46:221224}
1225
[email protected]e69bce3dd2009-04-20 22:05:121226// TODO(brettw) This should be on the TabContentsView.
[email protected]66ba4932009-06-04 19:22:131227void TabContents::RepositionSupressedPopupsToFit() {
[email protected]d6598c052008-11-05 19:03:251228 if (blocked_popups_)
[email protected]4d4c0b92009-06-08 23:13:011229 blocked_popups_->RepositionBlockedPopupContainer();
[email protected]d5f942ba2008-09-26 19:30:341230}
1231
[email protected]b9681312008-11-07 00:08:261232bool TabContents::ShowingBlockedPopupNotification() const {
1233 return blocked_popups_ != NULL &&
[email protected]2c4410d2009-05-06 23:46:221234 blocked_popups_->GetBlockedPopupCount() != 0;
[email protected]b9681312008-11-07 00:08:261235}
[email protected]616ed5a2008-11-21 22:27:241236
1237namespace {
1238bool TransitionIsReload(PageTransition::Type transition) {
1239 return PageTransition::StripQualifier(transition) == PageTransition::RELOAD;
1240}
1241}
1242
1243void TabContents::ExpireInfoBars(
1244 const NavigationController::LoadCommittedDetails& details) {
1245 // Only hide InfoBars when the user has done something that makes the main
1246 // frame load. We don't want various automatic or subframe navigations making
1247 // it disappear.
1248 if (!details.is_user_initiated_main_frame_load())
1249 return;
1250
[email protected]f86a07022008-11-25 01:06:051251 for (int i = infobar_delegate_count() - 1; i >= 0; --i) {
[email protected]616ed5a2008-11-21 22:27:241252 InfoBarDelegate* delegate = GetInfoBarDelegateAt(i);
[email protected]f86a07022008-11-25 01:06:051253 if (delegate->ShouldExpire(details))
[email protected]616ed5a2008-11-21 22:27:241254 RemoveInfoBar(delegate);
[email protected]616ed5a2008-11-21 22:27:241255 }
1256}
[email protected]fdd61c62009-04-22 19:22:571257
[email protected]96d185d2009-04-24 03:28:541258DOMUI* TabContents::GetDOMUIForCurrentState() {
1259 // When there is a pending navigation entry, we want to use the pending DOMUI
1260 // that goes along with it to control the basic flags. For example, we want to
1261 // show the pending URL in the URL bar, so we want the display_url flag to
1262 // be from the pending entry.
1263 //
1264 // The confusion comes because there are multiple possibilities for the
1265 // initial load in a tab as a side effect of the way the RenderViewHostManager
1266 // works.
1267 //
1268 // - For the very first tab the load looks "normal". The new tab DOM UI is
1269 // the pending one, and we want it to apply here.
1270 //
1271 // - For subsequent new tabs, they'll get a new SiteInstance which will then
1272 // get switched to the one previously associated with the new tab pages.
1273 // This switching will cause the manager to commit the RVH/DOMUI. So we'll
1274 // have a committed DOM UI in this case.
1275 //
1276 // This condition handles all of these cases:
1277 //
1278 // - First load in first tab: no committed nav entry + pending nav entry +
1279 // pending dom ui:
1280 // -> Use pending DOM UI if any.
1281 //
1282 // - First load in second tab: no committed nav entry + pending nav entry +
1283 // no pending DOM UI:
1284 // -> Use the committed DOM UI if any.
1285 //
1286 // - Second navigation in any tab: committed nav entry + pending nav entry:
1287 // -> Use pending DOM UI if any.
1288 //
1289 // - Normal state with no load: committed nav entry + no pending nav entry:
1290 // -> Use committed DOM UI.
1291 if (controller_.pending_entry() &&
1292 (controller_.GetLastCommittedEntry() ||
1293 render_manager_.pending_dom_ui()))
1294 return render_manager_.pending_dom_ui();
1295 return render_manager_.dom_ui();
1296}
[email protected]420ae012009-04-24 05:16:321297
1298void TabContents::DidNavigateMainFramePostCommit(
1299 const NavigationController::LoadCommittedDetails& details,
1300 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]c2e74fe82009-09-03 17:57:441301 if (opener_dom_ui_type_ != DOMUIFactory::kNoDOMUI) {
1302 // If this is a window.open navigation, use the same DOMUI as the renderer
1303 // that opened the window, as long as both renderers have the same
1304 // privileges.
1305 if (opener_dom_ui_type_ == DOMUIFactory::GetDOMUIType(GetURL())) {
1306 DOMUI* dom_ui = DOMUIFactory::CreateDOMUIForURL(this, GetURL());
1307 // dom_ui might be NULL if the URL refers to a non-existent extension.
1308 if (dom_ui) {
1309 render_manager_.SetDOMUIPostCommit(dom_ui);
1310 dom_ui->RenderViewCreated(render_view_host());
1311 }
1312 }
1313 opener_dom_ui_type_ = DOMUIFactory::kNoDOMUI;
1314 }
1315
[email protected]420ae012009-04-24 05:16:321316 if (details.is_user_initiated_main_frame_load()) {
1317 // Clear the status bubble. This is a workaround for a bug where WebKit
1318 // doesn't let us know that the cursor left an element during a
1319 // transition (this is also why the mouse cursor remains as a hand after
1320 // clicking on a link); see bugs 1184641 and 980803. We don't want to
1321 // clear the bubble when a user navigates to a named anchor in the same
1322 // page.
1323 UpdateTargetURL(details.entry->page_id(), GURL());
1324
1325 // UpdateHelpersForDidNavigate will handle the case where the password_form
1326 // origin is valid.
1327 // TODO(brettw) bug 1343111: Password manager stuff in here needs to be
1328 // cleaned up and covered by tests.
1329 if (!params.password_form.origin.is_valid())
1330 GetPasswordManager()->DidNavigate();
1331 }
1332
1333 // The keyword generator uses the navigation entries, so must be called after
1334 // the commit.
1335 GenerateKeywordIfNecessary(params);
1336
1337 // Allow the new page to set the title again.
1338 received_page_title_ = false;
1339
1340 // Get the favicon, either from history or request it from the net.
1341 fav_icon_helper_.FetchFavIcon(details.entry->url());
1342
[email protected]56ce6e52009-10-27 00:10:521343 // Clear all page and browser action state for this tab, unless this is an
1344 // in-page navigation.
[email protected]92c6f9b92009-10-24 04:35:081345 url_canon::Replacements<char> replacements;
1346 replacements.ClearRef();
1347 if (params.url.ReplaceComponents(replacements) !=
1348 params.referrer.ReplaceComponents(replacements)) {
[email protected]92c6f9b92009-10-24 04:35:081349 ExtensionsService* service = profile()->GetExtensionsService();
1350 if (service) {
1351 for (size_t i = 0; i < service->extensions()->size(); ++i) {
[email protected]5d246db22009-10-27 06:17:571352 ExtensionAction* browser_action =
[email protected]92c6f9b92009-10-24 04:35:081353 service->extensions()->at(i)->browser_action();
[email protected]56ce6e52009-10-27 00:10:521354 if (browser_action) {
1355 browser_action->ClearAllValuesForTab(controller().session_id().id());
1356 NotificationService::current()->Notify(
1357 NotificationType::EXTENSION_BROWSER_ACTION_UPDATED,
[email protected]5d246db22009-10-27 06:17:571358 Source<ExtensionAction>(browser_action),
[email protected]56ce6e52009-10-27 00:10:521359 NotificationService::NoDetails());
1360 }
[email protected]92c6f9b92009-10-24 04:35:081361
[email protected]5d246db22009-10-27 06:17:571362 ExtensionAction* page_action =
[email protected]56ce6e52009-10-27 00:10:521363 service->extensions()->at(i)->page_action();
1364 if (page_action) {
1365 page_action->ClearAllValuesForTab(controller().session_id().id());
1366 PageActionStateChanged();
1367 }
[email protected]92c6f9b92009-10-24 04:35:081368 }
[email protected]aece2c7f2009-08-27 20:43:171369 }
1370 }
[email protected]ce5c4502009-05-06 16:46:111371
[email protected]420ae012009-04-24 05:16:321372 // Close constrained popups if necessary.
1373 MaybeCloseChildWindows(details.previous_url, details.entry->url());
1374
1375 // Update the starred state.
1376 UpdateStarredStateForCurrentURL();
1377}
1378
1379void TabContents::DidNavigateAnyFramePostCommit(
1380 RenderViewHost* render_view_host,
1381 const NavigationController::LoadCommittedDetails& details,
1382 const ViewHostMsg_FrameNavigate_Params& params) {
1383 // If we navigate, start showing messages again. This does nothing to prevent
1384 // a malicious script from spamming messages, since the script could just
1385 // reload the page to stop blocking.
1386 suppress_javascript_messages_ = false;
1387
[email protected]420ae012009-04-24 05:16:321388 // Notify the password manager of the navigation or form submit.
1389 // TODO(brettw) bug 1343111: Password manager stuff in here needs to be
1390 // cleaned up and covered by tests.
1391 if (params.password_form.origin.is_valid())
1392 GetPasswordManager()->ProvisionallySavePassword(params.password_form);
1393}
1394
1395void TabContents::MaybeCloseChildWindows(const GURL& previous_url,
1396 const GURL& current_url) {
1397 if (net::RegistryControlledDomainService::SameDomainOrHost(
1398 previous_url, current_url))
1399 return;
1400
1401 // Clear out any child windows since we are leaving this page entirely.
1402 // We use indices instead of iterators in case CloseWindow does something
1403 // that may invalidate an iterator.
1404 int size = static_cast<int>(child_windows_.size());
1405 for (int i = size - 1; i >= 0; --i) {
1406 ConstrainedWindow* window = child_windows_[i];
1407 if (window)
1408 window->CloseConstrainedWindow();
1409 }
[email protected]e29300cd2009-07-23 21:14:591410
1411 // Close the popup container.
1412 if (blocked_popups_) {
1413 blocked_popups_->Destroy();
1414 blocked_popups_ = NULL;
1415 }
[email protected]420ae012009-04-24 05:16:321416}
1417
1418void TabContents::UpdateStarredStateForCurrentURL() {
1419 BookmarkModel* model = profile()->GetBookmarkModel();
1420 const bool old_state = is_starred_;
1421 is_starred_ = (model && model->IsBookmarked(GetURL()));
1422
1423 if (is_starred_ != old_state && delegate())
1424 delegate()->URLStarredChanged(this, is_starred_);
1425}
1426
1427void TabContents::UpdateAlternateErrorPageURL() {
1428 GURL url = GetAlternateErrorPageURL();
1429 render_view_host()->SetAlternateErrorPageURL(url);
1430}
1431
1432void TabContents::UpdateWebPreferences() {
1433 render_view_host()->UpdateWebPreferences(GetWebkitPrefs());
1434}
1435
1436void TabContents::UpdateMaxPageIDIfNecessary(SiteInstance* site_instance,
1437 RenderViewHost* rvh) {
1438 // If we are creating a RVH for a restored controller, then we might
1439 // have more page IDs than the SiteInstance's current max page ID. We must
1440 // make sure that the max page ID is larger than any restored page ID.
1441 // Note that it is ok for conflicting page IDs to exist in another tab
1442 // (i.e., NavigationController), but if any page ID is larger than the max,
1443 // the back/forward list will get confused.
1444 int max_restored_page_id = controller_.max_restored_page_id();
1445 if (max_restored_page_id > 0) {
1446 int curr_max_page_id = site_instance->max_page_id();
1447 if (max_restored_page_id > curr_max_page_id) {
1448 // Need to update the site instance immediately.
1449 site_instance->UpdateMaxPageID(max_restored_page_id);
1450
1451 // Also tell the renderer to update its internal representation. We
1452 // need to reserve enough IDs to make all restored page IDs less than
1453 // the max.
1454 if (curr_max_page_id < 0)
1455 curr_max_page_id = 0;
1456 rvh->ReservePageIDRange(max_restored_page_id - curr_max_page_id);
1457 }
1458 }
1459}
1460
1461void TabContents::UpdateHistoryForNavigation(
[email protected]ebe89e062009-08-13 23:16:541462 const GURL& virtual_url,
[email protected]befd8d822009-07-01 04:51:471463 const NavigationController::LoadCommittedDetails& details,
[email protected]420ae012009-04-24 05:16:321464 const ViewHostMsg_FrameNavigate_Params& params) {
1465 if (profile()->IsOffTheRecord())
1466 return;
1467
1468 // Add to history service.
1469 HistoryService* hs = profile()->GetHistoryService(Profile::IMPLICIT_ACCESS);
1470 if (hs) {
1471 if (PageTransition::IsMainFrame(params.transition) &&
[email protected]ebe89e062009-08-13 23:16:541472 virtual_url != params.url) {
1473 // Hack on the "virtual" URL so that it will appear in history. For some
[email protected]420ae012009-04-24 05:16:321474 // types of URLs, we will display a magic URL that is different from where
1475 // the page is actually navigated. We want the user to see in history
[email protected]ebe89e062009-08-13 23:16:541476 // what they saw in the URL bar, so we add the virtual URL as a redirect.
1477 // This only applies to the main frame, as the virtual URL doesn't apply
[email protected]420ae012009-04-24 05:16:321478 // to sub-frames.
1479 std::vector<GURL> redirects = params.redirects;
1480 if (!redirects.empty())
[email protected]ebe89e062009-08-13 23:16:541481 redirects.back() = virtual_url;
1482 hs->AddPage(virtual_url, this, params.page_id, params.referrer,
[email protected]befd8d822009-07-01 04:51:471483 params.transition, redirects, details.did_replace_entry);
[email protected]420ae012009-04-24 05:16:321484 } else {
1485 hs->AddPage(params.url, this, params.page_id, params.referrer,
[email protected]befd8d822009-07-01 04:51:471486 params.transition, params.redirects,
1487 details.did_replace_entry);
[email protected]420ae012009-04-24 05:16:321488 }
1489 }
1490}
1491
1492bool TabContents::UpdateTitleForEntry(NavigationEntry* entry,
1493 const std::wstring& title) {
1494 // For file URLs without a title, use the pathname instead. In the case of a
1495 // synthesized title, we don't want the update to count toward the "one set
1496 // per page of the title to history."
1497 std::wstring final_title;
1498 bool explicit_set;
1499 if (entry->url().SchemeIsFile() && title.empty()) {
1500 final_title = UTF8ToWide(entry->url().ExtractFileName());
1501 explicit_set = false; // Don't count synthetic titles toward the set limit.
1502 } else {
1503 TrimWhitespace(title, TRIM_ALL, &final_title);
1504 explicit_set = true;
1505 }
1506
1507 if (final_title == UTF16ToWideHack(entry->title()))
1508 return false; // Nothing changed, don't bother.
1509
1510 entry->set_title(WideToUTF16Hack(final_title));
1511
1512 // Update the history system for this page.
1513 if (!profile()->IsOffTheRecord() && !received_page_title_) {
1514 HistoryService* hs =
1515 profile()->GetHistoryService(Profile::IMPLICIT_ACCESS);
1516 if (hs)
[email protected]ebe89e062009-08-13 23:16:541517 hs->SetPageTitle(entry->virtual_url(), final_title);
[email protected]420ae012009-04-24 05:16:321518
1519 // Don't allow the title to be saved again for explicitly set ones.
1520 received_page_title_ = explicit_set;
1521 }
1522
1523 // Lastly, set the title for the view.
1524 view_->SetPageTitle(final_title);
1525
1526 return true;
1527}
1528
1529void TabContents::NotifySwapped() {
1530 // After sending out a swap notification, we need to send a disconnect
1531 // notification so that clients that pick up a pointer to |this| can NULL the
1532 // pointer. See Bug 1230284.
1533 notify_disconnection_ = true;
1534 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341535 NotificationType::TAB_CONTENTS_SWAPPED,
[email protected]d82ed61e2009-06-16 02:46:221536 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321537 NotificationService::NoDetails());
1538}
1539
1540void TabContents::NotifyConnected() {
1541 notify_disconnection_ = true;
1542 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341543 NotificationType::TAB_CONTENTS_CONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221544 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321545 NotificationService::NoDetails());
1546}
1547
1548void TabContents::NotifyDisconnected() {
1549 if (!notify_disconnection_)
1550 return;
1551
1552 notify_disconnection_ = false;
1553 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341554 NotificationType::TAB_CONTENTS_DISCONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221555 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321556 NotificationService::NoDetails());
1557}
1558
1559void TabContents::GenerateKeywordIfNecessary(
1560 const ViewHostMsg_FrameNavigate_Params& params) {
1561 if (!params.searchable_form_url.is_valid())
1562 return;
1563
1564 if (profile()->IsOffTheRecord())
1565 return;
1566
1567 int last_index = controller_.last_committed_entry_index();
1568 // When there was no previous page, the last index will be 0. This is
1569 // normally due to a form submit that opened in a new tab.
1570 // TODO(brettw) bug 916126: we should support keywords when form submits
1571 // happen in new tabs.
1572 if (last_index <= 0)
1573 return;
1574 const NavigationEntry* previous_entry =
1575 controller_.GetEntryAtIndex(last_index - 1);
1576 if (IsFormSubmit(previous_entry)) {
1577 // Only generate a keyword if the previous page wasn't itself a form
1578 // submit.
1579 return;
1580 }
1581
1582 GURL keyword_url = previous_entry->user_typed_url().is_valid() ?
1583 previous_entry->user_typed_url() : previous_entry->url();
1584 std::wstring keyword =
1585 TemplateURLModel::GenerateKeyword(keyword_url, true); // autodetected
1586 if (keyword.empty())
1587 return;
1588
1589 TemplateURLModel* url_model = profile()->GetTemplateURLModel();
1590 if (!url_model)
1591 return;
1592
1593 if (!url_model->loaded()) {
1594 url_model->Load();
1595 return;
1596 }
1597
1598 const TemplateURL* current_url;
1599 std::wstring url = UTF8ToWide(params.searchable_form_url.spec());
1600 if (!url_model->CanReplaceKeyword(keyword, url, &current_url))
1601 return;
1602
1603 if (current_url) {
1604 if (current_url->originating_url().is_valid()) {
1605 // The existing keyword was generated from an OpenSearch description
1606 // document, don't regenerate.
1607 return;
1608 }
1609 url_model->Remove(current_url);
1610 }
1611 TemplateURL* new_url = new TemplateURL();
1612 new_url->set_keyword(keyword);
1613 new_url->set_short_name(keyword);
1614 new_url->SetURL(url, 0, 0);
1615 new_url->add_input_encoding(params.searchable_form_encoding);
1616 DCHECK(controller_.GetLastCommittedEntry());
1617 const GURL& favicon_url =
1618 controller_.GetLastCommittedEntry()->favicon().url();
1619 if (favicon_url.is_valid()) {
1620 new_url->SetFavIconURL(favicon_url);
1621 } else {
1622 // The favicon url isn't valid. This means there really isn't a favicon,
1623 // or the favicon url wasn't obtained before the load started. This assumes
1624 // the later.
1625 // TODO(sky): Need a way to set the favicon that doesn't involve generating
1626 // its url.
1627 new_url->SetFavIconURL(TemplateURL::GenerateFaviconURL(params.referrer));
1628 }
1629 new_url->set_safe_for_autoreplace(true);
1630 url_model->Add(new_url);
1631}
1632
[email protected]bf5c2ff392009-07-08 16:24:331633void TabContents::OnUserGesture() {
1634 // See comment in RenderViewHostDelegate::OnUserGesture as to why we do this.
1635 DownloadRequestManager* drm = g_browser_process->download_request_manager();
1636 if (drm)
1637 drm->OnUserGesture(this);
[email protected]e7eaedde2009-09-25 20:09:491638 ExternalProtocolHandler::OnUserGesture();
[email protected]bf5c2ff392009-07-08 16:24:331639 controller_.OnUserGesture();
1640}
1641
1642void TabContents::OnFindReply(int request_id,
1643 int number_of_matches,
1644 const gfx::Rect& selection_rect,
1645 int active_match_ordinal,
1646 bool final_update) {
[email protected]3ce7ced2009-07-24 18:52:421647 // Ignore responses for requests that have been aborted.
1648 if (find_op_aborted_)
1649 return;
1650
[email protected]bf5c2ff392009-07-08 16:24:331651 // Ignore responses for requests other than the one we have most recently
1652 // issued. That way we won't act on stale results when the user has
1653 // already typed in another query.
1654 if (request_id != current_find_request_id_)
1655 return;
1656
1657 if (number_of_matches == -1)
1658 number_of_matches = last_search_result_.number_of_matches();
1659 if (active_match_ordinal == -1)
1660 active_match_ordinal = last_search_result_.active_match_ordinal();
1661
1662 gfx::Rect selection = selection_rect;
1663 if (selection.IsEmpty())
1664 selection = last_search_result_.selection_rect();
1665
1666 // Notify the UI, automation and any other observers that a find result was
1667 // found.
1668 last_search_result_ = FindNotificationDetails(request_id, number_of_matches,
1669 selection, active_match_ordinal,
1670 final_update);
1671 NotificationService::current()->Notify(
1672 NotificationType::FIND_RESULT_AVAILABLE,
1673 Source<TabContents>(this),
1674 Details<FindNotificationDetails>(&last_search_result_));
1675}
1676
1677void TabContents::GoToEntryAtOffset(int offset) {
[email protected]f9cc4c452009-10-13 14:56:381678 if (!delegate_ || delegate_->OnGoToEntryOffset(offset))
1679 controller_.GoToOffset(offset);
[email protected]bf5c2ff392009-07-08 16:24:331680}
1681
1682void TabContents::GetHistoryListCount(int* back_list_count,
1683 int* forward_list_count) {
1684 int current_index = controller_.last_committed_entry_index();
1685 *back_list_count = current_index;
1686 *forward_list_count = controller_.entry_count() - current_index - 1;
1687}
1688
1689void TabContents::OnMissingPluginStatus(int status) {
1690#if defined(OS_WIN)
1691// TODO(PORT): pull in when plug-ins work
1692 GetPluginInstaller()->OnMissingPluginStatus(status);
1693#endif
1694}
1695
1696void TabContents::OnCrashedPlugin(const FilePath& plugin_path) {
[email protected]bf5c2ff392009-07-08 16:24:331697 DCHECK(!plugin_path.value().empty());
1698
1699 std::wstring plugin_name = plugin_path.ToWStringHack();
[email protected]d32d46182009-07-23 00:09:351700#if defined(OS_WIN) || defined(OS_MACOSX)
[email protected]bf5c2ff392009-07-08 16:24:331701 scoped_ptr<FileVersionInfo> version_info(
1702 FileVersionInfo::CreateFileVersionInfo(plugin_path));
1703 if (version_info.get()) {
1704 const std::wstring& product_name = version_info->product_name();
[email protected]996fd702009-09-04 19:12:371705 if (!product_name.empty()) {
[email protected]bf5c2ff392009-07-08 16:24:331706 plugin_name = product_name;
[email protected]996fd702009-09-04 19:12:371707#if defined(OS_MACOSX)
1708 // Many plugins on the Mac have .plugin in the actual name, which looks
1709 // terrible, so look for that and strip it off if present.
1710 const std::wstring plugin_extension(L".plugin");
1711 if (EndsWith(plugin_name, plugin_extension, true))
1712 plugin_name.erase(plugin_name.length() - plugin_extension.length());
1713#endif // OS_MACOSX
1714 }
[email protected]bf5c2ff392009-07-08 16:24:331715 }
[email protected]4a560ed2009-07-13 23:35:261716#else
1717 NOTIMPLEMENTED() << " convert plugin path to plugin name";
1718#endif
[email protected]bf5c2ff392009-07-08 16:24:331719 AddInfoBar(new SimpleAlertInfoBarDelegate(
1720 this, l10n_util::GetStringF(IDS_PLUGIN_CRASHED_PROMPT, plugin_name),
1721 NULL));
[email protected]bf5c2ff392009-07-08 16:24:331722}
1723
1724void TabContents::OnCrashedWorker() {
1725 AddInfoBar(new SimpleAlertInfoBarDelegate(
1726 this, l10n_util::GetString(IDS_WEBWORKER_CRASHED_PROMPT),
1727 NULL));
1728}
1729
1730void TabContents::OnDidGetApplicationInfo(
1731 int32 page_id,
1732 const webkit_glue::WebApplicationInfo& info) {
[email protected]ed5431872009-11-17 08:39:511733 web_app_info_ = info;
[email protected]bf5c2ff392009-07-08 16:24:331734
[email protected]ed5431872009-11-17 08:39:511735 if (delegate())
1736 delegate()->OnDidGetApplicationInfo(this, page_id);
[email protected]bf5c2ff392009-07-08 16:24:331737}
1738
1739void TabContents::DidStartProvisionalLoadForFrame(
1740 RenderViewHost* render_view_host,
1741 bool is_main_frame,
1742 const GURL& url) {
1743 ProvisionalLoadDetails details(is_main_frame,
1744 controller_.IsURLInPageNavigation(url),
1745 url, std::string(), false);
1746 NotificationService::current()->Notify(
1747 NotificationType::FRAME_PROVISIONAL_LOAD_START,
1748 Source<NavigationController>(&controller_),
1749 Details<ProvisionalLoadDetails>(&details));
1750}
1751
1752void TabContents::DidStartReceivingResourceResponse(
1753 ResourceRequestDetails* details) {
1754 NotificationService::current()->Notify(
1755 NotificationType::RESOURCE_RESPONSE_STARTED,
1756 Source<NavigationController>(&controller()),
1757 Details<ResourceRequestDetails>(details));
1758}
1759
1760void TabContents::DidRedirectResource(ResourceRequestDetails* details) {
1761 NotificationService::current()->Notify(
1762 NotificationType::RESOURCE_RECEIVED_REDIRECT,
1763 Source<NavigationController>(&controller()),
1764 Details<ResourceRequestDetails>(details));
1765}
1766
1767void TabContents::DidLoadResourceFromMemoryCache(
1768 const GURL& url,
1769 const std::string& frame_origin,
1770 const std::string& main_frame_origin,
1771 const std::string& security_info) {
1772 // Send out a notification that we loaded a resource from our memory cache.
1773 int cert_id = 0, cert_status = 0, security_bits = 0;
1774 SSLManager::DeserializeSecurityInfo(security_info,
1775 &cert_id, &cert_status,
1776 &security_bits);
1777 LoadFromMemoryCacheDetails details(url, frame_origin, main_frame_origin,
[email protected]76543b92009-08-31 17:27:451778 process()->id(), cert_id, cert_status);
[email protected]bf5c2ff392009-07-08 16:24:331779
1780 NotificationService::current()->Notify(
1781 NotificationType::LOAD_FROM_MEMORY_CACHE,
1782 Source<NavigationController>(&controller_),
1783 Details<LoadFromMemoryCacheDetails>(&details));
1784}
1785
[email protected]e3d60e5d2009-09-25 21:08:291786void TabContents::DidDisplayInsecureContent() {
[email protected]09843b5f2009-09-26 08:30:591787 controller_.ssl_manager()->DidDisplayInsecureContent();
[email protected]e3d60e5d2009-09-25 21:08:291788}
1789
1790void TabContents::DidRunInsecureContent(const std::string& security_origin) {
[email protected]09843b5f2009-09-26 08:30:591791 controller_.ssl_manager()->DidRunInsecureContent(security_origin);
[email protected]e3d60e5d2009-09-25 21:08:291792}
1793
[email protected]bf5c2ff392009-07-08 16:24:331794void TabContents::DidFailProvisionalLoadWithError(
1795 RenderViewHost* render_view_host,
1796 bool is_main_frame,
1797 int error_code,
1798 const GURL& url,
1799 bool showing_repost_interstitial) {
1800 if (net::ERR_ABORTED == error_code) {
1801 // EVIL HACK ALERT! Ignore failed loads when we're showing interstitials.
1802 // This means that the interstitial won't be torn down properly, which is
1803 // bad. But if we have an interstitial, go back to another tab type, and
1804 // then load the same interstitial again, we could end up getting the first
1805 // interstitial's "failed" message (as a result of the cancel) when we're on
1806 // the second one.
1807 //
1808 // We can't tell this apart, so we think we're tearing down the current page
1809 // which will cause a crash later one. There is also some code in
1810 // RenderViewHostManager::RendererAbortedProvisionalLoad that is commented
1811 // out because of this problem.
1812 //
1813 // http://code.google.com/p/chromium/issues/detail?id=2855
1814 // Because this will not tear down the interstitial properly, if "back" is
1815 // back to another tab type, the interstitial will still be somewhat alive
1816 // in the previous tab type. If you navigate somewhere that activates the
1817 // tab with the interstitial again, you'll see a flash before the new load
1818 // commits of the interstitial page.
1819 if (showing_interstitial_page()) {
1820 LOG(WARNING) << "Discarding message during interstitial.";
1821 return;
1822 }
1823
1824 // This will discard our pending entry if we cancelled the load (e.g., if we
1825 // decided to download the file instead of load it). Only discard the
1826 // pending entry if the URLs match, otherwise the user initiated a navigate
1827 // before the page loaded so that the discard would discard the wrong entry.
1828 NavigationEntry* pending_entry = controller_.pending_entry();
1829 if (pending_entry && pending_entry->url() == url) {
1830 controller_.DiscardNonCommittedEntries();
1831 // Update the URL display.
1832 NotifyNavigationStateChanged(TabContents::INVALIDATE_URL);
1833 }
1834
1835 render_manager_.RendererAbortedProvisionalLoad(render_view_host);
1836 }
1837
1838 // Send out a notification that we failed a provisional load with an error.
1839 ProvisionalLoadDetails details(is_main_frame,
1840 controller_.IsURLInPageNavigation(url),
1841 url, std::string(), false);
1842 details.set_error_code(error_code);
1843
1844 NotificationService::current()->Notify(
1845 NotificationType::FAIL_PROVISIONAL_LOAD_WITH_ERROR,
1846 Source<NavigationController>(&controller_),
1847 Details<ProvisionalLoadDetails>(&details));
1848}
1849
1850void TabContents::DocumentLoadedInFrame() {
1851 controller_.DocumentLoadedInFrame();
1852}
1853
[email protected]8d3347f2009-07-09 22:00:211854RenderViewHostDelegate::View* TabContents::GetViewDelegate() {
[email protected]420ae012009-04-24 05:16:321855 return view_.get();
1856}
1857
[email protected]8d3347f2009-07-09 22:00:211858RenderViewHostDelegate::RendererManagement*
1859TabContents::GetRendererManagementDelegate() {
1860 return &render_manager_;
1861}
1862
[email protected]bf5c2ff392009-07-08 16:24:331863RenderViewHostDelegate::BrowserIntegration*
[email protected]8d3347f2009-07-09 22:00:211864 TabContents::GetBrowserIntegrationDelegate() {
1865 return this;
[email protected]bf5c2ff392009-07-08 16:24:331866}
1867
[email protected]8d3347f2009-07-09 22:00:211868RenderViewHostDelegate::Resource* TabContents::GetResourceDelegate() {
1869 return this;
[email protected]bf5c2ff392009-07-08 16:24:331870}
1871
[email protected]8d3347f2009-07-09 22:00:211872RenderViewHostDelegate::Save* TabContents::GetSaveDelegate() {
[email protected]420ae012009-04-24 05:16:321873 return save_package_.get(); // May be NULL, but we can return NULL.
1874}
1875
[email protected]8d3347f2009-07-09 22:00:211876RenderViewHostDelegate::Printing* TabContents::GetPrintingDelegate() {
1877 return &printing_;
1878}
1879
1880RenderViewHostDelegate::FavIcon* TabContents::GetFavIconDelegate() {
1881 return &fav_icon_helper_;
1882}
1883
[email protected]c54d269e2009-10-21 18:07:381884RenderViewHostDelegate::FormFieldHistory*
1885TabContents::GetFormFieldHistoryDelegate() {
[email protected]19289d02009-10-21 21:47:001886 if (form_field_history_manager_.get() == NULL)
1887 form_field_history_manager_.reset(new FormFieldHistoryManager(this));
1888 return form_field_history_manager_.get();
[email protected]bf5c2ff392009-07-08 16:24:331889}
1890
[email protected]21674df2009-10-28 19:51:211891RenderViewHostDelegate::AutoFill* TabContents::GetAutoFillDelegate() {
1892 if (autofill_manager_.get() == NULL)
1893 autofill_manager_.reset(new AutoFillManager(this));
1894 return autofill_manager_.get();
1895}
1896
[email protected]93623c5d2009-12-10 21:40:321897RendererPreferences TabContents::GetRendererPrefs(Profile* profile) const {
[email protected]840b1512009-07-21 16:53:461898 return renderer_preferences_;
[email protected]80d96fa2009-06-10 22:34:511899}
1900
[email protected]57c6a652009-05-04 07:58:341901TabContents* TabContents::GetAsTabContents() {
1902 return this;
1903}
1904
[email protected]3e7cf1362009-08-21 17:47:081905void TabContents::AddBlockedNotice(const GURL& url, const string16& reason) {
1906 CreateBlockedPopupContainerIfNecessary();
1907 blocked_popups_->AddBlockedNotice(url, reason);
1908}
1909
[email protected]7b291f92009-08-14 05:43:531910ViewType::Type TabContents::GetRenderViewType() const {
1911 return ViewType::TAB_CONTENTS;
1912}
1913
1914int TabContents::GetBrowserWindowID() const {
1915 return controller().window_id().id();
1916}
1917
[email protected]420ae012009-04-24 05:16:321918void TabContents::RenderViewCreated(RenderViewHost* render_view_host) {
[email protected]2e4633c2009-07-09 16:58:061919 NotificationService::current()->Notify(
1920 NotificationType::RENDER_VIEW_HOST_CREATED_FOR_TAB,
1921 Source<TabContents>(this),
1922 Details<RenderViewHost>(render_view_host));
[email protected]420ae012009-04-24 05:16:321923 NavigationEntry* entry = controller_.GetActiveEntry();
1924 if (!entry)
1925 return;
1926
1927 // When we're creating views, we're still doing initial setup, so we always
1928 // use the pending DOM UI rather than any possibly existing committed one.
1929 if (render_manager_.pending_dom_ui()) {
1930 render_manager_.pending_dom_ui()->RenderViewCreated(render_view_host);
1931 }
1932
1933 if (entry->IsViewSourceMode()) {
1934 // Put the renderer in view source mode.
1935 render_view_host->Send(
1936 new ViewMsg_EnableViewSourceMode(render_view_host->routing_id()));
1937 }
[email protected]0666aef2009-05-13 19:48:081938
1939 view()->RenderViewCreated(render_view_host);
[email protected]420ae012009-04-24 05:16:321940}
1941
1942void TabContents::RenderViewReady(RenderViewHost* rvh) {
1943 if (rvh != render_view_host()) {
1944 // Don't notify the world, since this came from a renderer in the
1945 // background.
1946 return;
1947 }
1948
1949 NotifyConnected();
[email protected]9d8a4642009-07-29 17:25:301950 bool was_crashed = is_crashed();
[email protected]420ae012009-04-24 05:16:321951 SetIsCrashed(false);
[email protected]9d8a4642009-07-29 17:25:301952
1953 // Restore the focus to the tab (otherwise the focus will be on the top
1954 // window).
1955 if (was_crashed && !FocusLocationBarByDefault())
1956 Focus();
[email protected]420ae012009-04-24 05:16:321957}
1958
1959void TabContents::RenderViewGone(RenderViewHost* rvh) {
1960 // Ask the print preview if this renderer was valuable.
1961 if (!printing_.OnRenderViewGone(rvh))
1962 return;
1963 if (rvh != render_view_host()) {
1964 // The pending page's RenderViewHost is gone.
1965 return;
1966 }
1967
1968 SetIsLoading(false, NULL);
1969 NotifyDisconnected();
1970 SetIsCrashed(true);
1971
[email protected]8e537302009-06-10 21:19:581972 // Tell the view that we've crashed so it can prepare the sad tab page.
[email protected]cf68b092010-01-15 20:05:551973 // Only do this if we're not in browser shutdown, so that TabContents
1974 // objects that are not in a browser (e.g., HTML dialogs) and thus are
1975 // visible do not flash a sad tab page.
1976 if (browser_shutdown::GetShutdownType() == browser_shutdown::NOT_VALID)
1977 view_->OnTabCrashed();
[email protected]420ae012009-04-24 05:16:321978
1979 // Hide any visible hung renderer warning for this web contents' process.
[email protected]e7b8d832009-11-20 22:41:001980 hung_renderer_dialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:321981}
1982
[email protected]2e4633c2009-07-09 16:58:061983void TabContents::RenderViewDeleted(RenderViewHost* rvh) {
1984 NotificationService::current()->Notify(
1985 NotificationType::RENDER_VIEW_HOST_DELETED,
1986 Source<TabContents>(this),
1987 Details<RenderViewHost>(rvh));
[email protected]fb7b79f72009-11-06 18:00:481988 render_manager_.RenderViewDeleted(rvh);
[email protected]2e4633c2009-07-09 16:58:061989}
1990
[email protected]420ae012009-04-24 05:16:321991void TabContents::DidNavigate(RenderViewHost* rvh,
1992 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]8030f012009-09-25 18:09:371993 int extra_invalidate_flags = 0;
1994
1995 if (PageTransition::IsMainFrame(params.transition)) {
[email protected]20ede072009-10-06 00:28:351996 bool was_bookmark_bar_visible = ShouldShowBookmarkBar();
[email protected]8030f012009-09-25 18:09:371997 bool was_extension_shelf_visible = IsExtensionShelfAlwaysVisible();
1998
[email protected]420ae012009-04-24 05:16:321999 render_manager_.DidNavigateMainFrame(rvh);
2000
[email protected]20ede072009-10-06 00:28:352001 if (was_bookmark_bar_visible != ShouldShowBookmarkBar())
[email protected]8030f012009-09-25 18:09:372002 extra_invalidate_flags |= INVALIDATE_BOOKMARK_BAR;
2003 if (was_extension_shelf_visible != IsExtensionShelfAlwaysVisible())
2004 extra_invalidate_flags |= INVALIDATE_EXTENSION_SHELF;
2005 }
2006
[email protected]420ae012009-04-24 05:16:322007 // Update the site of the SiteInstance if it doesn't have one yet.
2008 if (!GetSiteInstance()->has_site())
2009 GetSiteInstance()->SetSite(params.url);
2010
2011 // Need to update MIME type here because it's referred to in
2012 // UpdateNavigationCommands() called by RendererDidNavigate() to
2013 // determine whether or not to enable the encoding menu.
2014 // It's updated only for the main frame. For a subframe,
2015 // RenderView::UpdateURL does not set params.contents_mime_type.
2016 // (see http://code.google.com/p/chromium/issues/detail?id=2929 )
2017 // TODO(jungshik): Add a test for the encoding menu to avoid
2018 // regressing it again.
2019 if (PageTransition::IsMainFrame(params.transition))
2020 contents_mime_type_ = params.contents_mime_type;
2021
2022 NavigationController::LoadCommittedDetails details;
[email protected]8030f012009-09-25 18:09:372023 bool did_navigate = controller_.RendererDidNavigate(
2024 params, extra_invalidate_flags, &details);
[email protected]3e90d4a2009-07-03 17:38:392025
2026 // Update history. Note that this needs to happen after the entry is complete,
2027 // which WillNavigate[Main,Sub]Frame will do before this function is called.
[email protected]2e29d922009-10-22 22:13:352028 if (params.should_update_history &&
2029 (!delegate() || delegate()->ShouldAddNavigationsToHistory())) {
[email protected]3e90d4a2009-07-03 17:38:392030 // Most of the time, the displayURL matches the loaded URL, but for about:
2031 // URLs, we use a data: URL as the real value. We actually want to save
2032 // the about: URL to the history db and keep the data: URL hidden. This is
2033 // what the TabContents' URL getter does.
2034 UpdateHistoryForNavigation(GetURL(), details, params);
2035 }
2036
[email protected]76543b92009-08-31 17:27:452037 if (!did_navigate)
2038 return; // No navigation happened.
[email protected]420ae012009-04-24 05:16:322039
2040 // DO NOT ADD MORE STUFF TO THIS FUNCTION! Your component should either listen
2041 // for the appropriate notification (best) or you can add it to
2042 // DidNavigateMainFramePostCommit / DidNavigateAnyFramePostCommit (only if
2043 // necessary, please).
2044
2045 // Run post-commit tasks.
2046 if (details.is_main_frame)
2047 DidNavigateMainFramePostCommit(details, params);
2048 DidNavigateAnyFramePostCommit(rvh, details, params);
2049}
2050
2051void TabContents::UpdateState(RenderViewHost* rvh,
2052 int32 page_id,
2053 const std::string& state) {
2054 DCHECK(rvh == render_view_host());
2055
2056 // We must be prepared to handle state updates for any page, these occur
2057 // when the user is scrolling and entering form data, as well as when we're
2058 // leaving a page, in which case our state may have already been moved to
2059 // the next page. The navigation controller will look up the appropriate
2060 // NavigationEntry and update it when it is notified via the delegate.
2061
2062 int entry_index = controller_.GetEntryIndexWithPageID(
2063 GetSiteInstance(), page_id);
2064 if (entry_index < 0)
2065 return;
2066 NavigationEntry* entry = controller_.GetEntryAtIndex(entry_index);
2067
2068 if (state == entry->content_state())
2069 return; // Nothing to update.
2070 entry->set_content_state(state);
2071 controller_.NotifyEntryChanged(entry, entry_index);
2072}
2073
2074void TabContents::UpdateTitle(RenderViewHost* rvh,
2075 int32 page_id, const std::wstring& title) {
2076 // If we have a title, that's a pretty good indication that we've started
2077 // getting useful data.
2078 SetNotWaitingForResponse();
2079
2080 DCHECK(rvh == render_view_host());
2081 NavigationEntry* entry = controller_.GetEntryWithPageID(GetSiteInstance(),
2082 page_id);
2083 if (!entry || !UpdateTitleForEntry(entry, title))
2084 return;
2085
2086 // Broadcast notifications when the UI should be updated.
2087 if (entry == controller_.GetEntryAtOffset(0))
[email protected]f1cd5e82009-10-23 17:19:032088 NotifyNavigationStateChanged(INVALIDATE_TITLE);
[email protected]420ae012009-04-24 05:16:322089}
2090
[email protected]420ae012009-04-24 05:16:322091void TabContents::UpdateEncoding(RenderViewHost* render_view_host,
[email protected]41fc0322009-09-04 22:23:402092 const std::string& encoding) {
[email protected]420ae012009-04-24 05:16:322093 set_encoding(encoding);
2094}
2095
2096void TabContents::UpdateTargetURL(int32 page_id, const GURL& url) {
2097 if (delegate())
2098 delegate()->UpdateTargetURL(this, url);
2099}
2100
2101void TabContents::UpdateThumbnail(const GURL& url,
2102 const SkBitmap& bitmap,
2103 const ThumbnailScore& score) {
2104 // Tell History about this thumbnail
[email protected]449478302009-06-09 20:04:282105 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kThumbnailStore)) {
[email protected]72d0fc72009-07-09 17:30:492106 if (!profile()->IsOffTheRecord())
[email protected]42339402009-07-17 17:27:192107 profile()->GetThumbnailStore()->SetPageThumbnail(url, bitmap,
2108 score, true);
[email protected]449478302009-06-09 20:04:282109 } else {
2110 HistoryService* hs;
2111 if (!profile()->IsOffTheRecord() &&
2112 (hs = profile()->GetHistoryService(Profile::IMPLICIT_ACCESS))) {
2113 hs->SetPageThumbnail(url, bitmap, score);
2114 }
[email protected]420ae012009-04-24 05:16:322115 }
2116}
2117
[email protected]8922e1f2009-10-03 05:01:262118void TabContents::UpdateInspectorSettings(const std::string& raw_settings) {
[email protected]0df30122009-06-03 12:13:082119 profile()->GetPrefs()->SetString(prefs::kWebKitInspectorSettings,
[email protected]8922e1f2009-10-03 05:01:262120 UTF8ToWide(raw_settings));
[email protected]0df30122009-06-03 12:13:082121}
2122
[email protected]420ae012009-04-24 05:16:322123void TabContents::Close(RenderViewHost* rvh) {
[email protected]07707302009-11-06 00:50:292124 // The UI may be in an event-tracking loop, such as between the
2125 // mouse-down and mouse-up in text selection or a button click.
2126 // Defer the close until after tracking is complete, so that we
2127 // don't free objects out from under the UI.
2128 // TODO(shess): This could probably be integrated with the
2129 // IsDoingDrag() test below. Punting for now because I need more
2130 // research to understand how this impacts platforms other than Mac.
2131 // TODO(shess): This could get more fine-grained. For instance,
2132 // closing a tab in another window while selecting text in the
2133 // current window's Omnibox should be just fine.
2134 if (view()->IsEventTracking()) {
2135 view()->CloseTabAfterEventTracking();
2136 return;
2137 }
2138
[email protected]24a4d1062009-07-10 23:10:422139 // If we close the tab while we're in the middle of a drag, we'll crash.
2140 // Instead, cancel the drag and close it as soon as the drag ends.
2141 if (view()->IsDoingDrag()) {
2142 view()->CancelDragAndCloseTab();
2143 return;
2144 }
2145
[email protected]420ae012009-04-24 05:16:322146 // Ignore this if it comes from a RenderViewHost that we aren't showing.
2147 if (delegate() && rvh == render_view_host())
2148 delegate()->CloseContents(this);
2149}
2150
2151void TabContents::RequestMove(const gfx::Rect& new_bounds) {
2152 if (delegate() && delegate()->IsPopup(this))
2153 delegate()->MoveContents(this, new_bounds);
2154}
2155
[email protected]7ab1e7d62009-10-14 23:32:012156void TabContents::DidStartLoading() {
[email protected]420ae012009-04-24 05:16:322157 SetIsLoading(true, NULL);
2158}
2159
[email protected]7ab1e7d62009-10-14 23:32:012160void TabContents::DidStopLoading() {
[email protected]420ae012009-04-24 05:16:322161 scoped_ptr<LoadNotificationDetails> details;
2162
2163 NavigationEntry* entry = controller_.GetActiveEntry();
2164 // An entry may not exist for a stop when loading an initial blank page or
2165 // if an iframe injected by script into a blank page finishes loading.
2166 if (entry) {
[email protected]420ae012009-04-24 05:16:322167 base::TimeDelta elapsed = base::TimeTicks::Now() - current_load_start_;
2168
2169 details.reset(new LoadNotificationDetails(
[email protected]ebe89e062009-08-13 23:16:542170 entry->virtual_url(),
[email protected]420ae012009-04-24 05:16:322171 entry->transition_type(),
2172 elapsed,
2173 &controller_,
2174 controller_.GetCurrentEntryIndex()));
2175 }
2176
2177 // Tell PasswordManager we've finished a page load, which serves as a
2178 // green light to save pending passwords and reset itself.
2179 GetPasswordManager()->DidStopLoading();
2180
2181 SetIsLoading(false, details.get());
2182}
2183
[email protected]420ae012009-04-24 05:16:322184void TabContents::DidRedirectProvisionalLoad(int32 page_id,
2185 const GURL& source_url,
2186 const GURL& target_url) {
2187 NavigationEntry* entry;
2188 if (page_id == -1)
2189 entry = controller_.pending_entry();
2190 else
2191 entry = controller_.GetEntryWithPageID(GetSiteInstance(), page_id);
2192 if (!entry || entry->url() != source_url)
2193 return;
2194 entry->set_url(target_url);
2195}
2196
[email protected]420ae012009-04-24 05:16:322197void TabContents::RequestOpenURL(const GURL& url, const GURL& referrer,
2198 WindowOpenDisposition disposition) {
2199 if (render_manager_.dom_ui()) {
2200 // When we're a DOM UI, it will provide a page transition type for us (this
2201 // is so the new tab page can specify AUTO_BOOKMARK for automatically
2202 // generated suggestions).
2203 //
2204 // Note also that we hide the referrer for DOM UI pages. We don't really
[email protected]60e448982009-05-06 04:21:162205 // want web sites to see a referrer of "chrome://blah" (and some
2206 // chrome: URLs might have search terms or other stuff we don't want to
[email protected]420ae012009-04-24 05:16:322207 // send to the site), so we send no referrer.
2208 OpenURL(url, GURL(), disposition,
2209 render_manager_.dom_ui()->link_transition_type());
2210 } else {
2211 OpenURL(url, referrer, disposition, PageTransition::LINK);
2212 }
2213}
2214
[email protected]1ee614862009-07-06 20:29:182215void TabContents::DomOperationResponse(const std::string& json_string,
2216 int automation_id) {
2217}
2218
[email protected]420ae012009-04-24 05:16:322219void TabContents::ProcessDOMUIMessage(const std::string& message,
[email protected]e4dad9fb2009-10-06 18:15:582220 const Value* content,
[email protected]811bfe32009-07-01 08:46:252221 int request_id,
2222 bool has_callback) {
[email protected]420ae012009-04-24 05:16:322223 if (!render_manager_.dom_ui()) {
[email protected]c2e74fe82009-09-03 17:57:442224 // This can happen if someone uses window.open() to open an extension URL
2225 // from a non-extension context.
2226 render_view_host()->BlockExtensionRequest(request_id);
[email protected]420ae012009-04-24 05:16:322227 return;
2228 }
[email protected]811bfe32009-07-01 08:46:252229 render_manager_.dom_ui()->ProcessDOMUIMessage(message, content, request_id,
2230 has_callback);
[email protected]420ae012009-04-24 05:16:322231}
2232
2233void TabContents::ProcessExternalHostMessage(const std::string& message,
2234 const std::string& origin,
2235 const std::string& target) {
2236 if (delegate())
2237 delegate()->ForwardMessageToExternalHost(message, origin, target);
2238}
2239
[email protected]420ae012009-04-24 05:16:322240void TabContents::RunFileChooser(bool multiple_files,
2241 const string16& title,
2242 const FilePath& default_file) {
2243 if (!select_file_dialog_.get())
2244 select_file_dialog_ = SelectFileDialog::Create(this);
2245 SelectFileDialog::Type dialog_type =
2246 multiple_files ? SelectFileDialog::SELECT_OPEN_MULTI_FILE :
2247 SelectFileDialog::SELECT_OPEN_FILE;
2248 select_file_dialog_->SelectFile(dialog_type, title, default_file,
2249 NULL, 0, FILE_PATH_LITERAL(""),
2250 view_->GetTopLevelNativeWindow(), NULL);
2251}
2252
2253void TabContents::RunJavaScriptMessage(
2254 const std::wstring& message,
2255 const std::wstring& default_prompt,
2256 const GURL& frame_url,
2257 const int flags,
2258 IPC::Message* reply_msg,
2259 bool* did_suppress_message) {
2260 // Suppress javascript messages when requested and when inside a constrained
2261 // popup window (because that activates them and breaks them out of the
2262 // constrained window jail).
[email protected]91ed49a2009-07-07 21:29:552263 // Also suppress messages when showing an interstitial. The interstitial is
2264 // shown over the previous page, we don't want the hidden page dialogs to
2265 // interfere with the interstitial.
2266 bool suppress_this_message = suppress_javascript_messages_ ||
2267 showing_interstitial_page();
[email protected]420ae012009-04-24 05:16:322268 if (delegate())
2269 suppress_this_message |=
[email protected]b6c874582009-05-08 19:38:312270 (delegate()->GetConstrainingContents(this) != this);
[email protected]420ae012009-04-24 05:16:322271
2272 *did_suppress_message = suppress_this_message;
2273
2274 if (!suppress_this_message) {
2275 base::TimeDelta time_since_last_message(
2276 base::TimeTicks::Now() - last_javascript_message_dismissal_);
2277 bool show_suppress_checkbox = false;
2278 // Show a checkbox offering to suppress further messages if this message is
2279 // being displayed within kJavascriptMessageExpectedDelay of the last one.
2280 if (time_since_last_message <
2281 base::TimeDelta::FromMilliseconds(kJavascriptMessageExpectedDelay))
2282 show_suppress_checkbox = true;
2283
[email protected]d82ed61e2009-06-16 02:46:222284 RunJavascriptMessageBox(this, frame_url, flags, message, default_prompt,
[email protected]420ae012009-04-24 05:16:322285 show_suppress_checkbox, reply_msg);
2286 } else {
2287 // If we are suppressing messages, just reply as is if the user immediately
2288 // pressed "Cancel".
[email protected]beb440c2009-11-06 04:08:542289 OnMessageBoxClosed(reply_msg, false, std::wstring());
[email protected]420ae012009-04-24 05:16:322290 }
2291}
2292
2293void TabContents::RunBeforeUnloadConfirm(const std::wstring& message,
2294 IPC::Message* reply_msg) {
[email protected]7ab1e7d62009-10-14 23:32:012295 is_showing_before_unload_dialog_ = true;
[email protected]d82ed61e2009-06-16 02:46:222296 RunBeforeUnloadDialog(this, message, reply_msg);
[email protected]420ae012009-04-24 05:16:322297}
2298
2299void TabContents::ShowModalHTMLDialog(const GURL& url, int width, int height,
2300 const std::string& json_arguments,
2301 IPC::Message* reply_msg) {
2302 if (delegate()) {
2303 HtmlDialogUIDelegate* dialog_delegate =
2304 new ModalHtmlDialogDelegate(url, width, height, json_arguments,
[email protected]d82ed61e2009-06-16 02:46:222305 reply_msg, this);
[email protected]420ae012009-04-24 05:16:322306 delegate()->ShowHtmlDialog(dialog_delegate, NULL);
2307 }
2308}
2309
2310void TabContents::PasswordFormsSeen(
[email protected]ed3fb032009-06-16 19:50:562311 const std::vector<webkit_glue::PasswordForm>& forms) {
[email protected]420ae012009-04-24 05:16:322312 GetPasswordManager()->PasswordFormsSeen(forms);
2313}
2314
[email protected]420ae012009-04-24 05:16:322315// Checks to see if we should generate a keyword based on the OSDD, and if
2316// necessary uses TemplateURLFetcher to download the OSDD and create a keyword.
2317void TabContents::PageHasOSDD(RenderViewHost* render_view_host,
2318 int32 page_id, const GURL& url,
2319 bool autodetected) {
2320 // Make sure page_id is the current page, and the TemplateURLModel is loaded.
2321 DCHECK(url.is_valid());
2322 if (!IsActiveEntry(page_id))
2323 return;
2324 TemplateURLModel* url_model = profile()->GetTemplateURLModel();
2325 if (!url_model)
2326 return;
2327 if (!url_model->loaded()) {
2328 url_model->Load();
2329 return;
2330 }
2331 if (!profile()->GetTemplateURLFetcher())
2332 return;
2333
2334 if (profile()->IsOffTheRecord())
2335 return;
2336
2337 const NavigationEntry* entry = controller_.GetLastCommittedEntry();
2338 DCHECK(entry);
2339
2340 const NavigationEntry* base_entry = entry;
2341 if (IsFormSubmit(base_entry)) {
2342 // If the current page is a form submit, find the last page that was not
2343 // a form submit and use its url to generate the keyword from.
2344 int index = controller_.last_committed_entry_index() - 1;
2345 while (index >= 0 && IsFormSubmit(controller_.GetEntryAtIndex(index)))
2346 index--;
2347 if (index >= 0)
2348 base_entry = controller_.GetEntryAtIndex(index);
2349 else
2350 base_entry = NULL;
2351 }
2352
2353 // We want to use the user typed URL if available since that represents what
2354 // the user typed to get here, and fall back on the regular URL if not.
2355 if (!base_entry)
2356 return;
2357 GURL keyword_url = base_entry->user_typed_url().is_valid() ?
2358 base_entry->user_typed_url() : base_entry->url();
2359 if (!keyword_url.is_valid())
2360 return;
2361 std::wstring keyword = TemplateURLModel::GenerateKeyword(keyword_url,
2362 autodetected);
[email protected]a88dbdd52010-01-15 19:25:532363
2364 // For JS added OSDD empty keyword is OK because we will generate keyword
2365 // later from OSDD content.
2366 if (autodetected && keyword.empty())
[email protected]420ae012009-04-24 05:16:322367 return;
2368 const TemplateURL* template_url =
2369 url_model->GetTemplateURLForKeyword(keyword);
2370 if (template_url && (!template_url->safe_for_autoreplace() ||
2371 template_url->originating_url() == url)) {
2372 // Either there is a user created TemplateURL for this keyword, or the
2373 // keyword has the same OSDD url and we've parsed it.
2374 return;
2375 }
2376
2377 // Download the OpenSearch description document. If this is successful a
2378 // new keyword will be created when done.
[email protected]420ae012009-04-24 05:16:322379 profile()->GetTemplateURLFetcher()->ScheduleDownload(
2380 keyword,
2381 url,
2382 base_entry->favicon().url(),
[email protected]d0cc9fb52009-06-23 02:26:162383 this,
[email protected]420ae012009-04-24 05:16:322384 autodetected);
2385}
2386
[email protected]420ae012009-04-24 05:16:322387GURL TabContents::GetAlternateErrorPageURL() const {
2388 GURL url;
2389 // Disable alternate error pages when in OffTheRecord/Incognito mode.
2390 if (profile()->IsOffTheRecord())
2391 return url;
2392
2393 PrefService* prefs = profile()->GetPrefs();
2394 DCHECK(prefs);
2395 if (prefs->GetBoolean(prefs::kAlternateErrorPagesEnabled)) {
[email protected]a33457b82009-09-21 23:07:152396 url = google_util::AppendGoogleLocaleParam(
2397 GURL(google_util::kLinkDoctorBaseURL));
[email protected]420ae012009-04-24 05:16:322398 url = google_util::AppendGoogleTLDParam(url);
2399 }
2400 return url;
2401}
2402
2403WebPreferences TabContents::GetWebkitPrefs() {
2404 PrefService* prefs = render_view_host()->process()->profile()->GetPrefs();
[email protected]7ccddb8c2009-08-04 17:36:552405 bool is_dom_ui = false;
2406 return RenderViewHostDelegateHelper::GetWebkitPrefs(prefs, is_dom_ui);
[email protected]420ae012009-04-24 05:16:322407}
2408
[email protected]420ae012009-04-24 05:16:322409void TabContents::OnJSOutOfMemory() {
2410 AddInfoBar(new SimpleAlertInfoBarDelegate(
2411 this, l10n_util::GetString(IDS_JS_OUT_OF_MEMORY_PROMPT), NULL));
2412}
2413
[email protected]420ae012009-04-24 05:16:322414void TabContents::OnCrossSiteResponse(int new_render_process_host_id,
2415 int new_request_id) {
[email protected]57c6a652009-05-04 07:58:342416 // Allows the TabContents to react when a cross-site response is ready to be
[email protected]420ae012009-04-24 05:16:322417 // delivered to a pending RenderViewHost. We must first run the onunload
2418 // handler of the old RenderViewHost before we can allow it to proceed.
2419 render_manager_.OnCrossSiteResponse(new_render_process_host_id,
2420 new_request_id);
2421}
2422
2423bool TabContents::CanBlur() const {
2424 return delegate() ? delegate()->CanBlur() : true;
2425}
2426
2427gfx::Rect TabContents::GetRootWindowResizerRect() const {
2428 if (delegate())
2429 return delegate()->GetRootWindowResizerRect();
2430 return gfx::Rect();
2431}
2432
2433void TabContents::RendererUnresponsive(RenderViewHost* rvh,
2434 bool is_during_unload) {
2435 if (is_during_unload) {
2436 // Hang occurred while firing the beforeunload/unload handler.
2437 // Pretend the handler fired so tab closing continues as if it had.
[email protected]829e7612009-04-25 01:15:112438 rvh->set_sudden_termination_allowed(true);
[email protected]420ae012009-04-24 05:16:322439
2440 if (!render_manager_.ShouldCloseTabOnUnresponsiveRenderer())
2441 return;
2442
2443 // If the tab hangs in the beforeunload/unload handler there's really
2444 // nothing we can do to recover. Pretend the unload listeners have
2445 // all fired and close the tab. If the hang is in the beforeunload handler
2446 // then the user will not have the option of cancelling the close.
2447 Close(rvh);
2448 return;
2449 }
2450
2451 if (render_view_host() && render_view_host()->IsRenderViewLive())
[email protected]e7b8d832009-11-20 22:41:002452 hung_renderer_dialog::ShowForTabContents(this);
[email protected]420ae012009-04-24 05:16:322453}
2454
2455void TabContents::RendererResponsive(RenderViewHost* render_view_host) {
[email protected]e7b8d832009-11-20 22:41:002456 hung_renderer_dialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:322457}
2458
2459void TabContents::LoadStateChanged(const GURL& url,
[email protected]094e5b22009-09-25 04:23:562460 net::LoadState load_state,
2461 uint64 upload_position,
2462 uint64 upload_size) {
[email protected]420ae012009-04-24 05:16:322463 load_state_ = load_state;
[email protected]094e5b22009-09-25 04:23:562464 upload_position_ = upload_position;
2465 upload_size_ = upload_size;
[email protected]d686e812009-06-03 19:10:292466 std::wstring languages =
2467 profile()->GetPrefs()->GetString(prefs::kAcceptLanguages);
[email protected]d686e812009-06-03 19:10:292468 std::string host = url.host();
[email protected]ce85f602009-11-07 01:34:532469 load_state_host_ =
2470 net::IDNToUnicode(host.c_str(), host.size(), languages, NULL);
[email protected]420ae012009-04-24 05:16:322471 if (load_state_ == net::LOAD_STATE_READING_RESPONSE)
2472 SetNotWaitingForResponse();
2473 if (is_loading())
[email protected]c9cd2222009-05-06 05:16:502474 NotifyNavigationStateChanged(INVALIDATE_LOAD | INVALIDATE_TAB);
[email protected]420ae012009-04-24 05:16:322475}
2476
[email protected]1ee614862009-07-06 20:29:182477bool TabContents::IsExternalTabContainer() const {
2478 if (!delegate())
2479 return false;
2480
2481 return delegate()->IsExternalTabContainer();
2482}
2483
[email protected]1ee614862009-07-06 20:29:182484void TabContents::DidInsertCSS() {
2485 // This RVHDelegate function is used for extensions and not us.
[email protected]420ae012009-04-24 05:16:322486}
2487
[email protected]130efb02009-09-18 18:54:352488void TabContents::FocusedNodeChanged() {
2489 NotificationService::current()->Notify(
2490 NotificationType::FOCUS_CHANGED_IN_PAGE,
2491 Source<RenderViewHost>(render_view_host()),
2492 NotificationService::NoDetails());
2493}
2494
[email protected]420ae012009-04-24 05:16:322495void TabContents::FileSelected(const FilePath& path,
2496 int index, void* params) {
2497 render_view_host()->FileSelected(path);
2498}
2499
2500void TabContents::MultiFilesSelected(const std::vector<FilePath>& files,
2501 void* params) {
2502 render_view_host()->MultiFilesSelected(files);
2503}
2504
2505void TabContents::FileSelectionCanceled(void* params) {
2506 // If the user cancels choosing a file to upload we pass back an
2507 // empty vector.
2508 render_view_host()->MultiFilesSelected(std::vector<FilePath>());
2509}
2510
2511void TabContents::BeforeUnloadFiredFromRenderManager(
2512 bool proceed,
2513 bool* proceed_to_fire_unload) {
2514 if (delegate())
2515 delegate()->BeforeUnloadFired(this, proceed, proceed_to_fire_unload);
2516}
2517
2518void TabContents::UpdateRenderViewSizeForRenderManager() {
2519 // TODO(brettw) this is a hack. See TabContentsView::SizeContents.
2520 view_->SizeContents(view_->GetContainerSize());
2521}
2522
2523DOMUI* TabContents::CreateDOMUIForRenderManager(const GURL& url) {
[email protected]d82ed61e2009-06-16 02:46:222524 return DOMUIFactory::CreateDOMUIForURL(this, url);
[email protected]420ae012009-04-24 05:16:322525}
2526
2527NavigationEntry*
2528TabContents::GetLastCommittedNavigationEntryForRenderManager() {
2529 return controller_.GetLastCommittedEntry();
2530}
2531
2532bool TabContents::CreateRenderViewForRenderManager(
2533 RenderViewHost* render_view_host) {
[email protected]811bfe32009-07-01 08:46:252534 // If the pending navigation is to a DOMUI, tell the RenderView about any
2535 // bindings it will need enabled.
[email protected]420ae012009-04-24 05:16:322536 if (render_manager_.pending_dom_ui())
[email protected]811bfe32009-07-01 08:46:252537 render_view_host->AllowBindings(
2538 render_manager_.pending_dom_ui()->bindings());
[email protected]1adff062009-06-02 18:39:552539
[email protected]420ae012009-04-24 05:16:322540 RenderWidgetHostView* rwh_view = view_->CreateViewForWidget(render_view_host);
[email protected]1a98a932009-11-17 00:12:522541
2542 scoped_refptr<URLRequestContextGetter> request_context = request_context_;
2543 if (!request_context.get())
2544 request_context = profile()->GetRequestContext();
2545
2546 if (!render_view_host->CreateRenderView(request_context))
[email protected]420ae012009-04-24 05:16:322547 return false;
2548
2549 // Now that the RenderView has been created, we need to tell it its size.
2550 rwh_view->SetSize(view_->GetContainerSize());
2551
2552 UpdateMaxPageIDIfNecessary(render_view_host->site_instance(),
2553 render_view_host);
2554 return true;
2555}
2556
2557void TabContents::Observe(NotificationType type,
2558 const NotificationSource& source,
2559 const NotificationDetails& details) {
2560 switch (type.value) {
2561 case NotificationType::BOOKMARK_MODEL_LOADED:
2562 // BookmarkModel finished loading, fall through to update starred state.
2563 case NotificationType::URLS_STARRED: {
2564 // Somewhere, a URL has been starred.
2565 // Ignore notifications for profiles other than our current one.
2566 Profile* source_profile = Source<Profile>(source).ptr();
2567 if (!source_profile || !source_profile->IsSameProfile(profile()))
2568 return;
2569
2570 UpdateStarredStateForCurrentURL();
2571 break;
2572 }
2573 case NotificationType::PREF_CHANGED: {
2574 std::wstring* pref_name_in = Details<std::wstring>(details).ptr();
2575 DCHECK(Source<PrefService>(source).ptr() == profile()->GetPrefs());
2576 if (*pref_name_in == prefs::kAlternateErrorPagesEnabled) {
2577 UpdateAlternateErrorPageURL();
2578 } else if (*pref_name_in == prefs::kDefaultCharset ||
2579 StartsWithASCII(WideToUTF8(*pref_name_in), "webkit.webprefs.", true)
2580 ) {
2581 UpdateWebPreferences();
2582 } else {
2583 NOTREACHED() << "unexpected pref change notification" << *pref_name_in;
2584 }
2585 break;
2586 }
2587 case NotificationType::RENDER_WIDGET_HOST_DESTROYED:
2588 view_->RenderWidgetHostDestroyed(Source<RenderWidgetHost>(source).ptr());
2589 break;
2590
2591 case NotificationType::NAV_ENTRY_COMMITTED: {
2592 DCHECK(&controller_ == Source<NavigationController>(source).ptr());
2593
2594 NavigationController::LoadCommittedDetails& committed_details =
2595 *(Details<NavigationController::LoadCommittedDetails>(details).ptr());
2596 ExpireInfoBars(committed_details);
2597 break;
2598 }
2599
[email protected]d3ba77272009-09-03 00:06:092600#if defined(OS_LINUX)
2601 case NotificationType::BROWSER_THEME_CHANGED: {
[email protected]7ff431e2010-01-07 18:18:542602 renderer_preferences_util::UpdateFromSystemSettings(
2603 &renderer_preferences_, profile());
[email protected]d3ba77272009-09-03 00:06:092604 render_view_host()->SyncRendererPrefs();
2605 break;
2606 }
2607#endif
2608
[email protected]420ae012009-04-24 05:16:322609 default:
2610 NOTREACHED();
2611 }
2612}
[email protected]aacd7cc2009-09-02 14:51:102613
[email protected]beb440c2009-11-06 04:08:542614std::wstring TabContents::GetMessageBoxTitle(const GURL& frame_url,
2615 bool is_alert) {
2616 if (!frame_url.has_host())
2617 return l10n_util::GetString(
2618 is_alert ? IDS_JAVASCRIPT_ALERT_DEFAULT_TITLE
2619 : IDS_JAVASCRIPT_MESSAGEBOX_DEFAULT_TITLE);
2620
2621 // We really only want the scheme, hostname, and port.
2622 GURL::Replacements replacements;
2623 replacements.ClearUsername();
2624 replacements.ClearPassword();
2625 replacements.ClearPath();
2626 replacements.ClearQuery();
2627 replacements.ClearRef();
2628 GURL clean_url = frame_url.ReplaceComponents(replacements);
2629
2630 // TODO(brettw) it should be easier than this to do the correct language
2631 // handling without getting the accept language from the profile.
2632 std::wstring base_address = gfx::ElideUrl(clean_url, gfx::Font(), 0,
2633 profile()->GetPrefs()->GetString(prefs::kAcceptLanguages));
2634 // Force URL to have LTR directionality.
2635 if (l10n_util::GetTextDirection() == l10n_util::RIGHT_TO_LEFT)
2636 l10n_util::WrapStringWithLTRFormatting(&base_address);
2637
2638 return l10n_util::GetStringF(
2639 is_alert ? IDS_JAVASCRIPT_ALERT_TITLE : IDS_JAVASCRIPT_MESSAGEBOX_TITLE,
2640 base_address);
2641}
2642
2643gfx::NativeWindow TabContents::GetMessageBoxRootWindow() {
2644 return view_->GetTopLevelNativeWindow();
2645}
2646
2647void TabContents::OnMessageBoxClosed(IPC::Message* reply_msg,
2648 bool success,
2649 const std::wstring& prompt) {
2650 last_javascript_message_dismissal_ = base::TimeTicks::Now();
2651 if (is_showing_before_unload_dialog_ && !success) {
2652 // If a beforeunload dialog is canceled, we need to stop the throbber from
2653 // spinning, since we forced it to start spinning in Navigate.
2654 DidStopLoading();
2655
2656 tab_close_start_time_ = base::TimeTicks();
2657 }
2658 is_showing_before_unload_dialog_ = false;
2659 render_view_host()->JavaScriptMessageBoxClosed(reply_msg, success, prompt);
2660}
2661
2662void TabContents::SetSuppressMessageBoxes(bool suppress_message_boxes) {
2663 set_suppress_javascript_messages(suppress_message_boxes);
2664}
2665
[email protected]41fc0322009-09-04 22:23:402666void TabContents::set_encoding(const std::string& encoding) {
[email protected]aacd7cc2009-09-02 14:51:102667 encoding_ = CharacterEncoding::GetCanonicalEncodingNameByAliasName(encoding);
2668}