blob: 0251f0328076382d17e8cf5dd3ca7f55ba60e9cb [file] [log] [blame]
[email protected]ce5c4502009-05-06 16:46:111// Copyright (c) 2009 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]f3ec7742009-01-15 00:59:165#include "chrome/browser/tab_contents/tab_contents.h"
initial.commit09911bf2008-07-26 23:55:296
[email protected]beb440c2009-11-06 04:08:547#include "app/gfx/text_elider.h"
[email protected]a92b8642009-05-05 23:38:568#include "app/l10n_util.h"
[email protected]9929da92009-05-05 02:05:119#include "app/resource_bundle.h"
[email protected]420ae012009-04-24 05:16:3210#include "base/file_version_info.h"
11#include "base/process_util.h"
[email protected]96d185d2009-04-24 03:28:5412#include "base/string16.h"
[email protected]996fd702009-09-04 19:12:3713#include "base/string_util.h"
[email protected]96d185d2009-04-24 03:28:5414#include "base/time.h"
[email protected]21674df2009-10-28 19:51:2115#include "chrome/browser/autofill/autofill_manager.h"
[email protected]66ba4932009-06-04 19:22:1316#include "chrome/browser/blocked_popup_container.h"
[email protected]420ae012009-04-24 05:16:3217#include "chrome/browser/bookmarks/bookmark_model.h"
[email protected]96d185d2009-04-24 03:28:5418#include "chrome/browser/browser_process.h"
initial.commit09911bf2008-07-26 23:55:2919#include "chrome/browser/cert_store.h"
[email protected]aacd7cc2009-09-02 14:51:1020#include "chrome/browser/character_encoding.h"
[email protected]96d185d2009-04-24 03:28:5421#include "chrome/browser/debugger/devtools_manager.h"
[email protected]420ae012009-04-24 05:16:3222#include "chrome/browser/dom_operation_notification_details.h"
[email protected]96d185d2009-04-24 03:28:5423#include "chrome/browser/dom_ui/dom_ui.h"
24#include "chrome/browser/dom_ui/dom_ui_factory.h"
[email protected]bcef0dc02009-02-28 00:35:0225#include "chrome/browser/download/download_item_model.h"
[email protected]420ae012009-04-24 05:16:3226#include "chrome/browser/download/download_manager.h"
27#include "chrome/browser/download/download_request_manager.h"
[email protected]e7eaedde2009-09-25 20:09:4928#include "chrome/browser/external_protocol_handler.h"
[email protected]92c6f9b92009-10-24 04:35:0829#include "chrome/browser/extensions/extensions_service.h"
[email protected]0189bc722009-08-28 21:56:4830#include "chrome/browser/favicon_service.h"
[email protected]19289d02009-10-21 21:47:0031#include "chrome/browser/form_field_history_manager.h"
[email protected]420ae012009-04-24 05:16:3232#include "chrome/browser/google_util.h"
[email protected]8897c382009-05-06 17:53:2633#include "chrome/browser/hung_renderer_dialog.h"
[email protected]420ae012009-04-24 05:16:3234#include "chrome/browser/jsmessage_box_handler.h"
35#include "chrome/browser/load_from_memory_cache_details.h"
36#include "chrome/browser/load_notification_details.h"
[email protected]84abba62009-10-07 17:01:4437#include "chrome/browser/metrics/metric_event_duration_details.h"
[email protected]ea0e80892009-08-22 00:36:0538#include "chrome/browser/modal_html_dialog_delegate.h"
[email protected]c8865482009-07-23 20:40:1039#include "chrome/browser/omnibox_search_hint.h"
[email protected]fdd61c62009-04-22 19:22:5740#include "chrome/browser/password_manager/password_manager.h"
41#include "chrome/browser/plugin_installer.h"
[email protected]ce560f82009-06-03 09:39:4442#include "chrome/browser/profile.h"
[email protected]1db6ff152009-10-12 15:32:0743#include "chrome/browser/renderer_host/render_process_host.h"
[email protected]96d185d2009-04-24 03:28:5444#include "chrome/browser/renderer_host/render_widget_host_view.h"
[email protected]fd694982009-07-03 17:32:3745#include "chrome/browser/renderer_host/resource_request_details.h"
[email protected]1db6ff152009-10-12 15:32:0746#include "chrome/browser/renderer_host/site_instance.h"
[email protected]96d185d2009-04-24 03:28:5447#include "chrome/browser/renderer_host/web_cache_manager.h"
[email protected]bd1ad682009-05-15 22:19:1748#include "chrome/browser/tab_contents/infobar_delegate.h"
[email protected]7e383692009-06-12 19:14:5449#include "chrome/browser/tab_contents/interstitial_page.h"
[email protected]f3ec7742009-01-15 00:59:1650#include "chrome/browser/tab_contents/navigation_entry.h"
[email protected]de34a962009-06-26 23:22:4351#include "chrome/browser/tab_contents/provisional_load_details.h"
[email protected]f3ec7742009-01-15 00:59:1652#include "chrome/browser/tab_contents/tab_contents_delegate.h"
[email protected]fdd61c62009-04-22 19:22:5753#include "chrome/browser/tab_contents/tab_contents_view.h"
[email protected]58dca552009-06-17 00:35:0254#include "chrome/browser/tab_contents/thumbnail_generator.h"
[email protected]449478302009-06-09 20:04:2855#include "chrome/browser/thumbnail_store.h"
[email protected]420ae012009-04-24 05:16:3256#include "chrome/browser/search_engines/template_url_fetcher.h"
57#include "chrome/browser/search_engines/template_url_model.h"
[email protected]35f7d212009-04-29 21:19:2758#include "chrome/common/chrome_switches.h"
[email protected]5d246db22009-10-27 06:17:5759#include "chrome/common/extensions/extension_action.h"
[email protected]bfd04a62009-02-01 18:16:5660#include "chrome/common/notification_service.h"
initial.commit09911bf2008-07-26 23:55:2961#include "chrome/common/pref_names.h"
[email protected]1eb89e82008-08-15 12:27:0362#include "chrome/common/pref_service.h"
[email protected]420ae012009-04-24 05:16:3263#include "chrome/common/render_messages.h"
[email protected]1ee614862009-07-06 20:29:1864#include "chrome/common/renderer_preferences.h"
[email protected]96d185d2009-04-24 03:28:5465#include "chrome/common/url_constants.h"
[email protected]8897c382009-05-06 17:53:2666#include "grit/generated_resources.h"
[email protected]420ae012009-04-24 05:16:3267#include "grit/locale_settings.h"
68#include "net/base/mime_util.h"
69#include "net/base/net_errors.h"
[email protected]d686e812009-06-03 19:10:2970#include "net/base/net_util.h"
[email protected]420ae012009-04-24 05:16:3271#include "net/base/registry_controlled_domain.h"
[email protected]1ee614862009-07-06 20:29:1872#include "webkit/glue/password_form.h"
73#include "webkit/glue/webpreferences.h"
[email protected]3e45ba92009-02-20 21:09:0074
[email protected]304f8792009-08-19 21:28:0975#if defined(OS_CHROMEOS)
76// For GdkScreen
77#include <gdk/gdk.h>
[email protected]0ba5c5e2009-08-20 16:47:1378#endif // defined(OS_CHROMEOS)
79
[email protected]420ae012009-04-24 05:16:3280// Cross-Site Navigations
81//
82// If a TabContents is told to navigate to a different web site (as determined
83// by SiteInstance), it will replace its current RenderViewHost with a new
84// RenderViewHost dedicated to the new SiteInstance. This works as follows:
85//
86// - Navigate determines whether the destination is cross-site, and if so,
87// it creates a pending_render_view_host_ and moves into the PENDING
88// RendererState.
89// - The pending RVH is "suspended," so that no navigation messages are sent to
90// its renderer until the onbeforeunload JavaScript handler has a chance to
91// run in the current RVH.
92// - The pending RVH tells CrossSiteRequestManager (a thread-safe singleton)
93// that it has a pending cross-site request. ResourceDispatcherHost will
94// check for this when the response arrives.
95// - The current RVH runs its onbeforeunload handler. If it returns false, we
96// cancel all the pending logic and go back to NORMAL. Otherwise we allow
97// the pending RVH to send the navigation request to its renderer.
98// - ResourceDispatcherHost receives a ResourceRequest on the IO thread. It
99// checks CrossSiteRequestManager to see that the RVH responsible has a
100// pending cross-site request, and then installs a CrossSiteEventHandler.
101// - When RDH receives a response, the BufferedEventHandler determines whether
102// it is a download. If so, it sends a message to the new renderer causing
103// it to cancel the request, and the download proceeds in the download
104// thread. For now, we stay in a PENDING state (with a pending RVH) until
[email protected]57c6a652009-05-04 07:58:34105// the next DidNavigate event for this TabContents. This isn't ideal, but it
[email protected]420ae012009-04-24 05:16:32106// doesn't affect any functionality.
107// - After RDH receives a response and determines that it is safe and not a
108// download, it pauses the response to first run the old page's onunload
109// handler. It does this by asynchronously calling the OnCrossSiteResponse
[email protected]57c6a652009-05-04 07:58:34110// method of TabContents on the UI thread, which sends a ClosePage message
[email protected]420ae012009-04-24 05:16:32111// to the current RVH.
112// - Once the onunload handler is finished, a ClosePage_ACK message is sent to
113// the ResourceDispatcherHost, who unpauses the response. Data is then sent
114// to the pending RVH.
115// - The pending renderer sends a FrameNavigate message that invokes the
116// DidNavigate method. This replaces the current RVH with the
117// pending RVH and goes back to the NORMAL RendererState.
118
119namespace {
120
121// Amount of time we wait between when a key event is received and the renderer
122// is queried for its state and pushed to the NavigationEntry.
123const int kQueryStateDelay = 5000;
124
125const int kSyncWaitDelay = 40;
126
[email protected]304f8792009-08-19 21:28:09127#if defined(OS_CHROMEOS)
128// If a popup window is bigger than this fraction of the screen on chrome os,
129// turn it into a tab
130const float kMaxWidthFactor = 0.5;
131const float kMaxHeightFactor = 0.6;
132#endif
133
[email protected]420ae012009-04-24 05:16:32134// If another javascript message box is displayed within
135// kJavascriptMessageExpectedDelay of a previous javascript message box being
136// dismissed, display an option to suppress future message boxes from this
137// contents.
138const int kJavascriptMessageExpectedDelay = 1000;
139
[email protected]420ae012009-04-24 05:16:32140// The list of prefs we want to observe.
141const wchar_t* kPrefsToObserve[] = {
142 prefs::kAlternateErrorPagesEnabled,
143 prefs::kWebKitJavaEnabled,
144 prefs::kWebKitJavascriptEnabled,
145 prefs::kWebKitLoadsImagesAutomatically,
146 prefs::kWebKitPluginsEnabled,
147 prefs::kWebKitUsesUniversalDetector,
148 prefs::kWebKitSerifFontFamily,
149 prefs::kWebKitSansSerifFontFamily,
150 prefs::kWebKitFixedFontFamily,
151 prefs::kWebKitDefaultFontSize,
152 prefs::kWebKitDefaultFixedFontSize,
153 prefs::kDefaultCharset
154 // kWebKitStandardFontIsSerif needs to be added
155 // if we let users pick which font to use, serif or sans-serif when
156 // no font is specified or a CSS generic family (serif or sans-serif)
157 // is not specified.
158};
159
160const int kPrefsToObserveLength = arraysize(kPrefsToObserve);
161
[email protected]420ae012009-04-24 05:16:32162// Returns true if the entry's transition type is FORM_SUBMIT.
163bool IsFormSubmit(const NavigationEntry* entry) {
164 return (PageTransition::StripQualifier(entry->transition_type()) ==
165 PageTransition::FORM_SUBMIT);
166}
167
168#if defined(OS_WIN)
169
170BOOL CALLBACK InvalidateWindow(HWND hwnd, LPARAM lparam) {
171 // Note: erase is required to properly paint some widgets borders. This can
172 // be seen with textfields.
173 InvalidateRect(hwnd, NULL, TRUE);
174 return TRUE;
175}
176#endif
177
[email protected]5e369672009-11-03 23:48:30178ViewMsg_Navigate_Params::NavigationType GetNavigationType(
179 Profile* profile, const NavigationEntry& entry, bool reload) {
180 if (reload)
181 return ViewMsg_Navigate_Params::RELOAD;
182
183 if (entry.restore_type() == NavigationEntry::RESTORE_LAST_SESSION &&
184 profile->DidLastSessionExitCleanly())
185 return ViewMsg_Navigate_Params::RESTORE;
186
187 return ViewMsg_Navigate_Params::NORMAL;
188}
189
190void MakeNavigateParams(Profile* profile, const NavigationEntry& entry,
191 bool reload, ViewMsg_Navigate_Params* params) {
[email protected]056de2d2009-06-26 16:41:34192 params->page_id = entry.page_id();
193 params->url = entry.url();
194 params->referrer = entry.referrer();
195 params->transition = entry.transition_type();
196 params->state = entry.content_state();
[email protected]5e369672009-11-03 23:48:30197 params->navigation_type = GetNavigationType(profile, entry, reload);
[email protected]056de2d2009-06-26 16:41:34198 params->request_time = base::Time::Now();
199}
200
[email protected]420ae012009-04-24 05:16:32201} // namespace
202
203// -----------------------------------------------------------------------------
204
[email protected]fdd61c62009-04-22 19:22:57205// static
206int TabContents::find_request_id_counter_ = -1;
207
[email protected]420ae012009-04-24 05:16:32208
[email protected]420ae012009-04-24 05:16:32209TabContents::TabContents(Profile* profile,
210 SiteInstance* site_instance,
211 int routing_id,
[email protected]34ac70502009-09-25 17:07:23212 const TabContents* base_tab_contents)
[email protected]b680ad22009-04-15 23:19:42213 : delegate_(NULL),
[email protected]66ba4932009-06-04 19:22:13214 ALLOW_THIS_IN_INITIALIZER_LIST(controller_(this, profile)),
215 ALLOW_THIS_IN_INITIALIZER_LIST(view_(
[email protected]d82ed61e2009-06-16 02:46:22216 TabContentsView::Create(this))),
217 ALLOW_THIS_IN_INITIALIZER_LIST(render_manager_(this, this)),
[email protected]fdd61c62009-04-22 19:22:57218 property_bag_(),
219 registrar_(),
[email protected]d82ed61e2009-06-16 02:46:22220 ALLOW_THIS_IN_INITIALIZER_LIST(printing_(*this)),
[email protected]fdd61c62009-04-22 19:22:57221 save_package_(),
222 cancelable_consumer_(),
[email protected]19289d02009-10-21 21:47:00223 form_field_history_manager_(),
[email protected]21674df2009-10-28 19:51:21224 autofill_manager_(),
[email protected]fdd61c62009-04-22 19:22:57225 password_manager_(),
226 plugin_installer_(),
[email protected]d82ed61e2009-06-16 02:46:22227 ALLOW_THIS_IN_INITIALIZER_LIST(fav_icon_helper_(this)),
[email protected]fdd61c62009-04-22 19:22:57228 select_file_dialog_(),
[email protected]d5f942ba2008-09-26 19:30:34229 is_loading_(false),
initial.commit09911bf2008-07-26 23:55:29230 is_crashed_(false),
[email protected]d5f942ba2008-09-26 19:30:34231 waiting_for_response_(false),
[email protected]d5f942ba2008-09-26 19:30:34232 max_page_id_(-1),
[email protected]fdd61c62009-04-22 19:22:57233 current_load_start_(),
234 load_state_(net::LOAD_STATE_IDLE),
235 load_state_host_(),
[email protected]094e5b22009-09-25 04:23:56236 upload_size_(0),
237 upload_position_(0),
[email protected]fdd61c62009-04-22 19:22:57238 received_page_title_(false),
239 is_starred_(false),
240 contents_mime_type_(),
241 encoding_(),
[email protected]332af7732009-03-11 13:21:35242 blocked_popups_(NULL),
[email protected]fdd61c62009-04-22 19:22:57243 infobar_delegates_(),
[email protected]fdd61c62009-04-22 19:22:57244 find_ui_active_(false),
245 find_op_aborted_(false),
246 current_find_request_id_(find_request_id_counter_++),
247 find_text_(),
[email protected]e491f1c2009-05-22 20:28:12248 last_search_case_sensitive_(false),
249 last_search_prepopulate_text_(NULL),
250 last_search_result_(),
[email protected]fdd61c62009-04-22 19:22:57251 capturing_contents_(false),
252 is_being_destroyed_(false),
253 notify_disconnection_(false),
254 history_requests_(),
255#if defined(OS_WIN)
256 message_box_active_(CreateEvent(NULL, TRUE, FALSE, NULL)),
257#endif
258 last_javascript_message_dismissal_(),
[email protected]c2e74fe82009-09-03 17:57:44259 suppress_javascript_messages_(false),
[email protected]7ab1e7d62009-10-14 23:32:01260 is_showing_before_unload_dialog_(false),
[email protected]c2e74fe82009-09-03 17:57:44261 opener_dom_ui_type_(DOMUIFactory::kNoDOMUI) {
[email protected]16779842009-07-08 23:45:29262#if defined(OS_CHROMEOS)
[email protected]58dca552009-06-17 00:35:02263 // Make sure the thumbnailer is started before starting the render manager.
264 // The thumbnailer will want to listen for RVH creations, one of which will
265 // happen in RVHManager::Init.
266 ThumbnailGenerator* generator = g_browser_process->GetThumbnailGenerator();
267 if (generator)
268 generator->StartThumbnailing();
269#endif
270
[email protected]12636df2009-09-28 22:32:21271 render_manager_.Init(profile, site_instance, routing_id);
[email protected]420ae012009-04-24 05:16:32272
[email protected]34ac70502009-09-25 17:07:23273 // We have the initial size of the view be based on the size of the passed in
274 // tab contents (normally a tab from the same window).
275 view_->CreateView(base_tab_contents ?
276 base_tab_contents->view()->GetContainerSize() : gfx::Size());
[email protected]420ae012009-04-24 05:16:32277
278 // Register for notifications about all interested prefs change.
279 PrefService* prefs = profile->GetPrefs();
280 if (prefs) {
281 for (int i = 0; i < kPrefsToObserveLength; ++i)
282 prefs->AddPrefObserver(kPrefsToObserve[i], this);
283 }
284
285 // Register for notifications about URL starredness changing on any profile.
286 registrar_.Add(this, NotificationType::URLS_STARRED,
287 NotificationService::AllSources());
288 registrar_.Add(this, NotificationType::BOOKMARK_MODEL_LOADED,
289 NotificationService::AllSources());
290 registrar_.Add(this, NotificationType::RENDER_WIDGET_HOST_DESTROYED,
291 NotificationService::AllSources());
[email protected]d3ba77272009-09-03 00:06:09292#if defined(OS_LINUX)
293 registrar_.Add(this, NotificationType::BROWSER_THEME_CHANGED,
294 NotificationService::AllSources());
295#endif
[email protected]420ae012009-04-24 05:16:32296
297 // Keep a global copy of the previous search string (if any).
298 static string16 global_last_search = string16();
[email protected]e491f1c2009-05-22 20:28:12299 last_search_prepopulate_text_ = &global_last_search;
[email protected]840b1512009-07-21 16:53:46300
[email protected]c8865482009-07-23 20:40:10301 // Set-up the showing of the omnibox search infobar if applicable.
302 if (OmniboxSearchHint::IsEnabled(profile))
303 omnibox_search_hint_.reset(new OmniboxSearchHint(this));
304
[email protected]840b1512009-07-21 16:53:46305 view_->InitRendererPrefs(&renderer_preferences_);
[email protected]332af7732009-03-11 13:21:35306}
initial.commit09911bf2008-07-26 23:55:29307
308TabContents::~TabContents() {
[email protected]420ae012009-04-24 05:16:32309 is_being_destroyed_ = true;
310
[email protected]bfe4c1582009-10-02 18:11:09311 // We don't want any notifications while we're running our destructor.
[email protected]420ae012009-04-24 05:16:32312 registrar_.RemoveAll();
313
314 // Unregister the notifications of all observed prefs change.
315 PrefService* prefs = profile()->GetPrefs();
316 if (prefs) {
317 for (int i = 0; i < kPrefsToObserveLength; ++i)
318 prefs->RemovePrefObserver(kPrefsToObserve[i], this);
319 }
320
[email protected]420ae012009-04-24 05:16:32321 NotifyDisconnected();
[email protected]d82ed61e2009-06-16 02:46:22322 HungRendererDialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:32323
[email protected]420ae012009-04-24 05:16:32324 // First cleanly close all child windows.
325 // TODO(mpcomplete): handle case if MaybeCloseChildWindows() already asked
326 // some of these to close. CloseWindows is async, so it might get called
327 // twice before it runs.
328 int size = static_cast<int>(child_windows_.size());
329 for (int i = size - 1; i >= 0; --i) {
330 ConstrainedWindow* window = child_windows_[i];
331 if (window)
332 window->CloseConstrainedWindow();
333 }
334
[email protected]66ba4932009-06-04 19:22:13335 if (blocked_popups_)
336 blocked_popups_->Destroy();
337
[email protected]420ae012009-04-24 05:16:32338 // Notify any observer that have a reference on this tab contents.
339 NotificationService::current()->Notify(
340 NotificationType::TAB_CONTENTS_DESTROYED,
341 Source<TabContents>(this),
342 NotificationService::NoDetails());
343
[email protected]1e5e74f2009-05-27 20:55:12344 // Notify any lasting InfobarDelegates that have not yet been removed that
345 // whatever infobar they were handling in this TabContents has closed,
346 // because the TabContents is going away entirely.
347 // This must happen after the TAB_CONTENTS_DESTROYED notification as the
348 // notification may trigger infobars calls that access their delegate. (and
349 // some implementations of InfoBarDelegate do delete themselves on
350 // InfoBarClosed()).
351 for (int i = 0; i < infobar_delegate_count(); ++i) {
352 InfoBarDelegate* delegate = GetInfoBarDelegateAt(i);
353 delegate->InfoBarClosed();
354 }
355 infobar_delegates_.clear();
356
[email protected]420ae012009-04-24 05:16:32357 // TODO(brettw) this should be moved to the view.
358#if defined(OS_WIN)
359 // If we still have a window handle, destroy it. GetNativeView can return
360 // NULL if this contents was part of a window that closed.
361 if (GetNativeView())
362 ::DestroyWindow(GetNativeView());
363#endif
[email protected]7ab1e7d62009-10-14 23:32:01364
365 // OnCloseStarted isn't called in unit tests.
366 if (!tab_close_start_time_.is_null()) {
367 UMA_HISTOGRAM_TIMES("Tab.Close",
368 base::TimeTicks::Now() - tab_close_start_time_);
369 }
initial.commit09911bf2008-07-26 23:55:29370}
371
[email protected]d5f942ba2008-09-26 19:30:34372// static
373void TabContents::RegisterUserPrefs(PrefService* prefs) {
[email protected]420ae012009-04-24 05:16:32374 prefs->RegisterBooleanPref(prefs::kAlternateErrorPagesEnabled, true);
375
376 WebPreferences pref_defaults;
377 prefs->RegisterBooleanPref(prefs::kWebKitJavascriptEnabled,
378 pref_defaults.javascript_enabled);
379 prefs->RegisterBooleanPref(prefs::kWebKitWebSecurityEnabled,
380 pref_defaults.web_security_enabled);
381 prefs->RegisterBooleanPref(
382 prefs::kWebKitJavascriptCanOpenWindowsAutomatically, true);
383 prefs->RegisterBooleanPref(prefs::kWebKitLoadsImagesAutomatically,
384 pref_defaults.loads_images_automatically);
385 prefs->RegisterBooleanPref(prefs::kWebKitPluginsEnabled,
386 pref_defaults.plugins_enabled);
387 prefs->RegisterBooleanPref(prefs::kWebKitDomPasteEnabled,
388 pref_defaults.dom_paste_enabled);
389 prefs->RegisterBooleanPref(prefs::kWebKitShrinksStandaloneImagesToFit,
390 pref_defaults.shrinks_standalone_images_to_fit);
[email protected]0df30122009-06-03 12:13:08391 prefs->RegisterStringPref(prefs::kWebKitInspectorSettings,
[email protected]8922e1f2009-10-03 05:01:26392 UTF8ToWide(pref_defaults.inspector_settings));
[email protected]420ae012009-04-24 05:16:32393 prefs->RegisterBooleanPref(prefs::kWebKitTextAreasAreResizable,
394 pref_defaults.text_areas_are_resizable);
395 prefs->RegisterBooleanPref(prefs::kWebKitJavaEnabled,
396 pref_defaults.java_enabled);
397
398 prefs->RegisterLocalizedStringPref(prefs::kAcceptLanguages,
399 IDS_ACCEPT_LANGUAGES);
400 prefs->RegisterLocalizedStringPref(prefs::kDefaultCharset,
401 IDS_DEFAULT_ENCODING);
402 prefs->RegisterLocalizedBooleanPref(prefs::kWebKitStandardFontIsSerif,
403 IDS_STANDARD_FONT_IS_SERIF);
404 prefs->RegisterLocalizedStringPref(prefs::kWebKitFixedFontFamily,
405 IDS_FIXED_FONT_FAMILY);
406 prefs->RegisterLocalizedStringPref(prefs::kWebKitSerifFontFamily,
407 IDS_SERIF_FONT_FAMILY);
408 prefs->RegisterLocalizedStringPref(prefs::kWebKitSansSerifFontFamily,
409 IDS_SANS_SERIF_FONT_FAMILY);
410 prefs->RegisterLocalizedStringPref(prefs::kWebKitCursiveFontFamily,
411 IDS_CURSIVE_FONT_FAMILY);
412 prefs->RegisterLocalizedStringPref(prefs::kWebKitFantasyFontFamily,
413 IDS_FANTASY_FONT_FAMILY);
414 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitDefaultFontSize,
415 IDS_DEFAULT_FONT_SIZE);
416 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitDefaultFixedFontSize,
417 IDS_DEFAULT_FIXED_FONT_SIZE);
418 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitMinimumFontSize,
419 IDS_MINIMUM_FONT_SIZE);
420 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitMinimumLogicalFontSize,
421 IDS_MINIMUM_LOGICAL_FONT_SIZE);
422 prefs->RegisterLocalizedBooleanPref(prefs::kWebKitUsesUniversalDetector,
423 IDS_USES_UNIVERSAL_DETECTOR);
424 prefs->RegisterLocalizedStringPref(prefs::kStaticEncodings,
425 IDS_STATIC_ENCODING_LIST);
initial.commit09911bf2008-07-26 23:55:29426}
427
[email protected]13367f72009-07-08 16:39:02428// Returns true if contains content rendered by an extension.
429bool TabContents::HostsExtension() const {
430 return GetURL().SchemeIs(chrome::kExtensionScheme);
[email protected]d5f942ba2008-09-26 19:30:34431}
432
[email protected]6e95706d2009-04-23 22:55:01433PasswordManager* TabContents::GetPasswordManager() {
434 if (password_manager_.get() == NULL)
435 password_manager_.reset(new PasswordManager(this));
436 return password_manager_.get();
437}
438
439PluginInstaller* TabContents::GetPluginInstaller() {
440 if (plugin_installer_.get() == NULL)
441 plugin_installer_.reset(new PluginInstaller(this));
442 return plugin_installer_.get();
443}
444
[email protected]d5f942ba2008-09-26 19:30:34445const GURL& TabContents::GetURL() const {
446 // We may not have a navigation entry yet
[email protected]ce3fa3c2009-04-20 19:55:57447 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]ebe89e062009-08-13 23:16:54448 return entry ? entry->virtual_url() : GURL::EmptyGURL();
[email protected]d5f942ba2008-09-26 19:30:34449}
450
[email protected]96d185d2009-04-24 03:28:54451const string16& TabContents::GetTitle() const {
[email protected]4c6092c5b2009-06-06 00:23:55452 // Transient entries take precedence. They are used for interstitial pages
453 // that are shown on top of existing pages.
454 NavigationEntry* entry = controller_.GetTransientEntry();
455 if (entry)
456 return entry->GetTitleForDisplay(&controller_);
457
[email protected]96d185d2009-04-24 03:28:54458 DOMUI* our_dom_ui = render_manager_.pending_dom_ui() ?
459 render_manager_.pending_dom_ui() : render_manager_.dom_ui();
460 if (our_dom_ui) {
461 // Don't override the title in view source mode.
[email protected]4c6092c5b2009-06-06 00:23:55462 entry = controller_.GetActiveEntry();
[email protected]96d185d2009-04-24 03:28:54463 if (!(entry && entry->IsViewSourceMode())) {
464 // Give the DOM UI the chance to override our title.
465 const string16& title = our_dom_ui->overridden_title();
466 if (!title.empty())
467 return title;
468 }
469 }
470
471 // We use the title for the last committed entry rather than a pending
472 // navigation entry. For example, when the user types in a URL, we want to
473 // keep the old page's title until the new load has committed and we get a new
474 // title.
[email protected]96d185d2009-04-24 03:28:54475 entry = controller_.GetLastCommittedEntry();
476 if (entry)
477 return entry->GetTitleForDisplay(&controller_);
[email protected]96d185d2009-04-24 03:28:54478 return EmptyString16();
479}
480
[email protected]d5f942ba2008-09-26 19:30:34481int32 TabContents::GetMaxPageID() {
482 if (GetSiteInstance())
483 return GetSiteInstance()->max_page_id();
484 else
485 return max_page_id_;
486}
487
488void TabContents::UpdateMaxPageID(int32 page_id) {
489 // Ensure both the SiteInstance and RenderProcessHost update their max page
[email protected]57c6a652009-05-04 07:58:34490 // IDs in sync. Only TabContents will also have site instances, except during
[email protected]d5f942ba2008-09-26 19:30:34491 // testing.
492 if (GetSiteInstance())
493 GetSiteInstance()->UpdateMaxPageID(page_id);
[email protected]57c6a652009-05-04 07:58:34494 process()->UpdateMaxPageID(page_id);
[email protected]d5f942ba2008-09-26 19:30:34495}
496
[email protected]96d185d2009-04-24 03:28:54497SiteInstance* TabContents::GetSiteInstance() const {
498 return render_manager_.current_host()->site_instance();
499}
500
[email protected]d5f942ba2008-09-26 19:30:34501const std::wstring TabContents::GetDefaultTitle() const {
502 return l10n_util::GetString(IDS_DEFAULT_TAB_TITLE);
503}
504
[email protected]96d185d2009-04-24 03:28:54505bool TabContents::ShouldDisplayURL() {
[email protected]8ab24cc2009-11-13 20:56:15506 // Don't hide the url in view source mode and with interstitials.
[email protected]96d185d2009-04-24 03:28:54507 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]8ab24cc2009-11-13 20:56:15508 if (entry && (entry->IsViewSourceMode() ||
509 entry->page_type() == NavigationEntry::INTERSTITIAL_PAGE)) {
[email protected]96d185d2009-04-24 03:28:54510 return true;
[email protected]8ab24cc2009-11-13 20:56:15511 }
[email protected]83e3895b2009-06-11 00:07:16512
[email protected]96d185d2009-04-24 03:28:54513 DOMUI* dom_ui = GetDOMUIForCurrentState();
514 if (dom_ui)
515 return !dom_ui->should_hide_url();
516 return true;
517}
518
[email protected]d5f942ba2008-09-26 19:30:34519SkBitmap TabContents::GetFavIcon() const {
520 // Like GetTitle(), we also want to use the favicon for the last committed
521 // entry rather than a pending navigation entry.
[email protected]ce3fa3c2009-04-20 19:55:57522 NavigationEntry* entry = controller_.GetTransientEntry();
[email protected]cbab76d2008-10-13 22:42:47523 if (entry)
524 return entry->favicon().bitmap();
525
[email protected]ce3fa3c2009-04-20 19:55:57526 entry = controller_.GetLastCommittedEntry();
[email protected]d5f942ba2008-09-26 19:30:34527 if (entry)
528 return entry->favicon().bitmap();
[email protected]d5f942ba2008-09-26 19:30:34529 return SkBitmap();
530}
531
[email protected]0b303cc2009-09-28 22:35:15532bool TabContents::FavIconIsValid() const {
533 NavigationEntry* entry = controller_.GetTransientEntry();
534 if (entry)
535 return entry->favicon().is_valid();
536
537 entry = controller_.GetLastCommittedEntry();
538 if (entry)
539 return entry->favicon().is_valid();
540
541 return false;
542}
543
[email protected]96d185d2009-04-24 03:28:54544bool TabContents::ShouldDisplayFavIcon() {
545 // Always display a throbber during pending loads.
546 if (controller_.GetLastCommittedEntry() && controller_.pending_entry())
547 return true;
548
549 DOMUI* dom_ui = GetDOMUIForCurrentState();
550 if (dom_ui)
551 return !dom_ui->hide_favicon();
552 return true;
553}
554
[email protected]96d185d2009-04-24 03:28:54555std::wstring TabContents::GetStatusText() const {
556 if (!is_loading() || load_state_ == net::LOAD_STATE_IDLE)
557 return std::wstring();
558
559 switch (load_state_) {
560 case net::LOAD_STATE_WAITING_FOR_CACHE:
561 return l10n_util::GetString(IDS_LOAD_STATE_WAITING_FOR_CACHE);
562 case net::LOAD_STATE_RESOLVING_PROXY_FOR_URL:
563 return l10n_util::GetString(IDS_LOAD_STATE_RESOLVING_PROXY_FOR_URL);
564 case net::LOAD_STATE_RESOLVING_HOST:
565 return l10n_util::GetString(IDS_LOAD_STATE_RESOLVING_HOST);
566 case net::LOAD_STATE_CONNECTING:
567 return l10n_util::GetString(IDS_LOAD_STATE_CONNECTING);
568 case net::LOAD_STATE_SENDING_REQUEST:
[email protected]094e5b22009-09-25 04:23:56569 if (upload_size_)
570 return l10n_util::GetStringF(
571 IDS_LOAD_STATE_SENDING_REQUEST_WITH_PROGRESS,
572 static_cast<int>((100 * upload_position_) / upload_size_));
573 else
574 return l10n_util::GetString(IDS_LOAD_STATE_SENDING_REQUEST);
[email protected]96d185d2009-04-24 03:28:54575 case net::LOAD_STATE_WAITING_FOR_RESPONSE:
576 return l10n_util::GetStringF(IDS_LOAD_STATE_WAITING_FOR_RESPONSE,
577 load_state_host_);
578 // Ignore net::LOAD_STATE_READING_RESPONSE and net::LOAD_STATE_IDLE
579 case net::LOAD_STATE_IDLE:
580 case net::LOAD_STATE_READING_RESPONSE:
581 break;
582 }
583
584 return std::wstring();
585}
586
[email protected]d5f942ba2008-09-26 19:30:34587void TabContents::SetIsCrashed(bool state) {
588 if (state == is_crashed_)
589 return;
590
591 is_crashed_ = state;
[email protected]c9cd2222009-05-06 05:16:50592 NotifyNavigationStateChanged(INVALIDATE_TAB);
[email protected]d5f942ba2008-09-26 19:30:34593}
594
[email protected]744ef172009-10-16 21:53:46595void TabContents::PageActionStateChanged() {
596 NotifyNavigationStateChanged(TabContents::INVALIDATE_PAGE_ACTIONS);
[email protected]d7eaf572009-07-01 21:57:00597}
[email protected]ce5c4502009-05-06 16:46:11598
[email protected]d5f942ba2008-09-26 19:30:34599void TabContents::NotifyNavigationStateChanged(unsigned changed_flags) {
600 if (delegate_)
601 delegate_->NavigationStateChanged(this, changed_flags);
602}
603
[email protected]96d185d2009-04-24 03:28:54604void TabContents::DidBecomeSelected() {
605 controller_.SetActive(true);
[email protected]789e9152009-08-04 21:59:43606 if (render_widget_host_view()) {
[email protected]96d185d2009-04-24 03:28:54607 render_widget_host_view()->DidBecomeSelected();
[email protected]789e9152009-08-04 21:59:43608#if defined(OS_MACOSX)
609 render_widget_host_view()->SetActive(true);
610#endif
611 }
[email protected]96d185d2009-04-24 03:28:54612
[email protected]76543b92009-08-31 17:27:45613 WebCacheManager::GetInstance()->ObserveActivity(process()->id());
[email protected]96d185d2009-04-24 03:28:54614}
615
616void TabContents::WasHidden() {
617 if (!capturing_contents()) {
618 // |render_view_host()| can be NULL if the user middle clicks a link to open
619 // a tab in then background, then closes the tab before selecting it. This
620 // is because closing the tab calls TabContents::Destroy(), which removes
621 // the |render_view_host()|; then when we actually destroy the window,
622 // OnWindowPosChanged() notices and calls HideContents() (which calls us).
623 if (render_widget_host_view())
624 render_widget_host_view()->WasHidden();
[email protected]96d185d2009-04-24 03:28:54625 }
626
627 NotificationService::current()->Notify(
628 NotificationType::TAB_CONTENTS_HIDDEN,
629 Source<TabContents>(this),
630 NotificationService::NoDetails());
631}
632
[email protected]d5f942ba2008-09-26 19:30:34633void TabContents::Activate() {
634 if (delegate_)
635 delegate_->ActivateContents(this);
636}
637
[email protected]96d185d2009-04-24 03:28:54638void TabContents::ShowContents() {
639 if (render_widget_host_view())
640 render_widget_host_view()->DidBecomeSelected();
[email protected]96d185d2009-04-24 03:28:54641}
642
643void TabContents::HideContents() {
644 // TODO(pkasting): http://b/1239839 Right now we purposefully don't call
645 // our superclass HideContents(), because some callers want to be very picky
646 // about the order in which these get called. In addition to making the code
647 // here practically impossible to understand, this also means we end up
648 // calling TabContents::WasHidden() twice if callers call both versions of
[email protected]57c6a652009-05-04 07:58:34649 // HideContents() on a TabContents.
[email protected]96d185d2009-04-24 03:28:54650 WasHidden();
651}
652
[email protected]c0588052008-10-27 23:01:50653void TabContents::OpenURL(const GURL& url, const GURL& referrer,
[email protected]d5f942ba2008-09-26 19:30:34654 WindowOpenDisposition disposition,
655 PageTransition::Type transition) {
656 if (delegate_)
[email protected]c0588052008-10-27 23:01:50657 delegate_->OpenURLFromTab(this, url, referrer, disposition, transition);
[email protected]d5f942ba2008-09-26 19:30:34658}
659
660bool TabContents::NavigateToPendingEntry(bool reload) {
[email protected]96d185d2009-04-24 03:28:54661 const NavigationEntry& entry = *controller_.pending_entry();
662
663 RenderViewHost* dest_render_view_host = render_manager_.Navigate(entry);
664 if (!dest_render_view_host)
665 return false; // Unable to create the desired render view host.
666
667 // Tell DevTools agent that it is attached prior to the navigation.
[email protected]d9f9b792009-06-24 13:17:12668 DevToolsManager* devtools_manager = DevToolsManager::GetInstance();
[email protected]7e8e6b62009-05-08 11:28:32669 if (devtools_manager) { // NULL in unit tests.
[email protected]684b4ba22009-05-07 06:27:41670 devtools_manager->OnNavigatingToPendingEntry(
671 render_view_host(),
672 dest_render_view_host,
673 controller_.pending_entry()->url());
[email protected]7e8e6b62009-05-08 11:28:32674 }
[email protected]96d185d2009-04-24 03:28:54675
676 // Used for page load time metrics.
677 current_load_start_ = base::TimeTicks::Now();
678
679 // Navigate in the desired RenderViewHost.
[email protected]056de2d2009-06-26 16:41:34680 ViewMsg_Navigate_Params navigate_params;
[email protected]5e369672009-11-03 23:48:30681 MakeNavigateParams(profile(), entry, reload, &navigate_params);
[email protected]056de2d2009-06-26 16:41:34682 dest_render_view_host->Navigate(navigate_params);
[email protected]96d185d2009-04-24 03:28:54683
684 if (entry.page_id() == -1) {
685 // HACK!! This code suppresses javascript: URLs from being added to
686 // session history, which is what we want to do for javascript: URLs that
687 // do not generate content. What we really need is a message from the
688 // renderer telling us that a new page was not created. The same message
689 // could be used for mailto: URLs and the like.
690 if (entry.url().SchemeIs(chrome::kJavaScriptScheme))
691 return false;
692 }
693
694 // Clear any provisional password saves - this stops password infobars
695 // showing up on pages the user navigates to while the right page is
696 // loading.
697 GetPasswordManager()->ClearProvisionalSave();
698
699 if (reload && !profile()->IsOffTheRecord()) {
[email protected]0189bc722009-08-28 21:56:48700 FaviconService* favicon_service =
701 profile()->GetFaviconService(Profile::IMPLICIT_ACCESS);
702 if (favicon_service)
703 favicon_service->SetFaviconOutOfDateForPage(entry.url());
[email protected]96d185d2009-04-24 03:28:54704 }
705
[email protected]d5f942ba2008-09-26 19:30:34706 return true;
707}
708
[email protected]96d185d2009-04-24 03:28:54709void TabContents::Stop() {
710 render_manager_.Stop();
711 printing_.Stop();
712}
713
714void TabContents::Cut() {
715 render_view_host()->Cut();
716}
717
718void TabContents::Copy() {
719 render_view_host()->Copy();
720}
721
[email protected]c0cc3092009-09-12 08:27:27722#if defined(OS_MACOSX)
[email protected]a954bf72009-09-12 07:30:35723void TabContents::CopyToFindPboard() {
724 render_view_host()->CopyToFindPboard();
725}
[email protected]c0cc3092009-09-12 08:27:27726#endif
[email protected]a954bf72009-09-12 07:30:35727
[email protected]96d185d2009-04-24 03:28:54728void TabContents::Paste() {
729 render_view_host()->Paste();
730}
731
[email protected]7e786822009-11-07 00:00:27732#if defined(OS_MACOSX)
733void TabContents::PasteAndMatchStyle() {
734 render_view_host()->ForwardEditCommand("PasteAndMatchStyle", "");
735}
736#endif
737
[email protected]96d185d2009-04-24 03:28:54738void TabContents::DisassociateFromPopupCount() {
739 render_view_host()->DisassociateFromPopupCount();
740}
741
742TabContents* TabContents::Clone() {
743 // We create a new SiteInstance so that the new tab won't share processes
744 // with the old one. This can be changed in the future if we need it to share
745 // processes for some reason.
[email protected]420ae012009-04-24 05:16:32746 TabContents* tc = new TabContents(profile(),
[email protected]96d185d2009-04-24 03:28:54747 SiteInstance::CreateSiteInstance(profile()),
[email protected]12636df2009-09-28 22:32:21748 MSG_ROUTING_NONE, this);
[email protected]96d185d2009-04-24 03:28:54749 tc->controller().CopyStateFrom(controller_);
750 return tc;
751}
752
[email protected]4d677202009-07-19 07:37:12753void TabContents::ShowPageInfo(const GURL& url,
754 const NavigationEntry::SSLStatus& ssl,
755 bool show_history) {
756 if (!delegate_)
757 return;
758
[email protected]bb678332009-07-21 00:15:50759 delegate_->ShowPageInfo(profile(), url, ssl, show_history);
[email protected]4d677202009-07-19 07:37:12760}
761
initial.commit09911bf2008-07-26 23:55:29762ConstrainedWindow* TabContents::CreateConstrainedDialog(
[email protected]e8382172009-06-19 22:16:28763 ConstrainedWindowDelegate* delegate) {
initial.commit09911bf2008-07-26 23:55:29764 ConstrainedWindow* window =
[email protected]e8382172009-06-19 22:16:28765 ConstrainedWindow::CreateConstrainedDialog(this, delegate);
initial.commit09911bf2008-07-26 23:55:29766 child_windows_.push_back(window);
767 return window;
768}
769
770void TabContents::AddNewContents(TabContents* new_contents,
771 WindowOpenDisposition disposition,
772 const gfx::Rect& initial_pos,
[email protected]c88a70fe2009-05-05 20:00:22773 bool user_gesture,
774 const GURL& creator_url) {
initial.commit09911bf2008-07-26 23:55:29775 if (!delegate_)
776 return;
777
[email protected]35f7d212009-04-29 21:19:27778 if ((disposition == NEW_POPUP) && !user_gesture &&
779 !CommandLine::ForCurrentProcess()->HasSwitch(
780 switches::kDisablePopupBlocking)) {
[email protected]2c4410d2009-05-06 23:46:22781 // Unrequested popups from normal pages are constrained unless they're in
[email protected]b6c874582009-05-08 19:38:31782 // the whitelist. The popup owner will handle checking this.
783 delegate_->GetConstrainingContents(this)->AddPopup(new_contents,
784 initial_pos,
785 creator_url.is_valid() ? creator_url.host() : std::string());
786 } else {
[email protected]304f8792009-08-19 21:28:09787#if defined(OS_CHROMEOS)
788 if (disposition == NEW_POPUP) {
789 // If the popup is bigger than a given factor of the screen, then
790 // turn it into a foreground tab (on chrome os only)
791 GdkScreen* screen = gdk_screen_get_default();
792 int max_width = gdk_screen_get_width(screen) * kMaxWidthFactor;
793 int max_height = gdk_screen_get_height(screen) * kMaxHeightFactor;
794 if (initial_pos.width() > max_width ||
795 initial_pos.height() > max_height) {
796 disposition = NEW_FOREGROUND_TAB;
797 }
798 }
799#endif
800
[email protected]0aa55312008-10-17 21:53:08801 new_contents->DisassociateFromPopupCount();
initial.commit09911bf2008-07-26 23:55:29802 delegate_->AddNewContents(this, new_contents, disposition, initial_pos,
803 user_gesture);
[email protected]7f37dd92009-09-15 16:35:49804 NotificationService::current()->Notify(
805 NotificationType::TAB_ADDED,
806 Source<TabContentsDelegate>(delegate_),
807 Details<TabContents>(this));
initial.commit09911bf2008-07-26 23:55:29808 }
[email protected]b6c874582009-05-08 19:38:31809 PopupNotificationVisibilityChanged(ShowingBlockedPopupNotification());
initial.commit09911bf2008-07-26 23:55:29810}
811
initial.commit09911bf2008-07-26 23:55:29812void TabContents::CloseAllSuppressedPopups() {
[email protected]d6598c052008-11-05 19:03:25813 if (blocked_popups_)
[email protected]2c4410d2009-05-06 23:46:22814 blocked_popups_->CloseAll();
initial.commit09911bf2008-07-26 23:55:29815}
816
[email protected]912256b32009-09-18 09:47:35817void TabContents::ExecuteCode(int request_id, const std::string& extension_id,
[email protected]20ad2692009-11-20 18:27:20818 bool is_js_code, const std::string& code_string,
819 bool all_frames) {
[email protected]912256b32009-09-18 09:47:35820 render_view_host()->Send(
821 new ViewMsg_ExecuteCode(render_view_host()->routing_id(), request_id,
[email protected]20ad2692009-11-20 18:27:20822 extension_id, is_js_code, code_string,
823 all_frames));
[email protected]912256b32009-09-18 09:47:35824}
825
[email protected]96d185d2009-04-24 03:28:54826void TabContents::PopupNotificationVisibilityChanged(bool visible) {
827 render_view_host()->PopupNotificationVisibilityChanged(visible);
828}
829
[email protected]5c9e97a2009-09-09 23:48:30830gfx::NativeView TabContents::GetContentNativeView() const {
[email protected]96d185d2009-04-24 03:28:54831 return view_->GetContentNativeView();
832}
833
834gfx::NativeView TabContents::GetNativeView() const {
835 return view_->GetNativeView();
836}
837
838void TabContents::GetContainerBounds(gfx::Rect *out) const {
839 view_->GetContainerBounds(out);
840}
841
842void TabContents::Focus() {
843 view_->Focus();
844}
845
[email protected]90daadb42009-06-08 21:27:28846void TabContents::FocusThroughTabTraversal(bool reverse) {
[email protected]7e383692009-06-12 19:14:54847 if (showing_interstitial_page()) {
848 render_manager_.interstitial_page()->FocusThroughTabTraversal(reverse);
849 return;
850 }
[email protected]96d185d2009-04-24 03:28:54851 render_view_host()->SetInitialFocus(reverse);
852}
853
854bool TabContents::FocusLocationBarByDefault() {
855 DOMUI* dom_ui = GetDOMUIForCurrentState();
856 if (dom_ui)
857 return dom_ui->focus_location_bar_by_default();
[email protected]3e3f0eb2009-06-22 18:33:43858 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]e0d481582009-09-15 21:06:25859 if (entry && entry->url() == GURL(chrome::kAboutBlankURL))
[email protected]3e3f0eb2009-06-22 18:33:43860 return true;
[email protected]96d185d2009-04-24 03:28:54861 return false;
862}
863
[email protected]616ed5a2008-11-21 22:27:24864void TabContents::AddInfoBar(InfoBarDelegate* delegate) {
865 // Look through the existing InfoBarDelegates we have for a match. If we've
866 // already got one that matches, then we don't add the new one.
[email protected]f86a07022008-11-25 01:06:05867 for (int i = 0; i < infobar_delegate_count(); ++i) {
[email protected]616ed5a2008-11-21 22:27:24868 if (GetInfoBarDelegateAt(i)->EqualsDelegate(delegate))
869 return;
870 }
871
872 infobar_delegates_.push_back(delegate);
[email protected]bfd04a62009-02-01 18:16:56873 NotificationService::current()->Notify(
874 NotificationType::TAB_CONTENTS_INFOBAR_ADDED,
875 Source<TabContents>(this),
876 Details<InfoBarDelegate>(delegate));
[email protected]616ed5a2008-11-21 22:27:24877
878 // Add ourselves as an observer for navigations the first time a delegate is
879 // added. We use this notification to expire InfoBars that need to expire on
880 // page transitions.
881 if (infobar_delegates_.size() == 1) {
[email protected]bfd04a62009-02-01 18:16:56882 registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED,
[email protected]ce3fa3c2009-04-20 19:55:57883 Source<NavigationController>(&controller_));
[email protected]616ed5a2008-11-21 22:27:24884 }
885}
886
887void TabContents::RemoveInfoBar(InfoBarDelegate* delegate) {
888 std::vector<InfoBarDelegate*>::iterator it =
889 find(infobar_delegates_.begin(), infobar_delegates_.end(), delegate);
890 if (it != infobar_delegates_.end()) {
891 InfoBarDelegate* delegate = *it;
[email protected]bfd04a62009-02-01 18:16:56892 NotificationService::current()->Notify(
893 NotificationType::TAB_CONTENTS_INFOBAR_REMOVED,
894 Source<TabContents>(this),
895 Details<InfoBarDelegate>(delegate));
[email protected]f86a07022008-11-25 01:06:05896 infobar_delegates_.erase(it);
[email protected]616ed5a2008-11-21 22:27:24897
[email protected]6a02963e2009-01-06 16:58:03898 // Remove ourselves as an observer if we are tracking no more InfoBars.
899 if (infobar_delegates_.empty()) {
[email protected]bfd04a62009-02-01 18:16:56900 registrar_.Remove(this, NotificationType::NAV_ENTRY_COMMITTED,
[email protected]ce3fa3c2009-04-20 19:55:57901 Source<NavigationController>(&controller_));
[email protected]6a02963e2009-01-06 16:58:03902 }
[email protected]616ed5a2008-11-21 22:27:24903 }
904}
[email protected]3edd9522009-03-04 22:19:41905
[email protected]f81821c2009-08-07 01:29:47906void TabContents::ReplaceInfoBar(InfoBarDelegate* old_delegate,
907 InfoBarDelegate* new_delegate) {
908 std::vector<InfoBarDelegate*>::iterator it =
909 find(infobar_delegates_.begin(), infobar_delegates_.end(), old_delegate);
910 DCHECK(it != infobar_delegates_.end());
911
912 // Notify the container about the change of plans.
913 scoped_ptr<std::pair<InfoBarDelegate*, InfoBarDelegate*> > details(
914 new std::pair<InfoBarDelegate*, InfoBarDelegate*>(
915 old_delegate, new_delegate));
916 NotificationService::current()->Notify(
917 NotificationType::TAB_CONTENTS_INFOBAR_REPLACED,
918 Source<TabContents>(this),
919 Details<std::pair<InfoBarDelegate*, InfoBarDelegate*> >(details.get()));
920
921 // Remove the old one.
922 infobar_delegates_.erase(it);
923
924 // Add the new one.
925 infobar_delegates_.push_back(new_delegate);
926}
927
[email protected]20ede072009-10-06 00:28:35928bool TabContents::ShouldShowBookmarkBar() {
[email protected]aa9196f2009-10-22 16:20:27929 if (showing_interstitial_page())
930 return false;
931
[email protected]96d185d2009-04-24 03:28:54932 // See GetDOMUIForCurrentState() comment for more info. This case is very
933 // similar, but for non-first loads, we want to use the committed entry. This
934 // is so the bookmarks bar disappears at the same time the page does.
935 if (controller_.GetLastCommittedEntry()) {
936 // Not the first load, always use the committed DOM UI.
937 if (render_manager_.dom_ui())
938 return render_manager_.dom_ui()->force_bookmark_bar_visible();
939 return false; // Default.
940 }
941
942 // When it's the first load, we know either the pending one or the committed
943 // one will have the DOM UI in it (see GetDOMUIForCurrentState), and only one
944 // of them will be valid, so we can just check both.
945 if (render_manager_.pending_dom_ui())
946 return render_manager_.pending_dom_ui()->force_bookmark_bar_visible();
947 if (render_manager_.dom_ui())
948 return render_manager_.dom_ui()->force_bookmark_bar_visible();
949 return false; // Default.
950}
951
[email protected]d8f16ae2009-08-30 03:47:26952bool TabContents::IsExtensionShelfAlwaysVisible() {
953 // See GetDOMUIForCurrentState() comment for more info. This case is very
954 // similar, but for non-first loads, we want to use the committed entry. This
955 // is so the bookmarks bar disappears at the same time the page does.
956 if (controller_.GetLastCommittedEntry()) {
957 // Not the first load, always use the committed DOM UI.
958 if (render_manager_.dom_ui())
959 return render_manager_.dom_ui()->force_extension_shelf_visible();
960 return false; // Default.
961 }
962
963 // When it's the first load, we know either the pending one or the committed
964 // one will have the DOM UI in it (see GetDOMUIForCurrentState), and only one
965 // of them will be valid, so we can just check both.
966 if (render_manager_.pending_dom_ui())
967 return render_manager_.pending_dom_ui()->force_extension_shelf_visible();
968 if (render_manager_.dom_ui())
969 return render_manager_.dom_ui()->force_extension_shelf_visible();
970 return false; // Default.
971}
972
[email protected]3edd9522009-03-04 22:19:41973void TabContents::ToolbarSizeChanged(bool is_animating) {
974 TabContentsDelegate* d = delegate();
975 if (d)
976 d->ToolbarSizeChanged(this, is_animating);
977}
[email protected]616ed5a2008-11-21 22:27:24978
[email protected]fc6fb7fb2009-11-07 02:35:04979bool TabContents::CanDownload(int request_id) {
980 TabContentsDelegate* d = delegate();
981 if (d)
982 return d->CanDownload(request_id);
983 return true;
984}
985
[email protected]d5f942ba2008-09-26 19:30:34986void TabContents::OnStartDownload(DownloadItem* download) {
987 DCHECK(download);
[email protected]d5f942ba2008-09-26 19:30:34988
989 // Download in a constrained popup is shown in the tab that opened it.
[email protected]b6c874582009-05-08 19:38:31990 TabContents* tab_contents = delegate()->GetConstrainingContents(this);
[email protected]d5f942ba2008-09-26 19:30:34991
[email protected]59560e0b2009-06-04 03:30:22992 if (tab_contents && tab_contents->delegate())
993 tab_contents->delegate()->OnStartDownload(download);
[email protected]3edd9522009-03-04 22:19:41994}
[email protected]3edd9522009-03-04 22:19:41995
[email protected]d5f942ba2008-09-26 19:30:34996void TabContents::WillClose(ConstrainedWindow* window) {
997 ConstrainedWindowList::iterator it =
998 find(child_windows_.begin(), child_windows_.end(), window);
999 if (it != child_windows_.end())
1000 child_windows_.erase(it);
[email protected]66ba4932009-06-04 19:22:131001}
[email protected]d5f942ba2008-09-26 19:30:341002
[email protected]66ba4932009-06-04 19:22:131003void TabContents::WillCloseBlockedPopupContainer(
1004 BlockedPopupContainer* container) {
1005 DCHECK(blocked_popups_ == container);
1006 blocked_popups_ = NULL;
[email protected]d5f942ba2008-09-26 19:30:341007}
1008
[email protected]d5f942ba2008-09-26 19:30:341009void TabContents::DidMoveOrResize(ConstrainedWindow* window) {
[email protected]332af7732009-03-11 13:21:351010#if defined(OS_WIN)
[email protected]92edc472009-02-10 20:32:061011 UpdateWindow(GetNativeView());
[email protected]332af7732009-03-11 13:21:351012#endif
[email protected]d5f942ba2008-09-26 19:30:341013}
1014
[email protected]e491f1c2009-05-22 20:28:121015void TabContents::StartFinding(string16 find_text,
1016 bool forward_direction,
1017 bool case_sensitive) {
[email protected]6e95706d2009-04-23 22:55:011018 // If find_text is empty, it means FindNext was pressed with a keyboard
1019 // shortcut so unless we have something to search for we return early.
[email protected]e491f1c2009-05-22 20:28:121020 if (find_text.empty() && find_text_.empty()) {
1021 if (last_search_prepopulate_text_->empty())
1022 return;
1023 // Try whatever we searched for last in any tab.
1024 find_text = *last_search_prepopulate_text_;
1025 }
[email protected]6e95706d2009-04-23 22:55:011026
[email protected]101c90e2009-10-26 21:14:441027 // Keep track of the previous search.
1028 previous_find_text_ = find_text_;
1029
[email protected]6e95706d2009-04-23 22:55:011030 // This is a FindNext operation if we are searching for the same text again,
1031 // or if the passed in search text is empty (FindNext keyboard shortcut). The
1032 // exception to this is if the Find was aborted (then we don't want FindNext
1033 // because the highlighting has been cleared and we need it to reappear). We
1034 // therefore treat FindNext after an aborted Find operation as a full fledged
1035 // Find.
1036 bool find_next = (find_text_ == find_text || find_text.empty()) &&
[email protected]e491f1c2009-05-22 20:28:121037 (last_search_case_sensitive_ == case_sensitive) &&
[email protected]6e95706d2009-04-23 22:55:011038 !find_op_aborted_;
1039 if (!find_next)
1040 current_find_request_id_ = find_request_id_counter_++;
1041
1042 if (!find_text.empty())
1043 find_text_ = find_text;
[email protected]e491f1c2009-05-22 20:28:121044 last_search_case_sensitive_ = case_sensitive;
[email protected]6e95706d2009-04-23 22:55:011045
1046 find_op_aborted_ = false;
1047
1048 // Keep track of what the last search was across the tabs.
[email protected]e491f1c2009-05-22 20:28:121049 *last_search_prepopulate_text_ = find_text;
[email protected]6e95706d2009-04-23 22:55:011050
1051 render_view_host()->StartFinding(current_find_request_id_,
1052 find_text_,
1053 forward_direction,
[email protected]e491f1c2009-05-22 20:28:121054 case_sensitive,
[email protected]6e95706d2009-04-23 22:55:011055 find_next);
1056}
1057
1058void TabContents::StopFinding(bool clear_selection) {
[email protected]2d2dffc2009-06-09 21:41:001059 // When |clear_selection| is true, it means the find string has been cleared
1060 // by the user, but the UI has not been dismissed.
1061 if (!clear_selection)
1062 find_ui_active_ = false;
[email protected]101c90e2009-10-26 21:14:441063 find_text_.clear();
[email protected]6e95706d2009-04-23 22:55:011064 find_op_aborted_ = true;
[email protected]e491f1c2009-05-22 20:28:121065 last_search_result_ = FindNotificationDetails();
[email protected]6e95706d2009-04-23 22:55:011066 render_view_host()->StopFinding(clear_selection);
1067}
1068
[email protected]5c4266922009-07-10 16:41:271069void TabContents::GetPageLanguage() {
1070 render_view_host()->GetPageLanguage();
1071}
1072
[email protected]420ae012009-04-24 05:16:321073void TabContents::OnSavePage() {
1074 // If we can not save the page, try to download it.
1075 if (!SavePackage::IsSavableContents(contents_mime_type())) {
1076 DownloadManager* dlm = profile()->GetDownloadManager();
1077 const GURL& current_page_url = GetURL();
1078 if (dlm && current_page_url.is_valid())
[email protected]d82ed61e2009-06-16 02:46:221079 dlm->DownloadUrl(current_page_url, GURL(), "", this);
[email protected]420ae012009-04-24 05:16:321080 return;
1081 }
1082
1083 Stop();
1084
1085 // Create the save package and possibly prompt the user for the name to save
1086 // the page as. The user prompt is an asynchronous operation that runs on
1087 // another thread.
[email protected]d82ed61e2009-06-16 02:46:221088 save_package_ = new SavePackage(this);
[email protected]420ae012009-04-24 05:16:321089 save_package_->GetSaveInfo();
1090}
1091
1092// Used in automated testing to bypass prompting the user for file names.
1093// Instead, the names and paths are hard coded rather than running them through
1094// file name sanitation and extension / mime checking.
[email protected]828cabe2009-09-26 22:47:111095bool TabContents::SavePage(const FilePath& main_file, const FilePath& dir_path,
[email protected]420ae012009-04-24 05:16:321096 SavePackage::SavePackageType save_type) {
1097 // Stop the page from navigating.
1098 Stop();
1099
[email protected]828cabe2009-09-26 22:47:111100 save_package_ = new SavePackage(this, save_type, main_file, dir_path);
[email protected]cdf6cc82009-08-04 21:48:591101 return save_package_->Init();
[email protected]420ae012009-04-24 05:16:321102}
1103
1104void TabContents::PrintPreview() {
1105 // We don't show the print preview yet, only the print dialog.
1106 PrintNow();
1107}
1108
1109bool TabContents::PrintNow() {
1110 // We can't print interstitial page for now.
1111 if (showing_interstitial_page())
1112 return false;
1113
1114 return render_view_host()->PrintPages();
1115}
1116
[email protected]82270452009-06-19 15:58:011117void TabContents::PrintingDone(int document_cookie, bool success) {
1118 render_view_host()->PrintingDone(document_cookie, success);
1119}
1120
[email protected]420ae012009-04-24 05:16:321121bool TabContents::IsActiveEntry(int32 page_id) {
1122 NavigationEntry* active_entry = controller_.GetActiveEntry();
1123 return (active_entry != NULL &&
1124 active_entry->site_instance() == GetSiteInstance() &&
1125 active_entry->page_id() == page_id);
1126}
1127
[email protected]3f8eb7f2009-10-02 23:58:051128void TabContents::LogNewTabTime(const std::string& event_name) {
1129 // Not all new tab pages get timed. In those cases, we don't have a
1130 // new_tab_start_time_.
1131 if (new_tab_start_time_.is_null())
1132 return;
1133
1134 base::TimeDelta duration = base::TimeTicks::Now() - new_tab_start_time_;
[email protected]84abba62009-10-07 17:01:441135 MetricEventDurationDetails details(event_name,
1136 static_cast<int>(duration.InMilliseconds()));
1137
[email protected]7ab1e7d62009-10-14 23:32:011138 if (event_name == "Tab.NewTabScriptStart") {
1139 UMA_HISTOGRAM_TIMES("Tab.NewTabScriptStart", duration);
1140 } else if (event_name == "Tab.NewTabDOMContentLoaded") {
1141 UMA_HISTOGRAM_TIMES("Tab.NewTabDOMContentLoaded", duration);
1142 } else if (event_name == "Tab.NewTabOnload") {
1143 UMA_HISTOGRAM_TIMES("Tab.NewTabOnload", duration);
[email protected]3f8eb7f2009-10-02 23:58:051144 // The new tab page has finished loading; reset it.
1145 new_tab_start_time_ = base::TimeTicks();
[email protected]84abba62009-10-07 17:01:441146 } else {
1147 NOTREACHED();
[email protected]3f8eb7f2009-10-02 23:58:051148 }
[email protected]84abba62009-10-07 17:01:441149 NotificationService::current()->Notify(
1150 NotificationType::METRIC_EVENT_DURATION,
1151 Source<TabContents>(this),
1152 Details<MetricEventDurationDetails>(&details));
[email protected]3f8eb7f2009-10-02 23:58:051153}
1154
[email protected]7ab1e7d62009-10-14 23:32:011155void TabContents::OnCloseStarted() {
1156 if (tab_close_start_time_.is_null())
1157 tab_close_start_time_ = base::TimeTicks::Now();
1158}
1159
[email protected]96d185d2009-04-24 03:28:541160// Notifies the RenderWidgetHost instance about the fact that the page is
1161// loading, or done loading and calls the base implementation.
1162void TabContents::SetIsLoading(bool is_loading,
1163 LoadNotificationDetails* details) {
1164 if (is_loading == is_loading_)
1165 return;
1166
1167 if (!is_loading) {
1168 load_state_ = net::LOAD_STATE_IDLE;
1169 load_state_host_.clear();
[email protected]094e5b22009-09-25 04:23:561170 upload_size_ = 0;
1171 upload_position_ = 0;
[email protected]96d185d2009-04-24 03:28:541172 }
1173
1174 render_manager_.SetIsLoading(is_loading);
1175
1176 is_loading_ = is_loading;
1177 waiting_for_response_ = is_loading;
1178
1179 if (delegate_)
1180 delegate_->LoadingStateChanged(this);
[email protected]329581b2009-04-28 06:52:351181 NotifyNavigationStateChanged(INVALIDATE_LOAD);
[email protected]96d185d2009-04-24 03:28:541182
1183 NotificationType type = is_loading ? NotificationType::LOAD_START :
1184 NotificationType::LOAD_STOP;
[email protected]ce5c4502009-05-06 16:46:111185 NotificationDetails det = NotificationService::NoDetails();
[email protected]96d185d2009-04-24 03:28:541186 if (details)
1187 det = Details<LoadNotificationDetails>(details);
1188 NotificationService::current()->Notify(type,
1189 Source<NavigationController>(&controller_),
1190 det);
1191}
1192
[email protected]2c4410d2009-05-06 23:46:221193void TabContents::CreateBlockedPopupContainerIfNecessary() {
1194 if (blocked_popups_)
1195 return;
1196
[email protected]4d4c0b92009-06-08 23:13:011197 blocked_popups_ = BlockedPopupContainer::Create(this, profile());
[email protected]2c4410d2009-05-06 23:46:221198}
1199
[email protected]b6c874582009-05-08 19:38:311200void TabContents::AddPopup(TabContents* new_contents,
1201 const gfx::Rect& initial_pos,
1202 const std::string& host) {
[email protected]2c4410d2009-05-06 23:46:221203 CreateBlockedPopupContainerIfNecessary();
1204 blocked_popups_->AddTabContents(new_contents, initial_pos, host);
[email protected]2c4410d2009-05-06 23:46:221205}
1206
[email protected]e69bce3dd2009-04-20 22:05:121207// TODO(brettw) This should be on the TabContentsView.
[email protected]66ba4932009-06-04 19:22:131208void TabContents::RepositionSupressedPopupsToFit() {
[email protected]d6598c052008-11-05 19:03:251209 if (blocked_popups_)
[email protected]4d4c0b92009-06-08 23:13:011210 blocked_popups_->RepositionBlockedPopupContainer();
[email protected]d5f942ba2008-09-26 19:30:341211}
1212
[email protected]b9681312008-11-07 00:08:261213bool TabContents::ShowingBlockedPopupNotification() const {
1214 return blocked_popups_ != NULL &&
[email protected]2c4410d2009-05-06 23:46:221215 blocked_popups_->GetBlockedPopupCount() != 0;
[email protected]b9681312008-11-07 00:08:261216}
[email protected]616ed5a2008-11-21 22:27:241217
1218namespace {
1219bool TransitionIsReload(PageTransition::Type transition) {
1220 return PageTransition::StripQualifier(transition) == PageTransition::RELOAD;
1221}
1222}
1223
1224void TabContents::ExpireInfoBars(
1225 const NavigationController::LoadCommittedDetails& details) {
1226 // Only hide InfoBars when the user has done something that makes the main
1227 // frame load. We don't want various automatic or subframe navigations making
1228 // it disappear.
1229 if (!details.is_user_initiated_main_frame_load())
1230 return;
1231
[email protected]f86a07022008-11-25 01:06:051232 for (int i = infobar_delegate_count() - 1; i >= 0; --i) {
[email protected]616ed5a2008-11-21 22:27:241233 InfoBarDelegate* delegate = GetInfoBarDelegateAt(i);
[email protected]f86a07022008-11-25 01:06:051234 if (delegate->ShouldExpire(details))
[email protected]616ed5a2008-11-21 22:27:241235 RemoveInfoBar(delegate);
[email protected]616ed5a2008-11-21 22:27:241236 }
1237}
[email protected]fdd61c62009-04-22 19:22:571238
[email protected]96d185d2009-04-24 03:28:541239DOMUI* TabContents::GetDOMUIForCurrentState() {
1240 // When there is a pending navigation entry, we want to use the pending DOMUI
1241 // that goes along with it to control the basic flags. For example, we want to
1242 // show the pending URL in the URL bar, so we want the display_url flag to
1243 // be from the pending entry.
1244 //
1245 // The confusion comes because there are multiple possibilities for the
1246 // initial load in a tab as a side effect of the way the RenderViewHostManager
1247 // works.
1248 //
1249 // - For the very first tab the load looks "normal". The new tab DOM UI is
1250 // the pending one, and we want it to apply here.
1251 //
1252 // - For subsequent new tabs, they'll get a new SiteInstance which will then
1253 // get switched to the one previously associated with the new tab pages.
1254 // This switching will cause the manager to commit the RVH/DOMUI. So we'll
1255 // have a committed DOM UI in this case.
1256 //
1257 // This condition handles all of these cases:
1258 //
1259 // - First load in first tab: no committed nav entry + pending nav entry +
1260 // pending dom ui:
1261 // -> Use pending DOM UI if any.
1262 //
1263 // - First load in second tab: no committed nav entry + pending nav entry +
1264 // no pending DOM UI:
1265 // -> Use the committed DOM UI if any.
1266 //
1267 // - Second navigation in any tab: committed nav entry + pending nav entry:
1268 // -> Use pending DOM UI if any.
1269 //
1270 // - Normal state with no load: committed nav entry + no pending nav entry:
1271 // -> Use committed DOM UI.
1272 if (controller_.pending_entry() &&
1273 (controller_.GetLastCommittedEntry() ||
1274 render_manager_.pending_dom_ui()))
1275 return render_manager_.pending_dom_ui();
1276 return render_manager_.dom_ui();
1277}
[email protected]420ae012009-04-24 05:16:321278
1279void TabContents::DidNavigateMainFramePostCommit(
1280 const NavigationController::LoadCommittedDetails& details,
1281 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]c2e74fe82009-09-03 17:57:441282 if (opener_dom_ui_type_ != DOMUIFactory::kNoDOMUI) {
1283 // If this is a window.open navigation, use the same DOMUI as the renderer
1284 // that opened the window, as long as both renderers have the same
1285 // privileges.
1286 if (opener_dom_ui_type_ == DOMUIFactory::GetDOMUIType(GetURL())) {
1287 DOMUI* dom_ui = DOMUIFactory::CreateDOMUIForURL(this, GetURL());
1288 // dom_ui might be NULL if the URL refers to a non-existent extension.
1289 if (dom_ui) {
1290 render_manager_.SetDOMUIPostCommit(dom_ui);
1291 dom_ui->RenderViewCreated(render_view_host());
1292 }
1293 }
1294 opener_dom_ui_type_ = DOMUIFactory::kNoDOMUI;
1295 }
1296
[email protected]420ae012009-04-24 05:16:321297 if (details.is_user_initiated_main_frame_load()) {
1298 // Clear the status bubble. This is a workaround for a bug where WebKit
1299 // doesn't let us know that the cursor left an element during a
1300 // transition (this is also why the mouse cursor remains as a hand after
1301 // clicking on a link); see bugs 1184641 and 980803. We don't want to
1302 // clear the bubble when a user navigates to a named anchor in the same
1303 // page.
1304 UpdateTargetURL(details.entry->page_id(), GURL());
1305
1306 // UpdateHelpersForDidNavigate will handle the case where the password_form
1307 // origin is valid.
1308 // TODO(brettw) bug 1343111: Password manager stuff in here needs to be
1309 // cleaned up and covered by tests.
1310 if (!params.password_form.origin.is_valid())
1311 GetPasswordManager()->DidNavigate();
1312 }
1313
1314 // The keyword generator uses the navigation entries, so must be called after
1315 // the commit.
1316 GenerateKeywordIfNecessary(params);
1317
1318 // Allow the new page to set the title again.
1319 received_page_title_ = false;
1320
1321 // Get the favicon, either from history or request it from the net.
1322 fav_icon_helper_.FetchFavIcon(details.entry->url());
1323
[email protected]56ce6e52009-10-27 00:10:521324 // Clear all page and browser action state for this tab, unless this is an
1325 // in-page navigation.
[email protected]92c6f9b92009-10-24 04:35:081326 url_canon::Replacements<char> replacements;
1327 replacements.ClearRef();
1328 if (params.url.ReplaceComponents(replacements) !=
1329 params.referrer.ReplaceComponents(replacements)) {
[email protected]92c6f9b92009-10-24 04:35:081330 ExtensionsService* service = profile()->GetExtensionsService();
1331 if (service) {
1332 for (size_t i = 0; i < service->extensions()->size(); ++i) {
[email protected]5d246db22009-10-27 06:17:571333 ExtensionAction* browser_action =
[email protected]92c6f9b92009-10-24 04:35:081334 service->extensions()->at(i)->browser_action();
[email protected]56ce6e52009-10-27 00:10:521335 if (browser_action) {
1336 browser_action->ClearAllValuesForTab(controller().session_id().id());
1337 NotificationService::current()->Notify(
1338 NotificationType::EXTENSION_BROWSER_ACTION_UPDATED,
[email protected]5d246db22009-10-27 06:17:571339 Source<ExtensionAction>(browser_action),
[email protected]56ce6e52009-10-27 00:10:521340 NotificationService::NoDetails());
1341 }
[email protected]92c6f9b92009-10-24 04:35:081342
[email protected]5d246db22009-10-27 06:17:571343 ExtensionAction* page_action =
[email protected]56ce6e52009-10-27 00:10:521344 service->extensions()->at(i)->page_action();
1345 if (page_action) {
1346 page_action->ClearAllValuesForTab(controller().session_id().id());
1347 PageActionStateChanged();
1348 }
[email protected]92c6f9b92009-10-24 04:35:081349 }
[email protected]aece2c7f2009-08-27 20:43:171350 }
1351 }
[email protected]ce5c4502009-05-06 16:46:111352
[email protected]420ae012009-04-24 05:16:321353 // Close constrained popups if necessary.
1354 MaybeCloseChildWindows(details.previous_url, details.entry->url());
1355
1356 // Update the starred state.
1357 UpdateStarredStateForCurrentURL();
1358}
1359
1360void TabContents::DidNavigateAnyFramePostCommit(
1361 RenderViewHost* render_view_host,
1362 const NavigationController::LoadCommittedDetails& details,
1363 const ViewHostMsg_FrameNavigate_Params& params) {
1364 // If we navigate, start showing messages again. This does nothing to prevent
1365 // a malicious script from spamming messages, since the script could just
1366 // reload the page to stop blocking.
1367 suppress_javascript_messages_ = false;
1368
[email protected]420ae012009-04-24 05:16:321369 // Notify the password manager of the navigation or form submit.
1370 // TODO(brettw) bug 1343111: Password manager stuff in here needs to be
1371 // cleaned up and covered by tests.
1372 if (params.password_form.origin.is_valid())
1373 GetPasswordManager()->ProvisionallySavePassword(params.password_form);
1374}
1375
1376void TabContents::MaybeCloseChildWindows(const GURL& previous_url,
1377 const GURL& current_url) {
1378 if (net::RegistryControlledDomainService::SameDomainOrHost(
1379 previous_url, current_url))
1380 return;
1381
1382 // Clear out any child windows since we are leaving this page entirely.
1383 // We use indices instead of iterators in case CloseWindow does something
1384 // that may invalidate an iterator.
1385 int size = static_cast<int>(child_windows_.size());
1386 for (int i = size - 1; i >= 0; --i) {
1387 ConstrainedWindow* window = child_windows_[i];
1388 if (window)
1389 window->CloseConstrainedWindow();
1390 }
[email protected]e29300cd2009-07-23 21:14:591391
1392 // Close the popup container.
1393 if (blocked_popups_) {
1394 blocked_popups_->Destroy();
1395 blocked_popups_ = NULL;
1396 }
[email protected]420ae012009-04-24 05:16:321397}
1398
1399void TabContents::UpdateStarredStateForCurrentURL() {
1400 BookmarkModel* model = profile()->GetBookmarkModel();
1401 const bool old_state = is_starred_;
1402 is_starred_ = (model && model->IsBookmarked(GetURL()));
1403
1404 if (is_starred_ != old_state && delegate())
1405 delegate()->URLStarredChanged(this, is_starred_);
1406}
1407
1408void TabContents::UpdateAlternateErrorPageURL() {
1409 GURL url = GetAlternateErrorPageURL();
1410 render_view_host()->SetAlternateErrorPageURL(url);
1411}
1412
1413void TabContents::UpdateWebPreferences() {
1414 render_view_host()->UpdateWebPreferences(GetWebkitPrefs());
1415}
1416
1417void TabContents::UpdateMaxPageIDIfNecessary(SiteInstance* site_instance,
1418 RenderViewHost* rvh) {
1419 // If we are creating a RVH for a restored controller, then we might
1420 // have more page IDs than the SiteInstance's current max page ID. We must
1421 // make sure that the max page ID is larger than any restored page ID.
1422 // Note that it is ok for conflicting page IDs to exist in another tab
1423 // (i.e., NavigationController), but if any page ID is larger than the max,
1424 // the back/forward list will get confused.
1425 int max_restored_page_id = controller_.max_restored_page_id();
1426 if (max_restored_page_id > 0) {
1427 int curr_max_page_id = site_instance->max_page_id();
1428 if (max_restored_page_id > curr_max_page_id) {
1429 // Need to update the site instance immediately.
1430 site_instance->UpdateMaxPageID(max_restored_page_id);
1431
1432 // Also tell the renderer to update its internal representation. We
1433 // need to reserve enough IDs to make all restored page IDs less than
1434 // the max.
1435 if (curr_max_page_id < 0)
1436 curr_max_page_id = 0;
1437 rvh->ReservePageIDRange(max_restored_page_id - curr_max_page_id);
1438 }
1439 }
1440}
1441
1442void TabContents::UpdateHistoryForNavigation(
[email protected]ebe89e062009-08-13 23:16:541443 const GURL& virtual_url,
[email protected]befd8d822009-07-01 04:51:471444 const NavigationController::LoadCommittedDetails& details,
[email protected]420ae012009-04-24 05:16:321445 const ViewHostMsg_FrameNavigate_Params& params) {
1446 if (profile()->IsOffTheRecord())
1447 return;
1448
1449 // Add to history service.
1450 HistoryService* hs = profile()->GetHistoryService(Profile::IMPLICIT_ACCESS);
1451 if (hs) {
1452 if (PageTransition::IsMainFrame(params.transition) &&
[email protected]ebe89e062009-08-13 23:16:541453 virtual_url != params.url) {
1454 // Hack on the "virtual" URL so that it will appear in history. For some
[email protected]420ae012009-04-24 05:16:321455 // types of URLs, we will display a magic URL that is different from where
1456 // the page is actually navigated. We want the user to see in history
[email protected]ebe89e062009-08-13 23:16:541457 // what they saw in the URL bar, so we add the virtual URL as a redirect.
1458 // This only applies to the main frame, as the virtual URL doesn't apply
[email protected]420ae012009-04-24 05:16:321459 // to sub-frames.
1460 std::vector<GURL> redirects = params.redirects;
1461 if (!redirects.empty())
[email protected]ebe89e062009-08-13 23:16:541462 redirects.back() = virtual_url;
1463 hs->AddPage(virtual_url, this, params.page_id, params.referrer,
[email protected]befd8d822009-07-01 04:51:471464 params.transition, redirects, details.did_replace_entry);
[email protected]420ae012009-04-24 05:16:321465 } else {
1466 hs->AddPage(params.url, this, params.page_id, params.referrer,
[email protected]befd8d822009-07-01 04:51:471467 params.transition, params.redirects,
1468 details.did_replace_entry);
[email protected]420ae012009-04-24 05:16:321469 }
1470 }
1471}
1472
1473bool TabContents::UpdateTitleForEntry(NavigationEntry* entry,
1474 const std::wstring& title) {
1475 // For file URLs without a title, use the pathname instead. In the case of a
1476 // synthesized title, we don't want the update to count toward the "one set
1477 // per page of the title to history."
1478 std::wstring final_title;
1479 bool explicit_set;
1480 if (entry->url().SchemeIsFile() && title.empty()) {
1481 final_title = UTF8ToWide(entry->url().ExtractFileName());
1482 explicit_set = false; // Don't count synthetic titles toward the set limit.
1483 } else {
1484 TrimWhitespace(title, TRIM_ALL, &final_title);
1485 explicit_set = true;
1486 }
1487
1488 if (final_title == UTF16ToWideHack(entry->title()))
1489 return false; // Nothing changed, don't bother.
1490
1491 entry->set_title(WideToUTF16Hack(final_title));
1492
1493 // Update the history system for this page.
1494 if (!profile()->IsOffTheRecord() && !received_page_title_) {
1495 HistoryService* hs =
1496 profile()->GetHistoryService(Profile::IMPLICIT_ACCESS);
1497 if (hs)
[email protected]ebe89e062009-08-13 23:16:541498 hs->SetPageTitle(entry->virtual_url(), final_title);
[email protected]420ae012009-04-24 05:16:321499
1500 // Don't allow the title to be saved again for explicitly set ones.
1501 received_page_title_ = explicit_set;
1502 }
1503
1504 // Lastly, set the title for the view.
1505 view_->SetPageTitle(final_title);
1506
1507 return true;
1508}
1509
1510void TabContents::NotifySwapped() {
1511 // After sending out a swap notification, we need to send a disconnect
1512 // notification so that clients that pick up a pointer to |this| can NULL the
1513 // pointer. See Bug 1230284.
1514 notify_disconnection_ = true;
1515 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341516 NotificationType::TAB_CONTENTS_SWAPPED,
[email protected]d82ed61e2009-06-16 02:46:221517 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321518 NotificationService::NoDetails());
1519}
1520
1521void TabContents::NotifyConnected() {
1522 notify_disconnection_ = true;
1523 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341524 NotificationType::TAB_CONTENTS_CONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221525 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321526 NotificationService::NoDetails());
1527}
1528
1529void TabContents::NotifyDisconnected() {
1530 if (!notify_disconnection_)
1531 return;
1532
1533 notify_disconnection_ = false;
1534 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341535 NotificationType::TAB_CONTENTS_DISCONNECTED,
[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::GenerateKeywordIfNecessary(
1541 const ViewHostMsg_FrameNavigate_Params& params) {
1542 if (!params.searchable_form_url.is_valid())
1543 return;
1544
1545 if (profile()->IsOffTheRecord())
1546 return;
1547
1548 int last_index = controller_.last_committed_entry_index();
1549 // When there was no previous page, the last index will be 0. This is
1550 // normally due to a form submit that opened in a new tab.
1551 // TODO(brettw) bug 916126: we should support keywords when form submits
1552 // happen in new tabs.
1553 if (last_index <= 0)
1554 return;
1555 const NavigationEntry* previous_entry =
1556 controller_.GetEntryAtIndex(last_index - 1);
1557 if (IsFormSubmit(previous_entry)) {
1558 // Only generate a keyword if the previous page wasn't itself a form
1559 // submit.
1560 return;
1561 }
1562
1563 GURL keyword_url = previous_entry->user_typed_url().is_valid() ?
1564 previous_entry->user_typed_url() : previous_entry->url();
1565 std::wstring keyword =
1566 TemplateURLModel::GenerateKeyword(keyword_url, true); // autodetected
1567 if (keyword.empty())
1568 return;
1569
1570 TemplateURLModel* url_model = profile()->GetTemplateURLModel();
1571 if (!url_model)
1572 return;
1573
1574 if (!url_model->loaded()) {
1575 url_model->Load();
1576 return;
1577 }
1578
1579 const TemplateURL* current_url;
1580 std::wstring url = UTF8ToWide(params.searchable_form_url.spec());
1581 if (!url_model->CanReplaceKeyword(keyword, url, &current_url))
1582 return;
1583
1584 if (current_url) {
1585 if (current_url->originating_url().is_valid()) {
1586 // The existing keyword was generated from an OpenSearch description
1587 // document, don't regenerate.
1588 return;
1589 }
1590 url_model->Remove(current_url);
1591 }
1592 TemplateURL* new_url = new TemplateURL();
1593 new_url->set_keyword(keyword);
1594 new_url->set_short_name(keyword);
1595 new_url->SetURL(url, 0, 0);
1596 new_url->add_input_encoding(params.searchable_form_encoding);
1597 DCHECK(controller_.GetLastCommittedEntry());
1598 const GURL& favicon_url =
1599 controller_.GetLastCommittedEntry()->favicon().url();
1600 if (favicon_url.is_valid()) {
1601 new_url->SetFavIconURL(favicon_url);
1602 } else {
1603 // The favicon url isn't valid. This means there really isn't a favicon,
1604 // or the favicon url wasn't obtained before the load started. This assumes
1605 // the later.
1606 // TODO(sky): Need a way to set the favicon that doesn't involve generating
1607 // its url.
1608 new_url->SetFavIconURL(TemplateURL::GenerateFaviconURL(params.referrer));
1609 }
1610 new_url->set_safe_for_autoreplace(true);
1611 url_model->Add(new_url);
1612}
1613
[email protected]bf5c2ff392009-07-08 16:24:331614void TabContents::OnUserGesture() {
1615 // See comment in RenderViewHostDelegate::OnUserGesture as to why we do this.
1616 DownloadRequestManager* drm = g_browser_process->download_request_manager();
1617 if (drm)
1618 drm->OnUserGesture(this);
[email protected]e7eaedde2009-09-25 20:09:491619 ExternalProtocolHandler::OnUserGesture();
[email protected]bf5c2ff392009-07-08 16:24:331620 controller_.OnUserGesture();
1621}
1622
1623void TabContents::OnFindReply(int request_id,
1624 int number_of_matches,
1625 const gfx::Rect& selection_rect,
1626 int active_match_ordinal,
1627 bool final_update) {
[email protected]3ce7ced2009-07-24 18:52:421628 // Ignore responses for requests that have been aborted.
1629 if (find_op_aborted_)
1630 return;
1631
[email protected]bf5c2ff392009-07-08 16:24:331632 // Ignore responses for requests other than the one we have most recently
1633 // issued. That way we won't act on stale results when the user has
1634 // already typed in another query.
1635 if (request_id != current_find_request_id_)
1636 return;
1637
1638 if (number_of_matches == -1)
1639 number_of_matches = last_search_result_.number_of_matches();
1640 if (active_match_ordinal == -1)
1641 active_match_ordinal = last_search_result_.active_match_ordinal();
1642
1643 gfx::Rect selection = selection_rect;
1644 if (selection.IsEmpty())
1645 selection = last_search_result_.selection_rect();
1646
1647 // Notify the UI, automation and any other observers that a find result was
1648 // found.
1649 last_search_result_ = FindNotificationDetails(request_id, number_of_matches,
1650 selection, active_match_ordinal,
1651 final_update);
1652 NotificationService::current()->Notify(
1653 NotificationType::FIND_RESULT_AVAILABLE,
1654 Source<TabContents>(this),
1655 Details<FindNotificationDetails>(&last_search_result_));
1656}
1657
1658void TabContents::GoToEntryAtOffset(int offset) {
[email protected]f9cc4c452009-10-13 14:56:381659 if (!delegate_ || delegate_->OnGoToEntryOffset(offset))
1660 controller_.GoToOffset(offset);
[email protected]bf5c2ff392009-07-08 16:24:331661}
1662
1663void TabContents::GetHistoryListCount(int* back_list_count,
1664 int* forward_list_count) {
1665 int current_index = controller_.last_committed_entry_index();
1666 *back_list_count = current_index;
1667 *forward_list_count = controller_.entry_count() - current_index - 1;
1668}
1669
1670void TabContents::OnMissingPluginStatus(int status) {
1671#if defined(OS_WIN)
1672// TODO(PORT): pull in when plug-ins work
1673 GetPluginInstaller()->OnMissingPluginStatus(status);
1674#endif
1675}
1676
1677void TabContents::OnCrashedPlugin(const FilePath& plugin_path) {
[email protected]bf5c2ff392009-07-08 16:24:331678 DCHECK(!plugin_path.value().empty());
1679
1680 std::wstring plugin_name = plugin_path.ToWStringHack();
[email protected]d32d46182009-07-23 00:09:351681#if defined(OS_WIN) || defined(OS_MACOSX)
[email protected]bf5c2ff392009-07-08 16:24:331682 scoped_ptr<FileVersionInfo> version_info(
1683 FileVersionInfo::CreateFileVersionInfo(plugin_path));
1684 if (version_info.get()) {
1685 const std::wstring& product_name = version_info->product_name();
[email protected]996fd702009-09-04 19:12:371686 if (!product_name.empty()) {
[email protected]bf5c2ff392009-07-08 16:24:331687 plugin_name = product_name;
[email protected]996fd702009-09-04 19:12:371688#if defined(OS_MACOSX)
1689 // Many plugins on the Mac have .plugin in the actual name, which looks
1690 // terrible, so look for that and strip it off if present.
1691 const std::wstring plugin_extension(L".plugin");
1692 if (EndsWith(plugin_name, plugin_extension, true))
1693 plugin_name.erase(plugin_name.length() - plugin_extension.length());
1694#endif // OS_MACOSX
1695 }
[email protected]bf5c2ff392009-07-08 16:24:331696 }
[email protected]4a560ed2009-07-13 23:35:261697#else
1698 NOTIMPLEMENTED() << " convert plugin path to plugin name";
1699#endif
[email protected]bf5c2ff392009-07-08 16:24:331700 AddInfoBar(new SimpleAlertInfoBarDelegate(
1701 this, l10n_util::GetStringF(IDS_PLUGIN_CRASHED_PROMPT, plugin_name),
1702 NULL));
[email protected]bf5c2ff392009-07-08 16:24:331703}
1704
1705void TabContents::OnCrashedWorker() {
1706 AddInfoBar(new SimpleAlertInfoBarDelegate(
1707 this, l10n_util::GetString(IDS_WEBWORKER_CRASHED_PROMPT),
1708 NULL));
1709}
1710
1711void TabContents::OnDidGetApplicationInfo(
1712 int32 page_id,
1713 const webkit_glue::WebApplicationInfo& info) {
[email protected]ed5431872009-11-17 08:39:511714 web_app_info_ = info;
[email protected]bf5c2ff392009-07-08 16:24:331715
[email protected]ed5431872009-11-17 08:39:511716 if (delegate())
1717 delegate()->OnDidGetApplicationInfo(this, page_id);
[email protected]bf5c2ff392009-07-08 16:24:331718}
1719
1720void TabContents::DidStartProvisionalLoadForFrame(
1721 RenderViewHost* render_view_host,
1722 bool is_main_frame,
1723 const GURL& url) {
1724 ProvisionalLoadDetails details(is_main_frame,
1725 controller_.IsURLInPageNavigation(url),
1726 url, std::string(), false);
1727 NotificationService::current()->Notify(
1728 NotificationType::FRAME_PROVISIONAL_LOAD_START,
1729 Source<NavigationController>(&controller_),
1730 Details<ProvisionalLoadDetails>(&details));
1731}
1732
1733void TabContents::DidStartReceivingResourceResponse(
1734 ResourceRequestDetails* details) {
1735 NotificationService::current()->Notify(
1736 NotificationType::RESOURCE_RESPONSE_STARTED,
1737 Source<NavigationController>(&controller()),
1738 Details<ResourceRequestDetails>(details));
1739}
1740
1741void TabContents::DidRedirectResource(ResourceRequestDetails* details) {
1742 NotificationService::current()->Notify(
1743 NotificationType::RESOURCE_RECEIVED_REDIRECT,
1744 Source<NavigationController>(&controller()),
1745 Details<ResourceRequestDetails>(details));
1746}
1747
1748void TabContents::DidLoadResourceFromMemoryCache(
1749 const GURL& url,
1750 const std::string& frame_origin,
1751 const std::string& main_frame_origin,
1752 const std::string& security_info) {
1753 // Send out a notification that we loaded a resource from our memory cache.
1754 int cert_id = 0, cert_status = 0, security_bits = 0;
1755 SSLManager::DeserializeSecurityInfo(security_info,
1756 &cert_id, &cert_status,
1757 &security_bits);
1758 LoadFromMemoryCacheDetails details(url, frame_origin, main_frame_origin,
[email protected]76543b92009-08-31 17:27:451759 process()->id(), cert_id, cert_status);
[email protected]bf5c2ff392009-07-08 16:24:331760
1761 NotificationService::current()->Notify(
1762 NotificationType::LOAD_FROM_MEMORY_CACHE,
1763 Source<NavigationController>(&controller_),
1764 Details<LoadFromMemoryCacheDetails>(&details));
1765}
1766
[email protected]e3d60e5d2009-09-25 21:08:291767void TabContents::DidDisplayInsecureContent() {
[email protected]09843b5f2009-09-26 08:30:591768 controller_.ssl_manager()->DidDisplayInsecureContent();
[email protected]e3d60e5d2009-09-25 21:08:291769}
1770
1771void TabContents::DidRunInsecureContent(const std::string& security_origin) {
[email protected]09843b5f2009-09-26 08:30:591772 controller_.ssl_manager()->DidRunInsecureContent(security_origin);
[email protected]e3d60e5d2009-09-25 21:08:291773}
1774
[email protected]bf5c2ff392009-07-08 16:24:331775void TabContents::DidFailProvisionalLoadWithError(
1776 RenderViewHost* render_view_host,
1777 bool is_main_frame,
1778 int error_code,
1779 const GURL& url,
1780 bool showing_repost_interstitial) {
1781 if (net::ERR_ABORTED == error_code) {
1782 // EVIL HACK ALERT! Ignore failed loads when we're showing interstitials.
1783 // This means that the interstitial won't be torn down properly, which is
1784 // bad. But if we have an interstitial, go back to another tab type, and
1785 // then load the same interstitial again, we could end up getting the first
1786 // interstitial's "failed" message (as a result of the cancel) when we're on
1787 // the second one.
1788 //
1789 // We can't tell this apart, so we think we're tearing down the current page
1790 // which will cause a crash later one. There is also some code in
1791 // RenderViewHostManager::RendererAbortedProvisionalLoad that is commented
1792 // out because of this problem.
1793 //
1794 // http://code.google.com/p/chromium/issues/detail?id=2855
1795 // Because this will not tear down the interstitial properly, if "back" is
1796 // back to another tab type, the interstitial will still be somewhat alive
1797 // in the previous tab type. If you navigate somewhere that activates the
1798 // tab with the interstitial again, you'll see a flash before the new load
1799 // commits of the interstitial page.
1800 if (showing_interstitial_page()) {
1801 LOG(WARNING) << "Discarding message during interstitial.";
1802 return;
1803 }
1804
1805 // This will discard our pending entry if we cancelled the load (e.g., if we
1806 // decided to download the file instead of load it). Only discard the
1807 // pending entry if the URLs match, otherwise the user initiated a navigate
1808 // before the page loaded so that the discard would discard the wrong entry.
1809 NavigationEntry* pending_entry = controller_.pending_entry();
1810 if (pending_entry && pending_entry->url() == url) {
1811 controller_.DiscardNonCommittedEntries();
1812 // Update the URL display.
1813 NotifyNavigationStateChanged(TabContents::INVALIDATE_URL);
1814 }
1815
1816 render_manager_.RendererAbortedProvisionalLoad(render_view_host);
1817 }
1818
1819 // Send out a notification that we failed a provisional load with an error.
1820 ProvisionalLoadDetails details(is_main_frame,
1821 controller_.IsURLInPageNavigation(url),
1822 url, std::string(), false);
1823 details.set_error_code(error_code);
1824
1825 NotificationService::current()->Notify(
1826 NotificationType::FAIL_PROVISIONAL_LOAD_WITH_ERROR,
1827 Source<NavigationController>(&controller_),
1828 Details<ProvisionalLoadDetails>(&details));
1829}
1830
1831void TabContents::DocumentLoadedInFrame() {
1832 controller_.DocumentLoadedInFrame();
1833}
1834
[email protected]8d3347f2009-07-09 22:00:211835RenderViewHostDelegate::View* TabContents::GetViewDelegate() {
[email protected]420ae012009-04-24 05:16:321836 return view_.get();
1837}
1838
[email protected]8d3347f2009-07-09 22:00:211839RenderViewHostDelegate::RendererManagement*
1840TabContents::GetRendererManagementDelegate() {
1841 return &render_manager_;
1842}
1843
[email protected]bf5c2ff392009-07-08 16:24:331844RenderViewHostDelegate::BrowserIntegration*
[email protected]8d3347f2009-07-09 22:00:211845 TabContents::GetBrowserIntegrationDelegate() {
1846 return this;
[email protected]bf5c2ff392009-07-08 16:24:331847}
1848
[email protected]8d3347f2009-07-09 22:00:211849RenderViewHostDelegate::Resource* TabContents::GetResourceDelegate() {
1850 return this;
[email protected]bf5c2ff392009-07-08 16:24:331851}
1852
[email protected]8d3347f2009-07-09 22:00:211853RenderViewHostDelegate::Save* TabContents::GetSaveDelegate() {
[email protected]420ae012009-04-24 05:16:321854 return save_package_.get(); // May be NULL, but we can return NULL.
1855}
1856
[email protected]8d3347f2009-07-09 22:00:211857RenderViewHostDelegate::Printing* TabContents::GetPrintingDelegate() {
1858 return &printing_;
1859}
1860
1861RenderViewHostDelegate::FavIcon* TabContents::GetFavIconDelegate() {
1862 return &fav_icon_helper_;
1863}
1864
[email protected]c54d269e2009-10-21 18:07:381865RenderViewHostDelegate::FormFieldHistory*
1866TabContents::GetFormFieldHistoryDelegate() {
[email protected]19289d02009-10-21 21:47:001867 if (form_field_history_manager_.get() == NULL)
1868 form_field_history_manager_.reset(new FormFieldHistoryManager(this));
1869 return form_field_history_manager_.get();
[email protected]bf5c2ff392009-07-08 16:24:331870}
1871
[email protected]21674df2009-10-28 19:51:211872RenderViewHostDelegate::AutoFill* TabContents::GetAutoFillDelegate() {
1873 if (autofill_manager_.get() == NULL)
1874 autofill_manager_.reset(new AutoFillManager(this));
1875 return autofill_manager_.get();
1876}
1877
[email protected]80d96fa2009-06-10 22:34:511878RendererPreferences TabContents::GetRendererPrefs() const {
[email protected]840b1512009-07-21 16:53:461879 return renderer_preferences_;
[email protected]80d96fa2009-06-10 22:34:511880}
1881
[email protected]57c6a652009-05-04 07:58:341882TabContents* TabContents::GetAsTabContents() {
1883 return this;
1884}
1885
[email protected]3e7cf1362009-08-21 17:47:081886void TabContents::AddBlockedNotice(const GURL& url, const string16& reason) {
1887 CreateBlockedPopupContainerIfNecessary();
1888 blocked_popups_->AddBlockedNotice(url, reason);
1889}
1890
[email protected]7b291f92009-08-14 05:43:531891ViewType::Type TabContents::GetRenderViewType() const {
1892 return ViewType::TAB_CONTENTS;
1893}
1894
1895int TabContents::GetBrowserWindowID() const {
1896 return controller().window_id().id();
1897}
1898
[email protected]420ae012009-04-24 05:16:321899void TabContents::RenderViewCreated(RenderViewHost* render_view_host) {
[email protected]2e4633c2009-07-09 16:58:061900 NotificationService::current()->Notify(
1901 NotificationType::RENDER_VIEW_HOST_CREATED_FOR_TAB,
1902 Source<TabContents>(this),
1903 Details<RenderViewHost>(render_view_host));
[email protected]420ae012009-04-24 05:16:321904 NavigationEntry* entry = controller_.GetActiveEntry();
1905 if (!entry)
1906 return;
1907
1908 // When we're creating views, we're still doing initial setup, so we always
1909 // use the pending DOM UI rather than any possibly existing committed one.
1910 if (render_manager_.pending_dom_ui()) {
1911 render_manager_.pending_dom_ui()->RenderViewCreated(render_view_host);
1912 }
1913
1914 if (entry->IsViewSourceMode()) {
1915 // Put the renderer in view source mode.
1916 render_view_host->Send(
1917 new ViewMsg_EnableViewSourceMode(render_view_host->routing_id()));
1918 }
[email protected]0666aef2009-05-13 19:48:081919
1920 view()->RenderViewCreated(render_view_host);
[email protected]420ae012009-04-24 05:16:321921}
1922
1923void TabContents::RenderViewReady(RenderViewHost* rvh) {
1924 if (rvh != render_view_host()) {
1925 // Don't notify the world, since this came from a renderer in the
1926 // background.
1927 return;
1928 }
1929
1930 NotifyConnected();
[email protected]9d8a4642009-07-29 17:25:301931 bool was_crashed = is_crashed();
[email protected]420ae012009-04-24 05:16:321932 SetIsCrashed(false);
[email protected]9d8a4642009-07-29 17:25:301933
1934 // Restore the focus to the tab (otherwise the focus will be on the top
1935 // window).
1936 if (was_crashed && !FocusLocationBarByDefault())
1937 Focus();
[email protected]420ae012009-04-24 05:16:321938}
1939
1940void TabContents::RenderViewGone(RenderViewHost* rvh) {
1941 // Ask the print preview if this renderer was valuable.
1942 if (!printing_.OnRenderViewGone(rvh))
1943 return;
1944 if (rvh != render_view_host()) {
1945 // The pending page's RenderViewHost is gone.
1946 return;
1947 }
1948
1949 SetIsLoading(false, NULL);
1950 NotifyDisconnected();
1951 SetIsCrashed(true);
1952
[email protected]8e537302009-06-10 21:19:581953 // Tell the view that we've crashed so it can prepare the sad tab page.
1954 view_->OnTabCrashed();
[email protected]420ae012009-04-24 05:16:321955
1956 // Hide any visible hung renderer warning for this web contents' process.
[email protected]d82ed61e2009-06-16 02:46:221957 HungRendererDialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:321958}
1959
[email protected]2e4633c2009-07-09 16:58:061960void TabContents::RenderViewDeleted(RenderViewHost* rvh) {
1961 NotificationService::current()->Notify(
1962 NotificationType::RENDER_VIEW_HOST_DELETED,
1963 Source<TabContents>(this),
1964 Details<RenderViewHost>(rvh));
[email protected]fb7b79f72009-11-06 18:00:481965 render_manager_.RenderViewDeleted(rvh);
[email protected]2e4633c2009-07-09 16:58:061966}
1967
[email protected]420ae012009-04-24 05:16:321968void TabContents::DidNavigate(RenderViewHost* rvh,
1969 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]8030f012009-09-25 18:09:371970 int extra_invalidate_flags = 0;
1971
1972 if (PageTransition::IsMainFrame(params.transition)) {
[email protected]20ede072009-10-06 00:28:351973 bool was_bookmark_bar_visible = ShouldShowBookmarkBar();
[email protected]8030f012009-09-25 18:09:371974 bool was_extension_shelf_visible = IsExtensionShelfAlwaysVisible();
1975
[email protected]420ae012009-04-24 05:16:321976 render_manager_.DidNavigateMainFrame(rvh);
1977
[email protected]20ede072009-10-06 00:28:351978 if (was_bookmark_bar_visible != ShouldShowBookmarkBar())
[email protected]8030f012009-09-25 18:09:371979 extra_invalidate_flags |= INVALIDATE_BOOKMARK_BAR;
1980 if (was_extension_shelf_visible != IsExtensionShelfAlwaysVisible())
1981 extra_invalidate_flags |= INVALIDATE_EXTENSION_SHELF;
1982 }
1983
[email protected]420ae012009-04-24 05:16:321984 // Update the site of the SiteInstance if it doesn't have one yet.
1985 if (!GetSiteInstance()->has_site())
1986 GetSiteInstance()->SetSite(params.url);
1987
1988 // Need to update MIME type here because it's referred to in
1989 // UpdateNavigationCommands() called by RendererDidNavigate() to
1990 // determine whether or not to enable the encoding menu.
1991 // It's updated only for the main frame. For a subframe,
1992 // RenderView::UpdateURL does not set params.contents_mime_type.
1993 // (see http://code.google.com/p/chromium/issues/detail?id=2929 )
1994 // TODO(jungshik): Add a test for the encoding menu to avoid
1995 // regressing it again.
1996 if (PageTransition::IsMainFrame(params.transition))
1997 contents_mime_type_ = params.contents_mime_type;
1998
1999 NavigationController::LoadCommittedDetails details;
[email protected]8030f012009-09-25 18:09:372000 bool did_navigate = controller_.RendererDidNavigate(
2001 params, extra_invalidate_flags, &details);
[email protected]3e90d4a2009-07-03 17:38:392002
2003 // Update history. Note that this needs to happen after the entry is complete,
2004 // which WillNavigate[Main,Sub]Frame will do before this function is called.
[email protected]2e29d922009-10-22 22:13:352005 if (params.should_update_history &&
2006 (!delegate() || delegate()->ShouldAddNavigationsToHistory())) {
[email protected]3e90d4a2009-07-03 17:38:392007 // Most of the time, the displayURL matches the loaded URL, but for about:
2008 // URLs, we use a data: URL as the real value. We actually want to save
2009 // the about: URL to the history db and keep the data: URL hidden. This is
2010 // what the TabContents' URL getter does.
2011 UpdateHistoryForNavigation(GetURL(), details, params);
2012 }
2013
[email protected]76543b92009-08-31 17:27:452014 if (!did_navigate)
2015 return; // No navigation happened.
[email protected]420ae012009-04-24 05:16:322016
2017 // DO NOT ADD MORE STUFF TO THIS FUNCTION! Your component should either listen
2018 // for the appropriate notification (best) or you can add it to
2019 // DidNavigateMainFramePostCommit / DidNavigateAnyFramePostCommit (only if
2020 // necessary, please).
2021
2022 // Run post-commit tasks.
2023 if (details.is_main_frame)
2024 DidNavigateMainFramePostCommit(details, params);
2025 DidNavigateAnyFramePostCommit(rvh, details, params);
2026}
2027
2028void TabContents::UpdateState(RenderViewHost* rvh,
2029 int32 page_id,
2030 const std::string& state) {
2031 DCHECK(rvh == render_view_host());
2032
2033 // We must be prepared to handle state updates for any page, these occur
2034 // when the user is scrolling and entering form data, as well as when we're
2035 // leaving a page, in which case our state may have already been moved to
2036 // the next page. The navigation controller will look up the appropriate
2037 // NavigationEntry and update it when it is notified via the delegate.
2038
2039 int entry_index = controller_.GetEntryIndexWithPageID(
2040 GetSiteInstance(), page_id);
2041 if (entry_index < 0)
2042 return;
2043 NavigationEntry* entry = controller_.GetEntryAtIndex(entry_index);
2044
2045 if (state == entry->content_state())
2046 return; // Nothing to update.
2047 entry->set_content_state(state);
2048 controller_.NotifyEntryChanged(entry, entry_index);
2049}
2050
2051void TabContents::UpdateTitle(RenderViewHost* rvh,
2052 int32 page_id, const std::wstring& title) {
2053 // If we have a title, that's a pretty good indication that we've started
2054 // getting useful data.
2055 SetNotWaitingForResponse();
2056
2057 DCHECK(rvh == render_view_host());
2058 NavigationEntry* entry = controller_.GetEntryWithPageID(GetSiteInstance(),
2059 page_id);
2060 if (!entry || !UpdateTitleForEntry(entry, title))
2061 return;
2062
2063 // Broadcast notifications when the UI should be updated.
2064 if (entry == controller_.GetEntryAtOffset(0))
[email protected]f1cd5e82009-10-23 17:19:032065 NotifyNavigationStateChanged(INVALIDATE_TITLE);
[email protected]420ae012009-04-24 05:16:322066}
2067
[email protected]420ae012009-04-24 05:16:322068void TabContents::UpdateEncoding(RenderViewHost* render_view_host,
[email protected]41fc0322009-09-04 22:23:402069 const std::string& encoding) {
[email protected]420ae012009-04-24 05:16:322070 set_encoding(encoding);
2071}
2072
2073void TabContents::UpdateTargetURL(int32 page_id, const GURL& url) {
2074 if (delegate())
2075 delegate()->UpdateTargetURL(this, url);
2076}
2077
2078void TabContents::UpdateThumbnail(const GURL& url,
2079 const SkBitmap& bitmap,
2080 const ThumbnailScore& score) {
2081 // Tell History about this thumbnail
[email protected]449478302009-06-09 20:04:282082 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kThumbnailStore)) {
[email protected]72d0fc72009-07-09 17:30:492083 if (!profile()->IsOffTheRecord())
[email protected]42339402009-07-17 17:27:192084 profile()->GetThumbnailStore()->SetPageThumbnail(url, bitmap,
2085 score, true);
[email protected]449478302009-06-09 20:04:282086 } else {
2087 HistoryService* hs;
2088 if (!profile()->IsOffTheRecord() &&
2089 (hs = profile()->GetHistoryService(Profile::IMPLICIT_ACCESS))) {
2090 hs->SetPageThumbnail(url, bitmap, score);
2091 }
[email protected]420ae012009-04-24 05:16:322092 }
2093}
2094
[email protected]8922e1f2009-10-03 05:01:262095void TabContents::UpdateInspectorSettings(const std::string& raw_settings) {
[email protected]0df30122009-06-03 12:13:082096 profile()->GetPrefs()->SetString(prefs::kWebKitInspectorSettings,
[email protected]8922e1f2009-10-03 05:01:262097 UTF8ToWide(raw_settings));
[email protected]0df30122009-06-03 12:13:082098}
2099
[email protected]420ae012009-04-24 05:16:322100void TabContents::Close(RenderViewHost* rvh) {
[email protected]07707302009-11-06 00:50:292101 // The UI may be in an event-tracking loop, such as between the
2102 // mouse-down and mouse-up in text selection or a button click.
2103 // Defer the close until after tracking is complete, so that we
2104 // don't free objects out from under the UI.
2105 // TODO(shess): This could probably be integrated with the
2106 // IsDoingDrag() test below. Punting for now because I need more
2107 // research to understand how this impacts platforms other than Mac.
2108 // TODO(shess): This could get more fine-grained. For instance,
2109 // closing a tab in another window while selecting text in the
2110 // current window's Omnibox should be just fine.
2111 if (view()->IsEventTracking()) {
2112 view()->CloseTabAfterEventTracking();
2113 return;
2114 }
2115
[email protected]24a4d1062009-07-10 23:10:422116 // If we close the tab while we're in the middle of a drag, we'll crash.
2117 // Instead, cancel the drag and close it as soon as the drag ends.
2118 if (view()->IsDoingDrag()) {
2119 view()->CancelDragAndCloseTab();
2120 return;
2121 }
2122
[email protected]420ae012009-04-24 05:16:322123 // Ignore this if it comes from a RenderViewHost that we aren't showing.
2124 if (delegate() && rvh == render_view_host())
2125 delegate()->CloseContents(this);
2126}
2127
2128void TabContents::RequestMove(const gfx::Rect& new_bounds) {
2129 if (delegate() && delegate()->IsPopup(this))
2130 delegate()->MoveContents(this, new_bounds);
2131}
2132
[email protected]7ab1e7d62009-10-14 23:32:012133void TabContents::DidStartLoading() {
[email protected]420ae012009-04-24 05:16:322134 SetIsLoading(true, NULL);
2135}
2136
[email protected]7ab1e7d62009-10-14 23:32:012137void TabContents::DidStopLoading() {
[email protected]420ae012009-04-24 05:16:322138 scoped_ptr<LoadNotificationDetails> details;
2139
2140 NavigationEntry* entry = controller_.GetActiveEntry();
2141 // An entry may not exist for a stop when loading an initial blank page or
2142 // if an iframe injected by script into a blank page finishes loading.
2143 if (entry) {
[email protected]420ae012009-04-24 05:16:322144 base::TimeDelta elapsed = base::TimeTicks::Now() - current_load_start_;
2145
2146 details.reset(new LoadNotificationDetails(
[email protected]ebe89e062009-08-13 23:16:542147 entry->virtual_url(),
[email protected]420ae012009-04-24 05:16:322148 entry->transition_type(),
2149 elapsed,
2150 &controller_,
2151 controller_.GetCurrentEntryIndex()));
2152 }
2153
2154 // Tell PasswordManager we've finished a page load, which serves as a
2155 // green light to save pending passwords and reset itself.
2156 GetPasswordManager()->DidStopLoading();
2157
2158 SetIsLoading(false, details.get());
2159}
2160
[email protected]420ae012009-04-24 05:16:322161void TabContents::DidRedirectProvisionalLoad(int32 page_id,
2162 const GURL& source_url,
2163 const GURL& target_url) {
2164 NavigationEntry* entry;
2165 if (page_id == -1)
2166 entry = controller_.pending_entry();
2167 else
2168 entry = controller_.GetEntryWithPageID(GetSiteInstance(), page_id);
2169 if (!entry || entry->url() != source_url)
2170 return;
2171 entry->set_url(target_url);
2172}
2173
[email protected]420ae012009-04-24 05:16:322174void TabContents::RequestOpenURL(const GURL& url, const GURL& referrer,
2175 WindowOpenDisposition disposition) {
2176 if (render_manager_.dom_ui()) {
2177 // When we're a DOM UI, it will provide a page transition type for us (this
2178 // is so the new tab page can specify AUTO_BOOKMARK for automatically
2179 // generated suggestions).
2180 //
2181 // Note also that we hide the referrer for DOM UI pages. We don't really
[email protected]60e448982009-05-06 04:21:162182 // want web sites to see a referrer of "chrome://blah" (and some
2183 // chrome: URLs might have search terms or other stuff we don't want to
[email protected]420ae012009-04-24 05:16:322184 // send to the site), so we send no referrer.
2185 OpenURL(url, GURL(), disposition,
2186 render_manager_.dom_ui()->link_transition_type());
2187 } else {
2188 OpenURL(url, referrer, disposition, PageTransition::LINK);
2189 }
2190}
2191
[email protected]1ee614862009-07-06 20:29:182192void TabContents::DomOperationResponse(const std::string& json_string,
2193 int automation_id) {
2194}
2195
[email protected]420ae012009-04-24 05:16:322196void TabContents::ProcessDOMUIMessage(const std::string& message,
[email protected]e4dad9fb2009-10-06 18:15:582197 const Value* content,
[email protected]811bfe32009-07-01 08:46:252198 int request_id,
2199 bool has_callback) {
[email protected]420ae012009-04-24 05:16:322200 if (!render_manager_.dom_ui()) {
[email protected]c2e74fe82009-09-03 17:57:442201 // This can happen if someone uses window.open() to open an extension URL
2202 // from a non-extension context.
2203 render_view_host()->BlockExtensionRequest(request_id);
[email protected]420ae012009-04-24 05:16:322204 return;
2205 }
[email protected]811bfe32009-07-01 08:46:252206 render_manager_.dom_ui()->ProcessDOMUIMessage(message, content, request_id,
2207 has_callback);
[email protected]420ae012009-04-24 05:16:322208}
2209
2210void TabContents::ProcessExternalHostMessage(const std::string& message,
2211 const std::string& origin,
2212 const std::string& target) {
2213 if (delegate())
2214 delegate()->ForwardMessageToExternalHost(message, origin, target);
2215}
2216
[email protected]420ae012009-04-24 05:16:322217void TabContents::RunFileChooser(bool multiple_files,
2218 const string16& title,
2219 const FilePath& default_file) {
2220 if (!select_file_dialog_.get())
2221 select_file_dialog_ = SelectFileDialog::Create(this);
2222 SelectFileDialog::Type dialog_type =
2223 multiple_files ? SelectFileDialog::SELECT_OPEN_MULTI_FILE :
2224 SelectFileDialog::SELECT_OPEN_FILE;
2225 select_file_dialog_->SelectFile(dialog_type, title, default_file,
2226 NULL, 0, FILE_PATH_LITERAL(""),
2227 view_->GetTopLevelNativeWindow(), NULL);
2228}
2229
2230void TabContents::RunJavaScriptMessage(
2231 const std::wstring& message,
2232 const std::wstring& default_prompt,
2233 const GURL& frame_url,
2234 const int flags,
2235 IPC::Message* reply_msg,
2236 bool* did_suppress_message) {
2237 // Suppress javascript messages when requested and when inside a constrained
2238 // popup window (because that activates them and breaks them out of the
2239 // constrained window jail).
[email protected]91ed49a2009-07-07 21:29:552240 // Also suppress messages when showing an interstitial. The interstitial is
2241 // shown over the previous page, we don't want the hidden page dialogs to
2242 // interfere with the interstitial.
2243 bool suppress_this_message = suppress_javascript_messages_ ||
2244 showing_interstitial_page();
[email protected]420ae012009-04-24 05:16:322245 if (delegate())
2246 suppress_this_message |=
[email protected]b6c874582009-05-08 19:38:312247 (delegate()->GetConstrainingContents(this) != this);
[email protected]420ae012009-04-24 05:16:322248
2249 *did_suppress_message = suppress_this_message;
2250
2251 if (!suppress_this_message) {
2252 base::TimeDelta time_since_last_message(
2253 base::TimeTicks::Now() - last_javascript_message_dismissal_);
2254 bool show_suppress_checkbox = false;
2255 // Show a checkbox offering to suppress further messages if this message is
2256 // being displayed within kJavascriptMessageExpectedDelay of the last one.
2257 if (time_since_last_message <
2258 base::TimeDelta::FromMilliseconds(kJavascriptMessageExpectedDelay))
2259 show_suppress_checkbox = true;
2260
[email protected]d82ed61e2009-06-16 02:46:222261 RunJavascriptMessageBox(this, frame_url, flags, message, default_prompt,
[email protected]420ae012009-04-24 05:16:322262 show_suppress_checkbox, reply_msg);
2263 } else {
2264 // If we are suppressing messages, just reply as is if the user immediately
2265 // pressed "Cancel".
[email protected]beb440c2009-11-06 04:08:542266 OnMessageBoxClosed(reply_msg, false, std::wstring());
[email protected]420ae012009-04-24 05:16:322267 }
2268}
2269
2270void TabContents::RunBeforeUnloadConfirm(const std::wstring& message,
2271 IPC::Message* reply_msg) {
[email protected]7ab1e7d62009-10-14 23:32:012272 is_showing_before_unload_dialog_ = true;
[email protected]d82ed61e2009-06-16 02:46:222273 RunBeforeUnloadDialog(this, message, reply_msg);
[email protected]420ae012009-04-24 05:16:322274}
2275
2276void TabContents::ShowModalHTMLDialog(const GURL& url, int width, int height,
2277 const std::string& json_arguments,
2278 IPC::Message* reply_msg) {
2279 if (delegate()) {
2280 HtmlDialogUIDelegate* dialog_delegate =
2281 new ModalHtmlDialogDelegate(url, width, height, json_arguments,
[email protected]d82ed61e2009-06-16 02:46:222282 reply_msg, this);
[email protected]420ae012009-04-24 05:16:322283 delegate()->ShowHtmlDialog(dialog_delegate, NULL);
2284 }
2285}
2286
2287void TabContents::PasswordFormsSeen(
[email protected]ed3fb032009-06-16 19:50:562288 const std::vector<webkit_glue::PasswordForm>& forms) {
[email protected]420ae012009-04-24 05:16:322289 GetPasswordManager()->PasswordFormsSeen(forms);
2290}
2291
[email protected]420ae012009-04-24 05:16:322292// Checks to see if we should generate a keyword based on the OSDD, and if
2293// necessary uses TemplateURLFetcher to download the OSDD and create a keyword.
2294void TabContents::PageHasOSDD(RenderViewHost* render_view_host,
2295 int32 page_id, const GURL& url,
2296 bool autodetected) {
2297 // Make sure page_id is the current page, and the TemplateURLModel is loaded.
2298 DCHECK(url.is_valid());
2299 if (!IsActiveEntry(page_id))
2300 return;
2301 TemplateURLModel* url_model = profile()->GetTemplateURLModel();
2302 if (!url_model)
2303 return;
2304 if (!url_model->loaded()) {
2305 url_model->Load();
2306 return;
2307 }
2308 if (!profile()->GetTemplateURLFetcher())
2309 return;
2310
2311 if (profile()->IsOffTheRecord())
2312 return;
2313
2314 const NavigationEntry* entry = controller_.GetLastCommittedEntry();
2315 DCHECK(entry);
2316
2317 const NavigationEntry* base_entry = entry;
2318 if (IsFormSubmit(base_entry)) {
2319 // If the current page is a form submit, find the last page that was not
2320 // a form submit and use its url to generate the keyword from.
2321 int index = controller_.last_committed_entry_index() - 1;
2322 while (index >= 0 && IsFormSubmit(controller_.GetEntryAtIndex(index)))
2323 index--;
2324 if (index >= 0)
2325 base_entry = controller_.GetEntryAtIndex(index);
2326 else
2327 base_entry = NULL;
2328 }
2329
2330 // We want to use the user typed URL if available since that represents what
2331 // the user typed to get here, and fall back on the regular URL if not.
2332 if (!base_entry)
2333 return;
2334 GURL keyword_url = base_entry->user_typed_url().is_valid() ?
2335 base_entry->user_typed_url() : base_entry->url();
2336 if (!keyword_url.is_valid())
2337 return;
2338 std::wstring keyword = TemplateURLModel::GenerateKeyword(keyword_url,
2339 autodetected);
2340 if (keyword.empty())
2341 return;
2342 const TemplateURL* template_url =
2343 url_model->GetTemplateURLForKeyword(keyword);
2344 if (template_url && (!template_url->safe_for_autoreplace() ||
2345 template_url->originating_url() == url)) {
2346 // Either there is a user created TemplateURL for this keyword, or the
2347 // keyword has the same OSDD url and we've parsed it.
2348 return;
2349 }
2350
2351 // Download the OpenSearch description document. If this is successful a
2352 // new keyword will be created when done.
[email protected]420ae012009-04-24 05:16:322353 profile()->GetTemplateURLFetcher()->ScheduleDownload(
2354 keyword,
2355 url,
2356 base_entry->favicon().url(),
[email protected]d0cc9fb52009-06-23 02:26:162357 this,
[email protected]420ae012009-04-24 05:16:322358 autodetected);
2359}
2360
[email protected]420ae012009-04-24 05:16:322361GURL TabContents::GetAlternateErrorPageURL() const {
2362 GURL url;
2363 // Disable alternate error pages when in OffTheRecord/Incognito mode.
2364 if (profile()->IsOffTheRecord())
2365 return url;
2366
2367 PrefService* prefs = profile()->GetPrefs();
2368 DCHECK(prefs);
2369 if (prefs->GetBoolean(prefs::kAlternateErrorPagesEnabled)) {
[email protected]a33457b82009-09-21 23:07:152370 url = google_util::AppendGoogleLocaleParam(
2371 GURL(google_util::kLinkDoctorBaseURL));
[email protected]420ae012009-04-24 05:16:322372 url = google_util::AppendGoogleTLDParam(url);
2373 }
2374 return url;
2375}
2376
2377WebPreferences TabContents::GetWebkitPrefs() {
2378 PrefService* prefs = render_view_host()->process()->profile()->GetPrefs();
[email protected]7ccddb8c2009-08-04 17:36:552379 bool is_dom_ui = false;
2380 return RenderViewHostDelegateHelper::GetWebkitPrefs(prefs, is_dom_ui);
[email protected]420ae012009-04-24 05:16:322381}
2382
[email protected]420ae012009-04-24 05:16:322383void TabContents::OnJSOutOfMemory() {
2384 AddInfoBar(new SimpleAlertInfoBarDelegate(
2385 this, l10n_util::GetString(IDS_JS_OUT_OF_MEMORY_PROMPT), NULL));
2386}
2387
[email protected]420ae012009-04-24 05:16:322388void TabContents::OnCrossSiteResponse(int new_render_process_host_id,
2389 int new_request_id) {
[email protected]57c6a652009-05-04 07:58:342390 // Allows the TabContents to react when a cross-site response is ready to be
[email protected]420ae012009-04-24 05:16:322391 // delivered to a pending RenderViewHost. We must first run the onunload
2392 // handler of the old RenderViewHost before we can allow it to proceed.
2393 render_manager_.OnCrossSiteResponse(new_render_process_host_id,
2394 new_request_id);
2395}
2396
2397bool TabContents::CanBlur() const {
2398 return delegate() ? delegate()->CanBlur() : true;
2399}
2400
2401gfx::Rect TabContents::GetRootWindowResizerRect() const {
2402 if (delegate())
2403 return delegate()->GetRootWindowResizerRect();
2404 return gfx::Rect();
2405}
2406
2407void TabContents::RendererUnresponsive(RenderViewHost* rvh,
2408 bool is_during_unload) {
2409 if (is_during_unload) {
2410 // Hang occurred while firing the beforeunload/unload handler.
2411 // Pretend the handler fired so tab closing continues as if it had.
[email protected]829e7612009-04-25 01:15:112412 rvh->set_sudden_termination_allowed(true);
[email protected]420ae012009-04-24 05:16:322413
2414 if (!render_manager_.ShouldCloseTabOnUnresponsiveRenderer())
2415 return;
2416
2417 // If the tab hangs in the beforeunload/unload handler there's really
2418 // nothing we can do to recover. Pretend the unload listeners have
2419 // all fired and close the tab. If the hang is in the beforeunload handler
2420 // then the user will not have the option of cancelling the close.
2421 Close(rvh);
2422 return;
2423 }
2424
2425 if (render_view_host() && render_view_host()->IsRenderViewLive())
[email protected]d82ed61e2009-06-16 02:46:222426 HungRendererDialog::ShowForTabContents(this);
[email protected]420ae012009-04-24 05:16:322427}
2428
2429void TabContents::RendererResponsive(RenderViewHost* render_view_host) {
[email protected]d82ed61e2009-06-16 02:46:222430 HungRendererDialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:322431}
2432
2433void TabContents::LoadStateChanged(const GURL& url,
[email protected]094e5b22009-09-25 04:23:562434 net::LoadState load_state,
2435 uint64 upload_position,
2436 uint64 upload_size) {
[email protected]420ae012009-04-24 05:16:322437 load_state_ = load_state;
[email protected]094e5b22009-09-25 04:23:562438 upload_position_ = upload_position;
2439 upload_size_ = upload_size;
[email protected]d686e812009-06-03 19:10:292440 std::wstring languages =
2441 profile()->GetPrefs()->GetString(prefs::kAcceptLanguages);
[email protected]d686e812009-06-03 19:10:292442 std::string host = url.host();
[email protected]ce85f602009-11-07 01:34:532443 load_state_host_ =
2444 net::IDNToUnicode(host.c_str(), host.size(), languages, NULL);
[email protected]420ae012009-04-24 05:16:322445 if (load_state_ == net::LOAD_STATE_READING_RESPONSE)
2446 SetNotWaitingForResponse();
2447 if (is_loading())
[email protected]c9cd2222009-05-06 05:16:502448 NotifyNavigationStateChanged(INVALIDATE_LOAD | INVALIDATE_TAB);
[email protected]420ae012009-04-24 05:16:322449}
2450
[email protected]1ee614862009-07-06 20:29:182451bool TabContents::IsExternalTabContainer() const {
2452 if (!delegate())
2453 return false;
2454
2455 return delegate()->IsExternalTabContainer();
2456}
2457
[email protected]1ee614862009-07-06 20:29:182458void TabContents::DidInsertCSS() {
2459 // This RVHDelegate function is used for extensions and not us.
[email protected]420ae012009-04-24 05:16:322460}
2461
[email protected]130efb02009-09-18 18:54:352462void TabContents::FocusedNodeChanged() {
2463 NotificationService::current()->Notify(
2464 NotificationType::FOCUS_CHANGED_IN_PAGE,
2465 Source<RenderViewHost>(render_view_host()),
2466 NotificationService::NoDetails());
2467}
2468
[email protected]420ae012009-04-24 05:16:322469void TabContents::FileSelected(const FilePath& path,
2470 int index, void* params) {
2471 render_view_host()->FileSelected(path);
2472}
2473
2474void TabContents::MultiFilesSelected(const std::vector<FilePath>& files,
2475 void* params) {
2476 render_view_host()->MultiFilesSelected(files);
2477}
2478
2479void TabContents::FileSelectionCanceled(void* params) {
2480 // If the user cancels choosing a file to upload we pass back an
2481 // empty vector.
2482 render_view_host()->MultiFilesSelected(std::vector<FilePath>());
2483}
2484
2485void TabContents::BeforeUnloadFiredFromRenderManager(
2486 bool proceed,
2487 bool* proceed_to_fire_unload) {
2488 if (delegate())
2489 delegate()->BeforeUnloadFired(this, proceed, proceed_to_fire_unload);
2490}
2491
2492void TabContents::UpdateRenderViewSizeForRenderManager() {
2493 // TODO(brettw) this is a hack. See TabContentsView::SizeContents.
2494 view_->SizeContents(view_->GetContainerSize());
2495}
2496
2497DOMUI* TabContents::CreateDOMUIForRenderManager(const GURL& url) {
[email protected]d82ed61e2009-06-16 02:46:222498 return DOMUIFactory::CreateDOMUIForURL(this, url);
[email protected]420ae012009-04-24 05:16:322499}
2500
2501NavigationEntry*
2502TabContents::GetLastCommittedNavigationEntryForRenderManager() {
2503 return controller_.GetLastCommittedEntry();
2504}
2505
2506bool TabContents::CreateRenderViewForRenderManager(
2507 RenderViewHost* render_view_host) {
[email protected]811bfe32009-07-01 08:46:252508 // If the pending navigation is to a DOMUI, tell the RenderView about any
2509 // bindings it will need enabled.
[email protected]420ae012009-04-24 05:16:322510 if (render_manager_.pending_dom_ui())
[email protected]811bfe32009-07-01 08:46:252511 render_view_host->AllowBindings(
2512 render_manager_.pending_dom_ui()->bindings());
[email protected]1adff062009-06-02 18:39:552513
[email protected]420ae012009-04-24 05:16:322514 RenderWidgetHostView* rwh_view = view_->CreateViewForWidget(render_view_host);
[email protected]1a98a932009-11-17 00:12:522515
2516 scoped_refptr<URLRequestContextGetter> request_context = request_context_;
2517 if (!request_context.get())
2518 request_context = profile()->GetRequestContext();
2519
2520 if (!render_view_host->CreateRenderView(request_context))
[email protected]420ae012009-04-24 05:16:322521 return false;
2522
2523 // Now that the RenderView has been created, we need to tell it its size.
2524 rwh_view->SetSize(view_->GetContainerSize());
2525
2526 UpdateMaxPageIDIfNecessary(render_view_host->site_instance(),
2527 render_view_host);
2528 return true;
2529}
2530
2531void TabContents::Observe(NotificationType type,
2532 const NotificationSource& source,
2533 const NotificationDetails& details) {
2534 switch (type.value) {
2535 case NotificationType::BOOKMARK_MODEL_LOADED:
2536 // BookmarkModel finished loading, fall through to update starred state.
2537 case NotificationType::URLS_STARRED: {
2538 // Somewhere, a URL has been starred.
2539 // Ignore notifications for profiles other than our current one.
2540 Profile* source_profile = Source<Profile>(source).ptr();
2541 if (!source_profile || !source_profile->IsSameProfile(profile()))
2542 return;
2543
2544 UpdateStarredStateForCurrentURL();
2545 break;
2546 }
2547 case NotificationType::PREF_CHANGED: {
2548 std::wstring* pref_name_in = Details<std::wstring>(details).ptr();
2549 DCHECK(Source<PrefService>(source).ptr() == profile()->GetPrefs());
2550 if (*pref_name_in == prefs::kAlternateErrorPagesEnabled) {
2551 UpdateAlternateErrorPageURL();
2552 } else if (*pref_name_in == prefs::kDefaultCharset ||
2553 StartsWithASCII(WideToUTF8(*pref_name_in), "webkit.webprefs.", true)
2554 ) {
2555 UpdateWebPreferences();
2556 } else {
2557 NOTREACHED() << "unexpected pref change notification" << *pref_name_in;
2558 }
2559 break;
2560 }
2561 case NotificationType::RENDER_WIDGET_HOST_DESTROYED:
2562 view_->RenderWidgetHostDestroyed(Source<RenderWidgetHost>(source).ptr());
2563 break;
2564
2565 case NotificationType::NAV_ENTRY_COMMITTED: {
2566 DCHECK(&controller_ == Source<NavigationController>(source).ptr());
2567
2568 NavigationController::LoadCommittedDetails& committed_details =
2569 *(Details<NavigationController::LoadCommittedDetails>(details).ptr());
2570 ExpireInfoBars(committed_details);
2571 break;
2572 }
2573
[email protected]d3ba77272009-09-03 00:06:092574#if defined(OS_LINUX)
2575 case NotificationType::BROWSER_THEME_CHANGED: {
2576 render_view_host()->SyncRendererPrefs();
2577 break;
2578 }
2579#endif
2580
[email protected]420ae012009-04-24 05:16:322581 default:
2582 NOTREACHED();
2583 }
2584}
[email protected]aacd7cc2009-09-02 14:51:102585
[email protected]beb440c2009-11-06 04:08:542586std::wstring TabContents::GetMessageBoxTitle(const GURL& frame_url,
2587 bool is_alert) {
2588 if (!frame_url.has_host())
2589 return l10n_util::GetString(
2590 is_alert ? IDS_JAVASCRIPT_ALERT_DEFAULT_TITLE
2591 : IDS_JAVASCRIPT_MESSAGEBOX_DEFAULT_TITLE);
2592
2593 // We really only want the scheme, hostname, and port.
2594 GURL::Replacements replacements;
2595 replacements.ClearUsername();
2596 replacements.ClearPassword();
2597 replacements.ClearPath();
2598 replacements.ClearQuery();
2599 replacements.ClearRef();
2600 GURL clean_url = frame_url.ReplaceComponents(replacements);
2601
2602 // TODO(brettw) it should be easier than this to do the correct language
2603 // handling without getting the accept language from the profile.
2604 std::wstring base_address = gfx::ElideUrl(clean_url, gfx::Font(), 0,
2605 profile()->GetPrefs()->GetString(prefs::kAcceptLanguages));
2606 // Force URL to have LTR directionality.
2607 if (l10n_util::GetTextDirection() == l10n_util::RIGHT_TO_LEFT)
2608 l10n_util::WrapStringWithLTRFormatting(&base_address);
2609
2610 return l10n_util::GetStringF(
2611 is_alert ? IDS_JAVASCRIPT_ALERT_TITLE : IDS_JAVASCRIPT_MESSAGEBOX_TITLE,
2612 base_address);
2613}
2614
2615gfx::NativeWindow TabContents::GetMessageBoxRootWindow() {
2616 return view_->GetTopLevelNativeWindow();
2617}
2618
2619void TabContents::OnMessageBoxClosed(IPC::Message* reply_msg,
2620 bool success,
2621 const std::wstring& prompt) {
2622 last_javascript_message_dismissal_ = base::TimeTicks::Now();
2623 if (is_showing_before_unload_dialog_ && !success) {
2624 // If a beforeunload dialog is canceled, we need to stop the throbber from
2625 // spinning, since we forced it to start spinning in Navigate.
2626 DidStopLoading();
2627
2628 tab_close_start_time_ = base::TimeTicks();
2629 }
2630 is_showing_before_unload_dialog_ = false;
2631 render_view_host()->JavaScriptMessageBoxClosed(reply_msg, success, prompt);
2632}
2633
2634void TabContents::SetSuppressMessageBoxes(bool suppress_message_boxes) {
2635 set_suppress_javascript_messages(suppress_message_boxes);
2636}
2637
[email protected]41fc0322009-09-04 22:23:402638void TabContents::set_encoding(const std::string& encoding) {
[email protected]aacd7cc2009-09-02 14:51:102639 encoding_ = CharacterEncoding::GetCanonicalEncodingNameByAliasName(encoding);
2640}