blob: 5d384ce2494d6114d12f8f4336993a4e3ac0db36 [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]a92b8642009-05-05 23:38:567#include "app/l10n_util.h"
[email protected]9929da92009-05-05 02:05:118#include "app/resource_bundle.h"
[email protected]420ae012009-04-24 05:16:329#include "base/file_version_info.h"
10#include "base/process_util.h"
[email protected]96d185d2009-04-24 03:28:5411#include "base/string16.h"
[email protected]996fd702009-09-04 19:12:3712#include "base/string_util.h"
[email protected]96d185d2009-04-24 03:28:5413#include "base/time.h"
[email protected]fdd61c62009-04-22 19:22:5714#include "chrome/browser/autofill_manager.h"
[email protected]66ba4932009-06-04 19:22:1315#include "chrome/browser/blocked_popup_container.h"
[email protected]420ae012009-04-24 05:16:3216#include "chrome/browser/bookmarks/bookmark_model.h"
[email protected]96d185d2009-04-24 03:28:5417#include "chrome/browser/browser_process.h"
initial.commit09911bf2008-07-26 23:55:2918#include "chrome/browser/cert_store.h"
[email protected]aacd7cc2009-09-02 14:51:1019#include "chrome/browser/character_encoding.h"
[email protected]96d185d2009-04-24 03:28:5420#include "chrome/browser/debugger/devtools_manager.h"
[email protected]420ae012009-04-24 05:16:3221#include "chrome/browser/dom_operation_notification_details.h"
[email protected]96d185d2009-04-24 03:28:5422#include "chrome/browser/dom_ui/dom_ui.h"
23#include "chrome/browser/dom_ui/dom_ui_factory.h"
[email protected]bcef0dc02009-02-28 00:35:0224#include "chrome/browser/download/download_item_model.h"
[email protected]420ae012009-04-24 05:16:3225#include "chrome/browser/download/download_manager.h"
26#include "chrome/browser/download/download_request_manager.h"
[email protected]0189bc722009-08-28 21:56:4827#include "chrome/browser/favicon_service.h"
[email protected]420ae012009-04-24 05:16:3228#include "chrome/browser/gears_integration.h"
29#include "chrome/browser/google_util.h"
[email protected]8897c382009-05-06 17:53:2630#include "chrome/browser/hung_renderer_dialog.h"
[email protected]420ae012009-04-24 05:16:3231#include "chrome/browser/jsmessage_box_handler.h"
32#include "chrome/browser/load_from_memory_cache_details.h"
33#include "chrome/browser/load_notification_details.h"
[email protected]ea0e80892009-08-22 00:36:0534#include "chrome/browser/modal_html_dialog_delegate.h"
[email protected]c8865482009-07-23 20:40:1035#include "chrome/browser/omnibox_search_hint.h"
[email protected]fdd61c62009-04-22 19:22:5736#include "chrome/browser/password_manager/password_manager.h"
37#include "chrome/browser/plugin_installer.h"
[email protected]ce560f82009-06-03 09:39:4438#include "chrome/browser/profile.h"
[email protected]96d185d2009-04-24 03:28:5439#include "chrome/browser/renderer_host/render_widget_host_view.h"
[email protected]fd694982009-07-03 17:32:3740#include "chrome/browser/renderer_host/resource_request_details.h"
[email protected]96d185d2009-04-24 03:28:5441#include "chrome/browser/renderer_host/web_cache_manager.h"
[email protected]bd1ad682009-05-15 22:19:1742#include "chrome/browser/tab_contents/infobar_delegate.h"
[email protected]7e383692009-06-12 19:14:5443#include "chrome/browser/tab_contents/interstitial_page.h"
[email protected]f3ec7742009-01-15 00:59:1644#include "chrome/browser/tab_contents/navigation_entry.h"
[email protected]de34a962009-06-26 23:22:4345#include "chrome/browser/tab_contents/provisional_load_details.h"
[email protected]f3ec7742009-01-15 00:59:1646#include "chrome/browser/tab_contents/tab_contents_delegate.h"
[email protected]fdd61c62009-04-22 19:22:5747#include "chrome/browser/tab_contents/tab_contents_view.h"
[email protected]58dca552009-06-17 00:35:0248#include "chrome/browser/tab_contents/thumbnail_generator.h"
[email protected]449478302009-06-09 20:04:2849#include "chrome/browser/thumbnail_store.h"
[email protected]420ae012009-04-24 05:16:3250#include "chrome/browser/search_engines/template_url_fetcher.h"
51#include "chrome/browser/search_engines/template_url_model.h"
[email protected]35f7d212009-04-29 21:19:2752#include "chrome/common/chrome_switches.h"
[email protected]bfd04a62009-02-01 18:16:5653#include "chrome/common/notification_service.h"
[email protected]ce5c4502009-05-06 16:46:1154#include "chrome/common/page_action.h"
initial.commit09911bf2008-07-26 23:55:2955#include "chrome/common/pref_names.h"
[email protected]1eb89e82008-08-15 12:27:0356#include "chrome/common/pref_service.h"
[email protected]420ae012009-04-24 05:16:3257#include "chrome/common/render_messages.h"
[email protected]1ee614862009-07-06 20:29:1858#include "chrome/common/renderer_preferences.h"
[email protected]96d185d2009-04-24 03:28:5459#include "chrome/common/url_constants.h"
[email protected]8897c382009-05-06 17:53:2660#include "grit/generated_resources.h"
[email protected]420ae012009-04-24 05:16:3261#include "grit/locale_settings.h"
62#include "net/base/mime_util.h"
63#include "net/base/net_errors.h"
[email protected]d686e812009-06-03 19:10:2964#include "net/base/net_util.h"
[email protected]420ae012009-04-24 05:16:3265#include "net/base/registry_controlled_domain.h"
[email protected]1ee614862009-07-06 20:29:1866#include "webkit/glue/password_form.h"
67#include "webkit/glue/webpreferences.h"
[email protected]3e45ba92009-02-20 21:09:0068
[email protected]304f8792009-08-19 21:28:0969#if defined(OS_CHROMEOS)
70// For GdkScreen
71#include <gdk/gdk.h>
[email protected]0ba5c5e2009-08-20 16:47:1372#endif // defined(OS_CHROMEOS)
73
74#if defined(OS_LINUX)
75#include "chrome/browser/gtk/create_application_shortcuts_dialog_gtk.h"
[email protected]d3ba77272009-09-03 00:06:0976#include "chrome/browser/gtk/gtk_theme_provider.h"
[email protected]0ba5c5e2009-08-20 16:47:1377#endif // defined(OS_LINUX)
78
[email protected]420ae012009-04-24 05:16:3279// Cross-Site Navigations
80//
81// If a TabContents is told to navigate to a different web site (as determined
82// by SiteInstance), it will replace its current RenderViewHost with a new
83// RenderViewHost dedicated to the new SiteInstance. This works as follows:
84//
85// - Navigate determines whether the destination is cross-site, and if so,
86// it creates a pending_render_view_host_ and moves into the PENDING
87// RendererState.
88// - The pending RVH is "suspended," so that no navigation messages are sent to
89// its renderer until the onbeforeunload JavaScript handler has a chance to
90// run in the current RVH.
91// - The pending RVH tells CrossSiteRequestManager (a thread-safe singleton)
92// that it has a pending cross-site request. ResourceDispatcherHost will
93// check for this when the response arrives.
94// - The current RVH runs its onbeforeunload handler. If it returns false, we
95// cancel all the pending logic and go back to NORMAL. Otherwise we allow
96// the pending RVH to send the navigation request to its renderer.
97// - ResourceDispatcherHost receives a ResourceRequest on the IO thread. It
98// checks CrossSiteRequestManager to see that the RVH responsible has a
99// pending cross-site request, and then installs a CrossSiteEventHandler.
100// - When RDH receives a response, the BufferedEventHandler determines whether
101// it is a download. If so, it sends a message to the new renderer causing
102// it to cancel the request, and the download proceeds in the download
103// thread. For now, we stay in a PENDING state (with a pending RVH) until
[email protected]57c6a652009-05-04 07:58:34104// the next DidNavigate event for this TabContents. This isn't ideal, but it
[email protected]420ae012009-04-24 05:16:32105// doesn't affect any functionality.
106// - After RDH receives a response and determines that it is safe and not a
107// download, it pauses the response to first run the old page's onunload
108// handler. It does this by asynchronously calling the OnCrossSiteResponse
[email protected]57c6a652009-05-04 07:58:34109// method of TabContents on the UI thread, which sends a ClosePage message
[email protected]420ae012009-04-24 05:16:32110// to the current RVH.
111// - Once the onunload handler is finished, a ClosePage_ACK message is sent to
112// the ResourceDispatcherHost, who unpauses the response. Data is then sent
113// to the pending RVH.
114// - The pending renderer sends a FrameNavigate message that invokes the
115// DidNavigate method. This replaces the current RVH with the
116// pending RVH and goes back to the NORMAL RendererState.
117
118namespace {
119
120// Amount of time we wait between when a key event is received and the renderer
121// is queried for its state and pushed to the NavigationEntry.
122const int kQueryStateDelay = 5000;
123
124const int kSyncWaitDelay = 40;
125
[email protected]304f8792009-08-19 21:28:09126#if defined(OS_CHROMEOS)
127// If a popup window is bigger than this fraction of the screen on chrome os,
128// turn it into a tab
129const float kMaxWidthFactor = 0.5;
130const float kMaxHeightFactor = 0.6;
131#endif
132
[email protected]420ae012009-04-24 05:16:32133// If another javascript message box is displayed within
134// kJavascriptMessageExpectedDelay of a previous javascript message box being
135// dismissed, display an option to suppress future message boxes from this
136// contents.
137const int kJavascriptMessageExpectedDelay = 1000;
138
[email protected]2bd6ca1c2009-08-27 16:42:20139const char kLinkDoctorBaseURL[] =
140 "http://linkhelp.clients.google.com/tbproxy/lh/fixurl";
141
[email protected]420ae012009-04-24 05:16:32142// The list of prefs we want to observe.
143const wchar_t* kPrefsToObserve[] = {
144 prefs::kAlternateErrorPagesEnabled,
145 prefs::kWebKitJavaEnabled,
146 prefs::kWebKitJavascriptEnabled,
147 prefs::kWebKitLoadsImagesAutomatically,
148 prefs::kWebKitPluginsEnabled,
149 prefs::kWebKitUsesUniversalDetector,
150 prefs::kWebKitSerifFontFamily,
151 prefs::kWebKitSansSerifFontFamily,
152 prefs::kWebKitFixedFontFamily,
153 prefs::kWebKitDefaultFontSize,
154 prefs::kWebKitDefaultFixedFontSize,
155 prefs::kDefaultCharset
156 // kWebKitStandardFontIsSerif needs to be added
157 // if we let users pick which font to use, serif or sans-serif when
158 // no font is specified or a CSS generic family (serif or sans-serif)
159 // is not specified.
160};
161
162const int kPrefsToObserveLength = arraysize(kPrefsToObserve);
163
[email protected]420ae012009-04-24 05:16:32164// Returns true if the entry's transition type is FORM_SUBMIT.
165bool IsFormSubmit(const NavigationEntry* entry) {
166 return (PageTransition::StripQualifier(entry->transition_type()) ==
167 PageTransition::FORM_SUBMIT);
168}
169
170#if defined(OS_WIN)
171
172BOOL CALLBACK InvalidateWindow(HWND hwnd, LPARAM lparam) {
173 // Note: erase is required to properly paint some widgets borders. This can
174 // be seen with textfields.
175 InvalidateRect(hwnd, NULL, TRUE);
176 return TRUE;
177}
178#endif
179
[email protected]056de2d2009-06-26 16:41:34180void MakeNavigateParams(const NavigationEntry& entry, bool reload,
181 ViewMsg_Navigate_Params* params) {
182 params->page_id = entry.page_id();
183 params->url = entry.url();
184 params->referrer = entry.referrer();
185 params->transition = entry.transition_type();
186 params->state = entry.content_state();
187 params->reload = reload;
188 params->request_time = base::Time::Now();
189}
190
[email protected]420ae012009-04-24 05:16:32191} // namespace
192
193// -----------------------------------------------------------------------------
194
[email protected]fdd61c62009-04-22 19:22:57195// static
196int TabContents::find_request_id_counter_ = -1;
197
[email protected]420ae012009-04-24 05:16:32198class TabContents::GearsCreateShortcutCallbackFunctor {
199 public:
200 explicit GearsCreateShortcutCallbackFunctor(TabContents* contents)
201 : contents_(contents) {}
202
203 void Run(const GearsShortcutData2& shortcut_data, bool success) {
204 if (contents_)
205 contents_->OnGearsCreateShortcutDone(shortcut_data, success);
206 delete this;
207 }
208 void Cancel() {
209 contents_ = NULL;
210 }
211
212 private:
213 TabContents* contents_;
214};
215
[email protected]420ae012009-04-24 05:16:32216TabContents::TabContents(Profile* profile,
217 SiteInstance* site_instance,
218 int routing_id,
219 base::WaitableEvent* modal_dialog_event)
[email protected]b680ad22009-04-15 23:19:42220 : delegate_(NULL),
[email protected]66ba4932009-06-04 19:22:13221 ALLOW_THIS_IN_INITIALIZER_LIST(controller_(this, profile)),
222 ALLOW_THIS_IN_INITIALIZER_LIST(view_(
[email protected]d82ed61e2009-06-16 02:46:22223 TabContentsView::Create(this))),
224 ALLOW_THIS_IN_INITIALIZER_LIST(render_manager_(this, this)),
[email protected]fdd61c62009-04-22 19:22:57225 property_bag_(),
226 registrar_(),
[email protected]d82ed61e2009-06-16 02:46:22227 ALLOW_THIS_IN_INITIALIZER_LIST(printing_(*this)),
[email protected]fdd61c62009-04-22 19:22:57228 save_package_(),
229 cancelable_consumer_(),
230 autofill_manager_(),
231 password_manager_(),
232 plugin_installer_(),
[email protected]d82ed61e2009-06-16 02:46:22233 ALLOW_THIS_IN_INITIALIZER_LIST(fav_icon_helper_(this)),
[email protected]fdd61c62009-04-22 19:22:57234 select_file_dialog_(),
235 pending_install_(),
[email protected]d5f942ba2008-09-26 19:30:34236 is_loading_(false),
initial.commit09911bf2008-07-26 23:55:29237 is_crashed_(false),
[email protected]d5f942ba2008-09-26 19:30:34238 waiting_for_response_(false),
[email protected]d5f942ba2008-09-26 19:30:34239 max_page_id_(-1),
[email protected]fdd61c62009-04-22 19:22:57240 current_load_start_(),
241 load_state_(net::LOAD_STATE_IDLE),
242 load_state_host_(),
243 received_page_title_(false),
244 is_starred_(false),
245 contents_mime_type_(),
246 encoding_(),
[email protected]332af7732009-03-11 13:21:35247 blocked_popups_(NULL),
[email protected]fdd61c62009-04-22 19:22:57248 infobar_delegates_(),
[email protected]fdd61c62009-04-22 19:22:57249 find_ui_active_(false),
250 find_op_aborted_(false),
251 current_find_request_id_(find_request_id_counter_++),
252 find_text_(),
[email protected]e491f1c2009-05-22 20:28:12253 last_search_case_sensitive_(false),
254 last_search_prepopulate_text_(NULL),
255 last_search_result_(),
[email protected]fdd61c62009-04-22 19:22:57256 capturing_contents_(false),
257 is_being_destroyed_(false),
258 notify_disconnection_(false),
259 history_requests_(),
260#if defined(OS_WIN)
261 message_box_active_(CreateEvent(NULL, TRUE, FALSE, NULL)),
262#endif
263 last_javascript_message_dismissal_(),
[email protected]c2e74fe82009-09-03 17:57:44264 suppress_javascript_messages_(false),
265 opener_dom_ui_type_(DOMUIFactory::kNoDOMUI) {
[email protected]420ae012009-04-24 05:16:32266 pending_install_.page_id = 0;
267 pending_install_.callback_functor = NULL;
268
[email protected]16779842009-07-08 23:45:29269#if defined(OS_CHROMEOS)
[email protected]58dca552009-06-17 00:35:02270 // Make sure the thumbnailer is started before starting the render manager.
271 // The thumbnailer will want to listen for RVH creations, one of which will
272 // happen in RVHManager::Init.
273 ThumbnailGenerator* generator = g_browser_process->GetThumbnailGenerator();
274 if (generator)
275 generator->StartThumbnailing();
276#endif
277
[email protected]420ae012009-04-24 05:16:32278 render_manager_.Init(profile, site_instance, routing_id, modal_dialog_event);
279
280 view_->CreateView();
281
282 // Register for notifications about all interested prefs change.
283 PrefService* prefs = profile->GetPrefs();
284 if (prefs) {
285 for (int i = 0; i < kPrefsToObserveLength; ++i)
286 prefs->AddPrefObserver(kPrefsToObserve[i], this);
287 }
288
289 // Register for notifications about URL starredness changing on any profile.
290 registrar_.Add(this, NotificationType::URLS_STARRED,
291 NotificationService::AllSources());
292 registrar_.Add(this, NotificationType::BOOKMARK_MODEL_LOADED,
293 NotificationService::AllSources());
294 registrar_.Add(this, NotificationType::RENDER_WIDGET_HOST_DESTROYED,
295 NotificationService::AllSources());
[email protected]d3ba77272009-09-03 00:06:09296#if defined(OS_LINUX)
297 registrar_.Add(this, NotificationType::BROWSER_THEME_CHANGED,
298 NotificationService::AllSources());
299#endif
[email protected]420ae012009-04-24 05:16:32300
301 // Keep a global copy of the previous search string (if any).
302 static string16 global_last_search = string16();
[email protected]e491f1c2009-05-22 20:28:12303 last_search_prepopulate_text_ = &global_last_search;
[email protected]840b1512009-07-21 16:53:46304
[email protected]c8865482009-07-23 20:40:10305 // Set-up the showing of the omnibox search infobar if applicable.
306 if (OmniboxSearchHint::IsEnabled(profile))
307 omnibox_search_hint_.reset(new OmniboxSearchHint(this));
308
[email protected]840b1512009-07-21 16:53:46309 view_->InitRendererPrefs(&renderer_preferences_);
[email protected]332af7732009-03-11 13:21:35310}
initial.commit09911bf2008-07-26 23:55:29311
312TabContents::~TabContents() {
[email protected]420ae012009-04-24 05:16:32313 is_being_destroyed_ = true;
314
315 // We don't want any notifications while we're runnign our destructor.
316 registrar_.RemoveAll();
317
318 // Unregister the notifications of all observed prefs change.
319 PrefService* prefs = profile()->GetPrefs();
320 if (prefs) {
321 for (int i = 0; i < kPrefsToObserveLength; ++i)
322 prefs->RemovePrefObserver(kPrefsToObserve[i], this);
323 }
324
325 // Clean up subwindows like plugins and the find in page bar.
326 view_->OnContentsDestroy();
327
328 NotifyDisconnected();
[email protected]d82ed61e2009-06-16 02:46:22329 HungRendererDialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:32330
331 if (pending_install_.callback_functor)
332 pending_install_.callback_functor->Cancel();
333
334 // First cleanly close all child windows.
335 // TODO(mpcomplete): handle case if MaybeCloseChildWindows() already asked
336 // some of these to close. CloseWindows is async, so it might get called
337 // twice before it runs.
338 int size = static_cast<int>(child_windows_.size());
339 for (int i = size - 1; i >= 0; --i) {
340 ConstrainedWindow* window = child_windows_[i];
341 if (window)
342 window->CloseConstrainedWindow();
343 }
344
[email protected]66ba4932009-06-04 19:22:13345 if (blocked_popups_)
346 blocked_popups_->Destroy();
347
[email protected]420ae012009-04-24 05:16:32348 // Notify any observer that have a reference on this tab contents.
349 NotificationService::current()->Notify(
350 NotificationType::TAB_CONTENTS_DESTROYED,
351 Source<TabContents>(this),
352 NotificationService::NoDetails());
353
[email protected]1e5e74f2009-05-27 20:55:12354 // Notify any lasting InfobarDelegates that have not yet been removed that
355 // whatever infobar they were handling in this TabContents has closed,
356 // because the TabContents is going away entirely.
357 // This must happen after the TAB_CONTENTS_DESTROYED notification as the
358 // notification may trigger infobars calls that access their delegate. (and
359 // some implementations of InfoBarDelegate do delete themselves on
360 // InfoBarClosed()).
361 for (int i = 0; i < infobar_delegate_count(); ++i) {
362 InfoBarDelegate* delegate = GetInfoBarDelegateAt(i);
363 delegate->InfoBarClosed();
364 }
365 infobar_delegates_.clear();
366
[email protected]420ae012009-04-24 05:16:32367 // TODO(brettw) this should be moved to the view.
368#if defined(OS_WIN)
369 // If we still have a window handle, destroy it. GetNativeView can return
370 // NULL if this contents was part of a window that closed.
371 if (GetNativeView())
372 ::DestroyWindow(GetNativeView());
373#endif
initial.commit09911bf2008-07-26 23:55:29374}
375
[email protected]d5f942ba2008-09-26 19:30:34376// static
377void TabContents::RegisterUserPrefs(PrefService* prefs) {
[email protected]420ae012009-04-24 05:16:32378 prefs->RegisterBooleanPref(prefs::kAlternateErrorPagesEnabled, true);
379
380 WebPreferences pref_defaults;
381 prefs->RegisterBooleanPref(prefs::kWebKitJavascriptEnabled,
382 pref_defaults.javascript_enabled);
383 prefs->RegisterBooleanPref(prefs::kWebKitWebSecurityEnabled,
384 pref_defaults.web_security_enabled);
385 prefs->RegisterBooleanPref(
386 prefs::kWebKitJavascriptCanOpenWindowsAutomatically, true);
387 prefs->RegisterBooleanPref(prefs::kWebKitLoadsImagesAutomatically,
388 pref_defaults.loads_images_automatically);
389 prefs->RegisterBooleanPref(prefs::kWebKitPluginsEnabled,
390 pref_defaults.plugins_enabled);
391 prefs->RegisterBooleanPref(prefs::kWebKitDomPasteEnabled,
392 pref_defaults.dom_paste_enabled);
393 prefs->RegisterBooleanPref(prefs::kWebKitShrinksStandaloneImagesToFit,
394 pref_defaults.shrinks_standalone_images_to_fit);
[email protected]0df30122009-06-03 12:13:08395 prefs->RegisterStringPref(prefs::kWebKitInspectorSettings,
396 pref_defaults.inspector_settings);
[email protected]420ae012009-04-24 05:16:32397 prefs->RegisterBooleanPref(prefs::kWebKitTextAreasAreResizable,
398 pref_defaults.text_areas_are_resizable);
399 prefs->RegisterBooleanPref(prefs::kWebKitJavaEnabled,
400 pref_defaults.java_enabled);
401
402 prefs->RegisterLocalizedStringPref(prefs::kAcceptLanguages,
403 IDS_ACCEPT_LANGUAGES);
404 prefs->RegisterLocalizedStringPref(prefs::kDefaultCharset,
405 IDS_DEFAULT_ENCODING);
406 prefs->RegisterLocalizedBooleanPref(prefs::kWebKitStandardFontIsSerif,
407 IDS_STANDARD_FONT_IS_SERIF);
408 prefs->RegisterLocalizedStringPref(prefs::kWebKitFixedFontFamily,
409 IDS_FIXED_FONT_FAMILY);
410 prefs->RegisterLocalizedStringPref(prefs::kWebKitSerifFontFamily,
411 IDS_SERIF_FONT_FAMILY);
412 prefs->RegisterLocalizedStringPref(prefs::kWebKitSansSerifFontFamily,
413 IDS_SANS_SERIF_FONT_FAMILY);
414 prefs->RegisterLocalizedStringPref(prefs::kWebKitCursiveFontFamily,
415 IDS_CURSIVE_FONT_FAMILY);
416 prefs->RegisterLocalizedStringPref(prefs::kWebKitFantasyFontFamily,
417 IDS_FANTASY_FONT_FAMILY);
418 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitDefaultFontSize,
419 IDS_DEFAULT_FONT_SIZE);
420 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitDefaultFixedFontSize,
421 IDS_DEFAULT_FIXED_FONT_SIZE);
422 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitMinimumFontSize,
423 IDS_MINIMUM_FONT_SIZE);
424 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitMinimumLogicalFontSize,
425 IDS_MINIMUM_LOGICAL_FONT_SIZE);
426 prefs->RegisterLocalizedBooleanPref(prefs::kWebKitUsesUniversalDetector,
427 IDS_USES_UNIVERSAL_DETECTOR);
428 prefs->RegisterLocalizedStringPref(prefs::kStaticEncodings,
429 IDS_STATIC_ENCODING_LIST);
initial.commit09911bf2008-07-26 23:55:29430}
431
[email protected]13367f72009-07-08 16:39:02432// Returns true if contains content rendered by an extension.
433bool TabContents::HostsExtension() const {
434 return GetURL().SchemeIs(chrome::kExtensionScheme);
[email protected]d5f942ba2008-09-26 19:30:34435}
436
[email protected]6e95706d2009-04-23 22:55:01437PasswordManager* TabContents::GetPasswordManager() {
438 if (password_manager_.get() == NULL)
439 password_manager_.reset(new PasswordManager(this));
440 return password_manager_.get();
441}
442
443PluginInstaller* TabContents::GetPluginInstaller() {
444 if (plugin_installer_.get() == NULL)
445 plugin_installer_.reset(new PluginInstaller(this));
446 return plugin_installer_.get();
447}
448
[email protected]d5f942ba2008-09-26 19:30:34449const GURL& TabContents::GetURL() const {
450 // We may not have a navigation entry yet
[email protected]ce3fa3c2009-04-20 19:55:57451 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]ebe89e062009-08-13 23:16:54452 return entry ? entry->virtual_url() : GURL::EmptyGURL();
[email protected]d5f942ba2008-09-26 19:30:34453}
454
[email protected]96d185d2009-04-24 03:28:54455const string16& TabContents::GetTitle() const {
[email protected]4c6092c5b2009-06-06 00:23:55456 // Transient entries take precedence. They are used for interstitial pages
457 // that are shown on top of existing pages.
458 NavigationEntry* entry = controller_.GetTransientEntry();
459 if (entry)
460 return entry->GetTitleForDisplay(&controller_);
461
[email protected]96d185d2009-04-24 03:28:54462 DOMUI* our_dom_ui = render_manager_.pending_dom_ui() ?
463 render_manager_.pending_dom_ui() : render_manager_.dom_ui();
464 if (our_dom_ui) {
465 // Don't override the title in view source mode.
[email protected]4c6092c5b2009-06-06 00:23:55466 entry = controller_.GetActiveEntry();
[email protected]96d185d2009-04-24 03:28:54467 if (!(entry && entry->IsViewSourceMode())) {
468 // Give the DOM UI the chance to override our title.
469 const string16& title = our_dom_ui->overridden_title();
470 if (!title.empty())
471 return title;
472 }
473 }
474
475 // We use the title for the last committed entry rather than a pending
476 // navigation entry. For example, when the user types in a URL, we want to
477 // keep the old page's title until the new load has committed and we get a new
478 // title.
[email protected]96d185d2009-04-24 03:28:54479 entry = controller_.GetLastCommittedEntry();
480 if (entry)
481 return entry->GetTitleForDisplay(&controller_);
[email protected]96d185d2009-04-24 03:28:54482 return EmptyString16();
483}
484
[email protected]d5f942ba2008-09-26 19:30:34485int32 TabContents::GetMaxPageID() {
486 if (GetSiteInstance())
487 return GetSiteInstance()->max_page_id();
488 else
489 return max_page_id_;
490}
491
492void TabContents::UpdateMaxPageID(int32 page_id) {
493 // Ensure both the SiteInstance and RenderProcessHost update their max page
[email protected]57c6a652009-05-04 07:58:34494 // IDs in sync. Only TabContents will also have site instances, except during
[email protected]d5f942ba2008-09-26 19:30:34495 // testing.
496 if (GetSiteInstance())
497 GetSiteInstance()->UpdateMaxPageID(page_id);
[email protected]57c6a652009-05-04 07:58:34498 process()->UpdateMaxPageID(page_id);
[email protected]d5f942ba2008-09-26 19:30:34499}
500
[email protected]96d185d2009-04-24 03:28:54501SiteInstance* TabContents::GetSiteInstance() const {
502 return render_manager_.current_host()->site_instance();
503}
504
[email protected]d5f942ba2008-09-26 19:30:34505const std::wstring TabContents::GetDefaultTitle() const {
506 return l10n_util::GetString(IDS_DEFAULT_TAB_TITLE);
507}
508
[email protected]96d185d2009-04-24 03:28:54509bool TabContents::ShouldDisplayURL() {
510 // Don't hide the url in view source mode.
511 NavigationEntry* entry = controller_.GetActiveEntry();
512 if (entry && entry->IsViewSourceMode())
513 return true;
[email protected]83e3895b2009-06-11 00:07:16514
[email protected]96d185d2009-04-24 03:28:54515 DOMUI* dom_ui = GetDOMUIForCurrentState();
516 if (dom_ui)
517 return !dom_ui->should_hide_url();
518 return true;
519}
520
[email protected]d5f942ba2008-09-26 19:30:34521SkBitmap TabContents::GetFavIcon() const {
522 // Like GetTitle(), we also want to use the favicon for the last committed
523 // entry rather than a pending navigation entry.
[email protected]ce3fa3c2009-04-20 19:55:57524 NavigationEntry* entry = controller_.GetTransientEntry();
[email protected]cbab76d2008-10-13 22:42:47525 if (entry)
526 return entry->favicon().bitmap();
527
[email protected]ce3fa3c2009-04-20 19:55:57528 entry = controller_.GetLastCommittedEntry();
[email protected]d5f942ba2008-09-26 19:30:34529 if (entry)
530 return entry->favicon().bitmap();
[email protected]d5f942ba2008-09-26 19:30:34531 return SkBitmap();
532}
533
[email protected]96d185d2009-04-24 03:28:54534bool TabContents::ShouldDisplayFavIcon() {
535 // Always display a throbber during pending loads.
536 if (controller_.GetLastCommittedEntry() && controller_.pending_entry())
537 return true;
538
539 DOMUI* dom_ui = GetDOMUIForCurrentState();
540 if (dom_ui)
541 return !dom_ui->hide_favicon();
542 return true;
543}
544
[email protected]96d185d2009-04-24 03:28:54545std::wstring TabContents::GetStatusText() const {
546 if (!is_loading() || load_state_ == net::LOAD_STATE_IDLE)
547 return std::wstring();
548
549 switch (load_state_) {
550 case net::LOAD_STATE_WAITING_FOR_CACHE:
551 return l10n_util::GetString(IDS_LOAD_STATE_WAITING_FOR_CACHE);
552 case net::LOAD_STATE_RESOLVING_PROXY_FOR_URL:
553 return l10n_util::GetString(IDS_LOAD_STATE_RESOLVING_PROXY_FOR_URL);
554 case net::LOAD_STATE_RESOLVING_HOST:
555 return l10n_util::GetString(IDS_LOAD_STATE_RESOLVING_HOST);
556 case net::LOAD_STATE_CONNECTING:
557 return l10n_util::GetString(IDS_LOAD_STATE_CONNECTING);
558 case net::LOAD_STATE_SENDING_REQUEST:
559 return l10n_util::GetString(IDS_LOAD_STATE_SENDING_REQUEST);
560 case net::LOAD_STATE_WAITING_FOR_RESPONSE:
561 return l10n_util::GetStringF(IDS_LOAD_STATE_WAITING_FOR_RESPONSE,
562 load_state_host_);
563 // Ignore net::LOAD_STATE_READING_RESPONSE and net::LOAD_STATE_IDLE
564 case net::LOAD_STATE_IDLE:
565 case net::LOAD_STATE_READING_RESPONSE:
566 break;
567 }
568
569 return std::wstring();
570}
571
[email protected]d5f942ba2008-09-26 19:30:34572void TabContents::SetIsCrashed(bool state) {
573 if (state == is_crashed_)
574 return;
575
576 is_crashed_ = state;
[email protected]c9cd2222009-05-06 05:16:50577 NotifyNavigationStateChanged(INVALIDATE_TAB);
[email protected]d5f942ba2008-09-26 19:30:34578}
579
[email protected]f9b1a7b2009-06-22 17:26:38580void TabContents::SetPageActionEnabled(const PageAction* page_action,
[email protected]d7eaf572009-07-01 21:57:00581 bool enable,
582 const std::string& title,
583 int icon_id) {
[email protected]ce5c4502009-05-06 16:46:11584 DCHECK(page_action);
585
[email protected]d7eaf572009-07-01 21:57:00586 if (!enable &&
587 enabled_page_actions_.end() == enabled_page_actions_.find(page_action)) {
588 return; // Don't need to disable twice.
589 }
[email protected]ce5c4502009-05-06 16:46:11590
[email protected]d7eaf572009-07-01 21:57:00591 if (enable) {
592 enabled_page_actions_[page_action].reset(
593 new PageActionState(title, icon_id));
594 } else {
[email protected]f9b1a7b2009-06-22 17:26:38595 enabled_page_actions_.erase(page_action);
[email protected]d7eaf572009-07-01 21:57:00596 }
[email protected]ce5c4502009-05-06 16:46:11597}
598
[email protected]d7eaf572009-07-01 21:57:00599const PageActionState* TabContents::GetPageActionState(
600 const PageAction* page_action) {
601 if (enabled_page_actions_.end() == enabled_page_actions_.find(page_action))
602 return NULL;
[email protected]ce5c4502009-05-06 16:46:11603
[email protected]d7eaf572009-07-01 21:57:00604 return enabled_page_actions_[page_action].get();
605}
[email protected]ce5c4502009-05-06 16:46:11606
[email protected]d5f942ba2008-09-26 19:30:34607void TabContents::NotifyNavigationStateChanged(unsigned changed_flags) {
608 if (delegate_)
609 delegate_->NavigationStateChanged(this, changed_flags);
610}
611
[email protected]96d185d2009-04-24 03:28:54612void TabContents::DidBecomeSelected() {
613 controller_.SetActive(true);
[email protected]789e9152009-08-04 21:59:43614 if (render_widget_host_view()) {
[email protected]96d185d2009-04-24 03:28:54615 render_widget_host_view()->DidBecomeSelected();
[email protected]789e9152009-08-04 21:59:43616#if defined(OS_MACOSX)
617 render_widget_host_view()->SetActive(true);
618#endif
619 }
[email protected]96d185d2009-04-24 03:28:54620
[email protected]76543b92009-08-31 17:27:45621 WebCacheManager::GetInstance()->ObserveActivity(process()->id());
[email protected]96d185d2009-04-24 03:28:54622}
623
624void TabContents::WasHidden() {
625 if (!capturing_contents()) {
626 // |render_view_host()| can be NULL if the user middle clicks a link to open
627 // a tab in then background, then closes the tab before selecting it. This
628 // is because closing the tab calls TabContents::Destroy(), which removes
629 // the |render_view_host()|; then when we actually destroy the window,
630 // OnWindowPosChanged() notices and calls HideContents() (which calls us).
631 if (render_widget_host_view())
632 render_widget_host_view()->WasHidden();
[email protected]96d185d2009-04-24 03:28:54633 }
634
635 NotificationService::current()->Notify(
636 NotificationType::TAB_CONTENTS_HIDDEN,
637 Source<TabContents>(this),
638 NotificationService::NoDetails());
639}
640
[email protected]d5f942ba2008-09-26 19:30:34641void TabContents::Activate() {
642 if (delegate_)
643 delegate_->ActivateContents(this);
644}
645
[email protected]96d185d2009-04-24 03:28:54646void TabContents::ShowContents() {
647 if (render_widget_host_view())
648 render_widget_host_view()->DidBecomeSelected();
[email protected]96d185d2009-04-24 03:28:54649}
650
651void TabContents::HideContents() {
652 // TODO(pkasting): http://b/1239839 Right now we purposefully don't call
653 // our superclass HideContents(), because some callers want to be very picky
654 // about the order in which these get called. In addition to making the code
655 // here practically impossible to understand, this also means we end up
656 // calling TabContents::WasHidden() twice if callers call both versions of
[email protected]57c6a652009-05-04 07:58:34657 // HideContents() on a TabContents.
[email protected]96d185d2009-04-24 03:28:54658 WasHidden();
659}
660
[email protected]c0588052008-10-27 23:01:50661void TabContents::OpenURL(const GURL& url, const GURL& referrer,
[email protected]d5f942ba2008-09-26 19:30:34662 WindowOpenDisposition disposition,
663 PageTransition::Type transition) {
664 if (delegate_)
[email protected]c0588052008-10-27 23:01:50665 delegate_->OpenURLFromTab(this, url, referrer, disposition, transition);
[email protected]d5f942ba2008-09-26 19:30:34666}
667
668bool TabContents::NavigateToPendingEntry(bool reload) {
[email protected]96d185d2009-04-24 03:28:54669 const NavigationEntry& entry = *controller_.pending_entry();
670
671 RenderViewHost* dest_render_view_host = render_manager_.Navigate(entry);
672 if (!dest_render_view_host)
673 return false; // Unable to create the desired render view host.
674
675 // Tell DevTools agent that it is attached prior to the navigation.
[email protected]d9f9b792009-06-24 13:17:12676 DevToolsManager* devtools_manager = DevToolsManager::GetInstance();
[email protected]7e8e6b62009-05-08 11:28:32677 if (devtools_manager) { // NULL in unit tests.
[email protected]684b4ba22009-05-07 06:27:41678 devtools_manager->OnNavigatingToPendingEntry(
679 render_view_host(),
680 dest_render_view_host,
681 controller_.pending_entry()->url());
[email protected]7e8e6b62009-05-08 11:28:32682 }
[email protected]96d185d2009-04-24 03:28:54683
684 // Used for page load time metrics.
685 current_load_start_ = base::TimeTicks::Now();
686
687 // Navigate in the desired RenderViewHost.
[email protected]056de2d2009-06-26 16:41:34688 ViewMsg_Navigate_Params navigate_params;
689 MakeNavigateParams(entry, reload, &navigate_params);
690 dest_render_view_host->Navigate(navigate_params);
[email protected]96d185d2009-04-24 03:28:54691
692 if (entry.page_id() == -1) {
693 // HACK!! This code suppresses javascript: URLs from being added to
694 // session history, which is what we want to do for javascript: URLs that
695 // do not generate content. What we really need is a message from the
696 // renderer telling us that a new page was not created. The same message
697 // could be used for mailto: URLs and the like.
698 if (entry.url().SchemeIs(chrome::kJavaScriptScheme))
699 return false;
700 }
701
702 // Clear any provisional password saves - this stops password infobars
703 // showing up on pages the user navigates to while the right page is
704 // loading.
705 GetPasswordManager()->ClearProvisionalSave();
706
707 if (reload && !profile()->IsOffTheRecord()) {
[email protected]0189bc722009-08-28 21:56:48708 FaviconService* favicon_service =
709 profile()->GetFaviconService(Profile::IMPLICIT_ACCESS);
710 if (favicon_service)
711 favicon_service->SetFaviconOutOfDateForPage(entry.url());
[email protected]96d185d2009-04-24 03:28:54712 }
713
[email protected]d5f942ba2008-09-26 19:30:34714 return true;
715}
716
[email protected]96d185d2009-04-24 03:28:54717void TabContents::Stop() {
718 render_manager_.Stop();
719 printing_.Stop();
720}
721
722void TabContents::Cut() {
723 render_view_host()->Cut();
724}
725
726void TabContents::Copy() {
727 render_view_host()->Copy();
728}
729
[email protected]a954bf72009-09-12 07:30:35730void TabContents::CopyToFindPboard() {
731 render_view_host()->CopyToFindPboard();
732}
733
[email protected]96d185d2009-04-24 03:28:54734void TabContents::Paste() {
735 render_view_host()->Paste();
736}
737
738void TabContents::DisassociateFromPopupCount() {
739 render_view_host()->DisassociateFromPopupCount();
740}
741
742TabContents* TabContents::Clone() {
743 // We create a new SiteInstance so that the new tab won't share processes
744 // with the old one. This can be changed in the future if we need it to share
745 // processes for some reason.
[email protected]420ae012009-04-24 05:16:32746 TabContents* tc = new TabContents(profile(),
[email protected]96d185d2009-04-24 03:28:54747 SiteInstance::CreateSiteInstance(profile()),
748 MSG_ROUTING_NONE, NULL);
749 tc->controller().CopyStateFrom(controller_);
750 return tc;
751}
752
[email protected]420ae012009-04-24 05:16:32753void TabContents::CreateShortcut() {
754 NavigationEntry* entry = controller_.GetLastCommittedEntry();
755 if (!entry)
756 return;
757
[email protected]70b8d6b2009-08-20 16:56:24758#if defined(OS_LINUX) && !defined(TOOLKIT_VIEWS)
[email protected]0ba5c5e2009-08-20 16:47:13759 CreateApplicationShortcutsDialogGtk::Show(view()->GetTopLevelNativeWindow(),
760 GetURL(), GetTitle());
[email protected]b96aa932009-08-12 21:34:49761#else
[email protected]420ae012009-04-24 05:16:32762 // We only allow one pending install request. By resetting the page id we
763 // effectively cancel the pending install request.
764 pending_install_.page_id = entry->page_id();
765 pending_install_.icon = GetFavIcon();
766 pending_install_.title = UTF16ToWideHack(GetTitle());
767 pending_install_.url = GetURL();
768 if (pending_install_.callback_functor) {
769 pending_install_.callback_functor->Cancel();
770 pending_install_.callback_functor = NULL;
771 }
772 DCHECK(!pending_install_.icon.isNull()) << "Menu item should be disabled.";
773 if (pending_install_.title.empty())
774 pending_install_.title = UTF8ToWide(GetURL().spec());
775
776 // Request the application info. When done OnDidGetApplicationInfo is invoked
777 // and we'll create the shortcut.
778 render_view_host()->GetApplicationInfo(pending_install_.page_id);
[email protected]b96aa932009-08-12 21:34:49779#endif
[email protected]420ae012009-04-24 05:16:32780}
781
[email protected]4d677202009-07-19 07:37:12782void TabContents::ShowPageInfo(const GURL& url,
783 const NavigationEntry::SSLStatus& ssl,
784 bool show_history) {
785 if (!delegate_)
786 return;
787
[email protected]bb678332009-07-21 00:15:50788 delegate_->ShowPageInfo(profile(), url, ssl, show_history);
[email protected]4d677202009-07-19 07:37:12789}
790
initial.commit09911bf2008-07-26 23:55:29791ConstrainedWindow* TabContents::CreateConstrainedDialog(
[email protected]e8382172009-06-19 22:16:28792 ConstrainedWindowDelegate* delegate) {
initial.commit09911bf2008-07-26 23:55:29793 ConstrainedWindow* window =
[email protected]e8382172009-06-19 22:16:28794 ConstrainedWindow::CreateConstrainedDialog(this, delegate);
initial.commit09911bf2008-07-26 23:55:29795 child_windows_.push_back(window);
796 return window;
797}
798
799void TabContents::AddNewContents(TabContents* new_contents,
800 WindowOpenDisposition disposition,
801 const gfx::Rect& initial_pos,
[email protected]c88a70fe2009-05-05 20:00:22802 bool user_gesture,
803 const GURL& creator_url) {
initial.commit09911bf2008-07-26 23:55:29804 if (!delegate_)
805 return;
806
[email protected]35f7d212009-04-29 21:19:27807 if ((disposition == NEW_POPUP) && !user_gesture &&
808 !CommandLine::ForCurrentProcess()->HasSwitch(
809 switches::kDisablePopupBlocking)) {
[email protected]2c4410d2009-05-06 23:46:22810 // Unrequested popups from normal pages are constrained unless they're in
[email protected]b6c874582009-05-08 19:38:31811 // the whitelist. The popup owner will handle checking this.
812 delegate_->GetConstrainingContents(this)->AddPopup(new_contents,
813 initial_pos,
814 creator_url.is_valid() ? creator_url.host() : std::string());
815 } else {
[email protected]304f8792009-08-19 21:28:09816#if defined(OS_CHROMEOS)
817 if (disposition == NEW_POPUP) {
818 // If the popup is bigger than a given factor of the screen, then
819 // turn it into a foreground tab (on chrome os only)
820 GdkScreen* screen = gdk_screen_get_default();
821 int max_width = gdk_screen_get_width(screen) * kMaxWidthFactor;
822 int max_height = gdk_screen_get_height(screen) * kMaxHeightFactor;
823 if (initial_pos.width() > max_width ||
824 initial_pos.height() > max_height) {
825 disposition = NEW_FOREGROUND_TAB;
826 }
827 }
828#endif
829
[email protected]0aa55312008-10-17 21:53:08830 new_contents->DisassociateFromPopupCount();
initial.commit09911bf2008-07-26 23:55:29831 delegate_->AddNewContents(this, new_contents, disposition, initial_pos,
832 user_gesture);
833 }
[email protected]b6c874582009-05-08 19:38:31834 PopupNotificationVisibilityChanged(ShowingBlockedPopupNotification());
initial.commit09911bf2008-07-26 23:55:29835}
836
initial.commit09911bf2008-07-26 23:55:29837void TabContents::CloseAllSuppressedPopups() {
[email protected]d6598c052008-11-05 19:03:25838 if (blocked_popups_)
[email protected]2c4410d2009-05-06 23:46:22839 blocked_popups_->CloseAll();
initial.commit09911bf2008-07-26 23:55:29840}
841
[email protected]96d185d2009-04-24 03:28:54842void TabContents::PopupNotificationVisibilityChanged(bool visible) {
843 render_view_host()->PopupNotificationVisibilityChanged(visible);
844}
845
[email protected]5c9e97a2009-09-09 23:48:30846gfx::NativeView TabContents::GetContentNativeView() const {
[email protected]96d185d2009-04-24 03:28:54847 return view_->GetContentNativeView();
848}
849
850gfx::NativeView TabContents::GetNativeView() const {
851 return view_->GetNativeView();
852}
853
854void TabContents::GetContainerBounds(gfx::Rect *out) const {
855 view_->GetContainerBounds(out);
856}
857
858void TabContents::Focus() {
859 view_->Focus();
860}
861
[email protected]90daadb42009-06-08 21:27:28862void TabContents::FocusThroughTabTraversal(bool reverse) {
[email protected]7e383692009-06-12 19:14:54863 if (showing_interstitial_page()) {
864 render_manager_.interstitial_page()->FocusThroughTabTraversal(reverse);
865 return;
866 }
[email protected]96d185d2009-04-24 03:28:54867 render_view_host()->SetInitialFocus(reverse);
868}
869
870bool TabContents::FocusLocationBarByDefault() {
871 DOMUI* dom_ui = GetDOMUIForCurrentState();
872 if (dom_ui)
873 return dom_ui->focus_location_bar_by_default();
[email protected]3e3f0eb2009-06-22 18:33:43874 NavigationEntry* entry = controller_.GetActiveEntry();
875 if (entry && entry->url() == GURL("about:blank"))
876 return true;
[email protected]96d185d2009-04-24 03:28:54877 return false;
878}
879
[email protected]616ed5a2008-11-21 22:27:24880void TabContents::AddInfoBar(InfoBarDelegate* delegate) {
881 // Look through the existing InfoBarDelegates we have for a match. If we've
882 // already got one that matches, then we don't add the new one.
[email protected]f86a07022008-11-25 01:06:05883 for (int i = 0; i < infobar_delegate_count(); ++i) {
[email protected]616ed5a2008-11-21 22:27:24884 if (GetInfoBarDelegateAt(i)->EqualsDelegate(delegate))
885 return;
886 }
887
888 infobar_delegates_.push_back(delegate);
[email protected]bfd04a62009-02-01 18:16:56889 NotificationService::current()->Notify(
890 NotificationType::TAB_CONTENTS_INFOBAR_ADDED,
891 Source<TabContents>(this),
892 Details<InfoBarDelegate>(delegate));
[email protected]616ed5a2008-11-21 22:27:24893
894 // Add ourselves as an observer for navigations the first time a delegate is
895 // added. We use this notification to expire InfoBars that need to expire on
896 // page transitions.
897 if (infobar_delegates_.size() == 1) {
[email protected]bfd04a62009-02-01 18:16:56898 registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED,
[email protected]ce3fa3c2009-04-20 19:55:57899 Source<NavigationController>(&controller_));
[email protected]616ed5a2008-11-21 22:27:24900 }
901}
902
903void TabContents::RemoveInfoBar(InfoBarDelegate* delegate) {
904 std::vector<InfoBarDelegate*>::iterator it =
905 find(infobar_delegates_.begin(), infobar_delegates_.end(), delegate);
906 if (it != infobar_delegates_.end()) {
907 InfoBarDelegate* delegate = *it;
[email protected]bfd04a62009-02-01 18:16:56908 NotificationService::current()->Notify(
909 NotificationType::TAB_CONTENTS_INFOBAR_REMOVED,
910 Source<TabContents>(this),
911 Details<InfoBarDelegate>(delegate));
[email protected]f86a07022008-11-25 01:06:05912 infobar_delegates_.erase(it);
[email protected]616ed5a2008-11-21 22:27:24913
[email protected]6a02963e2009-01-06 16:58:03914 // Remove ourselves as an observer if we are tracking no more InfoBars.
915 if (infobar_delegates_.empty()) {
[email protected]bfd04a62009-02-01 18:16:56916 registrar_.Remove(this, NotificationType::NAV_ENTRY_COMMITTED,
[email protected]ce3fa3c2009-04-20 19:55:57917 Source<NavigationController>(&controller_));
[email protected]6a02963e2009-01-06 16:58:03918 }
[email protected]616ed5a2008-11-21 22:27:24919 }
920}
[email protected]3edd9522009-03-04 22:19:41921
[email protected]f81821c2009-08-07 01:29:47922void TabContents::ReplaceInfoBar(InfoBarDelegate* old_delegate,
923 InfoBarDelegate* new_delegate) {
924 std::vector<InfoBarDelegate*>::iterator it =
925 find(infobar_delegates_.begin(), infobar_delegates_.end(), old_delegate);
926 DCHECK(it != infobar_delegates_.end());
927
928 // Notify the container about the change of plans.
929 scoped_ptr<std::pair<InfoBarDelegate*, InfoBarDelegate*> > details(
930 new std::pair<InfoBarDelegate*, InfoBarDelegate*>(
931 old_delegate, new_delegate));
932 NotificationService::current()->Notify(
933 NotificationType::TAB_CONTENTS_INFOBAR_REPLACED,
934 Source<TabContents>(this),
935 Details<std::pair<InfoBarDelegate*, InfoBarDelegate*> >(details.get()));
936
937 // Remove the old one.
938 infobar_delegates_.erase(it);
939
940 // Add the new one.
941 infobar_delegates_.push_back(new_delegate);
942}
943
[email protected]96d185d2009-04-24 03:28:54944bool TabContents::IsBookmarkBarAlwaysVisible() {
945 // See GetDOMUIForCurrentState() comment for more info. This case is very
946 // similar, but for non-first loads, we want to use the committed entry. This
947 // is so the bookmarks bar disappears at the same time the page does.
948 if (controller_.GetLastCommittedEntry()) {
949 // Not the first load, always use the committed DOM UI.
950 if (render_manager_.dom_ui())
951 return render_manager_.dom_ui()->force_bookmark_bar_visible();
952 return false; // Default.
953 }
954
955 // When it's the first load, we know either the pending one or the committed
956 // one will have the DOM UI in it (see GetDOMUIForCurrentState), and only one
957 // of them will be valid, so we can just check both.
958 if (render_manager_.pending_dom_ui())
959 return render_manager_.pending_dom_ui()->force_bookmark_bar_visible();
960 if (render_manager_.dom_ui())
961 return render_manager_.dom_ui()->force_bookmark_bar_visible();
962 return false; // Default.
963}
964
[email protected]d8f16ae2009-08-30 03:47:26965bool TabContents::IsExtensionShelfAlwaysVisible() {
966 // See GetDOMUIForCurrentState() comment for more info. This case is very
967 // similar, but for non-first loads, we want to use the committed entry. This
968 // is so the bookmarks bar disappears at the same time the page does.
969 if (controller_.GetLastCommittedEntry()) {
970 // Not the first load, always use the committed DOM UI.
971 if (render_manager_.dom_ui())
972 return render_manager_.dom_ui()->force_extension_shelf_visible();
973 return false; // Default.
974 }
975
976 // When it's the first load, we know either the pending one or the committed
977 // one will have the DOM UI in it (see GetDOMUIForCurrentState), and only one
978 // of them will be valid, so we can just check both.
979 if (render_manager_.pending_dom_ui())
980 return render_manager_.pending_dom_ui()->force_extension_shelf_visible();
981 if (render_manager_.dom_ui())
982 return render_manager_.dom_ui()->force_extension_shelf_visible();
983 return false; // Default.
984}
985
[email protected]3edd9522009-03-04 22:19:41986void TabContents::ToolbarSizeChanged(bool is_animating) {
987 TabContentsDelegate* d = delegate();
988 if (d)
989 d->ToolbarSizeChanged(this, is_animating);
990}
[email protected]616ed5a2008-11-21 22:27:24991
[email protected]d5f942ba2008-09-26 19:30:34992void TabContents::OnStartDownload(DownloadItem* download) {
993 DCHECK(download);
[email protected]d5f942ba2008-09-26 19:30:34994
995 // Download in a constrained popup is shown in the tab that opened it.
[email protected]b6c874582009-05-08 19:38:31996 TabContents* tab_contents = delegate()->GetConstrainingContents(this);
[email protected]d5f942ba2008-09-26 19:30:34997
[email protected]59560e0b2009-06-04 03:30:22998 if (tab_contents && tab_contents->delegate())
999 tab_contents->delegate()->OnStartDownload(download);
[email protected]3edd9522009-03-04 22:19:411000}
[email protected]3edd9522009-03-04 22:19:411001
[email protected]d5f942ba2008-09-26 19:30:341002void TabContents::WillClose(ConstrainedWindow* window) {
1003 ConstrainedWindowList::iterator it =
1004 find(child_windows_.begin(), child_windows_.end(), window);
1005 if (it != child_windows_.end())
1006 child_windows_.erase(it);
[email protected]66ba4932009-06-04 19:22:131007}
[email protected]d5f942ba2008-09-26 19:30:341008
[email protected]66ba4932009-06-04 19:22:131009void TabContents::WillCloseBlockedPopupContainer(
1010 BlockedPopupContainer* container) {
1011 DCHECK(blocked_popups_ == container);
1012 blocked_popups_ = NULL;
[email protected]d5f942ba2008-09-26 19:30:341013}
1014
[email protected]d5f942ba2008-09-26 19:30:341015void TabContents::DidMoveOrResize(ConstrainedWindow* window) {
[email protected]332af7732009-03-11 13:21:351016#if defined(OS_WIN)
[email protected]92edc472009-02-10 20:32:061017 UpdateWindow(GetNativeView());
[email protected]332af7732009-03-11 13:21:351018#endif
[email protected]d5f942ba2008-09-26 19:30:341019}
1020
[email protected]e491f1c2009-05-22 20:28:121021void TabContents::StartFinding(string16 find_text,
1022 bool forward_direction,
1023 bool case_sensitive) {
[email protected]6e95706d2009-04-23 22:55:011024 // If find_text is empty, it means FindNext was pressed with a keyboard
1025 // shortcut so unless we have something to search for we return early.
[email protected]e491f1c2009-05-22 20:28:121026 if (find_text.empty() && find_text_.empty()) {
1027 if (last_search_prepopulate_text_->empty())
1028 return;
1029 // Try whatever we searched for last in any tab.
1030 find_text = *last_search_prepopulate_text_;
1031 }
[email protected]6e95706d2009-04-23 22:55:011032
1033 // This is a FindNext operation if we are searching for the same text again,
1034 // or if the passed in search text is empty (FindNext keyboard shortcut). The
1035 // exception to this is if the Find was aborted (then we don't want FindNext
1036 // because the highlighting has been cleared and we need it to reappear). We
1037 // therefore treat FindNext after an aborted Find operation as a full fledged
1038 // Find.
1039 bool find_next = (find_text_ == find_text || find_text.empty()) &&
[email protected]e491f1c2009-05-22 20:28:121040 (last_search_case_sensitive_ == case_sensitive) &&
[email protected]6e95706d2009-04-23 22:55:011041 !find_op_aborted_;
1042 if (!find_next)
1043 current_find_request_id_ = find_request_id_counter_++;
1044
1045 if (!find_text.empty())
1046 find_text_ = find_text;
[email protected]e491f1c2009-05-22 20:28:121047 last_search_case_sensitive_ = case_sensitive;
[email protected]6e95706d2009-04-23 22:55:011048
1049 find_op_aborted_ = false;
1050
1051 // Keep track of what the last search was across the tabs.
[email protected]e491f1c2009-05-22 20:28:121052 *last_search_prepopulate_text_ = find_text;
[email protected]6e95706d2009-04-23 22:55:011053
1054 render_view_host()->StartFinding(current_find_request_id_,
1055 find_text_,
1056 forward_direction,
[email protected]e491f1c2009-05-22 20:28:121057 case_sensitive,
[email protected]6e95706d2009-04-23 22:55:011058 find_next);
1059}
1060
1061void TabContents::StopFinding(bool clear_selection) {
[email protected]2d2dffc2009-06-09 21:41:001062 // When |clear_selection| is true, it means the find string has been cleared
1063 // by the user, but the UI has not been dismissed.
1064 if (!clear_selection)
1065 find_ui_active_ = false;
[email protected]6e95706d2009-04-23 22:55:011066 find_op_aborted_ = true;
[email protected]e491f1c2009-05-22 20:28:121067 last_search_result_ = FindNotificationDetails();
[email protected]6e95706d2009-04-23 22:55:011068 render_view_host()->StopFinding(clear_selection);
1069}
1070
[email protected]5c4266922009-07-10 16:41:271071void TabContents::GetPageLanguage() {
1072 render_view_host()->GetPageLanguage();
1073}
1074
[email protected]420ae012009-04-24 05:16:321075void TabContents::OnJavaScriptMessageBoxClosed(IPC::Message* reply_msg,
1076 bool success,
1077 const std::wstring& prompt) {
1078 last_javascript_message_dismissal_ = base::TimeTicks::Now();
1079 render_manager_.OnJavaScriptMessageBoxClosed(reply_msg, success, prompt);
1080}
1081
[email protected]571e31152009-05-15 19:34:021082void TabContents::OnJavaScriptMessageBoxWindowDestroyed() {
1083 render_manager_.OnJavaScriptMessageBoxWindowDestroyed();
1084}
1085
[email protected]420ae012009-04-24 05:16:321086void TabContents::OnSavePage() {
1087 // If we can not save the page, try to download it.
1088 if (!SavePackage::IsSavableContents(contents_mime_type())) {
1089 DownloadManager* dlm = profile()->GetDownloadManager();
1090 const GURL& current_page_url = GetURL();
1091 if (dlm && current_page_url.is_valid())
[email protected]d82ed61e2009-06-16 02:46:221092 dlm->DownloadUrl(current_page_url, GURL(), "", this);
[email protected]420ae012009-04-24 05:16:321093 return;
1094 }
1095
1096 Stop();
1097
1098 // Create the save package and possibly prompt the user for the name to save
1099 // the page as. The user prompt is an asynchronous operation that runs on
1100 // another thread.
[email protected]d82ed61e2009-06-16 02:46:221101 save_package_ = new SavePackage(this);
[email protected]420ae012009-04-24 05:16:321102 save_package_->GetSaveInfo();
1103}
1104
1105// Used in automated testing to bypass prompting the user for file names.
1106// Instead, the names and paths are hard coded rather than running them through
1107// file name sanitation and extension / mime checking.
[email protected]cdf6cc82009-08-04 21:48:591108bool TabContents::SavePage(const std::wstring& main_file,
[email protected]420ae012009-04-24 05:16:321109 const std::wstring& dir_path,
1110 SavePackage::SavePackageType save_type) {
1111 // Stop the page from navigating.
1112 Stop();
1113
[email protected]d82ed61e2009-06-16 02:46:221114 save_package_ = new SavePackage(this, save_type,
[email protected]420ae012009-04-24 05:16:321115 FilePath::FromWStringHack(main_file),
1116 FilePath::FromWStringHack(dir_path));
[email protected]cdf6cc82009-08-04 21:48:591117 return save_package_->Init();
[email protected]420ae012009-04-24 05:16:321118}
1119
1120void TabContents::PrintPreview() {
1121 // We don't show the print preview yet, only the print dialog.
1122 PrintNow();
1123}
1124
1125bool TabContents::PrintNow() {
1126 // We can't print interstitial page for now.
1127 if (showing_interstitial_page())
1128 return false;
1129
1130 return render_view_host()->PrintPages();
1131}
1132
[email protected]82270452009-06-19 15:58:011133void TabContents::PrintingDone(int document_cookie, bool success) {
1134 render_view_host()->PrintingDone(document_cookie, success);
1135}
1136
[email protected]420ae012009-04-24 05:16:321137bool TabContents::IsActiveEntry(int32 page_id) {
1138 NavigationEntry* active_entry = controller_.GetActiveEntry();
1139 return (active_entry != NULL &&
1140 active_entry->site_instance() == GetSiteInstance() &&
1141 active_entry->page_id() == page_id);
1142}
1143
[email protected]96d185d2009-04-24 03:28:541144// Notifies the RenderWidgetHost instance about the fact that the page is
1145// loading, or done loading and calls the base implementation.
1146void TabContents::SetIsLoading(bool is_loading,
1147 LoadNotificationDetails* details) {
1148 if (is_loading == is_loading_)
1149 return;
1150
1151 if (!is_loading) {
1152 load_state_ = net::LOAD_STATE_IDLE;
1153 load_state_host_.clear();
1154 }
1155
1156 render_manager_.SetIsLoading(is_loading);
1157
1158 is_loading_ = is_loading;
1159 waiting_for_response_ = is_loading;
1160
1161 if (delegate_)
1162 delegate_->LoadingStateChanged(this);
[email protected]329581b2009-04-28 06:52:351163 NotifyNavigationStateChanged(INVALIDATE_LOAD);
[email protected]96d185d2009-04-24 03:28:541164
1165 NotificationType type = is_loading ? NotificationType::LOAD_START :
1166 NotificationType::LOAD_STOP;
[email protected]ce5c4502009-05-06 16:46:111167 NotificationDetails det = NotificationService::NoDetails();
[email protected]96d185d2009-04-24 03:28:541168 if (details)
1169 det = Details<LoadNotificationDetails>(details);
1170 NotificationService::current()->Notify(type,
1171 Source<NavigationController>(&controller_),
1172 det);
1173}
1174
[email protected]2c4410d2009-05-06 23:46:221175void TabContents::CreateBlockedPopupContainerIfNecessary() {
1176 if (blocked_popups_)
1177 return;
1178
[email protected]4d4c0b92009-06-08 23:13:011179 blocked_popups_ = BlockedPopupContainer::Create(this, profile());
[email protected]2c4410d2009-05-06 23:46:221180}
1181
[email protected]b6c874582009-05-08 19:38:311182void TabContents::AddPopup(TabContents* new_contents,
1183 const gfx::Rect& initial_pos,
1184 const std::string& host) {
[email protected]2c4410d2009-05-06 23:46:221185 CreateBlockedPopupContainerIfNecessary();
1186 blocked_popups_->AddTabContents(new_contents, initial_pos, host);
[email protected]2c4410d2009-05-06 23:46:221187}
1188
[email protected]e69bce3dd2009-04-20 22:05:121189// TODO(brettw) This should be on the TabContentsView.
[email protected]66ba4932009-06-04 19:22:131190void TabContents::RepositionSupressedPopupsToFit() {
[email protected]d6598c052008-11-05 19:03:251191 if (blocked_popups_)
[email protected]4d4c0b92009-06-08 23:13:011192 blocked_popups_->RepositionBlockedPopupContainer();
[email protected]d5f942ba2008-09-26 19:30:341193}
1194
[email protected]b9681312008-11-07 00:08:261195bool TabContents::ShowingBlockedPopupNotification() const {
1196 return blocked_popups_ != NULL &&
[email protected]2c4410d2009-05-06 23:46:221197 blocked_popups_->GetBlockedPopupCount() != 0;
[email protected]b9681312008-11-07 00:08:261198}
[email protected]616ed5a2008-11-21 22:27:241199
1200namespace {
1201bool TransitionIsReload(PageTransition::Type transition) {
1202 return PageTransition::StripQualifier(transition) == PageTransition::RELOAD;
1203}
1204}
1205
1206void TabContents::ExpireInfoBars(
1207 const NavigationController::LoadCommittedDetails& details) {
1208 // Only hide InfoBars when the user has done something that makes the main
1209 // frame load. We don't want various automatic or subframe navigations making
1210 // it disappear.
1211 if (!details.is_user_initiated_main_frame_load())
1212 return;
1213
[email protected]f86a07022008-11-25 01:06:051214 for (int i = infobar_delegate_count() - 1; i >= 0; --i) {
[email protected]616ed5a2008-11-21 22:27:241215 InfoBarDelegate* delegate = GetInfoBarDelegateAt(i);
[email protected]f86a07022008-11-25 01:06:051216 if (delegate->ShouldExpire(details))
[email protected]616ed5a2008-11-21 22:27:241217 RemoveInfoBar(delegate);
[email protected]616ed5a2008-11-21 22:27:241218 }
1219}
[email protected]fdd61c62009-04-22 19:22:571220
1221void TabContents::OnGearsCreateShortcutDone(
1222 const GearsShortcutData2& shortcut_data, bool success) {
1223 NavigationEntry* current_entry = controller_.GetLastCommittedEntry();
1224 bool same_page =
1225 current_entry && pending_install_.page_id == current_entry->page_id();
1226
1227 if (success && same_page) {
1228 // Only switch to app mode if the user chose to create a shortcut and
1229 // we're still on the same page that it corresponded to.
1230 if (delegate())
1231 delegate()->ConvertContentsToApplication(this);
1232 }
1233
1234 // Reset the page id to indicate no requests are pending.
1235 pending_install_.page_id = 0;
1236 pending_install_.callback_functor = NULL;
1237}
[email protected]96d185d2009-04-24 03:28:541238
1239DOMUI* TabContents::GetDOMUIForCurrentState() {
1240 // When there is a pending navigation entry, we want to use the pending DOMUI
1241 // that goes along with it to control the basic flags. For example, we want to
1242 // show the pending URL in the URL bar, so we want the display_url flag to
1243 // be from the pending entry.
1244 //
1245 // The confusion comes because there are multiple possibilities for the
1246 // initial load in a tab as a side effect of the way the RenderViewHostManager
1247 // works.
1248 //
1249 // - For the very first tab the load looks "normal". The new tab DOM UI is
1250 // the pending one, and we want it to apply here.
1251 //
1252 // - For subsequent new tabs, they'll get a new SiteInstance which will then
1253 // get switched to the one previously associated with the new tab pages.
1254 // This switching will cause the manager to commit the RVH/DOMUI. So we'll
1255 // have a committed DOM UI in this case.
1256 //
1257 // This condition handles all of these cases:
1258 //
1259 // - First load in first tab: no committed nav entry + pending nav entry +
1260 // pending dom ui:
1261 // -> Use pending DOM UI if any.
1262 //
1263 // - First load in second tab: no committed nav entry + pending nav entry +
1264 // no pending DOM UI:
1265 // -> Use the committed DOM UI if any.
1266 //
1267 // - Second navigation in any tab: committed nav entry + pending nav entry:
1268 // -> Use pending DOM UI if any.
1269 //
1270 // - Normal state with no load: committed nav entry + no pending nav entry:
1271 // -> Use committed DOM UI.
1272 if (controller_.pending_entry() &&
1273 (controller_.GetLastCommittedEntry() ||
1274 render_manager_.pending_dom_ui()))
1275 return render_manager_.pending_dom_ui();
1276 return render_manager_.dom_ui();
1277}
[email protected]420ae012009-04-24 05:16:321278
1279void TabContents::DidNavigateMainFramePostCommit(
1280 const NavigationController::LoadCommittedDetails& details,
1281 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]c2e74fe82009-09-03 17:57:441282 if (opener_dom_ui_type_ != DOMUIFactory::kNoDOMUI) {
1283 // If this is a window.open navigation, use the same DOMUI as the renderer
1284 // that opened the window, as long as both renderers have the same
1285 // privileges.
1286 if (opener_dom_ui_type_ == DOMUIFactory::GetDOMUIType(GetURL())) {
1287 DOMUI* dom_ui = DOMUIFactory::CreateDOMUIForURL(this, GetURL());
1288 // dom_ui might be NULL if the URL refers to a non-existent extension.
1289 if (dom_ui) {
1290 render_manager_.SetDOMUIPostCommit(dom_ui);
1291 dom_ui->RenderViewCreated(render_view_host());
1292 }
1293 }
1294 opener_dom_ui_type_ = DOMUIFactory::kNoDOMUI;
1295 }
1296
[email protected]420ae012009-04-24 05:16:321297 if (details.is_user_initiated_main_frame_load()) {
1298 // Clear the status bubble. This is a workaround for a bug where WebKit
1299 // doesn't let us know that the cursor left an element during a
1300 // transition (this is also why the mouse cursor remains as a hand after
1301 // clicking on a link); see bugs 1184641 and 980803. We don't want to
1302 // clear the bubble when a user navigates to a named anchor in the same
1303 // page.
1304 UpdateTargetURL(details.entry->page_id(), GURL());
1305
1306 // UpdateHelpersForDidNavigate will handle the case where the password_form
1307 // origin is valid.
1308 // TODO(brettw) bug 1343111: Password manager stuff in here needs to be
1309 // cleaned up and covered by tests.
1310 if (!params.password_form.origin.is_valid())
1311 GetPasswordManager()->DidNavigate();
1312 }
1313
1314 // The keyword generator uses the navigation entries, so must be called after
1315 // the commit.
1316 GenerateKeywordIfNecessary(params);
1317
1318 // Allow the new page to set the title again.
1319 received_page_title_ = false;
1320
1321 // Get the favicon, either from history or request it from the net.
1322 fav_icon_helper_.FetchFavIcon(details.entry->url());
1323
[email protected]aece2c7f2009-08-27 20:43:171324 // Disable all page actions, unless this is an in-page navigation.
1325 if (!enabled_page_actions_.empty()) {
1326 url_canon::Replacements<char> replacements;
1327 replacements.ClearRef();
1328 if (params.url.ReplaceComponents(replacements) !=
1329 params.referrer.ReplaceComponents(replacements)) {
1330 enabled_page_actions_.clear();
1331 }
1332 }
[email protected]ce5c4502009-05-06 16:46:111333
[email protected]420ae012009-04-24 05:16:321334 // Close constrained popups if necessary.
1335 MaybeCloseChildWindows(details.previous_url, details.entry->url());
1336
1337 // Update the starred state.
1338 UpdateStarredStateForCurrentURL();
1339}
1340
1341void TabContents::DidNavigateAnyFramePostCommit(
1342 RenderViewHost* render_view_host,
1343 const NavigationController::LoadCommittedDetails& details,
1344 const ViewHostMsg_FrameNavigate_Params& params) {
1345 // If we navigate, start showing messages again. This does nothing to prevent
1346 // a malicious script from spamming messages, since the script could just
1347 // reload the page to stop blocking.
1348 suppress_javascript_messages_ = false;
1349
[email protected]420ae012009-04-24 05:16:321350 // Notify the password manager of the navigation or form submit.
1351 // TODO(brettw) bug 1343111: Password manager stuff in here needs to be
1352 // cleaned up and covered by tests.
1353 if (params.password_form.origin.is_valid())
1354 GetPasswordManager()->ProvisionallySavePassword(params.password_form);
1355}
1356
1357void TabContents::MaybeCloseChildWindows(const GURL& previous_url,
1358 const GURL& current_url) {
1359 if (net::RegistryControlledDomainService::SameDomainOrHost(
1360 previous_url, current_url))
1361 return;
1362
1363 // Clear out any child windows since we are leaving this page entirely.
1364 // We use indices instead of iterators in case CloseWindow does something
1365 // that may invalidate an iterator.
1366 int size = static_cast<int>(child_windows_.size());
1367 for (int i = size - 1; i >= 0; --i) {
1368 ConstrainedWindow* window = child_windows_[i];
1369 if (window)
1370 window->CloseConstrainedWindow();
1371 }
[email protected]e29300cd2009-07-23 21:14:591372
1373 // Close the popup container.
1374 if (blocked_popups_) {
1375 blocked_popups_->Destroy();
1376 blocked_popups_ = NULL;
1377 }
[email protected]420ae012009-04-24 05:16:321378}
1379
1380void TabContents::UpdateStarredStateForCurrentURL() {
1381 BookmarkModel* model = profile()->GetBookmarkModel();
1382 const bool old_state = is_starred_;
1383 is_starred_ = (model && model->IsBookmarked(GetURL()));
1384
1385 if (is_starred_ != old_state && delegate())
1386 delegate()->URLStarredChanged(this, is_starred_);
1387}
1388
1389void TabContents::UpdateAlternateErrorPageURL() {
1390 GURL url = GetAlternateErrorPageURL();
1391 render_view_host()->SetAlternateErrorPageURL(url);
1392}
1393
1394void TabContents::UpdateWebPreferences() {
1395 render_view_host()->UpdateWebPreferences(GetWebkitPrefs());
1396}
1397
1398void TabContents::UpdateMaxPageIDIfNecessary(SiteInstance* site_instance,
1399 RenderViewHost* rvh) {
1400 // If we are creating a RVH for a restored controller, then we might
1401 // have more page IDs than the SiteInstance's current max page ID. We must
1402 // make sure that the max page ID is larger than any restored page ID.
1403 // Note that it is ok for conflicting page IDs to exist in another tab
1404 // (i.e., NavigationController), but if any page ID is larger than the max,
1405 // the back/forward list will get confused.
1406 int max_restored_page_id = controller_.max_restored_page_id();
1407 if (max_restored_page_id > 0) {
1408 int curr_max_page_id = site_instance->max_page_id();
1409 if (max_restored_page_id > curr_max_page_id) {
1410 // Need to update the site instance immediately.
1411 site_instance->UpdateMaxPageID(max_restored_page_id);
1412
1413 // Also tell the renderer to update its internal representation. We
1414 // need to reserve enough IDs to make all restored page IDs less than
1415 // the max.
1416 if (curr_max_page_id < 0)
1417 curr_max_page_id = 0;
1418 rvh->ReservePageIDRange(max_restored_page_id - curr_max_page_id);
1419 }
1420 }
1421}
1422
1423void TabContents::UpdateHistoryForNavigation(
[email protected]ebe89e062009-08-13 23:16:541424 const GURL& virtual_url,
[email protected]befd8d822009-07-01 04:51:471425 const NavigationController::LoadCommittedDetails& details,
[email protected]420ae012009-04-24 05:16:321426 const ViewHostMsg_FrameNavigate_Params& params) {
1427 if (profile()->IsOffTheRecord())
1428 return;
1429
1430 // Add to history service.
1431 HistoryService* hs = profile()->GetHistoryService(Profile::IMPLICIT_ACCESS);
1432 if (hs) {
1433 if (PageTransition::IsMainFrame(params.transition) &&
[email protected]ebe89e062009-08-13 23:16:541434 virtual_url != params.url) {
1435 // Hack on the "virtual" URL so that it will appear in history. For some
[email protected]420ae012009-04-24 05:16:321436 // types of URLs, we will display a magic URL that is different from where
1437 // the page is actually navigated. We want the user to see in history
[email protected]ebe89e062009-08-13 23:16:541438 // what they saw in the URL bar, so we add the virtual URL as a redirect.
1439 // This only applies to the main frame, as the virtual URL doesn't apply
[email protected]420ae012009-04-24 05:16:321440 // to sub-frames.
1441 std::vector<GURL> redirects = params.redirects;
1442 if (!redirects.empty())
[email protected]ebe89e062009-08-13 23:16:541443 redirects.back() = virtual_url;
1444 hs->AddPage(virtual_url, this, params.page_id, params.referrer,
[email protected]befd8d822009-07-01 04:51:471445 params.transition, redirects, details.did_replace_entry);
[email protected]420ae012009-04-24 05:16:321446 } else {
1447 hs->AddPage(params.url, this, params.page_id, params.referrer,
[email protected]befd8d822009-07-01 04:51:471448 params.transition, params.redirects,
1449 details.did_replace_entry);
[email protected]420ae012009-04-24 05:16:321450 }
1451 }
1452}
1453
1454bool TabContents::UpdateTitleForEntry(NavigationEntry* entry,
1455 const std::wstring& title) {
1456 // For file URLs without a title, use the pathname instead. In the case of a
1457 // synthesized title, we don't want the update to count toward the "one set
1458 // per page of the title to history."
1459 std::wstring final_title;
1460 bool explicit_set;
1461 if (entry->url().SchemeIsFile() && title.empty()) {
1462 final_title = UTF8ToWide(entry->url().ExtractFileName());
1463 explicit_set = false; // Don't count synthetic titles toward the set limit.
1464 } else {
1465 TrimWhitespace(title, TRIM_ALL, &final_title);
1466 explicit_set = true;
1467 }
1468
1469 if (final_title == UTF16ToWideHack(entry->title()))
1470 return false; // Nothing changed, don't bother.
1471
1472 entry->set_title(WideToUTF16Hack(final_title));
1473
1474 // Update the history system for this page.
1475 if (!profile()->IsOffTheRecord() && !received_page_title_) {
1476 HistoryService* hs =
1477 profile()->GetHistoryService(Profile::IMPLICIT_ACCESS);
1478 if (hs)
[email protected]ebe89e062009-08-13 23:16:541479 hs->SetPageTitle(entry->virtual_url(), final_title);
[email protected]420ae012009-04-24 05:16:321480
1481 // Don't allow the title to be saved again for explicitly set ones.
1482 received_page_title_ = explicit_set;
1483 }
1484
1485 // Lastly, set the title for the view.
1486 view_->SetPageTitle(final_title);
1487
1488 return true;
1489}
1490
1491void TabContents::NotifySwapped() {
1492 // After sending out a swap notification, we need to send a disconnect
1493 // notification so that clients that pick up a pointer to |this| can NULL the
1494 // pointer. See Bug 1230284.
1495 notify_disconnection_ = true;
1496 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341497 NotificationType::TAB_CONTENTS_SWAPPED,
[email protected]d82ed61e2009-06-16 02:46:221498 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321499 NotificationService::NoDetails());
1500}
1501
1502void TabContents::NotifyConnected() {
1503 notify_disconnection_ = true;
1504 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341505 NotificationType::TAB_CONTENTS_CONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221506 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321507 NotificationService::NoDetails());
1508}
1509
1510void TabContents::NotifyDisconnected() {
1511 if (!notify_disconnection_)
1512 return;
1513
1514 notify_disconnection_ = false;
1515 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341516 NotificationType::TAB_CONTENTS_DISCONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221517 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321518 NotificationService::NoDetails());
1519}
1520
1521void TabContents::GenerateKeywordIfNecessary(
1522 const ViewHostMsg_FrameNavigate_Params& params) {
1523 if (!params.searchable_form_url.is_valid())
1524 return;
1525
1526 if (profile()->IsOffTheRecord())
1527 return;
1528
1529 int last_index = controller_.last_committed_entry_index();
1530 // When there was no previous page, the last index will be 0. This is
1531 // normally due to a form submit that opened in a new tab.
1532 // TODO(brettw) bug 916126: we should support keywords when form submits
1533 // happen in new tabs.
1534 if (last_index <= 0)
1535 return;
1536 const NavigationEntry* previous_entry =
1537 controller_.GetEntryAtIndex(last_index - 1);
1538 if (IsFormSubmit(previous_entry)) {
1539 // Only generate a keyword if the previous page wasn't itself a form
1540 // submit.
1541 return;
1542 }
1543
1544 GURL keyword_url = previous_entry->user_typed_url().is_valid() ?
1545 previous_entry->user_typed_url() : previous_entry->url();
1546 std::wstring keyword =
1547 TemplateURLModel::GenerateKeyword(keyword_url, true); // autodetected
1548 if (keyword.empty())
1549 return;
1550
1551 TemplateURLModel* url_model = profile()->GetTemplateURLModel();
1552 if (!url_model)
1553 return;
1554
1555 if (!url_model->loaded()) {
1556 url_model->Load();
1557 return;
1558 }
1559
1560 const TemplateURL* current_url;
1561 std::wstring url = UTF8ToWide(params.searchable_form_url.spec());
1562 if (!url_model->CanReplaceKeyword(keyword, url, &current_url))
1563 return;
1564
1565 if (current_url) {
1566 if (current_url->originating_url().is_valid()) {
1567 // The existing keyword was generated from an OpenSearch description
1568 // document, don't regenerate.
1569 return;
1570 }
1571 url_model->Remove(current_url);
1572 }
1573 TemplateURL* new_url = new TemplateURL();
1574 new_url->set_keyword(keyword);
1575 new_url->set_short_name(keyword);
1576 new_url->SetURL(url, 0, 0);
1577 new_url->add_input_encoding(params.searchable_form_encoding);
1578 DCHECK(controller_.GetLastCommittedEntry());
1579 const GURL& favicon_url =
1580 controller_.GetLastCommittedEntry()->favicon().url();
1581 if (favicon_url.is_valid()) {
1582 new_url->SetFavIconURL(favicon_url);
1583 } else {
1584 // The favicon url isn't valid. This means there really isn't a favicon,
1585 // or the favicon url wasn't obtained before the load started. This assumes
1586 // the later.
1587 // TODO(sky): Need a way to set the favicon that doesn't involve generating
1588 // its url.
1589 new_url->SetFavIconURL(TemplateURL::GenerateFaviconURL(params.referrer));
1590 }
1591 new_url->set_safe_for_autoreplace(true);
1592 url_model->Add(new_url);
1593}
1594
[email protected]bf5c2ff392009-07-08 16:24:331595void TabContents::OnUserGesture() {
1596 // See comment in RenderViewHostDelegate::OnUserGesture as to why we do this.
1597 DownloadRequestManager* drm = g_browser_process->download_request_manager();
1598 if (drm)
1599 drm->OnUserGesture(this);
1600 controller_.OnUserGesture();
1601}
1602
1603void TabContents::OnFindReply(int request_id,
1604 int number_of_matches,
1605 const gfx::Rect& selection_rect,
1606 int active_match_ordinal,
1607 bool final_update) {
[email protected]3ce7ced2009-07-24 18:52:421608 // Ignore responses for requests that have been aborted.
1609 if (find_op_aborted_)
1610 return;
1611
[email protected]bf5c2ff392009-07-08 16:24:331612 // Ignore responses for requests other than the one we have most recently
1613 // issued. That way we won't act on stale results when the user has
1614 // already typed in another query.
1615 if (request_id != current_find_request_id_)
1616 return;
1617
1618 if (number_of_matches == -1)
1619 number_of_matches = last_search_result_.number_of_matches();
1620 if (active_match_ordinal == -1)
1621 active_match_ordinal = last_search_result_.active_match_ordinal();
1622
1623 gfx::Rect selection = selection_rect;
1624 if (selection.IsEmpty())
1625 selection = last_search_result_.selection_rect();
1626
1627 // Notify the UI, automation and any other observers that a find result was
1628 // found.
1629 last_search_result_ = FindNotificationDetails(request_id, number_of_matches,
1630 selection, active_match_ordinal,
1631 final_update);
1632 NotificationService::current()->Notify(
1633 NotificationType::FIND_RESULT_AVAILABLE,
1634 Source<TabContents>(this),
1635 Details<FindNotificationDetails>(&last_search_result_));
1636}
1637
1638void TabContents::GoToEntryAtOffset(int offset) {
1639 controller_.GoToOffset(offset);
1640}
1641
1642void TabContents::GetHistoryListCount(int* back_list_count,
1643 int* forward_list_count) {
1644 int current_index = controller_.last_committed_entry_index();
1645 *back_list_count = current_index;
1646 *forward_list_count = controller_.entry_count() - current_index - 1;
1647}
1648
1649void TabContents::OnMissingPluginStatus(int status) {
1650#if defined(OS_WIN)
1651// TODO(PORT): pull in when plug-ins work
1652 GetPluginInstaller()->OnMissingPluginStatus(status);
1653#endif
1654}
1655
1656void TabContents::OnCrashedPlugin(const FilePath& plugin_path) {
[email protected]bf5c2ff392009-07-08 16:24:331657 DCHECK(!plugin_path.value().empty());
1658
1659 std::wstring plugin_name = plugin_path.ToWStringHack();
[email protected]d32d46182009-07-23 00:09:351660#if defined(OS_WIN) || defined(OS_MACOSX)
[email protected]bf5c2ff392009-07-08 16:24:331661 scoped_ptr<FileVersionInfo> version_info(
1662 FileVersionInfo::CreateFileVersionInfo(plugin_path));
1663 if (version_info.get()) {
1664 const std::wstring& product_name = version_info->product_name();
[email protected]996fd702009-09-04 19:12:371665 if (!product_name.empty()) {
[email protected]bf5c2ff392009-07-08 16:24:331666 plugin_name = product_name;
[email protected]996fd702009-09-04 19:12:371667#if defined(OS_MACOSX)
1668 // Many plugins on the Mac have .plugin in the actual name, which looks
1669 // terrible, so look for that and strip it off if present.
1670 const std::wstring plugin_extension(L".plugin");
1671 if (EndsWith(plugin_name, plugin_extension, true))
1672 plugin_name.erase(plugin_name.length() - plugin_extension.length());
1673#endif // OS_MACOSX
1674 }
[email protected]bf5c2ff392009-07-08 16:24:331675 }
[email protected]4a560ed2009-07-13 23:35:261676#else
1677 NOTIMPLEMENTED() << " convert plugin path to plugin name";
1678#endif
[email protected]bf5c2ff392009-07-08 16:24:331679 AddInfoBar(new SimpleAlertInfoBarDelegate(
1680 this, l10n_util::GetStringF(IDS_PLUGIN_CRASHED_PROMPT, plugin_name),
1681 NULL));
[email protected]bf5c2ff392009-07-08 16:24:331682}
1683
1684void TabContents::OnCrashedWorker() {
1685 AddInfoBar(new SimpleAlertInfoBarDelegate(
1686 this, l10n_util::GetString(IDS_WEBWORKER_CRASHED_PROMPT),
1687 NULL));
1688}
1689
1690void TabContents::OnDidGetApplicationInfo(
1691 int32 page_id,
1692 const webkit_glue::WebApplicationInfo& info) {
1693 if (pending_install_.page_id != page_id)
1694 return; // The user clicked create on a separate page. Ignore this.
1695
1696 pending_install_.callback_functor =
1697 new GearsCreateShortcutCallbackFunctor(this);
1698 GearsCreateShortcut(
1699 info, pending_install_.title, pending_install_.url, pending_install_.icon,
1700 NewCallback(pending_install_.callback_functor,
1701 &GearsCreateShortcutCallbackFunctor::Run));
1702}
1703
1704void TabContents::DidStartProvisionalLoadForFrame(
1705 RenderViewHost* render_view_host,
1706 bool is_main_frame,
1707 const GURL& url) {
1708 ProvisionalLoadDetails details(is_main_frame,
1709 controller_.IsURLInPageNavigation(url),
1710 url, std::string(), false);
1711 NotificationService::current()->Notify(
1712 NotificationType::FRAME_PROVISIONAL_LOAD_START,
1713 Source<NavigationController>(&controller_),
1714 Details<ProvisionalLoadDetails>(&details));
1715}
1716
1717void TabContents::DidStartReceivingResourceResponse(
1718 ResourceRequestDetails* details) {
1719 NotificationService::current()->Notify(
1720 NotificationType::RESOURCE_RESPONSE_STARTED,
1721 Source<NavigationController>(&controller()),
1722 Details<ResourceRequestDetails>(details));
1723}
1724
1725void TabContents::DidRedirectResource(ResourceRequestDetails* details) {
1726 NotificationService::current()->Notify(
1727 NotificationType::RESOURCE_RECEIVED_REDIRECT,
1728 Source<NavigationController>(&controller()),
1729 Details<ResourceRequestDetails>(details));
1730}
1731
1732void TabContents::DidLoadResourceFromMemoryCache(
1733 const GURL& url,
1734 const std::string& frame_origin,
1735 const std::string& main_frame_origin,
1736 const std::string& security_info) {
1737 // Send out a notification that we loaded a resource from our memory cache.
1738 int cert_id = 0, cert_status = 0, security_bits = 0;
1739 SSLManager::DeserializeSecurityInfo(security_info,
1740 &cert_id, &cert_status,
1741 &security_bits);
1742 LoadFromMemoryCacheDetails details(url, frame_origin, main_frame_origin,
[email protected]76543b92009-08-31 17:27:451743 process()->id(), cert_id, cert_status);
[email protected]bf5c2ff392009-07-08 16:24:331744
1745 NotificationService::current()->Notify(
1746 NotificationType::LOAD_FROM_MEMORY_CACHE,
1747 Source<NavigationController>(&controller_),
1748 Details<LoadFromMemoryCacheDetails>(&details));
1749}
1750
1751void TabContents::DidFailProvisionalLoadWithError(
1752 RenderViewHost* render_view_host,
1753 bool is_main_frame,
1754 int error_code,
1755 const GURL& url,
1756 bool showing_repost_interstitial) {
1757 if (net::ERR_ABORTED == error_code) {
1758 // EVIL HACK ALERT! Ignore failed loads when we're showing interstitials.
1759 // This means that the interstitial won't be torn down properly, which is
1760 // bad. But if we have an interstitial, go back to another tab type, and
1761 // then load the same interstitial again, we could end up getting the first
1762 // interstitial's "failed" message (as a result of the cancel) when we're on
1763 // the second one.
1764 //
1765 // We can't tell this apart, so we think we're tearing down the current page
1766 // which will cause a crash later one. There is also some code in
1767 // RenderViewHostManager::RendererAbortedProvisionalLoad that is commented
1768 // out because of this problem.
1769 //
1770 // http://code.google.com/p/chromium/issues/detail?id=2855
1771 // Because this will not tear down the interstitial properly, if "back" is
1772 // back to another tab type, the interstitial will still be somewhat alive
1773 // in the previous tab type. If you navigate somewhere that activates the
1774 // tab with the interstitial again, you'll see a flash before the new load
1775 // commits of the interstitial page.
1776 if (showing_interstitial_page()) {
1777 LOG(WARNING) << "Discarding message during interstitial.";
1778 return;
1779 }
1780
1781 // This will discard our pending entry if we cancelled the load (e.g., if we
1782 // decided to download the file instead of load it). Only discard the
1783 // pending entry if the URLs match, otherwise the user initiated a navigate
1784 // before the page loaded so that the discard would discard the wrong entry.
1785 NavigationEntry* pending_entry = controller_.pending_entry();
1786 if (pending_entry && pending_entry->url() == url) {
1787 controller_.DiscardNonCommittedEntries();
1788 // Update the URL display.
1789 NotifyNavigationStateChanged(TabContents::INVALIDATE_URL);
1790 }
1791
1792 render_manager_.RendererAbortedProvisionalLoad(render_view_host);
1793 }
1794
1795 // Send out a notification that we failed a provisional load with an error.
1796 ProvisionalLoadDetails details(is_main_frame,
1797 controller_.IsURLInPageNavigation(url),
1798 url, std::string(), false);
1799 details.set_error_code(error_code);
1800
1801 NotificationService::current()->Notify(
1802 NotificationType::FAIL_PROVISIONAL_LOAD_WITH_ERROR,
1803 Source<NavigationController>(&controller_),
1804 Details<ProvisionalLoadDetails>(&details));
1805}
1806
1807void TabContents::DocumentLoadedInFrame() {
1808 controller_.DocumentLoadedInFrame();
1809}
1810
[email protected]8d3347f2009-07-09 22:00:211811RenderViewHostDelegate::View* TabContents::GetViewDelegate() {
[email protected]420ae012009-04-24 05:16:321812 return view_.get();
1813}
1814
[email protected]8d3347f2009-07-09 22:00:211815RenderViewHostDelegate::RendererManagement*
1816TabContents::GetRendererManagementDelegate() {
1817 return &render_manager_;
1818}
1819
[email protected]bf5c2ff392009-07-08 16:24:331820RenderViewHostDelegate::BrowserIntegration*
[email protected]8d3347f2009-07-09 22:00:211821 TabContents::GetBrowserIntegrationDelegate() {
1822 return this;
[email protected]bf5c2ff392009-07-08 16:24:331823}
1824
[email protected]8d3347f2009-07-09 22:00:211825RenderViewHostDelegate::Resource* TabContents::GetResourceDelegate() {
1826 return this;
[email protected]bf5c2ff392009-07-08 16:24:331827}
1828
[email protected]8d3347f2009-07-09 22:00:211829RenderViewHostDelegate::Save* TabContents::GetSaveDelegate() {
[email protected]420ae012009-04-24 05:16:321830 return save_package_.get(); // May be NULL, but we can return NULL.
1831}
1832
[email protected]8d3347f2009-07-09 22:00:211833RenderViewHostDelegate::Printing* TabContents::GetPrintingDelegate() {
1834 return &printing_;
1835}
1836
1837RenderViewHostDelegate::FavIcon* TabContents::GetFavIconDelegate() {
1838 return &fav_icon_helper_;
1839}
1840
1841RenderViewHostDelegate::Autofill* TabContents::GetAutofillDelegate() {
1842 if (autofill_manager_.get() == NULL)
1843 autofill_manager_.reset(new AutofillManager(this));
1844 return autofill_manager_.get();
[email protected]bf5c2ff392009-07-08 16:24:331845}
1846
[email protected]80d96fa2009-06-10 22:34:511847RendererPreferences TabContents::GetRendererPrefs() const {
[email protected]840b1512009-07-21 16:53:461848 return renderer_preferences_;
[email protected]80d96fa2009-06-10 22:34:511849}
1850
[email protected]57c6a652009-05-04 07:58:341851TabContents* TabContents::GetAsTabContents() {
1852 return this;
1853}
1854
[email protected]3e7cf1362009-08-21 17:47:081855void TabContents::AddBlockedNotice(const GURL& url, const string16& reason) {
1856 CreateBlockedPopupContainerIfNecessary();
1857 blocked_popups_->AddBlockedNotice(url, reason);
1858}
1859
[email protected]7b291f92009-08-14 05:43:531860ViewType::Type TabContents::GetRenderViewType() const {
1861 return ViewType::TAB_CONTENTS;
1862}
1863
1864int TabContents::GetBrowserWindowID() const {
1865 return controller().window_id().id();
1866}
1867
[email protected]420ae012009-04-24 05:16:321868void TabContents::RenderViewCreated(RenderViewHost* render_view_host) {
[email protected]2e4633c2009-07-09 16:58:061869 NotificationService::current()->Notify(
1870 NotificationType::RENDER_VIEW_HOST_CREATED_FOR_TAB,
1871 Source<TabContents>(this),
1872 Details<RenderViewHost>(render_view_host));
[email protected]420ae012009-04-24 05:16:321873 NavigationEntry* entry = controller_.GetActiveEntry();
1874 if (!entry)
1875 return;
1876
1877 // When we're creating views, we're still doing initial setup, so we always
1878 // use the pending DOM UI rather than any possibly existing committed one.
1879 if (render_manager_.pending_dom_ui()) {
1880 render_manager_.pending_dom_ui()->RenderViewCreated(render_view_host);
1881 }
1882
1883 if (entry->IsViewSourceMode()) {
1884 // Put the renderer in view source mode.
1885 render_view_host->Send(
1886 new ViewMsg_EnableViewSourceMode(render_view_host->routing_id()));
1887 }
[email protected]0666aef2009-05-13 19:48:081888
1889 view()->RenderViewCreated(render_view_host);
[email protected]420ae012009-04-24 05:16:321890}
1891
1892void TabContents::RenderViewReady(RenderViewHost* rvh) {
1893 if (rvh != render_view_host()) {
1894 // Don't notify the world, since this came from a renderer in the
1895 // background.
1896 return;
1897 }
1898
1899 NotifyConnected();
[email protected]9d8a4642009-07-29 17:25:301900 bool was_crashed = is_crashed();
[email protected]420ae012009-04-24 05:16:321901 SetIsCrashed(false);
[email protected]9d8a4642009-07-29 17:25:301902
1903 // Restore the focus to the tab (otherwise the focus will be on the top
1904 // window).
1905 if (was_crashed && !FocusLocationBarByDefault())
1906 Focus();
[email protected]420ae012009-04-24 05:16:321907}
1908
1909void TabContents::RenderViewGone(RenderViewHost* rvh) {
1910 // Ask the print preview if this renderer was valuable.
1911 if (!printing_.OnRenderViewGone(rvh))
1912 return;
1913 if (rvh != render_view_host()) {
1914 // The pending page's RenderViewHost is gone.
1915 return;
1916 }
1917
1918 SetIsLoading(false, NULL);
1919 NotifyDisconnected();
1920 SetIsCrashed(true);
1921
[email protected]8e537302009-06-10 21:19:581922 // Tell the view that we've crashed so it can prepare the sad tab page.
1923 view_->OnTabCrashed();
[email protected]420ae012009-04-24 05:16:321924
1925 // Hide any visible hung renderer warning for this web contents' process.
[email protected]d82ed61e2009-06-16 02:46:221926 HungRendererDialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:321927}
1928
[email protected]2e4633c2009-07-09 16:58:061929void TabContents::RenderViewDeleted(RenderViewHost* rvh) {
1930 NotificationService::current()->Notify(
1931 NotificationType::RENDER_VIEW_HOST_DELETED,
1932 Source<TabContents>(this),
1933 Details<RenderViewHost>(rvh));
1934}
1935
[email protected]420ae012009-04-24 05:16:321936void TabContents::DidNavigate(RenderViewHost* rvh,
1937 const ViewHostMsg_FrameNavigate_Params& params) {
1938 if (PageTransition::IsMainFrame(params.transition))
1939 render_manager_.DidNavigateMainFrame(rvh);
1940
1941 // Update the site of the SiteInstance if it doesn't have one yet.
1942 if (!GetSiteInstance()->has_site())
1943 GetSiteInstance()->SetSite(params.url);
1944
1945 // Need to update MIME type here because it's referred to in
1946 // UpdateNavigationCommands() called by RendererDidNavigate() to
1947 // determine whether or not to enable the encoding menu.
1948 // It's updated only for the main frame. For a subframe,
1949 // RenderView::UpdateURL does not set params.contents_mime_type.
1950 // (see http://code.google.com/p/chromium/issues/detail?id=2929 )
1951 // TODO(jungshik): Add a test for the encoding menu to avoid
1952 // regressing it again.
1953 if (PageTransition::IsMainFrame(params.transition))
1954 contents_mime_type_ = params.contents_mime_type;
1955
1956 NavigationController::LoadCommittedDetails details;
[email protected]3e90d4a2009-07-03 17:38:391957 bool did_navigate = controller_.RendererDidNavigate(params, &details);
1958
1959 // Update history. Note that this needs to happen after the entry is complete,
1960 // which WillNavigate[Main,Sub]Frame will do before this function is called.
1961 if (params.should_update_history) {
1962 // Most of the time, the displayURL matches the loaded URL, but for about:
1963 // URLs, we use a data: URL as the real value. We actually want to save
1964 // the about: URL to the history db and keep the data: URL hidden. This is
1965 // what the TabContents' URL getter does.
1966 UpdateHistoryForNavigation(GetURL(), details, params);
1967 }
1968
[email protected]76543b92009-08-31 17:27:451969 if (!did_navigate)
1970 return; // No navigation happened.
[email protected]420ae012009-04-24 05:16:321971
1972 // DO NOT ADD MORE STUFF TO THIS FUNCTION! Your component should either listen
1973 // for the appropriate notification (best) or you can add it to
1974 // DidNavigateMainFramePostCommit / DidNavigateAnyFramePostCommit (only if
1975 // necessary, please).
1976
1977 // Run post-commit tasks.
1978 if (details.is_main_frame)
1979 DidNavigateMainFramePostCommit(details, params);
1980 DidNavigateAnyFramePostCommit(rvh, details, params);
1981}
1982
1983void TabContents::UpdateState(RenderViewHost* rvh,
1984 int32 page_id,
1985 const std::string& state) {
1986 DCHECK(rvh == render_view_host());
1987
1988 // We must be prepared to handle state updates for any page, these occur
1989 // when the user is scrolling and entering form data, as well as when we're
1990 // leaving a page, in which case our state may have already been moved to
1991 // the next page. The navigation controller will look up the appropriate
1992 // NavigationEntry and update it when it is notified via the delegate.
1993
1994 int entry_index = controller_.GetEntryIndexWithPageID(
1995 GetSiteInstance(), page_id);
1996 if (entry_index < 0)
1997 return;
1998 NavigationEntry* entry = controller_.GetEntryAtIndex(entry_index);
1999
2000 if (state == entry->content_state())
2001 return; // Nothing to update.
2002 entry->set_content_state(state);
2003 controller_.NotifyEntryChanged(entry, entry_index);
2004}
2005
2006void TabContents::UpdateTitle(RenderViewHost* rvh,
2007 int32 page_id, const std::wstring& title) {
2008 // If we have a title, that's a pretty good indication that we've started
2009 // getting useful data.
2010 SetNotWaitingForResponse();
2011
2012 DCHECK(rvh == render_view_host());
2013 NavigationEntry* entry = controller_.GetEntryWithPageID(GetSiteInstance(),
2014 page_id);
2015 if (!entry || !UpdateTitleForEntry(entry, title))
2016 return;
2017
2018 // Broadcast notifications when the UI should be updated.
2019 if (entry == controller_.GetEntryAtOffset(0))
[email protected]c9cd2222009-05-06 05:16:502020 NotifyNavigationStateChanged(INVALIDATE_TAB);
[email protected]420ae012009-04-24 05:16:322021}
2022
[email protected]420ae012009-04-24 05:16:322023void TabContents::UpdateEncoding(RenderViewHost* render_view_host,
[email protected]41fc0322009-09-04 22:23:402024 const std::string& encoding) {
[email protected]420ae012009-04-24 05:16:322025 set_encoding(encoding);
2026}
2027
2028void TabContents::UpdateTargetURL(int32 page_id, const GURL& url) {
2029 if (delegate())
2030 delegate()->UpdateTargetURL(this, url);
2031}
2032
2033void TabContents::UpdateThumbnail(const GURL& url,
2034 const SkBitmap& bitmap,
2035 const ThumbnailScore& score) {
2036 // Tell History about this thumbnail
[email protected]449478302009-06-09 20:04:282037 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kThumbnailStore)) {
[email protected]72d0fc72009-07-09 17:30:492038 if (!profile()->IsOffTheRecord())
[email protected]42339402009-07-17 17:27:192039 profile()->GetThumbnailStore()->SetPageThumbnail(url, bitmap,
2040 score, true);
[email protected]449478302009-06-09 20:04:282041 } else {
2042 HistoryService* hs;
2043 if (!profile()->IsOffTheRecord() &&
2044 (hs = profile()->GetHistoryService(Profile::IMPLICIT_ACCESS))) {
2045 hs->SetPageThumbnail(url, bitmap, score);
2046 }
[email protected]420ae012009-04-24 05:16:322047 }
2048}
2049
[email protected]0df30122009-06-03 12:13:082050void TabContents::UpdateInspectorSettings(const std::wstring& raw_settings) {
2051 profile()->GetPrefs()->SetString(prefs::kWebKitInspectorSettings,
2052 raw_settings);
2053}
2054
[email protected]420ae012009-04-24 05:16:322055void TabContents::Close(RenderViewHost* rvh) {
[email protected]24a4d1062009-07-10 23:10:422056 // If we close the tab while we're in the middle of a drag, we'll crash.
2057 // Instead, cancel the drag and close it as soon as the drag ends.
2058 if (view()->IsDoingDrag()) {
2059 view()->CancelDragAndCloseTab();
2060 return;
2061 }
2062
[email protected]420ae012009-04-24 05:16:322063 // Ignore this if it comes from a RenderViewHost that we aren't showing.
2064 if (delegate() && rvh == render_view_host())
2065 delegate()->CloseContents(this);
2066}
2067
2068void TabContents::RequestMove(const gfx::Rect& new_bounds) {
2069 if (delegate() && delegate()->IsPopup(this))
2070 delegate()->MoveContents(this, new_bounds);
2071}
2072
[email protected]329581b2009-04-28 06:52:352073void TabContents::DidStartLoading(RenderViewHost* rvh) {
[email protected]420ae012009-04-24 05:16:322074 SetIsLoading(true, NULL);
2075}
2076
[email protected]329581b2009-04-28 06:52:352077void TabContents::DidStopLoading(RenderViewHost* rvh) {
[email protected]420ae012009-04-24 05:16:322078 scoped_ptr<LoadNotificationDetails> details;
2079
2080 NavigationEntry* entry = controller_.GetActiveEntry();
2081 // An entry may not exist for a stop when loading an initial blank page or
2082 // if an iframe injected by script into a blank page finishes loading.
2083 if (entry) {
2084 scoped_ptr<base::ProcessMetrics> metrics(
2085 base::ProcessMetrics::CreateProcessMetrics(
2086 process()->process().handle()));
2087
2088 base::TimeDelta elapsed = base::TimeTicks::Now() - current_load_start_;
2089
2090 details.reset(new LoadNotificationDetails(
[email protected]ebe89e062009-08-13 23:16:542091 entry->virtual_url(),
[email protected]420ae012009-04-24 05:16:322092 entry->transition_type(),
2093 elapsed,
2094 &controller_,
2095 controller_.GetCurrentEntryIndex()));
2096 }
2097
2098 // Tell PasswordManager we've finished a page load, which serves as a
2099 // green light to save pending passwords and reset itself.
2100 GetPasswordManager()->DidStopLoading();
2101
2102 SetIsLoading(false, details.get());
2103}
2104
[email protected]420ae012009-04-24 05:16:322105void TabContents::DidRedirectProvisionalLoad(int32 page_id,
2106 const GURL& source_url,
2107 const GURL& target_url) {
2108 NavigationEntry* entry;
2109 if (page_id == -1)
2110 entry = controller_.pending_entry();
2111 else
2112 entry = controller_.GetEntryWithPageID(GetSiteInstance(), page_id);
2113 if (!entry || entry->url() != source_url)
2114 return;
2115 entry->set_url(target_url);
2116}
2117
[email protected]420ae012009-04-24 05:16:322118void TabContents::RequestOpenURL(const GURL& url, const GURL& referrer,
2119 WindowOpenDisposition disposition) {
2120 if (render_manager_.dom_ui()) {
2121 // When we're a DOM UI, it will provide a page transition type for us (this
2122 // is so the new tab page can specify AUTO_BOOKMARK for automatically
2123 // generated suggestions).
2124 //
2125 // Note also that we hide the referrer for DOM UI pages. We don't really
[email protected]60e448982009-05-06 04:21:162126 // want web sites to see a referrer of "chrome://blah" (and some
2127 // chrome: URLs might have search terms or other stuff we don't want to
[email protected]420ae012009-04-24 05:16:322128 // send to the site), so we send no referrer.
2129 OpenURL(url, GURL(), disposition,
2130 render_manager_.dom_ui()->link_transition_type());
2131 } else {
2132 OpenURL(url, referrer, disposition, PageTransition::LINK);
2133 }
2134}
2135
[email protected]1ee614862009-07-06 20:29:182136void TabContents::DomOperationResponse(const std::string& json_string,
2137 int automation_id) {
2138}
2139
[email protected]420ae012009-04-24 05:16:322140void TabContents::ProcessDOMUIMessage(const std::string& message,
[email protected]811bfe32009-07-01 08:46:252141 const std::string& content,
2142 int request_id,
2143 bool has_callback) {
[email protected]420ae012009-04-24 05:16:322144 if (!render_manager_.dom_ui()) {
[email protected]c2e74fe82009-09-03 17:57:442145 // This can happen if someone uses window.open() to open an extension URL
2146 // from a non-extension context.
2147 render_view_host()->BlockExtensionRequest(request_id);
[email protected]420ae012009-04-24 05:16:322148 return;
2149 }
[email protected]811bfe32009-07-01 08:46:252150 render_manager_.dom_ui()->ProcessDOMUIMessage(message, content, request_id,
2151 has_callback);
[email protected]420ae012009-04-24 05:16:322152}
2153
2154void TabContents::ProcessExternalHostMessage(const std::string& message,
2155 const std::string& origin,
2156 const std::string& target) {
2157 if (delegate())
2158 delegate()->ForwardMessageToExternalHost(message, origin, target);
2159}
2160
[email protected]420ae012009-04-24 05:16:322161void TabContents::RunFileChooser(bool multiple_files,
2162 const string16& title,
2163 const FilePath& default_file) {
2164 if (!select_file_dialog_.get())
2165 select_file_dialog_ = SelectFileDialog::Create(this);
2166 SelectFileDialog::Type dialog_type =
2167 multiple_files ? SelectFileDialog::SELECT_OPEN_MULTI_FILE :
2168 SelectFileDialog::SELECT_OPEN_FILE;
2169 select_file_dialog_->SelectFile(dialog_type, title, default_file,
2170 NULL, 0, FILE_PATH_LITERAL(""),
2171 view_->GetTopLevelNativeWindow(), NULL);
2172}
2173
2174void TabContents::RunJavaScriptMessage(
2175 const std::wstring& message,
2176 const std::wstring& default_prompt,
2177 const GURL& frame_url,
2178 const int flags,
2179 IPC::Message* reply_msg,
2180 bool* did_suppress_message) {
2181 // Suppress javascript messages when requested and when inside a constrained
2182 // popup window (because that activates them and breaks them out of the
2183 // constrained window jail).
[email protected]91ed49a2009-07-07 21:29:552184 // Also suppress messages when showing an interstitial. The interstitial is
2185 // shown over the previous page, we don't want the hidden page dialogs to
2186 // interfere with the interstitial.
2187 bool suppress_this_message = suppress_javascript_messages_ ||
2188 showing_interstitial_page();
[email protected]420ae012009-04-24 05:16:322189 if (delegate())
2190 suppress_this_message |=
[email protected]b6c874582009-05-08 19:38:312191 (delegate()->GetConstrainingContents(this) != this);
[email protected]420ae012009-04-24 05:16:322192
2193 *did_suppress_message = suppress_this_message;
2194
2195 if (!suppress_this_message) {
2196 base::TimeDelta time_since_last_message(
2197 base::TimeTicks::Now() - last_javascript_message_dismissal_);
2198 bool show_suppress_checkbox = false;
2199 // Show a checkbox offering to suppress further messages if this message is
2200 // being displayed within kJavascriptMessageExpectedDelay of the last one.
2201 if (time_since_last_message <
2202 base::TimeDelta::FromMilliseconds(kJavascriptMessageExpectedDelay))
2203 show_suppress_checkbox = true;
2204
[email protected]d82ed61e2009-06-16 02:46:222205 RunJavascriptMessageBox(this, frame_url, flags, message, default_prompt,
[email protected]420ae012009-04-24 05:16:322206 show_suppress_checkbox, reply_msg);
2207 } else {
2208 // If we are suppressing messages, just reply as is if the user immediately
2209 // pressed "Cancel".
2210 OnJavaScriptMessageBoxClosed(reply_msg, false, std::wstring());
2211 }
2212}
2213
2214void TabContents::RunBeforeUnloadConfirm(const std::wstring& message,
2215 IPC::Message* reply_msg) {
[email protected]d82ed61e2009-06-16 02:46:222216 RunBeforeUnloadDialog(this, message, reply_msg);
[email protected]420ae012009-04-24 05:16:322217}
2218
2219void TabContents::ShowModalHTMLDialog(const GURL& url, int width, int height,
2220 const std::string& json_arguments,
2221 IPC::Message* reply_msg) {
2222 if (delegate()) {
2223 HtmlDialogUIDelegate* dialog_delegate =
2224 new ModalHtmlDialogDelegate(url, width, height, json_arguments,
[email protected]d82ed61e2009-06-16 02:46:222225 reply_msg, this);
[email protected]420ae012009-04-24 05:16:322226 delegate()->ShowHtmlDialog(dialog_delegate, NULL);
2227 }
2228}
2229
2230void TabContents::PasswordFormsSeen(
[email protected]ed3fb032009-06-16 19:50:562231 const std::vector<webkit_glue::PasswordForm>& forms) {
[email protected]420ae012009-04-24 05:16:322232 GetPasswordManager()->PasswordFormsSeen(forms);
2233}
2234
[email protected]420ae012009-04-24 05:16:322235// Checks to see if we should generate a keyword based on the OSDD, and if
2236// necessary uses TemplateURLFetcher to download the OSDD and create a keyword.
2237void TabContents::PageHasOSDD(RenderViewHost* render_view_host,
2238 int32 page_id, const GURL& url,
2239 bool autodetected) {
2240 // Make sure page_id is the current page, and the TemplateURLModel is loaded.
2241 DCHECK(url.is_valid());
2242 if (!IsActiveEntry(page_id))
2243 return;
2244 TemplateURLModel* url_model = profile()->GetTemplateURLModel();
2245 if (!url_model)
2246 return;
2247 if (!url_model->loaded()) {
2248 url_model->Load();
2249 return;
2250 }
2251 if (!profile()->GetTemplateURLFetcher())
2252 return;
2253
2254 if (profile()->IsOffTheRecord())
2255 return;
2256
2257 const NavigationEntry* entry = controller_.GetLastCommittedEntry();
2258 DCHECK(entry);
2259
2260 const NavigationEntry* base_entry = entry;
2261 if (IsFormSubmit(base_entry)) {
2262 // If the current page is a form submit, find the last page that was not
2263 // a form submit and use its url to generate the keyword from.
2264 int index = controller_.last_committed_entry_index() - 1;
2265 while (index >= 0 && IsFormSubmit(controller_.GetEntryAtIndex(index)))
2266 index--;
2267 if (index >= 0)
2268 base_entry = controller_.GetEntryAtIndex(index);
2269 else
2270 base_entry = NULL;
2271 }
2272
2273 // We want to use the user typed URL if available since that represents what
2274 // the user typed to get here, and fall back on the regular URL if not.
2275 if (!base_entry)
2276 return;
2277 GURL keyword_url = base_entry->user_typed_url().is_valid() ?
2278 base_entry->user_typed_url() : base_entry->url();
2279 if (!keyword_url.is_valid())
2280 return;
2281 std::wstring keyword = TemplateURLModel::GenerateKeyword(keyword_url,
2282 autodetected);
2283 if (keyword.empty())
2284 return;
2285 const TemplateURL* template_url =
2286 url_model->GetTemplateURLForKeyword(keyword);
2287 if (template_url && (!template_url->safe_for_autoreplace() ||
2288 template_url->originating_url() == url)) {
2289 // Either there is a user created TemplateURL for this keyword, or the
2290 // keyword has the same OSDD url and we've parsed it.
2291 return;
2292 }
2293
2294 // Download the OpenSearch description document. If this is successful a
2295 // new keyword will be created when done.
[email protected]420ae012009-04-24 05:16:322296 profile()->GetTemplateURLFetcher()->ScheduleDownload(
2297 keyword,
2298 url,
2299 base_entry->favicon().url(),
[email protected]d0cc9fb52009-06-23 02:26:162300 this,
[email protected]420ae012009-04-24 05:16:322301 autodetected);
2302}
2303
[email protected]420ae012009-04-24 05:16:322304GURL TabContents::GetAlternateErrorPageURL() const {
2305 GURL url;
2306 // Disable alternate error pages when in OffTheRecord/Incognito mode.
2307 if (profile()->IsOffTheRecord())
2308 return url;
2309
2310 PrefService* prefs = profile()->GetPrefs();
2311 DCHECK(prefs);
2312 if (prefs->GetBoolean(prefs::kAlternateErrorPagesEnabled)) {
[email protected]2bd6ca1c2009-08-27 16:42:202313 url = google_util::AppendGoogleLocaleParam(GURL(kLinkDoctorBaseURL));
[email protected]420ae012009-04-24 05:16:322314 url = google_util::AppendGoogleTLDParam(url);
2315 }
2316 return url;
2317}
2318
2319WebPreferences TabContents::GetWebkitPrefs() {
2320 PrefService* prefs = render_view_host()->process()->profile()->GetPrefs();
[email protected]7ccddb8c2009-08-04 17:36:552321 bool is_dom_ui = false;
2322 return RenderViewHostDelegateHelper::GetWebkitPrefs(prefs, is_dom_ui);
[email protected]420ae012009-04-24 05:16:322323}
2324
[email protected]420ae012009-04-24 05:16:322325void TabContents::OnJSOutOfMemory() {
2326 AddInfoBar(new SimpleAlertInfoBarDelegate(
2327 this, l10n_util::GetString(IDS_JS_OUT_OF_MEMORY_PROMPT), NULL));
2328}
2329
[email protected]420ae012009-04-24 05:16:322330void TabContents::OnCrossSiteResponse(int new_render_process_host_id,
2331 int new_request_id) {
[email protected]57c6a652009-05-04 07:58:342332 // Allows the TabContents to react when a cross-site response is ready to be
[email protected]420ae012009-04-24 05:16:322333 // delivered to a pending RenderViewHost. We must first run the onunload
2334 // handler of the old RenderViewHost before we can allow it to proceed.
2335 render_manager_.OnCrossSiteResponse(new_render_process_host_id,
2336 new_request_id);
2337}
2338
2339bool TabContents::CanBlur() const {
2340 return delegate() ? delegate()->CanBlur() : true;
2341}
2342
2343gfx::Rect TabContents::GetRootWindowResizerRect() const {
2344 if (delegate())
2345 return delegate()->GetRootWindowResizerRect();
2346 return gfx::Rect();
2347}
2348
2349void TabContents::RendererUnresponsive(RenderViewHost* rvh,
2350 bool is_during_unload) {
2351 if (is_during_unload) {
2352 // Hang occurred while firing the beforeunload/unload handler.
2353 // Pretend the handler fired so tab closing continues as if it had.
[email protected]829e7612009-04-25 01:15:112354 rvh->set_sudden_termination_allowed(true);
[email protected]420ae012009-04-24 05:16:322355
2356 if (!render_manager_.ShouldCloseTabOnUnresponsiveRenderer())
2357 return;
2358
2359 // If the tab hangs in the beforeunload/unload handler there's really
2360 // nothing we can do to recover. Pretend the unload listeners have
2361 // all fired and close the tab. If the hang is in the beforeunload handler
2362 // then the user will not have the option of cancelling the close.
2363 Close(rvh);
2364 return;
2365 }
2366
2367 if (render_view_host() && render_view_host()->IsRenderViewLive())
[email protected]d82ed61e2009-06-16 02:46:222368 HungRendererDialog::ShowForTabContents(this);
[email protected]420ae012009-04-24 05:16:322369}
2370
2371void TabContents::RendererResponsive(RenderViewHost* render_view_host) {
[email protected]d82ed61e2009-06-16 02:46:222372 HungRendererDialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:322373}
2374
2375void TabContents::LoadStateChanged(const GURL& url,
2376 net::LoadState load_state) {
2377 load_state_ = load_state;
[email protected]d686e812009-06-03 19:10:292378 std::wstring languages =
2379 profile()->GetPrefs()->GetString(prefs::kAcceptLanguages);
2380 load_state_host_.clear();
2381 std::string host = url.host();
2382 net::IDNToUnicode(host.c_str(), host.size(), languages, &load_state_host_);
[email protected]420ae012009-04-24 05:16:322383 if (load_state_ == net::LOAD_STATE_READING_RESPONSE)
2384 SetNotWaitingForResponse();
2385 if (is_loading())
[email protected]c9cd2222009-05-06 05:16:502386 NotifyNavigationStateChanged(INVALIDATE_LOAD | INVALIDATE_TAB);
[email protected]420ae012009-04-24 05:16:322387}
2388
[email protected]1ee614862009-07-06 20:29:182389bool TabContents::IsExternalTabContainer() const {
2390 if (!delegate())
2391 return false;
2392
2393 return delegate()->IsExternalTabContainer();
2394}
2395
[email protected]1ee614862009-07-06 20:29:182396void TabContents::DidInsertCSS() {
2397 // This RVHDelegate function is used for extensions and not us.
[email protected]420ae012009-04-24 05:16:322398}
2399
2400void TabContents::FileSelected(const FilePath& path,
2401 int index, void* params) {
2402 render_view_host()->FileSelected(path);
2403}
2404
2405void TabContents::MultiFilesSelected(const std::vector<FilePath>& files,
2406 void* params) {
2407 render_view_host()->MultiFilesSelected(files);
2408}
2409
2410void TabContents::FileSelectionCanceled(void* params) {
2411 // If the user cancels choosing a file to upload we pass back an
2412 // empty vector.
2413 render_view_host()->MultiFilesSelected(std::vector<FilePath>());
2414}
2415
2416void TabContents::BeforeUnloadFiredFromRenderManager(
2417 bool proceed,
2418 bool* proceed_to_fire_unload) {
2419 if (delegate())
2420 delegate()->BeforeUnloadFired(this, proceed, proceed_to_fire_unload);
2421}
2422
2423void TabContents::UpdateRenderViewSizeForRenderManager() {
2424 // TODO(brettw) this is a hack. See TabContentsView::SizeContents.
2425 view_->SizeContents(view_->GetContainerSize());
2426}
2427
2428DOMUI* TabContents::CreateDOMUIForRenderManager(const GURL& url) {
[email protected]d82ed61e2009-06-16 02:46:222429 return DOMUIFactory::CreateDOMUIForURL(this, url);
[email protected]420ae012009-04-24 05:16:322430}
2431
2432NavigationEntry*
2433TabContents::GetLastCommittedNavigationEntryForRenderManager() {
2434 return controller_.GetLastCommittedEntry();
2435}
2436
2437bool TabContents::CreateRenderViewForRenderManager(
2438 RenderViewHost* render_view_host) {
[email protected]811bfe32009-07-01 08:46:252439 // If the pending navigation is to a DOMUI, tell the RenderView about any
2440 // bindings it will need enabled.
[email protected]420ae012009-04-24 05:16:322441 if (render_manager_.pending_dom_ui())
[email protected]811bfe32009-07-01 08:46:252442 render_view_host->AllowBindings(
2443 render_manager_.pending_dom_ui()->bindings());
[email protected]1adff062009-06-02 18:39:552444
[email protected]420ae012009-04-24 05:16:322445 RenderWidgetHostView* rwh_view = view_->CreateViewForWidget(render_view_host);
2446 if (!render_view_host->CreateRenderView())
2447 return false;
2448
2449 // Now that the RenderView has been created, we need to tell it its size.
2450 rwh_view->SetSize(view_->GetContainerSize());
2451
2452 UpdateMaxPageIDIfNecessary(render_view_host->site_instance(),
2453 render_view_host);
2454 return true;
2455}
2456
2457void TabContents::Observe(NotificationType type,
2458 const NotificationSource& source,
2459 const NotificationDetails& details) {
2460 switch (type.value) {
2461 case NotificationType::BOOKMARK_MODEL_LOADED:
2462 // BookmarkModel finished loading, fall through to update starred state.
2463 case NotificationType::URLS_STARRED: {
2464 // Somewhere, a URL has been starred.
2465 // Ignore notifications for profiles other than our current one.
2466 Profile* source_profile = Source<Profile>(source).ptr();
2467 if (!source_profile || !source_profile->IsSameProfile(profile()))
2468 return;
2469
2470 UpdateStarredStateForCurrentURL();
2471 break;
2472 }
2473 case NotificationType::PREF_CHANGED: {
2474 std::wstring* pref_name_in = Details<std::wstring>(details).ptr();
2475 DCHECK(Source<PrefService>(source).ptr() == profile()->GetPrefs());
2476 if (*pref_name_in == prefs::kAlternateErrorPagesEnabled) {
2477 UpdateAlternateErrorPageURL();
2478 } else if (*pref_name_in == prefs::kDefaultCharset ||
2479 StartsWithASCII(WideToUTF8(*pref_name_in), "webkit.webprefs.", true)
2480 ) {
2481 UpdateWebPreferences();
2482 } else {
2483 NOTREACHED() << "unexpected pref change notification" << *pref_name_in;
2484 }
2485 break;
2486 }
2487 case NotificationType::RENDER_WIDGET_HOST_DESTROYED:
2488 view_->RenderWidgetHostDestroyed(Source<RenderWidgetHost>(source).ptr());
2489 break;
2490
2491 case NotificationType::NAV_ENTRY_COMMITTED: {
2492 DCHECK(&controller_ == Source<NavigationController>(source).ptr());
2493
2494 NavigationController::LoadCommittedDetails& committed_details =
2495 *(Details<NavigationController::LoadCommittedDetails>(details).ptr());
2496 ExpireInfoBars(committed_details);
2497 break;
2498 }
2499
[email protected]d3ba77272009-09-03 00:06:092500#if defined(OS_LINUX)
2501 case NotificationType::BROWSER_THEME_CHANGED: {
2502 render_view_host()->SyncRendererPrefs();
2503 break;
2504 }
2505#endif
2506
[email protected]420ae012009-04-24 05:16:322507 default:
2508 NOTREACHED();
2509 }
2510}
[email protected]aacd7cc2009-09-02 14:51:102511
[email protected]41fc0322009-09-04 22:23:402512void TabContents::set_encoding(const std::string& encoding) {
[email protected]aacd7cc2009-09-02 14:51:102513 encoding_ = CharacterEncoding::GetCanonicalEncodingNameByAliasName(encoding);
2514}
2515