blob: 3a34459073159826054ea57dbf87340b99694495 [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/gears_integration.h"
33#include "chrome/browser/google_util.h"
[email protected]8897c382009-05-06 17:53:2634#include "chrome/browser/hung_renderer_dialog.h"
[email protected]420ae012009-04-24 05:16:3235#include "chrome/browser/jsmessage_box_handler.h"
36#include "chrome/browser/load_from_memory_cache_details.h"
37#include "chrome/browser/load_notification_details.h"
[email protected]84abba62009-10-07 17:01:4438#include "chrome/browser/metrics/metric_event_duration_details.h"
[email protected]ea0e80892009-08-22 00:36:0539#include "chrome/browser/modal_html_dialog_delegate.h"
[email protected]c8865482009-07-23 20:40:1040#include "chrome/browser/omnibox_search_hint.h"
[email protected]fdd61c62009-04-22 19:22:5741#include "chrome/browser/password_manager/password_manager.h"
42#include "chrome/browser/plugin_installer.h"
[email protected]ce560f82009-06-03 09:39:4443#include "chrome/browser/profile.h"
[email protected]1db6ff152009-10-12 15:32:0744#include "chrome/browser/renderer_host/render_process_host.h"
[email protected]96d185d2009-04-24 03:28:5445#include "chrome/browser/renderer_host/render_widget_host_view.h"
[email protected]fd694982009-07-03 17:32:3746#include "chrome/browser/renderer_host/resource_request_details.h"
[email protected]1db6ff152009-10-12 15:32:0747#include "chrome/browser/renderer_host/site_instance.h"
[email protected]96d185d2009-04-24 03:28:5448#include "chrome/browser/renderer_host/web_cache_manager.h"
[email protected]bd1ad682009-05-15 22:19:1749#include "chrome/browser/tab_contents/infobar_delegate.h"
[email protected]7e383692009-06-12 19:14:5450#include "chrome/browser/tab_contents/interstitial_page.h"
[email protected]f3ec7742009-01-15 00:59:1651#include "chrome/browser/tab_contents/navigation_entry.h"
[email protected]de34a962009-06-26 23:22:4352#include "chrome/browser/tab_contents/provisional_load_details.h"
[email protected]f3ec7742009-01-15 00:59:1653#include "chrome/browser/tab_contents/tab_contents_delegate.h"
[email protected]fdd61c62009-04-22 19:22:5754#include "chrome/browser/tab_contents/tab_contents_view.h"
[email protected]58dca552009-06-17 00:35:0255#include "chrome/browser/tab_contents/thumbnail_generator.h"
[email protected]449478302009-06-09 20:04:2856#include "chrome/browser/thumbnail_store.h"
[email protected]420ae012009-04-24 05:16:3257#include "chrome/browser/search_engines/template_url_fetcher.h"
58#include "chrome/browser/search_engines/template_url_model.h"
[email protected]35f7d212009-04-29 21:19:2759#include "chrome/common/chrome_switches.h"
[email protected]5d246db22009-10-27 06:17:5760#include "chrome/common/extensions/extension_action.h"
[email protected]bfd04a62009-02-01 18:16:5661#include "chrome/common/notification_service.h"
initial.commit09911bf2008-07-26 23:55:2962#include "chrome/common/pref_names.h"
[email protected]1eb89e82008-08-15 12:27:0363#include "chrome/common/pref_service.h"
[email protected]420ae012009-04-24 05:16:3264#include "chrome/common/render_messages.h"
[email protected]1ee614862009-07-06 20:29:1865#include "chrome/common/renderer_preferences.h"
[email protected]96d185d2009-04-24 03:28:5466#include "chrome/common/url_constants.h"
[email protected]8897c382009-05-06 17:53:2667#include "grit/generated_resources.h"
[email protected]420ae012009-04-24 05:16:3268#include "grit/locale_settings.h"
69#include "net/base/mime_util.h"
70#include "net/base/net_errors.h"
[email protected]d686e812009-06-03 19:10:2971#include "net/base/net_util.h"
[email protected]420ae012009-04-24 05:16:3272#include "net/base/registry_controlled_domain.h"
[email protected]1ee614862009-07-06 20:29:1873#include "webkit/glue/password_form.h"
74#include "webkit/glue/webpreferences.h"
[email protected]3e45ba92009-02-20 21:09:0075
[email protected]304f8792009-08-19 21:28:0976#if defined(OS_CHROMEOS)
77// For GdkScreen
78#include <gdk/gdk.h>
[email protected]0ba5c5e2009-08-20 16:47:1379#endif // defined(OS_CHROMEOS)
80
81#if defined(OS_LINUX)
82#include "chrome/browser/gtk/create_application_shortcuts_dialog_gtk.h"
[email protected]d3ba77272009-09-03 00:06:0983#include "chrome/browser/gtk/gtk_theme_provider.h"
[email protected]0ba5c5e2009-08-20 16:47:1384#endif // defined(OS_LINUX)
85
[email protected]420ae012009-04-24 05:16:3286// Cross-Site Navigations
87//
88// If a TabContents is told to navigate to a different web site (as determined
89// by SiteInstance), it will replace its current RenderViewHost with a new
90// RenderViewHost dedicated to the new SiteInstance. This works as follows:
91//
92// - Navigate determines whether the destination is cross-site, and if so,
93// it creates a pending_render_view_host_ and moves into the PENDING
94// RendererState.
95// - The pending RVH is "suspended," so that no navigation messages are sent to
96// its renderer until the onbeforeunload JavaScript handler has a chance to
97// run in the current RVH.
98// - The pending RVH tells CrossSiteRequestManager (a thread-safe singleton)
99// that it has a pending cross-site request. ResourceDispatcherHost will
100// check for this when the response arrives.
101// - The current RVH runs its onbeforeunload handler. If it returns false, we
102// cancel all the pending logic and go back to NORMAL. Otherwise we allow
103// the pending RVH to send the navigation request to its renderer.
104// - ResourceDispatcherHost receives a ResourceRequest on the IO thread. It
105// checks CrossSiteRequestManager to see that the RVH responsible has a
106// pending cross-site request, and then installs a CrossSiteEventHandler.
107// - When RDH receives a response, the BufferedEventHandler determines whether
108// it is a download. If so, it sends a message to the new renderer causing
109// it to cancel the request, and the download proceeds in the download
110// thread. For now, we stay in a PENDING state (with a pending RVH) until
[email protected]57c6a652009-05-04 07:58:34111// the next DidNavigate event for this TabContents. This isn't ideal, but it
[email protected]420ae012009-04-24 05:16:32112// doesn't affect any functionality.
113// - After RDH receives a response and determines that it is safe and not a
114// download, it pauses the response to first run the old page's onunload
115// handler. It does this by asynchronously calling the OnCrossSiteResponse
[email protected]57c6a652009-05-04 07:58:34116// method of TabContents on the UI thread, which sends a ClosePage message
[email protected]420ae012009-04-24 05:16:32117// to the current RVH.
118// - Once the onunload handler is finished, a ClosePage_ACK message is sent to
119// the ResourceDispatcherHost, who unpauses the response. Data is then sent
120// to the pending RVH.
121// - The pending renderer sends a FrameNavigate message that invokes the
122// DidNavigate method. This replaces the current RVH with the
123// pending RVH and goes back to the NORMAL RendererState.
124
125namespace {
126
127// Amount of time we wait between when a key event is received and the renderer
128// is queried for its state and pushed to the NavigationEntry.
129const int kQueryStateDelay = 5000;
130
131const int kSyncWaitDelay = 40;
132
[email protected]304f8792009-08-19 21:28:09133#if defined(OS_CHROMEOS)
134// If a popup window is bigger than this fraction of the screen on chrome os,
135// turn it into a tab
136const float kMaxWidthFactor = 0.5;
137const float kMaxHeightFactor = 0.6;
138#endif
139
[email protected]420ae012009-04-24 05:16:32140// If another javascript message box is displayed within
141// kJavascriptMessageExpectedDelay of a previous javascript message box being
142// dismissed, display an option to suppress future message boxes from this
143// contents.
144const int kJavascriptMessageExpectedDelay = 1000;
145
[email protected]420ae012009-04-24 05:16:32146// The list of prefs we want to observe.
147const wchar_t* kPrefsToObserve[] = {
148 prefs::kAlternateErrorPagesEnabled,
149 prefs::kWebKitJavaEnabled,
150 prefs::kWebKitJavascriptEnabled,
151 prefs::kWebKitLoadsImagesAutomatically,
152 prefs::kWebKitPluginsEnabled,
153 prefs::kWebKitUsesUniversalDetector,
154 prefs::kWebKitSerifFontFamily,
155 prefs::kWebKitSansSerifFontFamily,
156 prefs::kWebKitFixedFontFamily,
157 prefs::kWebKitDefaultFontSize,
158 prefs::kWebKitDefaultFixedFontSize,
159 prefs::kDefaultCharset
160 // kWebKitStandardFontIsSerif needs to be added
161 // if we let users pick which font to use, serif or sans-serif when
162 // no font is specified or a CSS generic family (serif or sans-serif)
163 // is not specified.
164};
165
166const int kPrefsToObserveLength = arraysize(kPrefsToObserve);
167
[email protected]420ae012009-04-24 05:16:32168// Returns true if the entry's transition type is FORM_SUBMIT.
169bool IsFormSubmit(const NavigationEntry* entry) {
170 return (PageTransition::StripQualifier(entry->transition_type()) ==
171 PageTransition::FORM_SUBMIT);
172}
173
174#if defined(OS_WIN)
175
176BOOL CALLBACK InvalidateWindow(HWND hwnd, LPARAM lparam) {
177 // Note: erase is required to properly paint some widgets borders. This can
178 // be seen with textfields.
179 InvalidateRect(hwnd, NULL, TRUE);
180 return TRUE;
181}
182#endif
183
[email protected]5e369672009-11-03 23:48:30184ViewMsg_Navigate_Params::NavigationType GetNavigationType(
185 Profile* profile, const NavigationEntry& entry, bool reload) {
186 if (reload)
187 return ViewMsg_Navigate_Params::RELOAD;
188
189 if (entry.restore_type() == NavigationEntry::RESTORE_LAST_SESSION &&
190 profile->DidLastSessionExitCleanly())
191 return ViewMsg_Navigate_Params::RESTORE;
192
193 return ViewMsg_Navigate_Params::NORMAL;
194}
195
196void MakeNavigateParams(Profile* profile, const NavigationEntry& entry,
197 bool reload, ViewMsg_Navigate_Params* params) {
[email protected]056de2d2009-06-26 16:41:34198 params->page_id = entry.page_id();
199 params->url = entry.url();
200 params->referrer = entry.referrer();
201 params->transition = entry.transition_type();
202 params->state = entry.content_state();
[email protected]5e369672009-11-03 23:48:30203 params->navigation_type = GetNavigationType(profile, entry, reload);
[email protected]056de2d2009-06-26 16:41:34204 params->request_time = base::Time::Now();
205}
206
[email protected]420ae012009-04-24 05:16:32207} // namespace
208
209// -----------------------------------------------------------------------------
210
[email protected]fdd61c62009-04-22 19:22:57211// static
212int TabContents::find_request_id_counter_ = -1;
213
[email protected]420ae012009-04-24 05:16:32214class TabContents::GearsCreateShortcutCallbackFunctor {
215 public:
216 explicit GearsCreateShortcutCallbackFunctor(TabContents* contents)
217 : contents_(contents) {}
218
219 void Run(const GearsShortcutData2& shortcut_data, bool success) {
220 if (contents_)
221 contents_->OnGearsCreateShortcutDone(shortcut_data, success);
222 delete this;
223 }
224 void Cancel() {
225 contents_ = NULL;
226 }
227
228 private:
229 TabContents* contents_;
230};
231
[email protected]420ae012009-04-24 05:16:32232TabContents::TabContents(Profile* profile,
233 SiteInstance* site_instance,
234 int routing_id,
[email protected]34ac70502009-09-25 17:07:23235 const TabContents* base_tab_contents)
[email protected]b680ad22009-04-15 23:19:42236 : delegate_(NULL),
[email protected]66ba4932009-06-04 19:22:13237 ALLOW_THIS_IN_INITIALIZER_LIST(controller_(this, profile)),
238 ALLOW_THIS_IN_INITIALIZER_LIST(view_(
[email protected]d82ed61e2009-06-16 02:46:22239 TabContentsView::Create(this))),
240 ALLOW_THIS_IN_INITIALIZER_LIST(render_manager_(this, this)),
[email protected]fdd61c62009-04-22 19:22:57241 property_bag_(),
242 registrar_(),
[email protected]d82ed61e2009-06-16 02:46:22243 ALLOW_THIS_IN_INITIALIZER_LIST(printing_(*this)),
[email protected]fdd61c62009-04-22 19:22:57244 save_package_(),
245 cancelable_consumer_(),
[email protected]19289d02009-10-21 21:47:00246 form_field_history_manager_(),
[email protected]21674df2009-10-28 19:51:21247 autofill_manager_(),
[email protected]fdd61c62009-04-22 19:22:57248 password_manager_(),
249 plugin_installer_(),
[email protected]d82ed61e2009-06-16 02:46:22250 ALLOW_THIS_IN_INITIALIZER_LIST(fav_icon_helper_(this)),
[email protected]fdd61c62009-04-22 19:22:57251 select_file_dialog_(),
252 pending_install_(),
[email protected]d5f942ba2008-09-26 19:30:34253 is_loading_(false),
initial.commit09911bf2008-07-26 23:55:29254 is_crashed_(false),
[email protected]d5f942ba2008-09-26 19:30:34255 waiting_for_response_(false),
[email protected]d5f942ba2008-09-26 19:30:34256 max_page_id_(-1),
[email protected]fdd61c62009-04-22 19:22:57257 current_load_start_(),
258 load_state_(net::LOAD_STATE_IDLE),
259 load_state_host_(),
[email protected]094e5b22009-09-25 04:23:56260 upload_size_(0),
261 upload_position_(0),
[email protected]fdd61c62009-04-22 19:22:57262 received_page_title_(false),
263 is_starred_(false),
264 contents_mime_type_(),
265 encoding_(),
[email protected]332af7732009-03-11 13:21:35266 blocked_popups_(NULL),
[email protected]fdd61c62009-04-22 19:22:57267 infobar_delegates_(),
[email protected]fdd61c62009-04-22 19:22:57268 find_ui_active_(false),
269 find_op_aborted_(false),
270 current_find_request_id_(find_request_id_counter_++),
271 find_text_(),
[email protected]e491f1c2009-05-22 20:28:12272 last_search_case_sensitive_(false),
273 last_search_prepopulate_text_(NULL),
274 last_search_result_(),
[email protected]fdd61c62009-04-22 19:22:57275 capturing_contents_(false),
276 is_being_destroyed_(false),
277 notify_disconnection_(false),
278 history_requests_(),
279#if defined(OS_WIN)
280 message_box_active_(CreateEvent(NULL, TRUE, FALSE, NULL)),
281#endif
282 last_javascript_message_dismissal_(),
[email protected]c2e74fe82009-09-03 17:57:44283 suppress_javascript_messages_(false),
[email protected]7ab1e7d62009-10-14 23:32:01284 is_showing_before_unload_dialog_(false),
[email protected]c2e74fe82009-09-03 17:57:44285 opener_dom_ui_type_(DOMUIFactory::kNoDOMUI) {
[email protected]420ae012009-04-24 05:16:32286 pending_install_.page_id = 0;
287 pending_install_.callback_functor = NULL;
288
[email protected]16779842009-07-08 23:45:29289#if defined(OS_CHROMEOS)
[email protected]58dca552009-06-17 00:35:02290 // Make sure the thumbnailer is started before starting the render manager.
291 // The thumbnailer will want to listen for RVH creations, one of which will
292 // happen in RVHManager::Init.
293 ThumbnailGenerator* generator = g_browser_process->GetThumbnailGenerator();
294 if (generator)
295 generator->StartThumbnailing();
296#endif
297
[email protected]12636df2009-09-28 22:32:21298 render_manager_.Init(profile, site_instance, routing_id);
[email protected]420ae012009-04-24 05:16:32299
[email protected]34ac70502009-09-25 17:07:23300 // We have the initial size of the view be based on the size of the passed in
301 // tab contents (normally a tab from the same window).
302 view_->CreateView(base_tab_contents ?
303 base_tab_contents->view()->GetContainerSize() : gfx::Size());
[email protected]420ae012009-04-24 05:16:32304
305 // Register for notifications about all interested prefs change.
306 PrefService* prefs = profile->GetPrefs();
307 if (prefs) {
308 for (int i = 0; i < kPrefsToObserveLength; ++i)
309 prefs->AddPrefObserver(kPrefsToObserve[i], this);
310 }
311
312 // Register for notifications about URL starredness changing on any profile.
313 registrar_.Add(this, NotificationType::URLS_STARRED,
314 NotificationService::AllSources());
315 registrar_.Add(this, NotificationType::BOOKMARK_MODEL_LOADED,
316 NotificationService::AllSources());
317 registrar_.Add(this, NotificationType::RENDER_WIDGET_HOST_DESTROYED,
318 NotificationService::AllSources());
[email protected]d3ba77272009-09-03 00:06:09319#if defined(OS_LINUX)
320 registrar_.Add(this, NotificationType::BROWSER_THEME_CHANGED,
321 NotificationService::AllSources());
322#endif
[email protected]420ae012009-04-24 05:16:32323
324 // Keep a global copy of the previous search string (if any).
325 static string16 global_last_search = string16();
[email protected]e491f1c2009-05-22 20:28:12326 last_search_prepopulate_text_ = &global_last_search;
[email protected]840b1512009-07-21 16:53:46327
[email protected]c8865482009-07-23 20:40:10328 // Set-up the showing of the omnibox search infobar if applicable.
329 if (OmniboxSearchHint::IsEnabled(profile))
330 omnibox_search_hint_.reset(new OmniboxSearchHint(this));
331
[email protected]840b1512009-07-21 16:53:46332 view_->InitRendererPrefs(&renderer_preferences_);
[email protected]332af7732009-03-11 13:21:35333}
initial.commit09911bf2008-07-26 23:55:29334
335TabContents::~TabContents() {
[email protected]420ae012009-04-24 05:16:32336 is_being_destroyed_ = true;
337
[email protected]bfe4c1582009-10-02 18:11:09338 // We don't want any notifications while we're running our destructor.
[email protected]420ae012009-04-24 05:16:32339 registrar_.RemoveAll();
340
341 // Unregister the notifications of all observed prefs change.
342 PrefService* prefs = profile()->GetPrefs();
343 if (prefs) {
344 for (int i = 0; i < kPrefsToObserveLength; ++i)
345 prefs->RemovePrefObserver(kPrefsToObserve[i], this);
346 }
347
[email protected]420ae012009-04-24 05:16:32348 NotifyDisconnected();
[email protected]d82ed61e2009-06-16 02:46:22349 HungRendererDialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:32350
351 if (pending_install_.callback_functor)
352 pending_install_.callback_functor->Cancel();
353
354 // First cleanly close all child windows.
355 // TODO(mpcomplete): handle case if MaybeCloseChildWindows() already asked
356 // some of these to close. CloseWindows is async, so it might get called
357 // twice before it runs.
358 int size = static_cast<int>(child_windows_.size());
359 for (int i = size - 1; i >= 0; --i) {
360 ConstrainedWindow* window = child_windows_[i];
361 if (window)
362 window->CloseConstrainedWindow();
363 }
364
[email protected]66ba4932009-06-04 19:22:13365 if (blocked_popups_)
366 blocked_popups_->Destroy();
367
[email protected]420ae012009-04-24 05:16:32368 // Notify any observer that have a reference on this tab contents.
369 NotificationService::current()->Notify(
370 NotificationType::TAB_CONTENTS_DESTROYED,
371 Source<TabContents>(this),
372 NotificationService::NoDetails());
373
[email protected]1e5e74f2009-05-27 20:55:12374 // Notify any lasting InfobarDelegates that have not yet been removed that
375 // whatever infobar they were handling in this TabContents has closed,
376 // because the TabContents is going away entirely.
377 // This must happen after the TAB_CONTENTS_DESTROYED notification as the
378 // notification may trigger infobars calls that access their delegate. (and
379 // some implementations of InfoBarDelegate do delete themselves on
380 // InfoBarClosed()).
381 for (int i = 0; i < infobar_delegate_count(); ++i) {
382 InfoBarDelegate* delegate = GetInfoBarDelegateAt(i);
383 delegate->InfoBarClosed();
384 }
385 infobar_delegates_.clear();
386
[email protected]420ae012009-04-24 05:16:32387 // TODO(brettw) this should be moved to the view.
388#if defined(OS_WIN)
389 // If we still have a window handle, destroy it. GetNativeView can return
390 // NULL if this contents was part of a window that closed.
391 if (GetNativeView())
392 ::DestroyWindow(GetNativeView());
393#endif
[email protected]7ab1e7d62009-10-14 23:32:01394
395 // OnCloseStarted isn't called in unit tests.
396 if (!tab_close_start_time_.is_null()) {
397 UMA_HISTOGRAM_TIMES("Tab.Close",
398 base::TimeTicks::Now() - tab_close_start_time_);
399 }
initial.commit09911bf2008-07-26 23:55:29400}
401
[email protected]d5f942ba2008-09-26 19:30:34402// static
403void TabContents::RegisterUserPrefs(PrefService* prefs) {
[email protected]420ae012009-04-24 05:16:32404 prefs->RegisterBooleanPref(prefs::kAlternateErrorPagesEnabled, true);
405
406 WebPreferences pref_defaults;
407 prefs->RegisterBooleanPref(prefs::kWebKitJavascriptEnabled,
408 pref_defaults.javascript_enabled);
409 prefs->RegisterBooleanPref(prefs::kWebKitWebSecurityEnabled,
410 pref_defaults.web_security_enabled);
411 prefs->RegisterBooleanPref(
412 prefs::kWebKitJavascriptCanOpenWindowsAutomatically, true);
413 prefs->RegisterBooleanPref(prefs::kWebKitLoadsImagesAutomatically,
414 pref_defaults.loads_images_automatically);
415 prefs->RegisterBooleanPref(prefs::kWebKitPluginsEnabled,
416 pref_defaults.plugins_enabled);
417 prefs->RegisterBooleanPref(prefs::kWebKitDomPasteEnabled,
418 pref_defaults.dom_paste_enabled);
419 prefs->RegisterBooleanPref(prefs::kWebKitShrinksStandaloneImagesToFit,
420 pref_defaults.shrinks_standalone_images_to_fit);
[email protected]0df30122009-06-03 12:13:08421 prefs->RegisterStringPref(prefs::kWebKitInspectorSettings,
[email protected]8922e1f2009-10-03 05:01:26422 UTF8ToWide(pref_defaults.inspector_settings));
[email protected]420ae012009-04-24 05:16:32423 prefs->RegisterBooleanPref(prefs::kWebKitTextAreasAreResizable,
424 pref_defaults.text_areas_are_resizable);
425 prefs->RegisterBooleanPref(prefs::kWebKitJavaEnabled,
426 pref_defaults.java_enabled);
427
428 prefs->RegisterLocalizedStringPref(prefs::kAcceptLanguages,
429 IDS_ACCEPT_LANGUAGES);
430 prefs->RegisterLocalizedStringPref(prefs::kDefaultCharset,
431 IDS_DEFAULT_ENCODING);
432 prefs->RegisterLocalizedBooleanPref(prefs::kWebKitStandardFontIsSerif,
433 IDS_STANDARD_FONT_IS_SERIF);
434 prefs->RegisterLocalizedStringPref(prefs::kWebKitFixedFontFamily,
435 IDS_FIXED_FONT_FAMILY);
436 prefs->RegisterLocalizedStringPref(prefs::kWebKitSerifFontFamily,
437 IDS_SERIF_FONT_FAMILY);
438 prefs->RegisterLocalizedStringPref(prefs::kWebKitSansSerifFontFamily,
439 IDS_SANS_SERIF_FONT_FAMILY);
440 prefs->RegisterLocalizedStringPref(prefs::kWebKitCursiveFontFamily,
441 IDS_CURSIVE_FONT_FAMILY);
442 prefs->RegisterLocalizedStringPref(prefs::kWebKitFantasyFontFamily,
443 IDS_FANTASY_FONT_FAMILY);
444 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitDefaultFontSize,
445 IDS_DEFAULT_FONT_SIZE);
446 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitDefaultFixedFontSize,
447 IDS_DEFAULT_FIXED_FONT_SIZE);
448 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitMinimumFontSize,
449 IDS_MINIMUM_FONT_SIZE);
450 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitMinimumLogicalFontSize,
451 IDS_MINIMUM_LOGICAL_FONT_SIZE);
452 prefs->RegisterLocalizedBooleanPref(prefs::kWebKitUsesUniversalDetector,
453 IDS_USES_UNIVERSAL_DETECTOR);
454 prefs->RegisterLocalizedStringPref(prefs::kStaticEncodings,
455 IDS_STATIC_ENCODING_LIST);
initial.commit09911bf2008-07-26 23:55:29456}
457
[email protected]13367f72009-07-08 16:39:02458// Returns true if contains content rendered by an extension.
459bool TabContents::HostsExtension() const {
460 return GetURL().SchemeIs(chrome::kExtensionScheme);
[email protected]d5f942ba2008-09-26 19:30:34461}
462
[email protected]6e95706d2009-04-23 22:55:01463PasswordManager* TabContents::GetPasswordManager() {
464 if (password_manager_.get() == NULL)
465 password_manager_.reset(new PasswordManager(this));
466 return password_manager_.get();
467}
468
469PluginInstaller* TabContents::GetPluginInstaller() {
470 if (plugin_installer_.get() == NULL)
471 plugin_installer_.reset(new PluginInstaller(this));
472 return plugin_installer_.get();
473}
474
[email protected]d5f942ba2008-09-26 19:30:34475const GURL& TabContents::GetURL() const {
476 // We may not have a navigation entry yet
[email protected]ce3fa3c2009-04-20 19:55:57477 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]ebe89e062009-08-13 23:16:54478 return entry ? entry->virtual_url() : GURL::EmptyGURL();
[email protected]d5f942ba2008-09-26 19:30:34479}
480
[email protected]96d185d2009-04-24 03:28:54481const string16& TabContents::GetTitle() const {
[email protected]4c6092c5b2009-06-06 00:23:55482 // Transient entries take precedence. They are used for interstitial pages
483 // that are shown on top of existing pages.
484 NavigationEntry* entry = controller_.GetTransientEntry();
485 if (entry)
486 return entry->GetTitleForDisplay(&controller_);
487
[email protected]96d185d2009-04-24 03:28:54488 DOMUI* our_dom_ui = render_manager_.pending_dom_ui() ?
489 render_manager_.pending_dom_ui() : render_manager_.dom_ui();
490 if (our_dom_ui) {
491 // Don't override the title in view source mode.
[email protected]4c6092c5b2009-06-06 00:23:55492 entry = controller_.GetActiveEntry();
[email protected]96d185d2009-04-24 03:28:54493 if (!(entry && entry->IsViewSourceMode())) {
494 // Give the DOM UI the chance to override our title.
495 const string16& title = our_dom_ui->overridden_title();
496 if (!title.empty())
497 return title;
498 }
499 }
500
501 // We use the title for the last committed entry rather than a pending
502 // navigation entry. For example, when the user types in a URL, we want to
503 // keep the old page's title until the new load has committed and we get a new
504 // title.
[email protected]96d185d2009-04-24 03:28:54505 entry = controller_.GetLastCommittedEntry();
506 if (entry)
507 return entry->GetTitleForDisplay(&controller_);
[email protected]96d185d2009-04-24 03:28:54508 return EmptyString16();
509}
510
[email protected]d5f942ba2008-09-26 19:30:34511int32 TabContents::GetMaxPageID() {
512 if (GetSiteInstance())
513 return GetSiteInstance()->max_page_id();
514 else
515 return max_page_id_;
516}
517
518void TabContents::UpdateMaxPageID(int32 page_id) {
519 // Ensure both the SiteInstance and RenderProcessHost update their max page
[email protected]57c6a652009-05-04 07:58:34520 // IDs in sync. Only TabContents will also have site instances, except during
[email protected]d5f942ba2008-09-26 19:30:34521 // testing.
522 if (GetSiteInstance())
523 GetSiteInstance()->UpdateMaxPageID(page_id);
[email protected]57c6a652009-05-04 07:58:34524 process()->UpdateMaxPageID(page_id);
[email protected]d5f942ba2008-09-26 19:30:34525}
526
[email protected]96d185d2009-04-24 03:28:54527SiteInstance* TabContents::GetSiteInstance() const {
528 return render_manager_.current_host()->site_instance();
529}
530
[email protected]d5f942ba2008-09-26 19:30:34531const std::wstring TabContents::GetDefaultTitle() const {
532 return l10n_util::GetString(IDS_DEFAULT_TAB_TITLE);
533}
534
[email protected]96d185d2009-04-24 03:28:54535bool TabContents::ShouldDisplayURL() {
536 // Don't hide the url in view source mode.
537 NavigationEntry* entry = controller_.GetActiveEntry();
538 if (entry && entry->IsViewSourceMode())
539 return true;
[email protected]83e3895b2009-06-11 00:07:16540
[email protected]96d185d2009-04-24 03:28:54541 DOMUI* dom_ui = GetDOMUIForCurrentState();
542 if (dom_ui)
543 return !dom_ui->should_hide_url();
544 return true;
545}
546
[email protected]d5f942ba2008-09-26 19:30:34547SkBitmap TabContents::GetFavIcon() const {
548 // Like GetTitle(), we also want to use the favicon for the last committed
549 // entry rather than a pending navigation entry.
[email protected]ce3fa3c2009-04-20 19:55:57550 NavigationEntry* entry = controller_.GetTransientEntry();
[email protected]cbab76d2008-10-13 22:42:47551 if (entry)
552 return entry->favicon().bitmap();
553
[email protected]ce3fa3c2009-04-20 19:55:57554 entry = controller_.GetLastCommittedEntry();
[email protected]d5f942ba2008-09-26 19:30:34555 if (entry)
556 return entry->favicon().bitmap();
[email protected]d5f942ba2008-09-26 19:30:34557 return SkBitmap();
558}
559
[email protected]0b303cc2009-09-28 22:35:15560bool TabContents::FavIconIsValid() const {
561 NavigationEntry* entry = controller_.GetTransientEntry();
562 if (entry)
563 return entry->favicon().is_valid();
564
565 entry = controller_.GetLastCommittedEntry();
566 if (entry)
567 return entry->favicon().is_valid();
568
569 return false;
570}
571
[email protected]96d185d2009-04-24 03:28:54572bool TabContents::ShouldDisplayFavIcon() {
573 // Always display a throbber during pending loads.
574 if (controller_.GetLastCommittedEntry() && controller_.pending_entry())
575 return true;
576
577 DOMUI* dom_ui = GetDOMUIForCurrentState();
578 if (dom_ui)
579 return !dom_ui->hide_favicon();
580 return true;
581}
582
[email protected]96d185d2009-04-24 03:28:54583std::wstring TabContents::GetStatusText() const {
584 if (!is_loading() || load_state_ == net::LOAD_STATE_IDLE)
585 return std::wstring();
586
587 switch (load_state_) {
588 case net::LOAD_STATE_WAITING_FOR_CACHE:
589 return l10n_util::GetString(IDS_LOAD_STATE_WAITING_FOR_CACHE);
590 case net::LOAD_STATE_RESOLVING_PROXY_FOR_URL:
591 return l10n_util::GetString(IDS_LOAD_STATE_RESOLVING_PROXY_FOR_URL);
592 case net::LOAD_STATE_RESOLVING_HOST:
593 return l10n_util::GetString(IDS_LOAD_STATE_RESOLVING_HOST);
594 case net::LOAD_STATE_CONNECTING:
595 return l10n_util::GetString(IDS_LOAD_STATE_CONNECTING);
596 case net::LOAD_STATE_SENDING_REQUEST:
[email protected]094e5b22009-09-25 04:23:56597 if (upload_size_)
598 return l10n_util::GetStringF(
599 IDS_LOAD_STATE_SENDING_REQUEST_WITH_PROGRESS,
600 static_cast<int>((100 * upload_position_) / upload_size_));
601 else
602 return l10n_util::GetString(IDS_LOAD_STATE_SENDING_REQUEST);
[email protected]96d185d2009-04-24 03:28:54603 case net::LOAD_STATE_WAITING_FOR_RESPONSE:
604 return l10n_util::GetStringF(IDS_LOAD_STATE_WAITING_FOR_RESPONSE,
605 load_state_host_);
606 // Ignore net::LOAD_STATE_READING_RESPONSE and net::LOAD_STATE_IDLE
607 case net::LOAD_STATE_IDLE:
608 case net::LOAD_STATE_READING_RESPONSE:
609 break;
610 }
611
612 return std::wstring();
613}
614
[email protected]d5f942ba2008-09-26 19:30:34615void TabContents::SetIsCrashed(bool state) {
616 if (state == is_crashed_)
617 return;
618
619 is_crashed_ = state;
[email protected]c9cd2222009-05-06 05:16:50620 NotifyNavigationStateChanged(INVALIDATE_TAB);
[email protected]d5f942ba2008-09-26 19:30:34621}
622
[email protected]744ef172009-10-16 21:53:46623void TabContents::PageActionStateChanged() {
624 NotifyNavigationStateChanged(TabContents::INVALIDATE_PAGE_ACTIONS);
[email protected]d7eaf572009-07-01 21:57:00625}
[email protected]ce5c4502009-05-06 16:46:11626
[email protected]d5f942ba2008-09-26 19:30:34627void TabContents::NotifyNavigationStateChanged(unsigned changed_flags) {
628 if (delegate_)
629 delegate_->NavigationStateChanged(this, changed_flags);
630}
631
[email protected]96d185d2009-04-24 03:28:54632void TabContents::DidBecomeSelected() {
633 controller_.SetActive(true);
[email protected]789e9152009-08-04 21:59:43634 if (render_widget_host_view()) {
[email protected]96d185d2009-04-24 03:28:54635 render_widget_host_view()->DidBecomeSelected();
[email protected]789e9152009-08-04 21:59:43636#if defined(OS_MACOSX)
637 render_widget_host_view()->SetActive(true);
638#endif
639 }
[email protected]96d185d2009-04-24 03:28:54640
[email protected]76543b92009-08-31 17:27:45641 WebCacheManager::GetInstance()->ObserveActivity(process()->id());
[email protected]96d185d2009-04-24 03:28:54642}
643
644void TabContents::WasHidden() {
645 if (!capturing_contents()) {
646 // |render_view_host()| can be NULL if the user middle clicks a link to open
647 // a tab in then background, then closes the tab before selecting it. This
648 // is because closing the tab calls TabContents::Destroy(), which removes
649 // the |render_view_host()|; then when we actually destroy the window,
650 // OnWindowPosChanged() notices and calls HideContents() (which calls us).
651 if (render_widget_host_view())
652 render_widget_host_view()->WasHidden();
[email protected]96d185d2009-04-24 03:28:54653 }
654
655 NotificationService::current()->Notify(
656 NotificationType::TAB_CONTENTS_HIDDEN,
657 Source<TabContents>(this),
658 NotificationService::NoDetails());
659}
660
[email protected]d5f942ba2008-09-26 19:30:34661void TabContents::Activate() {
662 if (delegate_)
663 delegate_->ActivateContents(this);
664}
665
[email protected]96d185d2009-04-24 03:28:54666void TabContents::ShowContents() {
667 if (render_widget_host_view())
668 render_widget_host_view()->DidBecomeSelected();
[email protected]96d185d2009-04-24 03:28:54669}
670
671void TabContents::HideContents() {
672 // TODO(pkasting): http://b/1239839 Right now we purposefully don't call
673 // our superclass HideContents(), because some callers want to be very picky
674 // about the order in which these get called. In addition to making the code
675 // here practically impossible to understand, this also means we end up
676 // calling TabContents::WasHidden() twice if callers call both versions of
[email protected]57c6a652009-05-04 07:58:34677 // HideContents() on a TabContents.
[email protected]96d185d2009-04-24 03:28:54678 WasHidden();
679}
680
[email protected]c0588052008-10-27 23:01:50681void TabContents::OpenURL(const GURL& url, const GURL& referrer,
[email protected]d5f942ba2008-09-26 19:30:34682 WindowOpenDisposition disposition,
683 PageTransition::Type transition) {
684 if (delegate_)
[email protected]c0588052008-10-27 23:01:50685 delegate_->OpenURLFromTab(this, url, referrer, disposition, transition);
[email protected]d5f942ba2008-09-26 19:30:34686}
687
688bool TabContents::NavigateToPendingEntry(bool reload) {
[email protected]96d185d2009-04-24 03:28:54689 const NavigationEntry& entry = *controller_.pending_entry();
690
691 RenderViewHost* dest_render_view_host = render_manager_.Navigate(entry);
692 if (!dest_render_view_host)
693 return false; // Unable to create the desired render view host.
694
695 // Tell DevTools agent that it is attached prior to the navigation.
[email protected]d9f9b792009-06-24 13:17:12696 DevToolsManager* devtools_manager = DevToolsManager::GetInstance();
[email protected]7e8e6b62009-05-08 11:28:32697 if (devtools_manager) { // NULL in unit tests.
[email protected]684b4ba22009-05-07 06:27:41698 devtools_manager->OnNavigatingToPendingEntry(
699 render_view_host(),
700 dest_render_view_host,
701 controller_.pending_entry()->url());
[email protected]7e8e6b62009-05-08 11:28:32702 }
[email protected]96d185d2009-04-24 03:28:54703
704 // Used for page load time metrics.
705 current_load_start_ = base::TimeTicks::Now();
706
707 // Navigate in the desired RenderViewHost.
[email protected]056de2d2009-06-26 16:41:34708 ViewMsg_Navigate_Params navigate_params;
[email protected]5e369672009-11-03 23:48:30709 MakeNavigateParams(profile(), entry, reload, &navigate_params);
[email protected]056de2d2009-06-26 16:41:34710 dest_render_view_host->Navigate(navigate_params);
[email protected]96d185d2009-04-24 03:28:54711
712 if (entry.page_id() == -1) {
713 // HACK!! This code suppresses javascript: URLs from being added to
714 // session history, which is what we want to do for javascript: URLs that
715 // do not generate content. What we really need is a message from the
716 // renderer telling us that a new page was not created. The same message
717 // could be used for mailto: URLs and the like.
718 if (entry.url().SchemeIs(chrome::kJavaScriptScheme))
719 return false;
720 }
721
722 // Clear any provisional password saves - this stops password infobars
723 // showing up on pages the user navigates to while the right page is
724 // loading.
725 GetPasswordManager()->ClearProvisionalSave();
726
727 if (reload && !profile()->IsOffTheRecord()) {
[email protected]0189bc722009-08-28 21:56:48728 FaviconService* favicon_service =
729 profile()->GetFaviconService(Profile::IMPLICIT_ACCESS);
730 if (favicon_service)
731 favicon_service->SetFaviconOutOfDateForPage(entry.url());
[email protected]96d185d2009-04-24 03:28:54732 }
733
[email protected]d5f942ba2008-09-26 19:30:34734 return true;
735}
736
[email protected]96d185d2009-04-24 03:28:54737void TabContents::Stop() {
738 render_manager_.Stop();
739 printing_.Stop();
740}
741
742void TabContents::Cut() {
743 render_view_host()->Cut();
744}
745
746void TabContents::Copy() {
747 render_view_host()->Copy();
748}
749
[email protected]c0cc3092009-09-12 08:27:27750#if defined(OS_MACOSX)
[email protected]a954bf72009-09-12 07:30:35751void TabContents::CopyToFindPboard() {
752 render_view_host()->CopyToFindPboard();
753}
[email protected]c0cc3092009-09-12 08:27:27754#endif
[email protected]a954bf72009-09-12 07:30:35755
[email protected]96d185d2009-04-24 03:28:54756void TabContents::Paste() {
757 render_view_host()->Paste();
758}
759
[email protected]7e786822009-11-07 00:00:27760#if defined(OS_MACOSX)
761void TabContents::PasteAndMatchStyle() {
762 render_view_host()->ForwardEditCommand("PasteAndMatchStyle", "");
763}
764#endif
765
[email protected]96d185d2009-04-24 03:28:54766void TabContents::DisassociateFromPopupCount() {
767 render_view_host()->DisassociateFromPopupCount();
768}
769
770TabContents* TabContents::Clone() {
771 // We create a new SiteInstance so that the new tab won't share processes
772 // with the old one. This can be changed in the future if we need it to share
773 // processes for some reason.
[email protected]420ae012009-04-24 05:16:32774 TabContents* tc = new TabContents(profile(),
[email protected]96d185d2009-04-24 03:28:54775 SiteInstance::CreateSiteInstance(profile()),
[email protected]12636df2009-09-28 22:32:21776 MSG_ROUTING_NONE, this);
[email protected]96d185d2009-04-24 03:28:54777 tc->controller().CopyStateFrom(controller_);
778 return tc;
779}
780
[email protected]420ae012009-04-24 05:16:32781void TabContents::CreateShortcut() {
782 NavigationEntry* entry = controller_.GetLastCommittedEntry();
783 if (!entry)
784 return;
785
[email protected]70b8d6b2009-08-20 16:56:24786#if defined(OS_LINUX) && !defined(TOOLKIT_VIEWS)
[email protected]0b303cc2009-09-28 22:35:15787 SkBitmap bitmap;
788 if (FavIconIsValid())
789 bitmap = GetFavIcon();
[email protected]0ba5c5e2009-08-20 16:47:13790 CreateApplicationShortcutsDialogGtk::Show(view()->GetTopLevelNativeWindow(),
[email protected]0b303cc2009-09-28 22:35:15791 GetURL(), GetTitle(), bitmap);
[email protected]b96aa932009-08-12 21:34:49792#else
[email protected]420ae012009-04-24 05:16:32793 // We only allow one pending install request. By resetting the page id we
794 // effectively cancel the pending install request.
795 pending_install_.page_id = entry->page_id();
796 pending_install_.icon = GetFavIcon();
[email protected]00e3f152009-10-20 19:25:42797 pending_install_.title = GetTitle();
[email protected]420ae012009-04-24 05:16:32798 pending_install_.url = GetURL();
799 if (pending_install_.callback_functor) {
800 pending_install_.callback_functor->Cancel();
801 pending_install_.callback_functor = NULL;
802 }
803 DCHECK(!pending_install_.icon.isNull()) << "Menu item should be disabled.";
804 if (pending_install_.title.empty())
[email protected]00e3f152009-10-20 19:25:42805 pending_install_.title = UTF8ToUTF16(GetURL().spec());
[email protected]420ae012009-04-24 05:16:32806
807 // Request the application info. When done OnDidGetApplicationInfo is invoked
808 // and we'll create the shortcut.
809 render_view_host()->GetApplicationInfo(pending_install_.page_id);
[email protected]b96aa932009-08-12 21:34:49810#endif
[email protected]420ae012009-04-24 05:16:32811}
812
[email protected]4d677202009-07-19 07:37:12813void TabContents::ShowPageInfo(const GURL& url,
814 const NavigationEntry::SSLStatus& ssl,
815 bool show_history) {
816 if (!delegate_)
817 return;
818
[email protected]bb678332009-07-21 00:15:50819 delegate_->ShowPageInfo(profile(), url, ssl, show_history);
[email protected]4d677202009-07-19 07:37:12820}
821
initial.commit09911bf2008-07-26 23:55:29822ConstrainedWindow* TabContents::CreateConstrainedDialog(
[email protected]e8382172009-06-19 22:16:28823 ConstrainedWindowDelegate* delegate) {
initial.commit09911bf2008-07-26 23:55:29824 ConstrainedWindow* window =
[email protected]e8382172009-06-19 22:16:28825 ConstrainedWindow::CreateConstrainedDialog(this, delegate);
initial.commit09911bf2008-07-26 23:55:29826 child_windows_.push_back(window);
827 return window;
828}
829
830void TabContents::AddNewContents(TabContents* new_contents,
831 WindowOpenDisposition disposition,
832 const gfx::Rect& initial_pos,
[email protected]c88a70fe2009-05-05 20:00:22833 bool user_gesture,
834 const GURL& creator_url) {
initial.commit09911bf2008-07-26 23:55:29835 if (!delegate_)
836 return;
837
[email protected]35f7d212009-04-29 21:19:27838 if ((disposition == NEW_POPUP) && !user_gesture &&
839 !CommandLine::ForCurrentProcess()->HasSwitch(
840 switches::kDisablePopupBlocking)) {
[email protected]2c4410d2009-05-06 23:46:22841 // Unrequested popups from normal pages are constrained unless they're in
[email protected]b6c874582009-05-08 19:38:31842 // the whitelist. The popup owner will handle checking this.
843 delegate_->GetConstrainingContents(this)->AddPopup(new_contents,
844 initial_pos,
845 creator_url.is_valid() ? creator_url.host() : std::string());
846 } else {
[email protected]304f8792009-08-19 21:28:09847#if defined(OS_CHROMEOS)
848 if (disposition == NEW_POPUP) {
849 // If the popup is bigger than a given factor of the screen, then
850 // turn it into a foreground tab (on chrome os only)
851 GdkScreen* screen = gdk_screen_get_default();
852 int max_width = gdk_screen_get_width(screen) * kMaxWidthFactor;
853 int max_height = gdk_screen_get_height(screen) * kMaxHeightFactor;
854 if (initial_pos.width() > max_width ||
855 initial_pos.height() > max_height) {
856 disposition = NEW_FOREGROUND_TAB;
857 }
858 }
859#endif
860
[email protected]0aa55312008-10-17 21:53:08861 new_contents->DisassociateFromPopupCount();
initial.commit09911bf2008-07-26 23:55:29862 delegate_->AddNewContents(this, new_contents, disposition, initial_pos,
863 user_gesture);
[email protected]7f37dd92009-09-15 16:35:49864 NotificationService::current()->Notify(
865 NotificationType::TAB_ADDED,
866 Source<TabContentsDelegate>(delegate_),
867 Details<TabContents>(this));
initial.commit09911bf2008-07-26 23:55:29868 }
[email protected]b6c874582009-05-08 19:38:31869 PopupNotificationVisibilityChanged(ShowingBlockedPopupNotification());
initial.commit09911bf2008-07-26 23:55:29870}
871
initial.commit09911bf2008-07-26 23:55:29872void TabContents::CloseAllSuppressedPopups() {
[email protected]d6598c052008-11-05 19:03:25873 if (blocked_popups_)
[email protected]2c4410d2009-05-06 23:46:22874 blocked_popups_->CloseAll();
initial.commit09911bf2008-07-26 23:55:29875}
876
[email protected]912256b32009-09-18 09:47:35877void TabContents::ExecuteCode(int request_id, const std::string& extension_id,
878 bool is_js_code, const std::string& code_string) {
879 render_view_host()->Send(
880 new ViewMsg_ExecuteCode(render_view_host()->routing_id(), request_id,
881 extension_id, is_js_code, code_string));
882}
883
[email protected]96d185d2009-04-24 03:28:54884void TabContents::PopupNotificationVisibilityChanged(bool visible) {
885 render_view_host()->PopupNotificationVisibilityChanged(visible);
886}
887
[email protected]5c9e97a2009-09-09 23:48:30888gfx::NativeView TabContents::GetContentNativeView() const {
[email protected]96d185d2009-04-24 03:28:54889 return view_->GetContentNativeView();
890}
891
892gfx::NativeView TabContents::GetNativeView() const {
893 return view_->GetNativeView();
894}
895
896void TabContents::GetContainerBounds(gfx::Rect *out) const {
897 view_->GetContainerBounds(out);
898}
899
900void TabContents::Focus() {
901 view_->Focus();
902}
903
[email protected]90daadb42009-06-08 21:27:28904void TabContents::FocusThroughTabTraversal(bool reverse) {
[email protected]7e383692009-06-12 19:14:54905 if (showing_interstitial_page()) {
906 render_manager_.interstitial_page()->FocusThroughTabTraversal(reverse);
907 return;
908 }
[email protected]96d185d2009-04-24 03:28:54909 render_view_host()->SetInitialFocus(reverse);
910}
911
912bool TabContents::FocusLocationBarByDefault() {
913 DOMUI* dom_ui = GetDOMUIForCurrentState();
914 if (dom_ui)
915 return dom_ui->focus_location_bar_by_default();
[email protected]3e3f0eb2009-06-22 18:33:43916 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]e0d481582009-09-15 21:06:25917 if (entry && entry->url() == GURL(chrome::kAboutBlankURL))
[email protected]3e3f0eb2009-06-22 18:33:43918 return true;
[email protected]96d185d2009-04-24 03:28:54919 return false;
920}
921
[email protected]616ed5a2008-11-21 22:27:24922void TabContents::AddInfoBar(InfoBarDelegate* delegate) {
923 // Look through the existing InfoBarDelegates we have for a match. If we've
924 // already got one that matches, then we don't add the new one.
[email protected]f86a07022008-11-25 01:06:05925 for (int i = 0; i < infobar_delegate_count(); ++i) {
[email protected]616ed5a2008-11-21 22:27:24926 if (GetInfoBarDelegateAt(i)->EqualsDelegate(delegate))
927 return;
928 }
929
930 infobar_delegates_.push_back(delegate);
[email protected]bfd04a62009-02-01 18:16:56931 NotificationService::current()->Notify(
932 NotificationType::TAB_CONTENTS_INFOBAR_ADDED,
933 Source<TabContents>(this),
934 Details<InfoBarDelegate>(delegate));
[email protected]616ed5a2008-11-21 22:27:24935
936 // Add ourselves as an observer for navigations the first time a delegate is
937 // added. We use this notification to expire InfoBars that need to expire on
938 // page transitions.
939 if (infobar_delegates_.size() == 1) {
[email protected]bfd04a62009-02-01 18:16:56940 registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED,
[email protected]ce3fa3c2009-04-20 19:55:57941 Source<NavigationController>(&controller_));
[email protected]616ed5a2008-11-21 22:27:24942 }
943}
944
945void TabContents::RemoveInfoBar(InfoBarDelegate* delegate) {
946 std::vector<InfoBarDelegate*>::iterator it =
947 find(infobar_delegates_.begin(), infobar_delegates_.end(), delegate);
948 if (it != infobar_delegates_.end()) {
949 InfoBarDelegate* delegate = *it;
[email protected]bfd04a62009-02-01 18:16:56950 NotificationService::current()->Notify(
951 NotificationType::TAB_CONTENTS_INFOBAR_REMOVED,
952 Source<TabContents>(this),
953 Details<InfoBarDelegate>(delegate));
[email protected]f86a07022008-11-25 01:06:05954 infobar_delegates_.erase(it);
[email protected]616ed5a2008-11-21 22:27:24955
[email protected]6a02963e2009-01-06 16:58:03956 // Remove ourselves as an observer if we are tracking no more InfoBars.
957 if (infobar_delegates_.empty()) {
[email protected]bfd04a62009-02-01 18:16:56958 registrar_.Remove(this, NotificationType::NAV_ENTRY_COMMITTED,
[email protected]ce3fa3c2009-04-20 19:55:57959 Source<NavigationController>(&controller_));
[email protected]6a02963e2009-01-06 16:58:03960 }
[email protected]616ed5a2008-11-21 22:27:24961 }
962}
[email protected]3edd9522009-03-04 22:19:41963
[email protected]f81821c2009-08-07 01:29:47964void TabContents::ReplaceInfoBar(InfoBarDelegate* old_delegate,
965 InfoBarDelegate* new_delegate) {
966 std::vector<InfoBarDelegate*>::iterator it =
967 find(infobar_delegates_.begin(), infobar_delegates_.end(), old_delegate);
968 DCHECK(it != infobar_delegates_.end());
969
970 // Notify the container about the change of plans.
971 scoped_ptr<std::pair<InfoBarDelegate*, InfoBarDelegate*> > details(
972 new std::pair<InfoBarDelegate*, InfoBarDelegate*>(
973 old_delegate, new_delegate));
974 NotificationService::current()->Notify(
975 NotificationType::TAB_CONTENTS_INFOBAR_REPLACED,
976 Source<TabContents>(this),
977 Details<std::pair<InfoBarDelegate*, InfoBarDelegate*> >(details.get()));
978
979 // Remove the old one.
980 infobar_delegates_.erase(it);
981
982 // Add the new one.
983 infobar_delegates_.push_back(new_delegate);
984}
985
[email protected]20ede072009-10-06 00:28:35986bool TabContents::ShouldShowBookmarkBar() {
[email protected]aa9196f2009-10-22 16:20:27987 if (showing_interstitial_page())
988 return false;
989
[email protected]96d185d2009-04-24 03:28:54990 // See GetDOMUIForCurrentState() comment for more info. This case is very
991 // similar, but for non-first loads, we want to use the committed entry. This
992 // is so the bookmarks bar disappears at the same time the page does.
993 if (controller_.GetLastCommittedEntry()) {
994 // Not the first load, always use the committed DOM UI.
995 if (render_manager_.dom_ui())
996 return render_manager_.dom_ui()->force_bookmark_bar_visible();
997 return false; // Default.
998 }
999
1000 // When it's the first load, we know either the pending one or the committed
1001 // one will have the DOM UI in it (see GetDOMUIForCurrentState), and only one
1002 // of them will be valid, so we can just check both.
1003 if (render_manager_.pending_dom_ui())
1004 return render_manager_.pending_dom_ui()->force_bookmark_bar_visible();
1005 if (render_manager_.dom_ui())
1006 return render_manager_.dom_ui()->force_bookmark_bar_visible();
1007 return false; // Default.
1008}
1009
[email protected]d8f16ae2009-08-30 03:47:261010bool TabContents::IsExtensionShelfAlwaysVisible() {
1011 // See GetDOMUIForCurrentState() comment for more info. This case is very
1012 // similar, but for non-first loads, we want to use the committed entry. This
1013 // is so the bookmarks bar disappears at the same time the page does.
1014 if (controller_.GetLastCommittedEntry()) {
1015 // Not the first load, always use the committed DOM UI.
1016 if (render_manager_.dom_ui())
1017 return render_manager_.dom_ui()->force_extension_shelf_visible();
1018 return false; // Default.
1019 }
1020
1021 // When it's the first load, we know either the pending one or the committed
1022 // one will have the DOM UI in it (see GetDOMUIForCurrentState), and only one
1023 // of them will be valid, so we can just check both.
1024 if (render_manager_.pending_dom_ui())
1025 return render_manager_.pending_dom_ui()->force_extension_shelf_visible();
1026 if (render_manager_.dom_ui())
1027 return render_manager_.dom_ui()->force_extension_shelf_visible();
1028 return false; // Default.
1029}
1030
[email protected]3edd9522009-03-04 22:19:411031void TabContents::ToolbarSizeChanged(bool is_animating) {
1032 TabContentsDelegate* d = delegate();
1033 if (d)
1034 d->ToolbarSizeChanged(this, is_animating);
1035}
[email protected]616ed5a2008-11-21 22:27:241036
[email protected]d5f942ba2008-09-26 19:30:341037void TabContents::OnStartDownload(DownloadItem* download) {
1038 DCHECK(download);
[email protected]d5f942ba2008-09-26 19:30:341039
1040 // Download in a constrained popup is shown in the tab that opened it.
[email protected]b6c874582009-05-08 19:38:311041 TabContents* tab_contents = delegate()->GetConstrainingContents(this);
[email protected]d5f942ba2008-09-26 19:30:341042
[email protected]59560e0b2009-06-04 03:30:221043 if (tab_contents && tab_contents->delegate())
1044 tab_contents->delegate()->OnStartDownload(download);
[email protected]3edd9522009-03-04 22:19:411045}
[email protected]3edd9522009-03-04 22:19:411046
[email protected]d5f942ba2008-09-26 19:30:341047void TabContents::WillClose(ConstrainedWindow* window) {
1048 ConstrainedWindowList::iterator it =
1049 find(child_windows_.begin(), child_windows_.end(), window);
1050 if (it != child_windows_.end())
1051 child_windows_.erase(it);
[email protected]66ba4932009-06-04 19:22:131052}
[email protected]d5f942ba2008-09-26 19:30:341053
[email protected]66ba4932009-06-04 19:22:131054void TabContents::WillCloseBlockedPopupContainer(
1055 BlockedPopupContainer* container) {
1056 DCHECK(blocked_popups_ == container);
1057 blocked_popups_ = NULL;
[email protected]d5f942ba2008-09-26 19:30:341058}
1059
[email protected]d5f942ba2008-09-26 19:30:341060void TabContents::DidMoveOrResize(ConstrainedWindow* window) {
[email protected]332af7732009-03-11 13:21:351061#if defined(OS_WIN)
[email protected]92edc472009-02-10 20:32:061062 UpdateWindow(GetNativeView());
[email protected]332af7732009-03-11 13:21:351063#endif
[email protected]d5f942ba2008-09-26 19:30:341064}
1065
[email protected]e491f1c2009-05-22 20:28:121066void TabContents::StartFinding(string16 find_text,
1067 bool forward_direction,
1068 bool case_sensitive) {
[email protected]6e95706d2009-04-23 22:55:011069 // If find_text is empty, it means FindNext was pressed with a keyboard
1070 // shortcut so unless we have something to search for we return early.
[email protected]e491f1c2009-05-22 20:28:121071 if (find_text.empty() && find_text_.empty()) {
1072 if (last_search_prepopulate_text_->empty())
1073 return;
1074 // Try whatever we searched for last in any tab.
1075 find_text = *last_search_prepopulate_text_;
1076 }
[email protected]6e95706d2009-04-23 22:55:011077
[email protected]101c90e2009-10-26 21:14:441078 // Keep track of the previous search.
1079 previous_find_text_ = find_text_;
1080
[email protected]6e95706d2009-04-23 22:55:011081 // This is a FindNext operation if we are searching for the same text again,
1082 // or if the passed in search text is empty (FindNext keyboard shortcut). The
1083 // exception to this is if the Find was aborted (then we don't want FindNext
1084 // because the highlighting has been cleared and we need it to reappear). We
1085 // therefore treat FindNext after an aborted Find operation as a full fledged
1086 // Find.
1087 bool find_next = (find_text_ == find_text || find_text.empty()) &&
[email protected]e491f1c2009-05-22 20:28:121088 (last_search_case_sensitive_ == case_sensitive) &&
[email protected]6e95706d2009-04-23 22:55:011089 !find_op_aborted_;
1090 if (!find_next)
1091 current_find_request_id_ = find_request_id_counter_++;
1092
1093 if (!find_text.empty())
1094 find_text_ = find_text;
[email protected]e491f1c2009-05-22 20:28:121095 last_search_case_sensitive_ = case_sensitive;
[email protected]6e95706d2009-04-23 22:55:011096
1097 find_op_aborted_ = false;
1098
1099 // Keep track of what the last search was across the tabs.
[email protected]e491f1c2009-05-22 20:28:121100 *last_search_prepopulate_text_ = find_text;
[email protected]6e95706d2009-04-23 22:55:011101
1102 render_view_host()->StartFinding(current_find_request_id_,
1103 find_text_,
1104 forward_direction,
[email protected]e491f1c2009-05-22 20:28:121105 case_sensitive,
[email protected]6e95706d2009-04-23 22:55:011106 find_next);
1107}
1108
1109void TabContents::StopFinding(bool clear_selection) {
[email protected]2d2dffc2009-06-09 21:41:001110 // When |clear_selection| is true, it means the find string has been cleared
1111 // by the user, but the UI has not been dismissed.
1112 if (!clear_selection)
1113 find_ui_active_ = false;
[email protected]101c90e2009-10-26 21:14:441114 find_text_.clear();
[email protected]6e95706d2009-04-23 22:55:011115 find_op_aborted_ = true;
[email protected]e491f1c2009-05-22 20:28:121116 last_search_result_ = FindNotificationDetails();
[email protected]6e95706d2009-04-23 22:55:011117 render_view_host()->StopFinding(clear_selection);
1118}
1119
[email protected]5c4266922009-07-10 16:41:271120void TabContents::GetPageLanguage() {
1121 render_view_host()->GetPageLanguage();
1122}
1123
[email protected]420ae012009-04-24 05:16:321124void TabContents::OnSavePage() {
1125 // If we can not save the page, try to download it.
1126 if (!SavePackage::IsSavableContents(contents_mime_type())) {
1127 DownloadManager* dlm = profile()->GetDownloadManager();
1128 const GURL& current_page_url = GetURL();
1129 if (dlm && current_page_url.is_valid())
[email protected]d82ed61e2009-06-16 02:46:221130 dlm->DownloadUrl(current_page_url, GURL(), "", this);
[email protected]420ae012009-04-24 05:16:321131 return;
1132 }
1133
1134 Stop();
1135
1136 // Create the save package and possibly prompt the user for the name to save
1137 // the page as. The user prompt is an asynchronous operation that runs on
1138 // another thread.
[email protected]d82ed61e2009-06-16 02:46:221139 save_package_ = new SavePackage(this);
[email protected]420ae012009-04-24 05:16:321140 save_package_->GetSaveInfo();
1141}
1142
1143// Used in automated testing to bypass prompting the user for file names.
1144// Instead, the names and paths are hard coded rather than running them through
1145// file name sanitation and extension / mime checking.
[email protected]828cabe2009-09-26 22:47:111146bool TabContents::SavePage(const FilePath& main_file, const FilePath& dir_path,
[email protected]420ae012009-04-24 05:16:321147 SavePackage::SavePackageType save_type) {
1148 // Stop the page from navigating.
1149 Stop();
1150
[email protected]828cabe2009-09-26 22:47:111151 save_package_ = new SavePackage(this, save_type, main_file, dir_path);
[email protected]cdf6cc82009-08-04 21:48:591152 return save_package_->Init();
[email protected]420ae012009-04-24 05:16:321153}
1154
1155void TabContents::PrintPreview() {
1156 // We don't show the print preview yet, only the print dialog.
1157 PrintNow();
1158}
1159
1160bool TabContents::PrintNow() {
1161 // We can't print interstitial page for now.
1162 if (showing_interstitial_page())
1163 return false;
1164
1165 return render_view_host()->PrintPages();
1166}
1167
[email protected]82270452009-06-19 15:58:011168void TabContents::PrintingDone(int document_cookie, bool success) {
1169 render_view_host()->PrintingDone(document_cookie, success);
1170}
1171
[email protected]420ae012009-04-24 05:16:321172bool TabContents::IsActiveEntry(int32 page_id) {
1173 NavigationEntry* active_entry = controller_.GetActiveEntry();
1174 return (active_entry != NULL &&
1175 active_entry->site_instance() == GetSiteInstance() &&
1176 active_entry->page_id() == page_id);
1177}
1178
[email protected]3f8eb7f2009-10-02 23:58:051179void TabContents::LogNewTabTime(const std::string& event_name) {
1180 // Not all new tab pages get timed. In those cases, we don't have a
1181 // new_tab_start_time_.
1182 if (new_tab_start_time_.is_null())
1183 return;
1184
1185 base::TimeDelta duration = base::TimeTicks::Now() - new_tab_start_time_;
[email protected]84abba62009-10-07 17:01:441186 MetricEventDurationDetails details(event_name,
1187 static_cast<int>(duration.InMilliseconds()));
1188
[email protected]7ab1e7d62009-10-14 23:32:011189 if (event_name == "Tab.NewTabScriptStart") {
1190 UMA_HISTOGRAM_TIMES("Tab.NewTabScriptStart", duration);
1191 } else if (event_name == "Tab.NewTabDOMContentLoaded") {
1192 UMA_HISTOGRAM_TIMES("Tab.NewTabDOMContentLoaded", duration);
1193 } else if (event_name == "Tab.NewTabOnload") {
1194 UMA_HISTOGRAM_TIMES("Tab.NewTabOnload", duration);
[email protected]3f8eb7f2009-10-02 23:58:051195 // The new tab page has finished loading; reset it.
1196 new_tab_start_time_ = base::TimeTicks();
[email protected]84abba62009-10-07 17:01:441197 } else {
1198 NOTREACHED();
[email protected]3f8eb7f2009-10-02 23:58:051199 }
[email protected]84abba62009-10-07 17:01:441200 NotificationService::current()->Notify(
1201 NotificationType::METRIC_EVENT_DURATION,
1202 Source<TabContents>(this),
1203 Details<MetricEventDurationDetails>(&details));
[email protected]3f8eb7f2009-10-02 23:58:051204}
1205
[email protected]7ab1e7d62009-10-14 23:32:011206void TabContents::OnCloseStarted() {
1207 if (tab_close_start_time_.is_null())
1208 tab_close_start_time_ = base::TimeTicks::Now();
1209}
1210
[email protected]96d185d2009-04-24 03:28:541211// Notifies the RenderWidgetHost instance about the fact that the page is
1212// loading, or done loading and calls the base implementation.
1213void TabContents::SetIsLoading(bool is_loading,
1214 LoadNotificationDetails* details) {
1215 if (is_loading == is_loading_)
1216 return;
1217
1218 if (!is_loading) {
1219 load_state_ = net::LOAD_STATE_IDLE;
1220 load_state_host_.clear();
[email protected]094e5b22009-09-25 04:23:561221 upload_size_ = 0;
1222 upload_position_ = 0;
[email protected]96d185d2009-04-24 03:28:541223 }
1224
1225 render_manager_.SetIsLoading(is_loading);
1226
1227 is_loading_ = is_loading;
1228 waiting_for_response_ = is_loading;
1229
1230 if (delegate_)
1231 delegate_->LoadingStateChanged(this);
[email protected]329581b2009-04-28 06:52:351232 NotifyNavigationStateChanged(INVALIDATE_LOAD);
[email protected]96d185d2009-04-24 03:28:541233
1234 NotificationType type = is_loading ? NotificationType::LOAD_START :
1235 NotificationType::LOAD_STOP;
[email protected]ce5c4502009-05-06 16:46:111236 NotificationDetails det = NotificationService::NoDetails();
[email protected]96d185d2009-04-24 03:28:541237 if (details)
1238 det = Details<LoadNotificationDetails>(details);
1239 NotificationService::current()->Notify(type,
1240 Source<NavigationController>(&controller_),
1241 det);
1242}
1243
[email protected]2c4410d2009-05-06 23:46:221244void TabContents::CreateBlockedPopupContainerIfNecessary() {
1245 if (blocked_popups_)
1246 return;
1247
[email protected]4d4c0b92009-06-08 23:13:011248 blocked_popups_ = BlockedPopupContainer::Create(this, profile());
[email protected]2c4410d2009-05-06 23:46:221249}
1250
[email protected]b6c874582009-05-08 19:38:311251void TabContents::AddPopup(TabContents* new_contents,
1252 const gfx::Rect& initial_pos,
1253 const std::string& host) {
[email protected]2c4410d2009-05-06 23:46:221254 CreateBlockedPopupContainerIfNecessary();
1255 blocked_popups_->AddTabContents(new_contents, initial_pos, host);
[email protected]2c4410d2009-05-06 23:46:221256}
1257
[email protected]e69bce3dd2009-04-20 22:05:121258// TODO(brettw) This should be on the TabContentsView.
[email protected]66ba4932009-06-04 19:22:131259void TabContents::RepositionSupressedPopupsToFit() {
[email protected]d6598c052008-11-05 19:03:251260 if (blocked_popups_)
[email protected]4d4c0b92009-06-08 23:13:011261 blocked_popups_->RepositionBlockedPopupContainer();
[email protected]d5f942ba2008-09-26 19:30:341262}
1263
[email protected]b9681312008-11-07 00:08:261264bool TabContents::ShowingBlockedPopupNotification() const {
1265 return blocked_popups_ != NULL &&
[email protected]2c4410d2009-05-06 23:46:221266 blocked_popups_->GetBlockedPopupCount() != 0;
[email protected]b9681312008-11-07 00:08:261267}
[email protected]616ed5a2008-11-21 22:27:241268
1269namespace {
1270bool TransitionIsReload(PageTransition::Type transition) {
1271 return PageTransition::StripQualifier(transition) == PageTransition::RELOAD;
1272}
1273}
1274
1275void TabContents::ExpireInfoBars(
1276 const NavigationController::LoadCommittedDetails& details) {
1277 // Only hide InfoBars when the user has done something that makes the main
1278 // frame load. We don't want various automatic or subframe navigations making
1279 // it disappear.
1280 if (!details.is_user_initiated_main_frame_load())
1281 return;
1282
[email protected]f86a07022008-11-25 01:06:051283 for (int i = infobar_delegate_count() - 1; i >= 0; --i) {
[email protected]616ed5a2008-11-21 22:27:241284 InfoBarDelegate* delegate = GetInfoBarDelegateAt(i);
[email protected]f86a07022008-11-25 01:06:051285 if (delegate->ShouldExpire(details))
[email protected]616ed5a2008-11-21 22:27:241286 RemoveInfoBar(delegate);
[email protected]616ed5a2008-11-21 22:27:241287 }
1288}
[email protected]fdd61c62009-04-22 19:22:571289
1290void TabContents::OnGearsCreateShortcutDone(
1291 const GearsShortcutData2& shortcut_data, bool success) {
1292 NavigationEntry* current_entry = controller_.GetLastCommittedEntry();
1293 bool same_page =
1294 current_entry && pending_install_.page_id == current_entry->page_id();
1295
1296 if (success && same_page) {
1297 // Only switch to app mode if the user chose to create a shortcut and
1298 // we're still on the same page that it corresponded to.
1299 if (delegate())
1300 delegate()->ConvertContentsToApplication(this);
1301 }
1302
1303 // Reset the page id to indicate no requests are pending.
1304 pending_install_.page_id = 0;
1305 pending_install_.callback_functor = NULL;
1306}
[email protected]96d185d2009-04-24 03:28:541307
1308DOMUI* TabContents::GetDOMUIForCurrentState() {
1309 // When there is a pending navigation entry, we want to use the pending DOMUI
1310 // that goes along with it to control the basic flags. For example, we want to
1311 // show the pending URL in the URL bar, so we want the display_url flag to
1312 // be from the pending entry.
1313 //
1314 // The confusion comes because there are multiple possibilities for the
1315 // initial load in a tab as a side effect of the way the RenderViewHostManager
1316 // works.
1317 //
1318 // - For the very first tab the load looks "normal". The new tab DOM UI is
1319 // the pending one, and we want it to apply here.
1320 //
1321 // - For subsequent new tabs, they'll get a new SiteInstance which will then
1322 // get switched to the one previously associated with the new tab pages.
1323 // This switching will cause the manager to commit the RVH/DOMUI. So we'll
1324 // have a committed DOM UI in this case.
1325 //
1326 // This condition handles all of these cases:
1327 //
1328 // - First load in first tab: no committed nav entry + pending nav entry +
1329 // pending dom ui:
1330 // -> Use pending DOM UI if any.
1331 //
1332 // - First load in second tab: no committed nav entry + pending nav entry +
1333 // no pending DOM UI:
1334 // -> Use the committed DOM UI if any.
1335 //
1336 // - Second navigation in any tab: committed nav entry + pending nav entry:
1337 // -> Use pending DOM UI if any.
1338 //
1339 // - Normal state with no load: committed nav entry + no pending nav entry:
1340 // -> Use committed DOM UI.
1341 if (controller_.pending_entry() &&
1342 (controller_.GetLastCommittedEntry() ||
1343 render_manager_.pending_dom_ui()))
1344 return render_manager_.pending_dom_ui();
1345 return render_manager_.dom_ui();
1346}
[email protected]420ae012009-04-24 05:16:321347
1348void TabContents::DidNavigateMainFramePostCommit(
1349 const NavigationController::LoadCommittedDetails& details,
1350 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]c2e74fe82009-09-03 17:57:441351 if (opener_dom_ui_type_ != DOMUIFactory::kNoDOMUI) {
1352 // If this is a window.open navigation, use the same DOMUI as the renderer
1353 // that opened the window, as long as both renderers have the same
1354 // privileges.
1355 if (opener_dom_ui_type_ == DOMUIFactory::GetDOMUIType(GetURL())) {
1356 DOMUI* dom_ui = DOMUIFactory::CreateDOMUIForURL(this, GetURL());
1357 // dom_ui might be NULL if the URL refers to a non-existent extension.
1358 if (dom_ui) {
1359 render_manager_.SetDOMUIPostCommit(dom_ui);
1360 dom_ui->RenderViewCreated(render_view_host());
1361 }
1362 }
1363 opener_dom_ui_type_ = DOMUIFactory::kNoDOMUI;
1364 }
1365
[email protected]420ae012009-04-24 05:16:321366 if (details.is_user_initiated_main_frame_load()) {
1367 // Clear the status bubble. This is a workaround for a bug where WebKit
1368 // doesn't let us know that the cursor left an element during a
1369 // transition (this is also why the mouse cursor remains as a hand after
1370 // clicking on a link); see bugs 1184641 and 980803. We don't want to
1371 // clear the bubble when a user navigates to a named anchor in the same
1372 // page.
1373 UpdateTargetURL(details.entry->page_id(), GURL());
1374
1375 // UpdateHelpersForDidNavigate will handle the case where the password_form
1376 // origin is valid.
1377 // TODO(brettw) bug 1343111: Password manager stuff in here needs to be
1378 // cleaned up and covered by tests.
1379 if (!params.password_form.origin.is_valid())
1380 GetPasswordManager()->DidNavigate();
1381 }
1382
1383 // The keyword generator uses the navigation entries, so must be called after
1384 // the commit.
1385 GenerateKeywordIfNecessary(params);
1386
1387 // Allow the new page to set the title again.
1388 received_page_title_ = false;
1389
1390 // Get the favicon, either from history or request it from the net.
1391 fav_icon_helper_.FetchFavIcon(details.entry->url());
1392
[email protected]56ce6e52009-10-27 00:10:521393 // Clear all page and browser action state for this tab, unless this is an
1394 // in-page navigation.
[email protected]92c6f9b92009-10-24 04:35:081395 url_canon::Replacements<char> replacements;
1396 replacements.ClearRef();
1397 if (params.url.ReplaceComponents(replacements) !=
1398 params.referrer.ReplaceComponents(replacements)) {
[email protected]92c6f9b92009-10-24 04:35:081399 ExtensionsService* service = profile()->GetExtensionsService();
1400 if (service) {
1401 for (size_t i = 0; i < service->extensions()->size(); ++i) {
[email protected]5d246db22009-10-27 06:17:571402 ExtensionAction* browser_action =
[email protected]92c6f9b92009-10-24 04:35:081403 service->extensions()->at(i)->browser_action();
[email protected]56ce6e52009-10-27 00:10:521404 if (browser_action) {
1405 browser_action->ClearAllValuesForTab(controller().session_id().id());
1406 NotificationService::current()->Notify(
1407 NotificationType::EXTENSION_BROWSER_ACTION_UPDATED,
[email protected]5d246db22009-10-27 06:17:571408 Source<ExtensionAction>(browser_action),
[email protected]56ce6e52009-10-27 00:10:521409 NotificationService::NoDetails());
1410 }
[email protected]92c6f9b92009-10-24 04:35:081411
[email protected]5d246db22009-10-27 06:17:571412 ExtensionAction* page_action =
[email protected]56ce6e52009-10-27 00:10:521413 service->extensions()->at(i)->page_action();
1414 if (page_action) {
1415 page_action->ClearAllValuesForTab(controller().session_id().id());
1416 PageActionStateChanged();
1417 }
[email protected]92c6f9b92009-10-24 04:35:081418 }
[email protected]aece2c7f2009-08-27 20:43:171419 }
1420 }
[email protected]ce5c4502009-05-06 16:46:111421
[email protected]420ae012009-04-24 05:16:321422 // Close constrained popups if necessary.
1423 MaybeCloseChildWindows(details.previous_url, details.entry->url());
1424
1425 // Update the starred state.
1426 UpdateStarredStateForCurrentURL();
1427}
1428
1429void TabContents::DidNavigateAnyFramePostCommit(
1430 RenderViewHost* render_view_host,
1431 const NavigationController::LoadCommittedDetails& details,
1432 const ViewHostMsg_FrameNavigate_Params& params) {
1433 // If we navigate, start showing messages again. This does nothing to prevent
1434 // a malicious script from spamming messages, since the script could just
1435 // reload the page to stop blocking.
1436 suppress_javascript_messages_ = false;
1437
[email protected]420ae012009-04-24 05:16:321438 // Notify the password manager of the navigation or form submit.
1439 // TODO(brettw) bug 1343111: Password manager stuff in here needs to be
1440 // cleaned up and covered by tests.
1441 if (params.password_form.origin.is_valid())
1442 GetPasswordManager()->ProvisionallySavePassword(params.password_form);
1443}
1444
1445void TabContents::MaybeCloseChildWindows(const GURL& previous_url,
1446 const GURL& current_url) {
1447 if (net::RegistryControlledDomainService::SameDomainOrHost(
1448 previous_url, current_url))
1449 return;
1450
1451 // Clear out any child windows since we are leaving this page entirely.
1452 // We use indices instead of iterators in case CloseWindow does something
1453 // that may invalidate an iterator.
1454 int size = static_cast<int>(child_windows_.size());
1455 for (int i = size - 1; i >= 0; --i) {
1456 ConstrainedWindow* window = child_windows_[i];
1457 if (window)
1458 window->CloseConstrainedWindow();
1459 }
[email protected]e29300cd2009-07-23 21:14:591460
1461 // Close the popup container.
1462 if (blocked_popups_) {
1463 blocked_popups_->Destroy();
1464 blocked_popups_ = NULL;
1465 }
[email protected]420ae012009-04-24 05:16:321466}
1467
1468void TabContents::UpdateStarredStateForCurrentURL() {
1469 BookmarkModel* model = profile()->GetBookmarkModel();
1470 const bool old_state = is_starred_;
1471 is_starred_ = (model && model->IsBookmarked(GetURL()));
1472
1473 if (is_starred_ != old_state && delegate())
1474 delegate()->URLStarredChanged(this, is_starred_);
1475}
1476
1477void TabContents::UpdateAlternateErrorPageURL() {
1478 GURL url = GetAlternateErrorPageURL();
1479 render_view_host()->SetAlternateErrorPageURL(url);
1480}
1481
1482void TabContents::UpdateWebPreferences() {
1483 render_view_host()->UpdateWebPreferences(GetWebkitPrefs());
1484}
1485
1486void TabContents::UpdateMaxPageIDIfNecessary(SiteInstance* site_instance,
1487 RenderViewHost* rvh) {
1488 // If we are creating a RVH for a restored controller, then we might
1489 // have more page IDs than the SiteInstance's current max page ID. We must
1490 // make sure that the max page ID is larger than any restored page ID.
1491 // Note that it is ok for conflicting page IDs to exist in another tab
1492 // (i.e., NavigationController), but if any page ID is larger than the max,
1493 // the back/forward list will get confused.
1494 int max_restored_page_id = controller_.max_restored_page_id();
1495 if (max_restored_page_id > 0) {
1496 int curr_max_page_id = site_instance->max_page_id();
1497 if (max_restored_page_id > curr_max_page_id) {
1498 // Need to update the site instance immediately.
1499 site_instance->UpdateMaxPageID(max_restored_page_id);
1500
1501 // Also tell the renderer to update its internal representation. We
1502 // need to reserve enough IDs to make all restored page IDs less than
1503 // the max.
1504 if (curr_max_page_id < 0)
1505 curr_max_page_id = 0;
1506 rvh->ReservePageIDRange(max_restored_page_id - curr_max_page_id);
1507 }
1508 }
1509}
1510
1511void TabContents::UpdateHistoryForNavigation(
[email protected]ebe89e062009-08-13 23:16:541512 const GURL& virtual_url,
[email protected]befd8d822009-07-01 04:51:471513 const NavigationController::LoadCommittedDetails& details,
[email protected]420ae012009-04-24 05:16:321514 const ViewHostMsg_FrameNavigate_Params& params) {
1515 if (profile()->IsOffTheRecord())
1516 return;
1517
1518 // Add to history service.
1519 HistoryService* hs = profile()->GetHistoryService(Profile::IMPLICIT_ACCESS);
1520 if (hs) {
1521 if (PageTransition::IsMainFrame(params.transition) &&
[email protected]ebe89e062009-08-13 23:16:541522 virtual_url != params.url) {
1523 // Hack on the "virtual" URL so that it will appear in history. For some
[email protected]420ae012009-04-24 05:16:321524 // types of URLs, we will display a magic URL that is different from where
1525 // the page is actually navigated. We want the user to see in history
[email protected]ebe89e062009-08-13 23:16:541526 // what they saw in the URL bar, so we add the virtual URL as a redirect.
1527 // This only applies to the main frame, as the virtual URL doesn't apply
[email protected]420ae012009-04-24 05:16:321528 // to sub-frames.
1529 std::vector<GURL> redirects = params.redirects;
1530 if (!redirects.empty())
[email protected]ebe89e062009-08-13 23:16:541531 redirects.back() = virtual_url;
1532 hs->AddPage(virtual_url, this, params.page_id, params.referrer,
[email protected]befd8d822009-07-01 04:51:471533 params.transition, redirects, details.did_replace_entry);
[email protected]420ae012009-04-24 05:16:321534 } else {
1535 hs->AddPage(params.url, this, params.page_id, params.referrer,
[email protected]befd8d822009-07-01 04:51:471536 params.transition, params.redirects,
1537 details.did_replace_entry);
[email protected]420ae012009-04-24 05:16:321538 }
1539 }
1540}
1541
1542bool TabContents::UpdateTitleForEntry(NavigationEntry* entry,
1543 const std::wstring& title) {
1544 // For file URLs without a title, use the pathname instead. In the case of a
1545 // synthesized title, we don't want the update to count toward the "one set
1546 // per page of the title to history."
1547 std::wstring final_title;
1548 bool explicit_set;
1549 if (entry->url().SchemeIsFile() && title.empty()) {
1550 final_title = UTF8ToWide(entry->url().ExtractFileName());
1551 explicit_set = false; // Don't count synthetic titles toward the set limit.
1552 } else {
1553 TrimWhitespace(title, TRIM_ALL, &final_title);
1554 explicit_set = true;
1555 }
1556
1557 if (final_title == UTF16ToWideHack(entry->title()))
1558 return false; // Nothing changed, don't bother.
1559
1560 entry->set_title(WideToUTF16Hack(final_title));
1561
1562 // Update the history system for this page.
1563 if (!profile()->IsOffTheRecord() && !received_page_title_) {
1564 HistoryService* hs =
1565 profile()->GetHistoryService(Profile::IMPLICIT_ACCESS);
1566 if (hs)
[email protected]ebe89e062009-08-13 23:16:541567 hs->SetPageTitle(entry->virtual_url(), final_title);
[email protected]420ae012009-04-24 05:16:321568
1569 // Don't allow the title to be saved again for explicitly set ones.
1570 received_page_title_ = explicit_set;
1571 }
1572
1573 // Lastly, set the title for the view.
1574 view_->SetPageTitle(final_title);
1575
1576 return true;
1577}
1578
1579void TabContents::NotifySwapped() {
1580 // After sending out a swap notification, we need to send a disconnect
1581 // notification so that clients that pick up a pointer to |this| can NULL the
1582 // pointer. See Bug 1230284.
1583 notify_disconnection_ = true;
1584 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341585 NotificationType::TAB_CONTENTS_SWAPPED,
[email protected]d82ed61e2009-06-16 02:46:221586 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321587 NotificationService::NoDetails());
1588}
1589
1590void TabContents::NotifyConnected() {
1591 notify_disconnection_ = true;
1592 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341593 NotificationType::TAB_CONTENTS_CONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221594 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321595 NotificationService::NoDetails());
1596}
1597
1598void TabContents::NotifyDisconnected() {
1599 if (!notify_disconnection_)
1600 return;
1601
1602 notify_disconnection_ = false;
1603 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341604 NotificationType::TAB_CONTENTS_DISCONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221605 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321606 NotificationService::NoDetails());
1607}
1608
1609void TabContents::GenerateKeywordIfNecessary(
1610 const ViewHostMsg_FrameNavigate_Params& params) {
1611 if (!params.searchable_form_url.is_valid())
1612 return;
1613
1614 if (profile()->IsOffTheRecord())
1615 return;
1616
1617 int last_index = controller_.last_committed_entry_index();
1618 // When there was no previous page, the last index will be 0. This is
1619 // normally due to a form submit that opened in a new tab.
1620 // TODO(brettw) bug 916126: we should support keywords when form submits
1621 // happen in new tabs.
1622 if (last_index <= 0)
1623 return;
1624 const NavigationEntry* previous_entry =
1625 controller_.GetEntryAtIndex(last_index - 1);
1626 if (IsFormSubmit(previous_entry)) {
1627 // Only generate a keyword if the previous page wasn't itself a form
1628 // submit.
1629 return;
1630 }
1631
1632 GURL keyword_url = previous_entry->user_typed_url().is_valid() ?
1633 previous_entry->user_typed_url() : previous_entry->url();
1634 std::wstring keyword =
1635 TemplateURLModel::GenerateKeyword(keyword_url, true); // autodetected
1636 if (keyword.empty())
1637 return;
1638
1639 TemplateURLModel* url_model = profile()->GetTemplateURLModel();
1640 if (!url_model)
1641 return;
1642
1643 if (!url_model->loaded()) {
1644 url_model->Load();
1645 return;
1646 }
1647
1648 const TemplateURL* current_url;
1649 std::wstring url = UTF8ToWide(params.searchable_form_url.spec());
1650 if (!url_model->CanReplaceKeyword(keyword, url, &current_url))
1651 return;
1652
1653 if (current_url) {
1654 if (current_url->originating_url().is_valid()) {
1655 // The existing keyword was generated from an OpenSearch description
1656 // document, don't regenerate.
1657 return;
1658 }
1659 url_model->Remove(current_url);
1660 }
1661 TemplateURL* new_url = new TemplateURL();
1662 new_url->set_keyword(keyword);
1663 new_url->set_short_name(keyword);
1664 new_url->SetURL(url, 0, 0);
1665 new_url->add_input_encoding(params.searchable_form_encoding);
1666 DCHECK(controller_.GetLastCommittedEntry());
1667 const GURL& favicon_url =
1668 controller_.GetLastCommittedEntry()->favicon().url();
1669 if (favicon_url.is_valid()) {
1670 new_url->SetFavIconURL(favicon_url);
1671 } else {
1672 // The favicon url isn't valid. This means there really isn't a favicon,
1673 // or the favicon url wasn't obtained before the load started. This assumes
1674 // the later.
1675 // TODO(sky): Need a way to set the favicon that doesn't involve generating
1676 // its url.
1677 new_url->SetFavIconURL(TemplateURL::GenerateFaviconURL(params.referrer));
1678 }
1679 new_url->set_safe_for_autoreplace(true);
1680 url_model->Add(new_url);
1681}
1682
[email protected]bf5c2ff392009-07-08 16:24:331683void TabContents::OnUserGesture() {
1684 // See comment in RenderViewHostDelegate::OnUserGesture as to why we do this.
1685 DownloadRequestManager* drm = g_browser_process->download_request_manager();
1686 if (drm)
1687 drm->OnUserGesture(this);
[email protected]e7eaedde2009-09-25 20:09:491688 ExternalProtocolHandler::OnUserGesture();
[email protected]bf5c2ff392009-07-08 16:24:331689 controller_.OnUserGesture();
1690}
1691
1692void TabContents::OnFindReply(int request_id,
1693 int number_of_matches,
1694 const gfx::Rect& selection_rect,
1695 int active_match_ordinal,
1696 bool final_update) {
[email protected]3ce7ced2009-07-24 18:52:421697 // Ignore responses for requests that have been aborted.
1698 if (find_op_aborted_)
1699 return;
1700
[email protected]bf5c2ff392009-07-08 16:24:331701 // Ignore responses for requests other than the one we have most recently
1702 // issued. That way we won't act on stale results when the user has
1703 // already typed in another query.
1704 if (request_id != current_find_request_id_)
1705 return;
1706
1707 if (number_of_matches == -1)
1708 number_of_matches = last_search_result_.number_of_matches();
1709 if (active_match_ordinal == -1)
1710 active_match_ordinal = last_search_result_.active_match_ordinal();
1711
1712 gfx::Rect selection = selection_rect;
1713 if (selection.IsEmpty())
1714 selection = last_search_result_.selection_rect();
1715
1716 // Notify the UI, automation and any other observers that a find result was
1717 // found.
1718 last_search_result_ = FindNotificationDetails(request_id, number_of_matches,
1719 selection, active_match_ordinal,
1720 final_update);
1721 NotificationService::current()->Notify(
1722 NotificationType::FIND_RESULT_AVAILABLE,
1723 Source<TabContents>(this),
1724 Details<FindNotificationDetails>(&last_search_result_));
1725}
1726
1727void TabContents::GoToEntryAtOffset(int offset) {
[email protected]f9cc4c452009-10-13 14:56:381728 if (!delegate_ || delegate_->OnGoToEntryOffset(offset))
1729 controller_.GoToOffset(offset);
[email protected]bf5c2ff392009-07-08 16:24:331730}
1731
1732void TabContents::GetHistoryListCount(int* back_list_count,
1733 int* forward_list_count) {
1734 int current_index = controller_.last_committed_entry_index();
1735 *back_list_count = current_index;
1736 *forward_list_count = controller_.entry_count() - current_index - 1;
1737}
1738
1739void TabContents::OnMissingPluginStatus(int status) {
1740#if defined(OS_WIN)
1741// TODO(PORT): pull in when plug-ins work
1742 GetPluginInstaller()->OnMissingPluginStatus(status);
1743#endif
1744}
1745
1746void TabContents::OnCrashedPlugin(const FilePath& plugin_path) {
[email protected]bf5c2ff392009-07-08 16:24:331747 DCHECK(!plugin_path.value().empty());
1748
1749 std::wstring plugin_name = plugin_path.ToWStringHack();
[email protected]d32d46182009-07-23 00:09:351750#if defined(OS_WIN) || defined(OS_MACOSX)
[email protected]bf5c2ff392009-07-08 16:24:331751 scoped_ptr<FileVersionInfo> version_info(
1752 FileVersionInfo::CreateFileVersionInfo(plugin_path));
1753 if (version_info.get()) {
1754 const std::wstring& product_name = version_info->product_name();
[email protected]996fd702009-09-04 19:12:371755 if (!product_name.empty()) {
[email protected]bf5c2ff392009-07-08 16:24:331756 plugin_name = product_name;
[email protected]996fd702009-09-04 19:12:371757#if defined(OS_MACOSX)
1758 // Many plugins on the Mac have .plugin in the actual name, which looks
1759 // terrible, so look for that and strip it off if present.
1760 const std::wstring plugin_extension(L".plugin");
1761 if (EndsWith(plugin_name, plugin_extension, true))
1762 plugin_name.erase(plugin_name.length() - plugin_extension.length());
1763#endif // OS_MACOSX
1764 }
[email protected]bf5c2ff392009-07-08 16:24:331765 }
[email protected]4a560ed2009-07-13 23:35:261766#else
1767 NOTIMPLEMENTED() << " convert plugin path to plugin name";
1768#endif
[email protected]bf5c2ff392009-07-08 16:24:331769 AddInfoBar(new SimpleAlertInfoBarDelegate(
1770 this, l10n_util::GetStringF(IDS_PLUGIN_CRASHED_PROMPT, plugin_name),
1771 NULL));
[email protected]bf5c2ff392009-07-08 16:24:331772}
1773
1774void TabContents::OnCrashedWorker() {
1775 AddInfoBar(new SimpleAlertInfoBarDelegate(
1776 this, l10n_util::GetString(IDS_WEBWORKER_CRASHED_PROMPT),
1777 NULL));
1778}
1779
1780void TabContents::OnDidGetApplicationInfo(
1781 int32 page_id,
1782 const webkit_glue::WebApplicationInfo& info) {
1783 if (pending_install_.page_id != page_id)
1784 return; // The user clicked create on a separate page. Ignore this.
1785
1786 pending_install_.callback_functor =
1787 new GearsCreateShortcutCallbackFunctor(this);
1788 GearsCreateShortcut(
1789 info, pending_install_.title, pending_install_.url, pending_install_.icon,
1790 NewCallback(pending_install_.callback_functor,
1791 &GearsCreateShortcutCallbackFunctor::Run));
1792}
1793
1794void TabContents::DidStartProvisionalLoadForFrame(
1795 RenderViewHost* render_view_host,
1796 bool is_main_frame,
1797 const GURL& url) {
1798 ProvisionalLoadDetails details(is_main_frame,
1799 controller_.IsURLInPageNavigation(url),
1800 url, std::string(), false);
1801 NotificationService::current()->Notify(
1802 NotificationType::FRAME_PROVISIONAL_LOAD_START,
1803 Source<NavigationController>(&controller_),
1804 Details<ProvisionalLoadDetails>(&details));
1805}
1806
1807void TabContents::DidStartReceivingResourceResponse(
1808 ResourceRequestDetails* details) {
1809 NotificationService::current()->Notify(
1810 NotificationType::RESOURCE_RESPONSE_STARTED,
1811 Source<NavigationController>(&controller()),
1812 Details<ResourceRequestDetails>(details));
1813}
1814
1815void TabContents::DidRedirectResource(ResourceRequestDetails* details) {
1816 NotificationService::current()->Notify(
1817 NotificationType::RESOURCE_RECEIVED_REDIRECT,
1818 Source<NavigationController>(&controller()),
1819 Details<ResourceRequestDetails>(details));
1820}
1821
1822void TabContents::DidLoadResourceFromMemoryCache(
1823 const GURL& url,
1824 const std::string& frame_origin,
1825 const std::string& main_frame_origin,
1826 const std::string& security_info) {
1827 // Send out a notification that we loaded a resource from our memory cache.
1828 int cert_id = 0, cert_status = 0, security_bits = 0;
1829 SSLManager::DeserializeSecurityInfo(security_info,
1830 &cert_id, &cert_status,
1831 &security_bits);
1832 LoadFromMemoryCacheDetails details(url, frame_origin, main_frame_origin,
[email protected]76543b92009-08-31 17:27:451833 process()->id(), cert_id, cert_status);
[email protected]bf5c2ff392009-07-08 16:24:331834
1835 NotificationService::current()->Notify(
1836 NotificationType::LOAD_FROM_MEMORY_CACHE,
1837 Source<NavigationController>(&controller_),
1838 Details<LoadFromMemoryCacheDetails>(&details));
1839}
1840
[email protected]e3d60e5d2009-09-25 21:08:291841void TabContents::DidDisplayInsecureContent() {
[email protected]09843b5f2009-09-26 08:30:591842 controller_.ssl_manager()->DidDisplayInsecureContent();
[email protected]e3d60e5d2009-09-25 21:08:291843}
1844
1845void TabContents::DidRunInsecureContent(const std::string& security_origin) {
[email protected]09843b5f2009-09-26 08:30:591846 controller_.ssl_manager()->DidRunInsecureContent(security_origin);
[email protected]e3d60e5d2009-09-25 21:08:291847}
1848
[email protected]bf5c2ff392009-07-08 16:24:331849void TabContents::DidFailProvisionalLoadWithError(
1850 RenderViewHost* render_view_host,
1851 bool is_main_frame,
1852 int error_code,
1853 const GURL& url,
1854 bool showing_repost_interstitial) {
1855 if (net::ERR_ABORTED == error_code) {
1856 // EVIL HACK ALERT! Ignore failed loads when we're showing interstitials.
1857 // This means that the interstitial won't be torn down properly, which is
1858 // bad. But if we have an interstitial, go back to another tab type, and
1859 // then load the same interstitial again, we could end up getting the first
1860 // interstitial's "failed" message (as a result of the cancel) when we're on
1861 // the second one.
1862 //
1863 // We can't tell this apart, so we think we're tearing down the current page
1864 // which will cause a crash later one. There is also some code in
1865 // RenderViewHostManager::RendererAbortedProvisionalLoad that is commented
1866 // out because of this problem.
1867 //
1868 // http://code.google.com/p/chromium/issues/detail?id=2855
1869 // Because this will not tear down the interstitial properly, if "back" is
1870 // back to another tab type, the interstitial will still be somewhat alive
1871 // in the previous tab type. If you navigate somewhere that activates the
1872 // tab with the interstitial again, you'll see a flash before the new load
1873 // commits of the interstitial page.
1874 if (showing_interstitial_page()) {
1875 LOG(WARNING) << "Discarding message during interstitial.";
1876 return;
1877 }
1878
1879 // This will discard our pending entry if we cancelled the load (e.g., if we
1880 // decided to download the file instead of load it). Only discard the
1881 // pending entry if the URLs match, otherwise the user initiated a navigate
1882 // before the page loaded so that the discard would discard the wrong entry.
1883 NavigationEntry* pending_entry = controller_.pending_entry();
1884 if (pending_entry && pending_entry->url() == url) {
1885 controller_.DiscardNonCommittedEntries();
1886 // Update the URL display.
1887 NotifyNavigationStateChanged(TabContents::INVALIDATE_URL);
1888 }
1889
1890 render_manager_.RendererAbortedProvisionalLoad(render_view_host);
1891 }
1892
1893 // Send out a notification that we failed a provisional load with an error.
1894 ProvisionalLoadDetails details(is_main_frame,
1895 controller_.IsURLInPageNavigation(url),
1896 url, std::string(), false);
1897 details.set_error_code(error_code);
1898
1899 NotificationService::current()->Notify(
1900 NotificationType::FAIL_PROVISIONAL_LOAD_WITH_ERROR,
1901 Source<NavigationController>(&controller_),
1902 Details<ProvisionalLoadDetails>(&details));
1903}
1904
1905void TabContents::DocumentLoadedInFrame() {
1906 controller_.DocumentLoadedInFrame();
1907}
1908
[email protected]8d3347f2009-07-09 22:00:211909RenderViewHostDelegate::View* TabContents::GetViewDelegate() {
[email protected]420ae012009-04-24 05:16:321910 return view_.get();
1911}
1912
[email protected]8d3347f2009-07-09 22:00:211913RenderViewHostDelegate::RendererManagement*
1914TabContents::GetRendererManagementDelegate() {
1915 return &render_manager_;
1916}
1917
[email protected]bf5c2ff392009-07-08 16:24:331918RenderViewHostDelegate::BrowserIntegration*
[email protected]8d3347f2009-07-09 22:00:211919 TabContents::GetBrowserIntegrationDelegate() {
1920 return this;
[email protected]bf5c2ff392009-07-08 16:24:331921}
1922
[email protected]8d3347f2009-07-09 22:00:211923RenderViewHostDelegate::Resource* TabContents::GetResourceDelegate() {
1924 return this;
[email protected]bf5c2ff392009-07-08 16:24:331925}
1926
[email protected]8d3347f2009-07-09 22:00:211927RenderViewHostDelegate::Save* TabContents::GetSaveDelegate() {
[email protected]420ae012009-04-24 05:16:321928 return save_package_.get(); // May be NULL, but we can return NULL.
1929}
1930
[email protected]8d3347f2009-07-09 22:00:211931RenderViewHostDelegate::Printing* TabContents::GetPrintingDelegate() {
1932 return &printing_;
1933}
1934
1935RenderViewHostDelegate::FavIcon* TabContents::GetFavIconDelegate() {
1936 return &fav_icon_helper_;
1937}
1938
[email protected]c54d269e2009-10-21 18:07:381939RenderViewHostDelegate::FormFieldHistory*
1940TabContents::GetFormFieldHistoryDelegate() {
[email protected]19289d02009-10-21 21:47:001941 if (form_field_history_manager_.get() == NULL)
1942 form_field_history_manager_.reset(new FormFieldHistoryManager(this));
1943 return form_field_history_manager_.get();
[email protected]bf5c2ff392009-07-08 16:24:331944}
1945
[email protected]21674df2009-10-28 19:51:211946RenderViewHostDelegate::AutoFill* TabContents::GetAutoFillDelegate() {
1947 if (autofill_manager_.get() == NULL)
1948 autofill_manager_.reset(new AutoFillManager(this));
1949 return autofill_manager_.get();
1950}
1951
[email protected]80d96fa2009-06-10 22:34:511952RendererPreferences TabContents::GetRendererPrefs() const {
[email protected]840b1512009-07-21 16:53:461953 return renderer_preferences_;
[email protected]80d96fa2009-06-10 22:34:511954}
1955
[email protected]57c6a652009-05-04 07:58:341956TabContents* TabContents::GetAsTabContents() {
1957 return this;
1958}
1959
[email protected]3e7cf1362009-08-21 17:47:081960void TabContents::AddBlockedNotice(const GURL& url, const string16& reason) {
1961 CreateBlockedPopupContainerIfNecessary();
1962 blocked_popups_->AddBlockedNotice(url, reason);
1963}
1964
[email protected]7b291f92009-08-14 05:43:531965ViewType::Type TabContents::GetRenderViewType() const {
1966 return ViewType::TAB_CONTENTS;
1967}
1968
1969int TabContents::GetBrowserWindowID() const {
1970 return controller().window_id().id();
1971}
1972
[email protected]420ae012009-04-24 05:16:321973void TabContents::RenderViewCreated(RenderViewHost* render_view_host) {
[email protected]2e4633c2009-07-09 16:58:061974 NotificationService::current()->Notify(
1975 NotificationType::RENDER_VIEW_HOST_CREATED_FOR_TAB,
1976 Source<TabContents>(this),
1977 Details<RenderViewHost>(render_view_host));
[email protected]420ae012009-04-24 05:16:321978 NavigationEntry* entry = controller_.GetActiveEntry();
1979 if (!entry)
1980 return;
1981
1982 // When we're creating views, we're still doing initial setup, so we always
1983 // use the pending DOM UI rather than any possibly existing committed one.
1984 if (render_manager_.pending_dom_ui()) {
1985 render_manager_.pending_dom_ui()->RenderViewCreated(render_view_host);
1986 }
1987
1988 if (entry->IsViewSourceMode()) {
1989 // Put the renderer in view source mode.
1990 render_view_host->Send(
1991 new ViewMsg_EnableViewSourceMode(render_view_host->routing_id()));
1992 }
[email protected]0666aef2009-05-13 19:48:081993
1994 view()->RenderViewCreated(render_view_host);
[email protected]420ae012009-04-24 05:16:321995}
1996
1997void TabContents::RenderViewReady(RenderViewHost* rvh) {
1998 if (rvh != render_view_host()) {
1999 // Don't notify the world, since this came from a renderer in the
2000 // background.
2001 return;
2002 }
2003
2004 NotifyConnected();
[email protected]9d8a4642009-07-29 17:25:302005 bool was_crashed = is_crashed();
[email protected]420ae012009-04-24 05:16:322006 SetIsCrashed(false);
[email protected]9d8a4642009-07-29 17:25:302007
2008 // Restore the focus to the tab (otherwise the focus will be on the top
2009 // window).
2010 if (was_crashed && !FocusLocationBarByDefault())
2011 Focus();
[email protected]420ae012009-04-24 05:16:322012}
2013
2014void TabContents::RenderViewGone(RenderViewHost* rvh) {
2015 // Ask the print preview if this renderer was valuable.
2016 if (!printing_.OnRenderViewGone(rvh))
2017 return;
2018 if (rvh != render_view_host()) {
2019 // The pending page's RenderViewHost is gone.
2020 return;
2021 }
2022
2023 SetIsLoading(false, NULL);
2024 NotifyDisconnected();
2025 SetIsCrashed(true);
2026
[email protected]8e537302009-06-10 21:19:582027 // Tell the view that we've crashed so it can prepare the sad tab page.
2028 view_->OnTabCrashed();
[email protected]420ae012009-04-24 05:16:322029
2030 // Hide any visible hung renderer warning for this web contents' process.
[email protected]d82ed61e2009-06-16 02:46:222031 HungRendererDialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:322032}
2033
[email protected]2e4633c2009-07-09 16:58:062034void TabContents::RenderViewDeleted(RenderViewHost* rvh) {
2035 NotificationService::current()->Notify(
2036 NotificationType::RENDER_VIEW_HOST_DELETED,
2037 Source<TabContents>(this),
2038 Details<RenderViewHost>(rvh));
[email protected]fb7b79f72009-11-06 18:00:482039 render_manager_.RenderViewDeleted(rvh);
[email protected]2e4633c2009-07-09 16:58:062040}
2041
[email protected]420ae012009-04-24 05:16:322042void TabContents::DidNavigate(RenderViewHost* rvh,
2043 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]8030f012009-09-25 18:09:372044 int extra_invalidate_flags = 0;
2045
2046 if (PageTransition::IsMainFrame(params.transition)) {
[email protected]20ede072009-10-06 00:28:352047 bool was_bookmark_bar_visible = ShouldShowBookmarkBar();
[email protected]8030f012009-09-25 18:09:372048 bool was_extension_shelf_visible = IsExtensionShelfAlwaysVisible();
2049
[email protected]420ae012009-04-24 05:16:322050 render_manager_.DidNavigateMainFrame(rvh);
2051
[email protected]20ede072009-10-06 00:28:352052 if (was_bookmark_bar_visible != ShouldShowBookmarkBar())
[email protected]8030f012009-09-25 18:09:372053 extra_invalidate_flags |= INVALIDATE_BOOKMARK_BAR;
2054 if (was_extension_shelf_visible != IsExtensionShelfAlwaysVisible())
2055 extra_invalidate_flags |= INVALIDATE_EXTENSION_SHELF;
2056 }
2057
[email protected]420ae012009-04-24 05:16:322058 // Update the site of the SiteInstance if it doesn't have one yet.
2059 if (!GetSiteInstance()->has_site())
2060 GetSiteInstance()->SetSite(params.url);
2061
2062 // Need to update MIME type here because it's referred to in
2063 // UpdateNavigationCommands() called by RendererDidNavigate() to
2064 // determine whether or not to enable the encoding menu.
2065 // It's updated only for the main frame. For a subframe,
2066 // RenderView::UpdateURL does not set params.contents_mime_type.
2067 // (see http://code.google.com/p/chromium/issues/detail?id=2929 )
2068 // TODO(jungshik): Add a test for the encoding menu to avoid
2069 // regressing it again.
2070 if (PageTransition::IsMainFrame(params.transition))
2071 contents_mime_type_ = params.contents_mime_type;
2072
2073 NavigationController::LoadCommittedDetails details;
[email protected]8030f012009-09-25 18:09:372074 bool did_navigate = controller_.RendererDidNavigate(
2075 params, extra_invalidate_flags, &details);
[email protected]3e90d4a2009-07-03 17:38:392076
2077 // Update history. Note that this needs to happen after the entry is complete,
2078 // which WillNavigate[Main,Sub]Frame will do before this function is called.
[email protected]2e29d922009-10-22 22:13:352079 if (params.should_update_history &&
2080 (!delegate() || delegate()->ShouldAddNavigationsToHistory())) {
[email protected]3e90d4a2009-07-03 17:38:392081 // Most of the time, the displayURL matches the loaded URL, but for about:
2082 // URLs, we use a data: URL as the real value. We actually want to save
2083 // the about: URL to the history db and keep the data: URL hidden. This is
2084 // what the TabContents' URL getter does.
2085 UpdateHistoryForNavigation(GetURL(), details, params);
2086 }
2087
[email protected]76543b92009-08-31 17:27:452088 if (!did_navigate)
2089 return; // No navigation happened.
[email protected]420ae012009-04-24 05:16:322090
2091 // DO NOT ADD MORE STUFF TO THIS FUNCTION! Your component should either listen
2092 // for the appropriate notification (best) or you can add it to
2093 // DidNavigateMainFramePostCommit / DidNavigateAnyFramePostCommit (only if
2094 // necessary, please).
2095
2096 // Run post-commit tasks.
2097 if (details.is_main_frame)
2098 DidNavigateMainFramePostCommit(details, params);
2099 DidNavigateAnyFramePostCommit(rvh, details, params);
2100}
2101
2102void TabContents::UpdateState(RenderViewHost* rvh,
2103 int32 page_id,
2104 const std::string& state) {
2105 DCHECK(rvh == render_view_host());
2106
2107 // We must be prepared to handle state updates for any page, these occur
2108 // when the user is scrolling and entering form data, as well as when we're
2109 // leaving a page, in which case our state may have already been moved to
2110 // the next page. The navigation controller will look up the appropriate
2111 // NavigationEntry and update it when it is notified via the delegate.
2112
2113 int entry_index = controller_.GetEntryIndexWithPageID(
2114 GetSiteInstance(), page_id);
2115 if (entry_index < 0)
2116 return;
2117 NavigationEntry* entry = controller_.GetEntryAtIndex(entry_index);
2118
2119 if (state == entry->content_state())
2120 return; // Nothing to update.
2121 entry->set_content_state(state);
2122 controller_.NotifyEntryChanged(entry, entry_index);
2123}
2124
2125void TabContents::UpdateTitle(RenderViewHost* rvh,
2126 int32 page_id, const std::wstring& title) {
2127 // If we have a title, that's a pretty good indication that we've started
2128 // getting useful data.
2129 SetNotWaitingForResponse();
2130
2131 DCHECK(rvh == render_view_host());
2132 NavigationEntry* entry = controller_.GetEntryWithPageID(GetSiteInstance(),
2133 page_id);
2134 if (!entry || !UpdateTitleForEntry(entry, title))
2135 return;
2136
2137 // Broadcast notifications when the UI should be updated.
2138 if (entry == controller_.GetEntryAtOffset(0))
[email protected]f1cd5e82009-10-23 17:19:032139 NotifyNavigationStateChanged(INVALIDATE_TITLE);
[email protected]420ae012009-04-24 05:16:322140}
2141
[email protected]420ae012009-04-24 05:16:322142void TabContents::UpdateEncoding(RenderViewHost* render_view_host,
[email protected]41fc0322009-09-04 22:23:402143 const std::string& encoding) {
[email protected]420ae012009-04-24 05:16:322144 set_encoding(encoding);
2145}
2146
2147void TabContents::UpdateTargetURL(int32 page_id, const GURL& url) {
2148 if (delegate())
2149 delegate()->UpdateTargetURL(this, url);
2150}
2151
2152void TabContents::UpdateThumbnail(const GURL& url,
2153 const SkBitmap& bitmap,
2154 const ThumbnailScore& score) {
2155 // Tell History about this thumbnail
[email protected]449478302009-06-09 20:04:282156 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kThumbnailStore)) {
[email protected]72d0fc72009-07-09 17:30:492157 if (!profile()->IsOffTheRecord())
[email protected]42339402009-07-17 17:27:192158 profile()->GetThumbnailStore()->SetPageThumbnail(url, bitmap,
2159 score, true);
[email protected]449478302009-06-09 20:04:282160 } else {
2161 HistoryService* hs;
2162 if (!profile()->IsOffTheRecord() &&
2163 (hs = profile()->GetHistoryService(Profile::IMPLICIT_ACCESS))) {
2164 hs->SetPageThumbnail(url, bitmap, score);
2165 }
[email protected]420ae012009-04-24 05:16:322166 }
2167}
2168
[email protected]8922e1f2009-10-03 05:01:262169void TabContents::UpdateInspectorSettings(const std::string& raw_settings) {
[email protected]0df30122009-06-03 12:13:082170 profile()->GetPrefs()->SetString(prefs::kWebKitInspectorSettings,
[email protected]8922e1f2009-10-03 05:01:262171 UTF8ToWide(raw_settings));
[email protected]0df30122009-06-03 12:13:082172}
2173
[email protected]420ae012009-04-24 05:16:322174void TabContents::Close(RenderViewHost* rvh) {
[email protected]07707302009-11-06 00:50:292175 // The UI may be in an event-tracking loop, such as between the
2176 // mouse-down and mouse-up in text selection or a button click.
2177 // Defer the close until after tracking is complete, so that we
2178 // don't free objects out from under the UI.
2179 // TODO(shess): This could probably be integrated with the
2180 // IsDoingDrag() test below. Punting for now because I need more
2181 // research to understand how this impacts platforms other than Mac.
2182 // TODO(shess): This could get more fine-grained. For instance,
2183 // closing a tab in another window while selecting text in the
2184 // current window's Omnibox should be just fine.
2185 if (view()->IsEventTracking()) {
2186 view()->CloseTabAfterEventTracking();
2187 return;
2188 }
2189
[email protected]24a4d1062009-07-10 23:10:422190 // If we close the tab while we're in the middle of a drag, we'll crash.
2191 // Instead, cancel the drag and close it as soon as the drag ends.
2192 if (view()->IsDoingDrag()) {
2193 view()->CancelDragAndCloseTab();
2194 return;
2195 }
2196
[email protected]420ae012009-04-24 05:16:322197 // Ignore this if it comes from a RenderViewHost that we aren't showing.
2198 if (delegate() && rvh == render_view_host())
2199 delegate()->CloseContents(this);
2200}
2201
2202void TabContents::RequestMove(const gfx::Rect& new_bounds) {
2203 if (delegate() && delegate()->IsPopup(this))
2204 delegate()->MoveContents(this, new_bounds);
2205}
2206
[email protected]7ab1e7d62009-10-14 23:32:012207void TabContents::DidStartLoading() {
[email protected]420ae012009-04-24 05:16:322208 SetIsLoading(true, NULL);
2209}
2210
[email protected]7ab1e7d62009-10-14 23:32:012211void TabContents::DidStopLoading() {
[email protected]420ae012009-04-24 05:16:322212 scoped_ptr<LoadNotificationDetails> details;
2213
2214 NavigationEntry* entry = controller_.GetActiveEntry();
2215 // An entry may not exist for a stop when loading an initial blank page or
2216 // if an iframe injected by script into a blank page finishes loading.
2217 if (entry) {
2218 scoped_ptr<base::ProcessMetrics> metrics(
2219 base::ProcessMetrics::CreateProcessMetrics(
2220 process()->process().handle()));
2221
2222 base::TimeDelta elapsed = base::TimeTicks::Now() - current_load_start_;
2223
2224 details.reset(new LoadNotificationDetails(
[email protected]ebe89e062009-08-13 23:16:542225 entry->virtual_url(),
[email protected]420ae012009-04-24 05:16:322226 entry->transition_type(),
2227 elapsed,
2228 &controller_,
2229 controller_.GetCurrentEntryIndex()));
2230 }
2231
2232 // Tell PasswordManager we've finished a page load, which serves as a
2233 // green light to save pending passwords and reset itself.
2234 GetPasswordManager()->DidStopLoading();
2235
2236 SetIsLoading(false, details.get());
2237}
2238
[email protected]420ae012009-04-24 05:16:322239void TabContents::DidRedirectProvisionalLoad(int32 page_id,
2240 const GURL& source_url,
2241 const GURL& target_url) {
2242 NavigationEntry* entry;
2243 if (page_id == -1)
2244 entry = controller_.pending_entry();
2245 else
2246 entry = controller_.GetEntryWithPageID(GetSiteInstance(), page_id);
2247 if (!entry || entry->url() != source_url)
2248 return;
2249 entry->set_url(target_url);
2250}
2251
[email protected]420ae012009-04-24 05:16:322252void TabContents::RequestOpenURL(const GURL& url, const GURL& referrer,
2253 WindowOpenDisposition disposition) {
2254 if (render_manager_.dom_ui()) {
2255 // When we're a DOM UI, it will provide a page transition type for us (this
2256 // is so the new tab page can specify AUTO_BOOKMARK for automatically
2257 // generated suggestions).
2258 //
2259 // Note also that we hide the referrer for DOM UI pages. We don't really
[email protected]60e448982009-05-06 04:21:162260 // want web sites to see a referrer of "chrome://blah" (and some
2261 // chrome: URLs might have search terms or other stuff we don't want to
[email protected]420ae012009-04-24 05:16:322262 // send to the site), so we send no referrer.
2263 OpenURL(url, GURL(), disposition,
2264 render_manager_.dom_ui()->link_transition_type());
2265 } else {
2266 OpenURL(url, referrer, disposition, PageTransition::LINK);
2267 }
2268}
2269
[email protected]1ee614862009-07-06 20:29:182270void TabContents::DomOperationResponse(const std::string& json_string,
2271 int automation_id) {
2272}
2273
[email protected]420ae012009-04-24 05:16:322274void TabContents::ProcessDOMUIMessage(const std::string& message,
[email protected]e4dad9fb2009-10-06 18:15:582275 const Value* content,
[email protected]811bfe32009-07-01 08:46:252276 int request_id,
2277 bool has_callback) {
[email protected]420ae012009-04-24 05:16:322278 if (!render_manager_.dom_ui()) {
[email protected]c2e74fe82009-09-03 17:57:442279 // This can happen if someone uses window.open() to open an extension URL
2280 // from a non-extension context.
2281 render_view_host()->BlockExtensionRequest(request_id);
[email protected]420ae012009-04-24 05:16:322282 return;
2283 }
[email protected]811bfe32009-07-01 08:46:252284 render_manager_.dom_ui()->ProcessDOMUIMessage(message, content, request_id,
2285 has_callback);
[email protected]420ae012009-04-24 05:16:322286}
2287
2288void TabContents::ProcessExternalHostMessage(const std::string& message,
2289 const std::string& origin,
2290 const std::string& target) {
2291 if (delegate())
2292 delegate()->ForwardMessageToExternalHost(message, origin, target);
2293}
2294
[email protected]420ae012009-04-24 05:16:322295void TabContents::RunFileChooser(bool multiple_files,
2296 const string16& title,
2297 const FilePath& default_file) {
2298 if (!select_file_dialog_.get())
2299 select_file_dialog_ = SelectFileDialog::Create(this);
2300 SelectFileDialog::Type dialog_type =
2301 multiple_files ? SelectFileDialog::SELECT_OPEN_MULTI_FILE :
2302 SelectFileDialog::SELECT_OPEN_FILE;
2303 select_file_dialog_->SelectFile(dialog_type, title, default_file,
2304 NULL, 0, FILE_PATH_LITERAL(""),
2305 view_->GetTopLevelNativeWindow(), NULL);
2306}
2307
2308void TabContents::RunJavaScriptMessage(
2309 const std::wstring& message,
2310 const std::wstring& default_prompt,
2311 const GURL& frame_url,
2312 const int flags,
2313 IPC::Message* reply_msg,
2314 bool* did_suppress_message) {
2315 // Suppress javascript messages when requested and when inside a constrained
2316 // popup window (because that activates them and breaks them out of the
2317 // constrained window jail).
[email protected]91ed49a2009-07-07 21:29:552318 // Also suppress messages when showing an interstitial. The interstitial is
2319 // shown over the previous page, we don't want the hidden page dialogs to
2320 // interfere with the interstitial.
2321 bool suppress_this_message = suppress_javascript_messages_ ||
2322 showing_interstitial_page();
[email protected]420ae012009-04-24 05:16:322323 if (delegate())
2324 suppress_this_message |=
[email protected]b6c874582009-05-08 19:38:312325 (delegate()->GetConstrainingContents(this) != this);
[email protected]420ae012009-04-24 05:16:322326
2327 *did_suppress_message = suppress_this_message;
2328
2329 if (!suppress_this_message) {
2330 base::TimeDelta time_since_last_message(
2331 base::TimeTicks::Now() - last_javascript_message_dismissal_);
2332 bool show_suppress_checkbox = false;
2333 // Show a checkbox offering to suppress further messages if this message is
2334 // being displayed within kJavascriptMessageExpectedDelay of the last one.
2335 if (time_since_last_message <
2336 base::TimeDelta::FromMilliseconds(kJavascriptMessageExpectedDelay))
2337 show_suppress_checkbox = true;
2338
[email protected]d82ed61e2009-06-16 02:46:222339 RunJavascriptMessageBox(this, frame_url, flags, message, default_prompt,
[email protected]420ae012009-04-24 05:16:322340 show_suppress_checkbox, reply_msg);
2341 } else {
2342 // If we are suppressing messages, just reply as is if the user immediately
2343 // pressed "Cancel".
[email protected]beb440c2009-11-06 04:08:542344 OnMessageBoxClosed(reply_msg, false, std::wstring());
[email protected]420ae012009-04-24 05:16:322345 }
2346}
2347
2348void TabContents::RunBeforeUnloadConfirm(const std::wstring& message,
2349 IPC::Message* reply_msg) {
[email protected]7ab1e7d62009-10-14 23:32:012350 is_showing_before_unload_dialog_ = true;
[email protected]d82ed61e2009-06-16 02:46:222351 RunBeforeUnloadDialog(this, message, reply_msg);
[email protected]420ae012009-04-24 05:16:322352}
2353
2354void TabContents::ShowModalHTMLDialog(const GURL& url, int width, int height,
2355 const std::string& json_arguments,
2356 IPC::Message* reply_msg) {
2357 if (delegate()) {
2358 HtmlDialogUIDelegate* dialog_delegate =
2359 new ModalHtmlDialogDelegate(url, width, height, json_arguments,
[email protected]d82ed61e2009-06-16 02:46:222360 reply_msg, this);
[email protected]420ae012009-04-24 05:16:322361 delegate()->ShowHtmlDialog(dialog_delegate, NULL);
2362 }
2363}
2364
2365void TabContents::PasswordFormsSeen(
[email protected]ed3fb032009-06-16 19:50:562366 const std::vector<webkit_glue::PasswordForm>& forms) {
[email protected]420ae012009-04-24 05:16:322367 GetPasswordManager()->PasswordFormsSeen(forms);
2368}
2369
[email protected]420ae012009-04-24 05:16:322370// Checks to see if we should generate a keyword based on the OSDD, and if
2371// necessary uses TemplateURLFetcher to download the OSDD and create a keyword.
2372void TabContents::PageHasOSDD(RenderViewHost* render_view_host,
2373 int32 page_id, const GURL& url,
2374 bool autodetected) {
2375 // Make sure page_id is the current page, and the TemplateURLModel is loaded.
2376 DCHECK(url.is_valid());
2377 if (!IsActiveEntry(page_id))
2378 return;
2379 TemplateURLModel* url_model = profile()->GetTemplateURLModel();
2380 if (!url_model)
2381 return;
2382 if (!url_model->loaded()) {
2383 url_model->Load();
2384 return;
2385 }
2386 if (!profile()->GetTemplateURLFetcher())
2387 return;
2388
2389 if (profile()->IsOffTheRecord())
2390 return;
2391
2392 const NavigationEntry* entry = controller_.GetLastCommittedEntry();
2393 DCHECK(entry);
2394
2395 const NavigationEntry* base_entry = entry;
2396 if (IsFormSubmit(base_entry)) {
2397 // If the current page is a form submit, find the last page that was not
2398 // a form submit and use its url to generate the keyword from.
2399 int index = controller_.last_committed_entry_index() - 1;
2400 while (index >= 0 && IsFormSubmit(controller_.GetEntryAtIndex(index)))
2401 index--;
2402 if (index >= 0)
2403 base_entry = controller_.GetEntryAtIndex(index);
2404 else
2405 base_entry = NULL;
2406 }
2407
2408 // We want to use the user typed URL if available since that represents what
2409 // the user typed to get here, and fall back on the regular URL if not.
2410 if (!base_entry)
2411 return;
2412 GURL keyword_url = base_entry->user_typed_url().is_valid() ?
2413 base_entry->user_typed_url() : base_entry->url();
2414 if (!keyword_url.is_valid())
2415 return;
2416 std::wstring keyword = TemplateURLModel::GenerateKeyword(keyword_url,
2417 autodetected);
2418 if (keyword.empty())
2419 return;
2420 const TemplateURL* template_url =
2421 url_model->GetTemplateURLForKeyword(keyword);
2422 if (template_url && (!template_url->safe_for_autoreplace() ||
2423 template_url->originating_url() == url)) {
2424 // Either there is a user created TemplateURL for this keyword, or the
2425 // keyword has the same OSDD url and we've parsed it.
2426 return;
2427 }
2428
2429 // Download the OpenSearch description document. If this is successful a
2430 // new keyword will be created when done.
[email protected]420ae012009-04-24 05:16:322431 profile()->GetTemplateURLFetcher()->ScheduleDownload(
2432 keyword,
2433 url,
2434 base_entry->favicon().url(),
[email protected]d0cc9fb52009-06-23 02:26:162435 this,
[email protected]420ae012009-04-24 05:16:322436 autodetected);
2437}
2438
[email protected]420ae012009-04-24 05:16:322439GURL TabContents::GetAlternateErrorPageURL() const {
2440 GURL url;
2441 // Disable alternate error pages when in OffTheRecord/Incognito mode.
2442 if (profile()->IsOffTheRecord())
2443 return url;
2444
2445 PrefService* prefs = profile()->GetPrefs();
2446 DCHECK(prefs);
2447 if (prefs->GetBoolean(prefs::kAlternateErrorPagesEnabled)) {
[email protected]a33457b82009-09-21 23:07:152448 url = google_util::AppendGoogleLocaleParam(
2449 GURL(google_util::kLinkDoctorBaseURL));
[email protected]420ae012009-04-24 05:16:322450 url = google_util::AppendGoogleTLDParam(url);
2451 }
2452 return url;
2453}
2454
2455WebPreferences TabContents::GetWebkitPrefs() {
2456 PrefService* prefs = render_view_host()->process()->profile()->GetPrefs();
[email protected]7ccddb8c2009-08-04 17:36:552457 bool is_dom_ui = false;
2458 return RenderViewHostDelegateHelper::GetWebkitPrefs(prefs, is_dom_ui);
[email protected]420ae012009-04-24 05:16:322459}
2460
[email protected]420ae012009-04-24 05:16:322461void TabContents::OnJSOutOfMemory() {
2462 AddInfoBar(new SimpleAlertInfoBarDelegate(
2463 this, l10n_util::GetString(IDS_JS_OUT_OF_MEMORY_PROMPT), NULL));
2464}
2465
[email protected]420ae012009-04-24 05:16:322466void TabContents::OnCrossSiteResponse(int new_render_process_host_id,
2467 int new_request_id) {
[email protected]57c6a652009-05-04 07:58:342468 // Allows the TabContents to react when a cross-site response is ready to be
[email protected]420ae012009-04-24 05:16:322469 // delivered to a pending RenderViewHost. We must first run the onunload
2470 // handler of the old RenderViewHost before we can allow it to proceed.
2471 render_manager_.OnCrossSiteResponse(new_render_process_host_id,
2472 new_request_id);
2473}
2474
2475bool TabContents::CanBlur() const {
2476 return delegate() ? delegate()->CanBlur() : true;
2477}
2478
2479gfx::Rect TabContents::GetRootWindowResizerRect() const {
2480 if (delegate())
2481 return delegate()->GetRootWindowResizerRect();
2482 return gfx::Rect();
2483}
2484
2485void TabContents::RendererUnresponsive(RenderViewHost* rvh,
2486 bool is_during_unload) {
2487 if (is_during_unload) {
2488 // Hang occurred while firing the beforeunload/unload handler.
2489 // Pretend the handler fired so tab closing continues as if it had.
[email protected]829e7612009-04-25 01:15:112490 rvh->set_sudden_termination_allowed(true);
[email protected]420ae012009-04-24 05:16:322491
2492 if (!render_manager_.ShouldCloseTabOnUnresponsiveRenderer())
2493 return;
2494
2495 // If the tab hangs in the beforeunload/unload handler there's really
2496 // nothing we can do to recover. Pretend the unload listeners have
2497 // all fired and close the tab. If the hang is in the beforeunload handler
2498 // then the user will not have the option of cancelling the close.
2499 Close(rvh);
2500 return;
2501 }
2502
2503 if (render_view_host() && render_view_host()->IsRenderViewLive())
[email protected]d82ed61e2009-06-16 02:46:222504 HungRendererDialog::ShowForTabContents(this);
[email protected]420ae012009-04-24 05:16:322505}
2506
2507void TabContents::RendererResponsive(RenderViewHost* render_view_host) {
[email protected]d82ed61e2009-06-16 02:46:222508 HungRendererDialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:322509}
2510
2511void TabContents::LoadStateChanged(const GURL& url,
[email protected]094e5b22009-09-25 04:23:562512 net::LoadState load_state,
2513 uint64 upload_position,
2514 uint64 upload_size) {
[email protected]420ae012009-04-24 05:16:322515 load_state_ = load_state;
[email protected]094e5b22009-09-25 04:23:562516 upload_position_ = upload_position;
2517 upload_size_ = upload_size;
[email protected]d686e812009-06-03 19:10:292518 std::wstring languages =
2519 profile()->GetPrefs()->GetString(prefs::kAcceptLanguages);
2520 load_state_host_.clear();
2521 std::string host = url.host();
2522 net::IDNToUnicode(host.c_str(), host.size(), languages, &load_state_host_);
[email protected]420ae012009-04-24 05:16:322523 if (load_state_ == net::LOAD_STATE_READING_RESPONSE)
2524 SetNotWaitingForResponse();
2525 if (is_loading())
[email protected]c9cd2222009-05-06 05:16:502526 NotifyNavigationStateChanged(INVALIDATE_LOAD | INVALIDATE_TAB);
[email protected]420ae012009-04-24 05:16:322527}
2528
[email protected]1ee614862009-07-06 20:29:182529bool TabContents::IsExternalTabContainer() const {
2530 if (!delegate())
2531 return false;
2532
2533 return delegate()->IsExternalTabContainer();
2534}
2535
[email protected]1ee614862009-07-06 20:29:182536void TabContents::DidInsertCSS() {
2537 // This RVHDelegate function is used for extensions and not us.
[email protected]420ae012009-04-24 05:16:322538}
2539
[email protected]130efb02009-09-18 18:54:352540void TabContents::FocusedNodeChanged() {
2541 NotificationService::current()->Notify(
2542 NotificationType::FOCUS_CHANGED_IN_PAGE,
2543 Source<RenderViewHost>(render_view_host()),
2544 NotificationService::NoDetails());
2545}
2546
[email protected]420ae012009-04-24 05:16:322547void TabContents::FileSelected(const FilePath& path,
2548 int index, void* params) {
2549 render_view_host()->FileSelected(path);
2550}
2551
2552void TabContents::MultiFilesSelected(const std::vector<FilePath>& files,
2553 void* params) {
2554 render_view_host()->MultiFilesSelected(files);
2555}
2556
2557void TabContents::FileSelectionCanceled(void* params) {
2558 // If the user cancels choosing a file to upload we pass back an
2559 // empty vector.
2560 render_view_host()->MultiFilesSelected(std::vector<FilePath>());
2561}
2562
2563void TabContents::BeforeUnloadFiredFromRenderManager(
2564 bool proceed,
2565 bool* proceed_to_fire_unload) {
2566 if (delegate())
2567 delegate()->BeforeUnloadFired(this, proceed, proceed_to_fire_unload);
2568}
2569
2570void TabContents::UpdateRenderViewSizeForRenderManager() {
2571 // TODO(brettw) this is a hack. See TabContentsView::SizeContents.
2572 view_->SizeContents(view_->GetContainerSize());
2573}
2574
2575DOMUI* TabContents::CreateDOMUIForRenderManager(const GURL& url) {
[email protected]d82ed61e2009-06-16 02:46:222576 return DOMUIFactory::CreateDOMUIForURL(this, url);
[email protected]420ae012009-04-24 05:16:322577}
2578
2579NavigationEntry*
2580TabContents::GetLastCommittedNavigationEntryForRenderManager() {
2581 return controller_.GetLastCommittedEntry();
2582}
2583
2584bool TabContents::CreateRenderViewForRenderManager(
2585 RenderViewHost* render_view_host) {
[email protected]811bfe32009-07-01 08:46:252586 // If the pending navigation is to a DOMUI, tell the RenderView about any
2587 // bindings it will need enabled.
[email protected]420ae012009-04-24 05:16:322588 if (render_manager_.pending_dom_ui())
[email protected]811bfe32009-07-01 08:46:252589 render_view_host->AllowBindings(
2590 render_manager_.pending_dom_ui()->bindings());
[email protected]1adff062009-06-02 18:39:552591
[email protected]420ae012009-04-24 05:16:322592 RenderWidgetHostView* rwh_view = view_->CreateViewForWidget(render_view_host);
2593 if (!render_view_host->CreateRenderView())
2594 return false;
2595
2596 // Now that the RenderView has been created, we need to tell it its size.
2597 rwh_view->SetSize(view_->GetContainerSize());
2598
2599 UpdateMaxPageIDIfNecessary(render_view_host->site_instance(),
2600 render_view_host);
2601 return true;
2602}
2603
2604void TabContents::Observe(NotificationType type,
2605 const NotificationSource& source,
2606 const NotificationDetails& details) {
2607 switch (type.value) {
2608 case NotificationType::BOOKMARK_MODEL_LOADED:
2609 // BookmarkModel finished loading, fall through to update starred state.
2610 case NotificationType::URLS_STARRED: {
2611 // Somewhere, a URL has been starred.
2612 // Ignore notifications for profiles other than our current one.
2613 Profile* source_profile = Source<Profile>(source).ptr();
2614 if (!source_profile || !source_profile->IsSameProfile(profile()))
2615 return;
2616
2617 UpdateStarredStateForCurrentURL();
2618 break;
2619 }
2620 case NotificationType::PREF_CHANGED: {
2621 std::wstring* pref_name_in = Details<std::wstring>(details).ptr();
2622 DCHECK(Source<PrefService>(source).ptr() == profile()->GetPrefs());
2623 if (*pref_name_in == prefs::kAlternateErrorPagesEnabled) {
2624 UpdateAlternateErrorPageURL();
2625 } else if (*pref_name_in == prefs::kDefaultCharset ||
2626 StartsWithASCII(WideToUTF8(*pref_name_in), "webkit.webprefs.", true)
2627 ) {
2628 UpdateWebPreferences();
2629 } else {
2630 NOTREACHED() << "unexpected pref change notification" << *pref_name_in;
2631 }
2632 break;
2633 }
2634 case NotificationType::RENDER_WIDGET_HOST_DESTROYED:
2635 view_->RenderWidgetHostDestroyed(Source<RenderWidgetHost>(source).ptr());
2636 break;
2637
2638 case NotificationType::NAV_ENTRY_COMMITTED: {
2639 DCHECK(&controller_ == Source<NavigationController>(source).ptr());
2640
2641 NavigationController::LoadCommittedDetails& committed_details =
2642 *(Details<NavigationController::LoadCommittedDetails>(details).ptr());
2643 ExpireInfoBars(committed_details);
2644 break;
2645 }
2646
[email protected]d3ba77272009-09-03 00:06:092647#if defined(OS_LINUX)
2648 case NotificationType::BROWSER_THEME_CHANGED: {
2649 render_view_host()->SyncRendererPrefs();
2650 break;
2651 }
2652#endif
2653
[email protected]420ae012009-04-24 05:16:322654 default:
2655 NOTREACHED();
2656 }
2657}
[email protected]aacd7cc2009-09-02 14:51:102658
[email protected]beb440c2009-11-06 04:08:542659std::wstring TabContents::GetMessageBoxTitle(const GURL& frame_url,
2660 bool is_alert) {
2661 if (!frame_url.has_host())
2662 return l10n_util::GetString(
2663 is_alert ? IDS_JAVASCRIPT_ALERT_DEFAULT_TITLE
2664 : IDS_JAVASCRIPT_MESSAGEBOX_DEFAULT_TITLE);
2665
2666 // We really only want the scheme, hostname, and port.
2667 GURL::Replacements replacements;
2668 replacements.ClearUsername();
2669 replacements.ClearPassword();
2670 replacements.ClearPath();
2671 replacements.ClearQuery();
2672 replacements.ClearRef();
2673 GURL clean_url = frame_url.ReplaceComponents(replacements);
2674
2675 // TODO(brettw) it should be easier than this to do the correct language
2676 // handling without getting the accept language from the profile.
2677 std::wstring base_address = gfx::ElideUrl(clean_url, gfx::Font(), 0,
2678 profile()->GetPrefs()->GetString(prefs::kAcceptLanguages));
2679 // Force URL to have LTR directionality.
2680 if (l10n_util::GetTextDirection() == l10n_util::RIGHT_TO_LEFT)
2681 l10n_util::WrapStringWithLTRFormatting(&base_address);
2682
2683 return l10n_util::GetStringF(
2684 is_alert ? IDS_JAVASCRIPT_ALERT_TITLE : IDS_JAVASCRIPT_MESSAGEBOX_TITLE,
2685 base_address);
2686}
2687
2688gfx::NativeWindow TabContents::GetMessageBoxRootWindow() {
2689 return view_->GetTopLevelNativeWindow();
2690}
2691
2692void TabContents::OnMessageBoxClosed(IPC::Message* reply_msg,
2693 bool success,
2694 const std::wstring& prompt) {
2695 last_javascript_message_dismissal_ = base::TimeTicks::Now();
2696 if (is_showing_before_unload_dialog_ && !success) {
2697 // If a beforeunload dialog is canceled, we need to stop the throbber from
2698 // spinning, since we forced it to start spinning in Navigate.
2699 DidStopLoading();
2700
2701 tab_close_start_time_ = base::TimeTicks();
2702 }
2703 is_showing_before_unload_dialog_ = false;
2704 render_view_host()->JavaScriptMessageBoxClosed(reply_msg, success, prompt);
2705}
2706
2707void TabContents::SetSuppressMessageBoxes(bool suppress_message_boxes) {
2708 set_suppress_javascript_messages(suppress_message_boxes);
2709}
2710
[email protected]41fc0322009-09-04 22:23:402711void TabContents::set_encoding(const std::string& encoding) {
[email protected]aacd7cc2009-09-02 14:51:102712 encoding_ = CharacterEncoding::GetCanonicalEncodingNameByAliasName(encoding);
2713}