blob: 0e1673a2e6f5d5ca71121a50c655ad53a01339e1 [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
730void TabContents::Paste() {
731 render_view_host()->Paste();
732}
733
734void TabContents::DisassociateFromPopupCount() {
735 render_view_host()->DisassociateFromPopupCount();
736}
737
738TabContents* TabContents::Clone() {
739 // We create a new SiteInstance so that the new tab won't share processes
740 // with the old one. This can be changed in the future if we need it to share
741 // processes for some reason.
[email protected]420ae012009-04-24 05:16:32742 TabContents* tc = new TabContents(profile(),
[email protected]96d185d2009-04-24 03:28:54743 SiteInstance::CreateSiteInstance(profile()),
744 MSG_ROUTING_NONE, NULL);
745 tc->controller().CopyStateFrom(controller_);
746 return tc;
747}
748
[email protected]420ae012009-04-24 05:16:32749void TabContents::CreateShortcut() {
750 NavigationEntry* entry = controller_.GetLastCommittedEntry();
751 if (!entry)
752 return;
753
[email protected]70b8d6b2009-08-20 16:56:24754#if defined(OS_LINUX) && !defined(TOOLKIT_VIEWS)
[email protected]0ba5c5e2009-08-20 16:47:13755 CreateApplicationShortcutsDialogGtk::Show(view()->GetTopLevelNativeWindow(),
756 GetURL(), GetTitle());
[email protected]b96aa932009-08-12 21:34:49757#else
[email protected]420ae012009-04-24 05:16:32758 // We only allow one pending install request. By resetting the page id we
759 // effectively cancel the pending install request.
760 pending_install_.page_id = entry->page_id();
761 pending_install_.icon = GetFavIcon();
762 pending_install_.title = UTF16ToWideHack(GetTitle());
763 pending_install_.url = GetURL();
764 if (pending_install_.callback_functor) {
765 pending_install_.callback_functor->Cancel();
766 pending_install_.callback_functor = NULL;
767 }
768 DCHECK(!pending_install_.icon.isNull()) << "Menu item should be disabled.";
769 if (pending_install_.title.empty())
770 pending_install_.title = UTF8ToWide(GetURL().spec());
771
772 // Request the application info. When done OnDidGetApplicationInfo is invoked
773 // and we'll create the shortcut.
774 render_view_host()->GetApplicationInfo(pending_install_.page_id);
[email protected]b96aa932009-08-12 21:34:49775#endif
[email protected]420ae012009-04-24 05:16:32776}
777
[email protected]4d677202009-07-19 07:37:12778void TabContents::ShowPageInfo(const GURL& url,
779 const NavigationEntry::SSLStatus& ssl,
780 bool show_history) {
781 if (!delegate_)
782 return;
783
[email protected]bb678332009-07-21 00:15:50784 delegate_->ShowPageInfo(profile(), url, ssl, show_history);
[email protected]4d677202009-07-19 07:37:12785}
786
initial.commit09911bf2008-07-26 23:55:29787ConstrainedWindow* TabContents::CreateConstrainedDialog(
[email protected]e8382172009-06-19 22:16:28788 ConstrainedWindowDelegate* delegate) {
initial.commit09911bf2008-07-26 23:55:29789 ConstrainedWindow* window =
[email protected]e8382172009-06-19 22:16:28790 ConstrainedWindow::CreateConstrainedDialog(this, delegate);
initial.commit09911bf2008-07-26 23:55:29791 child_windows_.push_back(window);
792 return window;
793}
794
795void TabContents::AddNewContents(TabContents* new_contents,
796 WindowOpenDisposition disposition,
797 const gfx::Rect& initial_pos,
[email protected]c88a70fe2009-05-05 20:00:22798 bool user_gesture,
799 const GURL& creator_url) {
initial.commit09911bf2008-07-26 23:55:29800 if (!delegate_)
801 return;
802
[email protected]35f7d212009-04-29 21:19:27803 if ((disposition == NEW_POPUP) && !user_gesture &&
804 !CommandLine::ForCurrentProcess()->HasSwitch(
805 switches::kDisablePopupBlocking)) {
[email protected]2c4410d2009-05-06 23:46:22806 // Unrequested popups from normal pages are constrained unless they're in
[email protected]b6c874582009-05-08 19:38:31807 // the whitelist. The popup owner will handle checking this.
808 delegate_->GetConstrainingContents(this)->AddPopup(new_contents,
809 initial_pos,
810 creator_url.is_valid() ? creator_url.host() : std::string());
811 } else {
[email protected]304f8792009-08-19 21:28:09812#if defined(OS_CHROMEOS)
813 if (disposition == NEW_POPUP) {
814 // If the popup is bigger than a given factor of the screen, then
815 // turn it into a foreground tab (on chrome os only)
816 GdkScreen* screen = gdk_screen_get_default();
817 int max_width = gdk_screen_get_width(screen) * kMaxWidthFactor;
818 int max_height = gdk_screen_get_height(screen) * kMaxHeightFactor;
819 if (initial_pos.width() > max_width ||
820 initial_pos.height() > max_height) {
821 disposition = NEW_FOREGROUND_TAB;
822 }
823 }
824#endif
825
[email protected]0aa55312008-10-17 21:53:08826 new_contents->DisassociateFromPopupCount();
initial.commit09911bf2008-07-26 23:55:29827 delegate_->AddNewContents(this, new_contents, disposition, initial_pos,
828 user_gesture);
829 }
[email protected]b6c874582009-05-08 19:38:31830 PopupNotificationVisibilityChanged(ShowingBlockedPopupNotification());
initial.commit09911bf2008-07-26 23:55:29831}
832
initial.commit09911bf2008-07-26 23:55:29833void TabContents::CloseAllSuppressedPopups() {
[email protected]d6598c052008-11-05 19:03:25834 if (blocked_popups_)
[email protected]2c4410d2009-05-06 23:46:22835 blocked_popups_->CloseAll();
initial.commit09911bf2008-07-26 23:55:29836}
837
[email protected]96d185d2009-04-24 03:28:54838void TabContents::PopupNotificationVisibilityChanged(bool visible) {
839 render_view_host()->PopupNotificationVisibilityChanged(visible);
840}
841
[email protected]5c9e97a2009-09-09 23:48:30842gfx::NativeView TabContents::GetContentNativeView() const {
[email protected]96d185d2009-04-24 03:28:54843 return view_->GetContentNativeView();
844}
845
846gfx::NativeView TabContents::GetNativeView() const {
847 return view_->GetNativeView();
848}
849
850void TabContents::GetContainerBounds(gfx::Rect *out) const {
851 view_->GetContainerBounds(out);
852}
853
854void TabContents::Focus() {
855 view_->Focus();
856}
857
[email protected]90daadb42009-06-08 21:27:28858void TabContents::FocusThroughTabTraversal(bool reverse) {
[email protected]7e383692009-06-12 19:14:54859 if (showing_interstitial_page()) {
860 render_manager_.interstitial_page()->FocusThroughTabTraversal(reverse);
861 return;
862 }
[email protected]96d185d2009-04-24 03:28:54863 render_view_host()->SetInitialFocus(reverse);
864}
865
866bool TabContents::FocusLocationBarByDefault() {
867 DOMUI* dom_ui = GetDOMUIForCurrentState();
868 if (dom_ui)
869 return dom_ui->focus_location_bar_by_default();
[email protected]3e3f0eb2009-06-22 18:33:43870 NavigationEntry* entry = controller_.GetActiveEntry();
871 if (entry && entry->url() == GURL("about:blank"))
872 return true;
[email protected]96d185d2009-04-24 03:28:54873 return false;
874}
875
[email protected]616ed5a2008-11-21 22:27:24876void TabContents::AddInfoBar(InfoBarDelegate* delegate) {
877 // Look through the existing InfoBarDelegates we have for a match. If we've
878 // already got one that matches, then we don't add the new one.
[email protected]f86a07022008-11-25 01:06:05879 for (int i = 0; i < infobar_delegate_count(); ++i) {
[email protected]616ed5a2008-11-21 22:27:24880 if (GetInfoBarDelegateAt(i)->EqualsDelegate(delegate))
881 return;
882 }
883
884 infobar_delegates_.push_back(delegate);
[email protected]bfd04a62009-02-01 18:16:56885 NotificationService::current()->Notify(
886 NotificationType::TAB_CONTENTS_INFOBAR_ADDED,
887 Source<TabContents>(this),
888 Details<InfoBarDelegate>(delegate));
[email protected]616ed5a2008-11-21 22:27:24889
890 // Add ourselves as an observer for navigations the first time a delegate is
891 // added. We use this notification to expire InfoBars that need to expire on
892 // page transitions.
893 if (infobar_delegates_.size() == 1) {
[email protected]bfd04a62009-02-01 18:16:56894 registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED,
[email protected]ce3fa3c2009-04-20 19:55:57895 Source<NavigationController>(&controller_));
[email protected]616ed5a2008-11-21 22:27:24896 }
897}
898
899void TabContents::RemoveInfoBar(InfoBarDelegate* delegate) {
900 std::vector<InfoBarDelegate*>::iterator it =
901 find(infobar_delegates_.begin(), infobar_delegates_.end(), delegate);
902 if (it != infobar_delegates_.end()) {
903 InfoBarDelegate* delegate = *it;
[email protected]bfd04a62009-02-01 18:16:56904 NotificationService::current()->Notify(
905 NotificationType::TAB_CONTENTS_INFOBAR_REMOVED,
906 Source<TabContents>(this),
907 Details<InfoBarDelegate>(delegate));
[email protected]f86a07022008-11-25 01:06:05908 infobar_delegates_.erase(it);
[email protected]616ed5a2008-11-21 22:27:24909
[email protected]6a02963e2009-01-06 16:58:03910 // Remove ourselves as an observer if we are tracking no more InfoBars.
911 if (infobar_delegates_.empty()) {
[email protected]bfd04a62009-02-01 18:16:56912 registrar_.Remove(this, NotificationType::NAV_ENTRY_COMMITTED,
[email protected]ce3fa3c2009-04-20 19:55:57913 Source<NavigationController>(&controller_));
[email protected]6a02963e2009-01-06 16:58:03914 }
[email protected]616ed5a2008-11-21 22:27:24915 }
916}
[email protected]3edd9522009-03-04 22:19:41917
[email protected]f81821c2009-08-07 01:29:47918void TabContents::ReplaceInfoBar(InfoBarDelegate* old_delegate,
919 InfoBarDelegate* new_delegate) {
920 std::vector<InfoBarDelegate*>::iterator it =
921 find(infobar_delegates_.begin(), infobar_delegates_.end(), old_delegate);
922 DCHECK(it != infobar_delegates_.end());
923
924 // Notify the container about the change of plans.
925 scoped_ptr<std::pair<InfoBarDelegate*, InfoBarDelegate*> > details(
926 new std::pair<InfoBarDelegate*, InfoBarDelegate*>(
927 old_delegate, new_delegate));
928 NotificationService::current()->Notify(
929 NotificationType::TAB_CONTENTS_INFOBAR_REPLACED,
930 Source<TabContents>(this),
931 Details<std::pair<InfoBarDelegate*, InfoBarDelegate*> >(details.get()));
932
933 // Remove the old one.
934 infobar_delegates_.erase(it);
935
936 // Add the new one.
937 infobar_delegates_.push_back(new_delegate);
938}
939
[email protected]96d185d2009-04-24 03:28:54940bool TabContents::IsBookmarkBarAlwaysVisible() {
941 // See GetDOMUIForCurrentState() comment for more info. This case is very
942 // similar, but for non-first loads, we want to use the committed entry. This
943 // is so the bookmarks bar disappears at the same time the page does.
944 if (controller_.GetLastCommittedEntry()) {
945 // Not the first load, always use the committed DOM UI.
946 if (render_manager_.dom_ui())
947 return render_manager_.dom_ui()->force_bookmark_bar_visible();
948 return false; // Default.
949 }
950
951 // When it's the first load, we know either the pending one or the committed
952 // one will have the DOM UI in it (see GetDOMUIForCurrentState), and only one
953 // of them will be valid, so we can just check both.
954 if (render_manager_.pending_dom_ui())
955 return render_manager_.pending_dom_ui()->force_bookmark_bar_visible();
956 if (render_manager_.dom_ui())
957 return render_manager_.dom_ui()->force_bookmark_bar_visible();
958 return false; // Default.
959}
960
[email protected]d8f16ae2009-08-30 03:47:26961bool TabContents::IsExtensionShelfAlwaysVisible() {
962 // See GetDOMUIForCurrentState() comment for more info. This case is very
963 // similar, but for non-first loads, we want to use the committed entry. This
964 // is so the bookmarks bar disappears at the same time the page does.
965 if (controller_.GetLastCommittedEntry()) {
966 // Not the first load, always use the committed DOM UI.
967 if (render_manager_.dom_ui())
968 return render_manager_.dom_ui()->force_extension_shelf_visible();
969 return false; // Default.
970 }
971
972 // When it's the first load, we know either the pending one or the committed
973 // one will have the DOM UI in it (see GetDOMUIForCurrentState), and only one
974 // of them will be valid, so we can just check both.
975 if (render_manager_.pending_dom_ui())
976 return render_manager_.pending_dom_ui()->force_extension_shelf_visible();
977 if (render_manager_.dom_ui())
978 return render_manager_.dom_ui()->force_extension_shelf_visible();
979 return false; // Default.
980}
981
[email protected]3edd9522009-03-04 22:19:41982void TabContents::ToolbarSizeChanged(bool is_animating) {
983 TabContentsDelegate* d = delegate();
984 if (d)
985 d->ToolbarSizeChanged(this, is_animating);
986}
[email protected]616ed5a2008-11-21 22:27:24987
[email protected]d5f942ba2008-09-26 19:30:34988void TabContents::OnStartDownload(DownloadItem* download) {
989 DCHECK(download);
[email protected]d5f942ba2008-09-26 19:30:34990
991 // Download in a constrained popup is shown in the tab that opened it.
[email protected]b6c874582009-05-08 19:38:31992 TabContents* tab_contents = delegate()->GetConstrainingContents(this);
[email protected]d5f942ba2008-09-26 19:30:34993
[email protected]59560e0b2009-06-04 03:30:22994 if (tab_contents && tab_contents->delegate())
995 tab_contents->delegate()->OnStartDownload(download);
[email protected]3edd9522009-03-04 22:19:41996}
[email protected]3edd9522009-03-04 22:19:41997
[email protected]d5f942ba2008-09-26 19:30:34998void TabContents::WillClose(ConstrainedWindow* window) {
999 ConstrainedWindowList::iterator it =
1000 find(child_windows_.begin(), child_windows_.end(), window);
1001 if (it != child_windows_.end())
1002 child_windows_.erase(it);
[email protected]66ba4932009-06-04 19:22:131003}
[email protected]d5f942ba2008-09-26 19:30:341004
[email protected]66ba4932009-06-04 19:22:131005void TabContents::WillCloseBlockedPopupContainer(
1006 BlockedPopupContainer* container) {
1007 DCHECK(blocked_popups_ == container);
1008 blocked_popups_ = NULL;
[email protected]d5f942ba2008-09-26 19:30:341009}
1010
[email protected]d5f942ba2008-09-26 19:30:341011void TabContents::DidMoveOrResize(ConstrainedWindow* window) {
[email protected]332af7732009-03-11 13:21:351012#if defined(OS_WIN)
[email protected]92edc472009-02-10 20:32:061013 UpdateWindow(GetNativeView());
[email protected]332af7732009-03-11 13:21:351014#endif
[email protected]d5f942ba2008-09-26 19:30:341015}
1016
[email protected]e491f1c2009-05-22 20:28:121017void TabContents::StartFinding(string16 find_text,
1018 bool forward_direction,
1019 bool case_sensitive) {
[email protected]6e95706d2009-04-23 22:55:011020 // If find_text is empty, it means FindNext was pressed with a keyboard
1021 // shortcut so unless we have something to search for we return early.
[email protected]e491f1c2009-05-22 20:28:121022 if (find_text.empty() && find_text_.empty()) {
1023 if (last_search_prepopulate_text_->empty())
1024 return;
1025 // Try whatever we searched for last in any tab.
1026 find_text = *last_search_prepopulate_text_;
1027 }
[email protected]6e95706d2009-04-23 22:55:011028
1029 // This is a FindNext operation if we are searching for the same text again,
1030 // or if the passed in search text is empty (FindNext keyboard shortcut). The
1031 // exception to this is if the Find was aborted (then we don't want FindNext
1032 // because the highlighting has been cleared and we need it to reappear). We
1033 // therefore treat FindNext after an aborted Find operation as a full fledged
1034 // Find.
1035 bool find_next = (find_text_ == find_text || find_text.empty()) &&
[email protected]e491f1c2009-05-22 20:28:121036 (last_search_case_sensitive_ == case_sensitive) &&
[email protected]6e95706d2009-04-23 22:55:011037 !find_op_aborted_;
1038 if (!find_next)
1039 current_find_request_id_ = find_request_id_counter_++;
1040
1041 if (!find_text.empty())
1042 find_text_ = find_text;
[email protected]e491f1c2009-05-22 20:28:121043 last_search_case_sensitive_ = case_sensitive;
[email protected]6e95706d2009-04-23 22:55:011044
1045 find_op_aborted_ = false;
1046
1047 // Keep track of what the last search was across the tabs.
[email protected]e491f1c2009-05-22 20:28:121048 *last_search_prepopulate_text_ = find_text;
[email protected]6e95706d2009-04-23 22:55:011049
1050 render_view_host()->StartFinding(current_find_request_id_,
1051 find_text_,
1052 forward_direction,
[email protected]e491f1c2009-05-22 20:28:121053 case_sensitive,
[email protected]6e95706d2009-04-23 22:55:011054 find_next);
1055}
1056
1057void TabContents::StopFinding(bool clear_selection) {
[email protected]2d2dffc2009-06-09 21:41:001058 // When |clear_selection| is true, it means the find string has been cleared
1059 // by the user, but the UI has not been dismissed.
1060 if (!clear_selection)
1061 find_ui_active_ = false;
[email protected]6e95706d2009-04-23 22:55:011062 find_op_aborted_ = true;
[email protected]e491f1c2009-05-22 20:28:121063 last_search_result_ = FindNotificationDetails();
[email protected]6e95706d2009-04-23 22:55:011064 render_view_host()->StopFinding(clear_selection);
1065}
1066
[email protected]5c4266922009-07-10 16:41:271067void TabContents::GetPageLanguage() {
1068 render_view_host()->GetPageLanguage();
1069}
1070
[email protected]420ae012009-04-24 05:16:321071void TabContents::OnJavaScriptMessageBoxClosed(IPC::Message* reply_msg,
1072 bool success,
1073 const std::wstring& prompt) {
1074 last_javascript_message_dismissal_ = base::TimeTicks::Now();
1075 render_manager_.OnJavaScriptMessageBoxClosed(reply_msg, success, prompt);
1076}
1077
[email protected]571e31152009-05-15 19:34:021078void TabContents::OnJavaScriptMessageBoxWindowDestroyed() {
1079 render_manager_.OnJavaScriptMessageBoxWindowDestroyed();
1080}
1081
[email protected]420ae012009-04-24 05:16:321082void TabContents::OnSavePage() {
1083 // If we can not save the page, try to download it.
1084 if (!SavePackage::IsSavableContents(contents_mime_type())) {
1085 DownloadManager* dlm = profile()->GetDownloadManager();
1086 const GURL& current_page_url = GetURL();
1087 if (dlm && current_page_url.is_valid())
[email protected]d82ed61e2009-06-16 02:46:221088 dlm->DownloadUrl(current_page_url, GURL(), "", this);
[email protected]420ae012009-04-24 05:16:321089 return;
1090 }
1091
1092 Stop();
1093
1094 // Create the save package and possibly prompt the user for the name to save
1095 // the page as. The user prompt is an asynchronous operation that runs on
1096 // another thread.
[email protected]d82ed61e2009-06-16 02:46:221097 save_package_ = new SavePackage(this);
[email protected]420ae012009-04-24 05:16:321098 save_package_->GetSaveInfo();
1099}
1100
1101// Used in automated testing to bypass prompting the user for file names.
1102// Instead, the names and paths are hard coded rather than running them through
1103// file name sanitation and extension / mime checking.
[email protected]cdf6cc82009-08-04 21:48:591104bool TabContents::SavePage(const std::wstring& main_file,
[email protected]420ae012009-04-24 05:16:321105 const std::wstring& dir_path,
1106 SavePackage::SavePackageType save_type) {
1107 // Stop the page from navigating.
1108 Stop();
1109
[email protected]d82ed61e2009-06-16 02:46:221110 save_package_ = new SavePackage(this, save_type,
[email protected]420ae012009-04-24 05:16:321111 FilePath::FromWStringHack(main_file),
1112 FilePath::FromWStringHack(dir_path));
[email protected]cdf6cc82009-08-04 21:48:591113 return save_package_->Init();
[email protected]420ae012009-04-24 05:16:321114}
1115
1116void TabContents::PrintPreview() {
1117 // We don't show the print preview yet, only the print dialog.
1118 PrintNow();
1119}
1120
1121bool TabContents::PrintNow() {
1122 // We can't print interstitial page for now.
1123 if (showing_interstitial_page())
1124 return false;
1125
1126 return render_view_host()->PrintPages();
1127}
1128
[email protected]82270452009-06-19 15:58:011129void TabContents::PrintingDone(int document_cookie, bool success) {
1130 render_view_host()->PrintingDone(document_cookie, success);
1131}
1132
[email protected]420ae012009-04-24 05:16:321133bool TabContents::IsActiveEntry(int32 page_id) {
1134 NavigationEntry* active_entry = controller_.GetActiveEntry();
1135 return (active_entry != NULL &&
1136 active_entry->site_instance() == GetSiteInstance() &&
1137 active_entry->page_id() == page_id);
1138}
1139
[email protected]96d185d2009-04-24 03:28:541140// Notifies the RenderWidgetHost instance about the fact that the page is
1141// loading, or done loading and calls the base implementation.
1142void TabContents::SetIsLoading(bool is_loading,
1143 LoadNotificationDetails* details) {
1144 if (is_loading == is_loading_)
1145 return;
1146
1147 if (!is_loading) {
1148 load_state_ = net::LOAD_STATE_IDLE;
1149 load_state_host_.clear();
1150 }
1151
1152 render_manager_.SetIsLoading(is_loading);
1153
1154 is_loading_ = is_loading;
1155 waiting_for_response_ = is_loading;
1156
1157 if (delegate_)
1158 delegate_->LoadingStateChanged(this);
[email protected]329581b2009-04-28 06:52:351159 NotifyNavigationStateChanged(INVALIDATE_LOAD);
[email protected]96d185d2009-04-24 03:28:541160
1161 NotificationType type = is_loading ? NotificationType::LOAD_START :
1162 NotificationType::LOAD_STOP;
[email protected]ce5c4502009-05-06 16:46:111163 NotificationDetails det = NotificationService::NoDetails();
[email protected]96d185d2009-04-24 03:28:541164 if (details)
1165 det = Details<LoadNotificationDetails>(details);
1166 NotificationService::current()->Notify(type,
1167 Source<NavigationController>(&controller_),
1168 det);
1169}
1170
[email protected]2c4410d2009-05-06 23:46:221171void TabContents::CreateBlockedPopupContainerIfNecessary() {
1172 if (blocked_popups_)
1173 return;
1174
[email protected]4d4c0b92009-06-08 23:13:011175 blocked_popups_ = BlockedPopupContainer::Create(this, profile());
[email protected]2c4410d2009-05-06 23:46:221176}
1177
[email protected]b6c874582009-05-08 19:38:311178void TabContents::AddPopup(TabContents* new_contents,
1179 const gfx::Rect& initial_pos,
1180 const std::string& host) {
[email protected]2c4410d2009-05-06 23:46:221181 CreateBlockedPopupContainerIfNecessary();
1182 blocked_popups_->AddTabContents(new_contents, initial_pos, host);
[email protected]2c4410d2009-05-06 23:46:221183}
1184
[email protected]e69bce3dd2009-04-20 22:05:121185// TODO(brettw) This should be on the TabContentsView.
[email protected]66ba4932009-06-04 19:22:131186void TabContents::RepositionSupressedPopupsToFit() {
[email protected]d6598c052008-11-05 19:03:251187 if (blocked_popups_)
[email protected]4d4c0b92009-06-08 23:13:011188 blocked_popups_->RepositionBlockedPopupContainer();
[email protected]d5f942ba2008-09-26 19:30:341189}
1190
[email protected]b9681312008-11-07 00:08:261191bool TabContents::ShowingBlockedPopupNotification() const {
1192 return blocked_popups_ != NULL &&
[email protected]2c4410d2009-05-06 23:46:221193 blocked_popups_->GetBlockedPopupCount() != 0;
[email protected]b9681312008-11-07 00:08:261194}
[email protected]616ed5a2008-11-21 22:27:241195
1196namespace {
1197bool TransitionIsReload(PageTransition::Type transition) {
1198 return PageTransition::StripQualifier(transition) == PageTransition::RELOAD;
1199}
1200}
1201
1202void TabContents::ExpireInfoBars(
1203 const NavigationController::LoadCommittedDetails& details) {
1204 // Only hide InfoBars when the user has done something that makes the main
1205 // frame load. We don't want various automatic or subframe navigations making
1206 // it disappear.
1207 if (!details.is_user_initiated_main_frame_load())
1208 return;
1209
[email protected]f86a07022008-11-25 01:06:051210 for (int i = infobar_delegate_count() - 1; i >= 0; --i) {
[email protected]616ed5a2008-11-21 22:27:241211 InfoBarDelegate* delegate = GetInfoBarDelegateAt(i);
[email protected]f86a07022008-11-25 01:06:051212 if (delegate->ShouldExpire(details))
[email protected]616ed5a2008-11-21 22:27:241213 RemoveInfoBar(delegate);
[email protected]616ed5a2008-11-21 22:27:241214 }
1215}
[email protected]fdd61c62009-04-22 19:22:571216
1217void TabContents::OnGearsCreateShortcutDone(
1218 const GearsShortcutData2& shortcut_data, bool success) {
1219 NavigationEntry* current_entry = controller_.GetLastCommittedEntry();
1220 bool same_page =
1221 current_entry && pending_install_.page_id == current_entry->page_id();
1222
1223 if (success && same_page) {
1224 // Only switch to app mode if the user chose to create a shortcut and
1225 // we're still on the same page that it corresponded to.
1226 if (delegate())
1227 delegate()->ConvertContentsToApplication(this);
1228 }
1229
1230 // Reset the page id to indicate no requests are pending.
1231 pending_install_.page_id = 0;
1232 pending_install_.callback_functor = NULL;
1233}
[email protected]96d185d2009-04-24 03:28:541234
1235DOMUI* TabContents::GetDOMUIForCurrentState() {
1236 // When there is a pending navigation entry, we want to use the pending DOMUI
1237 // that goes along with it to control the basic flags. For example, we want to
1238 // show the pending URL in the URL bar, so we want the display_url flag to
1239 // be from the pending entry.
1240 //
1241 // The confusion comes because there are multiple possibilities for the
1242 // initial load in a tab as a side effect of the way the RenderViewHostManager
1243 // works.
1244 //
1245 // - For the very first tab the load looks "normal". The new tab DOM UI is
1246 // the pending one, and we want it to apply here.
1247 //
1248 // - For subsequent new tabs, they'll get a new SiteInstance which will then
1249 // get switched to the one previously associated with the new tab pages.
1250 // This switching will cause the manager to commit the RVH/DOMUI. So we'll
1251 // have a committed DOM UI in this case.
1252 //
1253 // This condition handles all of these cases:
1254 //
1255 // - First load in first tab: no committed nav entry + pending nav entry +
1256 // pending dom ui:
1257 // -> Use pending DOM UI if any.
1258 //
1259 // - First load in second tab: no committed nav entry + pending nav entry +
1260 // no pending DOM UI:
1261 // -> Use the committed DOM UI if any.
1262 //
1263 // - Second navigation in any tab: committed nav entry + pending nav entry:
1264 // -> Use pending DOM UI if any.
1265 //
1266 // - Normal state with no load: committed nav entry + no pending nav entry:
1267 // -> Use committed DOM UI.
1268 if (controller_.pending_entry() &&
1269 (controller_.GetLastCommittedEntry() ||
1270 render_manager_.pending_dom_ui()))
1271 return render_manager_.pending_dom_ui();
1272 return render_manager_.dom_ui();
1273}
[email protected]420ae012009-04-24 05:16:321274
1275void TabContents::DidNavigateMainFramePostCommit(
1276 const NavigationController::LoadCommittedDetails& details,
1277 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]c2e74fe82009-09-03 17:57:441278 if (opener_dom_ui_type_ != DOMUIFactory::kNoDOMUI) {
1279 // If this is a window.open navigation, use the same DOMUI as the renderer
1280 // that opened the window, as long as both renderers have the same
1281 // privileges.
1282 if (opener_dom_ui_type_ == DOMUIFactory::GetDOMUIType(GetURL())) {
1283 DOMUI* dom_ui = DOMUIFactory::CreateDOMUIForURL(this, GetURL());
1284 // dom_ui might be NULL if the URL refers to a non-existent extension.
1285 if (dom_ui) {
1286 render_manager_.SetDOMUIPostCommit(dom_ui);
1287 dom_ui->RenderViewCreated(render_view_host());
1288 }
1289 }
1290 opener_dom_ui_type_ = DOMUIFactory::kNoDOMUI;
1291 }
1292
[email protected]420ae012009-04-24 05:16:321293 if (details.is_user_initiated_main_frame_load()) {
1294 // Clear the status bubble. This is a workaround for a bug where WebKit
1295 // doesn't let us know that the cursor left an element during a
1296 // transition (this is also why the mouse cursor remains as a hand after
1297 // clicking on a link); see bugs 1184641 and 980803. We don't want to
1298 // clear the bubble when a user navigates to a named anchor in the same
1299 // page.
1300 UpdateTargetURL(details.entry->page_id(), GURL());
1301
1302 // UpdateHelpersForDidNavigate will handle the case where the password_form
1303 // origin is valid.
1304 // TODO(brettw) bug 1343111: Password manager stuff in here needs to be
1305 // cleaned up and covered by tests.
1306 if (!params.password_form.origin.is_valid())
1307 GetPasswordManager()->DidNavigate();
1308 }
1309
1310 // The keyword generator uses the navigation entries, so must be called after
1311 // the commit.
1312 GenerateKeywordIfNecessary(params);
1313
1314 // Allow the new page to set the title again.
1315 received_page_title_ = false;
1316
1317 // Get the favicon, either from history or request it from the net.
1318 fav_icon_helper_.FetchFavIcon(details.entry->url());
1319
[email protected]aece2c7f2009-08-27 20:43:171320 // Disable all page actions, unless this is an in-page navigation.
1321 if (!enabled_page_actions_.empty()) {
1322 url_canon::Replacements<char> replacements;
1323 replacements.ClearRef();
1324 if (params.url.ReplaceComponents(replacements) !=
1325 params.referrer.ReplaceComponents(replacements)) {
1326 enabled_page_actions_.clear();
1327 }
1328 }
[email protected]ce5c4502009-05-06 16:46:111329
[email protected]420ae012009-04-24 05:16:321330 // Close constrained popups if necessary.
1331 MaybeCloseChildWindows(details.previous_url, details.entry->url());
1332
1333 // Update the starred state.
1334 UpdateStarredStateForCurrentURL();
1335}
1336
1337void TabContents::DidNavigateAnyFramePostCommit(
1338 RenderViewHost* render_view_host,
1339 const NavigationController::LoadCommittedDetails& details,
1340 const ViewHostMsg_FrameNavigate_Params& params) {
1341 // If we navigate, start showing messages again. This does nothing to prevent
1342 // a malicious script from spamming messages, since the script could just
1343 // reload the page to stop blocking.
1344 suppress_javascript_messages_ = false;
1345
[email protected]420ae012009-04-24 05:16:321346 // Notify the password manager of the navigation or form submit.
1347 // TODO(brettw) bug 1343111: Password manager stuff in here needs to be
1348 // cleaned up and covered by tests.
1349 if (params.password_form.origin.is_valid())
1350 GetPasswordManager()->ProvisionallySavePassword(params.password_form);
1351}
1352
1353void TabContents::MaybeCloseChildWindows(const GURL& previous_url,
1354 const GURL& current_url) {
1355 if (net::RegistryControlledDomainService::SameDomainOrHost(
1356 previous_url, current_url))
1357 return;
1358
1359 // Clear out any child windows since we are leaving this page entirely.
1360 // We use indices instead of iterators in case CloseWindow does something
1361 // that may invalidate an iterator.
1362 int size = static_cast<int>(child_windows_.size());
1363 for (int i = size - 1; i >= 0; --i) {
1364 ConstrainedWindow* window = child_windows_[i];
1365 if (window)
1366 window->CloseConstrainedWindow();
1367 }
[email protected]e29300cd2009-07-23 21:14:591368
1369 // Close the popup container.
1370 if (blocked_popups_) {
1371 blocked_popups_->Destroy();
1372 blocked_popups_ = NULL;
1373 }
[email protected]420ae012009-04-24 05:16:321374}
1375
1376void TabContents::UpdateStarredStateForCurrentURL() {
1377 BookmarkModel* model = profile()->GetBookmarkModel();
1378 const bool old_state = is_starred_;
1379 is_starred_ = (model && model->IsBookmarked(GetURL()));
1380
1381 if (is_starred_ != old_state && delegate())
1382 delegate()->URLStarredChanged(this, is_starred_);
1383}
1384
1385void TabContents::UpdateAlternateErrorPageURL() {
1386 GURL url = GetAlternateErrorPageURL();
1387 render_view_host()->SetAlternateErrorPageURL(url);
1388}
1389
1390void TabContents::UpdateWebPreferences() {
1391 render_view_host()->UpdateWebPreferences(GetWebkitPrefs());
1392}
1393
1394void TabContents::UpdateMaxPageIDIfNecessary(SiteInstance* site_instance,
1395 RenderViewHost* rvh) {
1396 // If we are creating a RVH for a restored controller, then we might
1397 // have more page IDs than the SiteInstance's current max page ID. We must
1398 // make sure that the max page ID is larger than any restored page ID.
1399 // Note that it is ok for conflicting page IDs to exist in another tab
1400 // (i.e., NavigationController), but if any page ID is larger than the max,
1401 // the back/forward list will get confused.
1402 int max_restored_page_id = controller_.max_restored_page_id();
1403 if (max_restored_page_id > 0) {
1404 int curr_max_page_id = site_instance->max_page_id();
1405 if (max_restored_page_id > curr_max_page_id) {
1406 // Need to update the site instance immediately.
1407 site_instance->UpdateMaxPageID(max_restored_page_id);
1408
1409 // Also tell the renderer to update its internal representation. We
1410 // need to reserve enough IDs to make all restored page IDs less than
1411 // the max.
1412 if (curr_max_page_id < 0)
1413 curr_max_page_id = 0;
1414 rvh->ReservePageIDRange(max_restored_page_id - curr_max_page_id);
1415 }
1416 }
1417}
1418
1419void TabContents::UpdateHistoryForNavigation(
[email protected]ebe89e062009-08-13 23:16:541420 const GURL& virtual_url,
[email protected]befd8d822009-07-01 04:51:471421 const NavigationController::LoadCommittedDetails& details,
[email protected]420ae012009-04-24 05:16:321422 const ViewHostMsg_FrameNavigate_Params& params) {
1423 if (profile()->IsOffTheRecord())
1424 return;
1425
1426 // Add to history service.
1427 HistoryService* hs = profile()->GetHistoryService(Profile::IMPLICIT_ACCESS);
1428 if (hs) {
1429 if (PageTransition::IsMainFrame(params.transition) &&
[email protected]ebe89e062009-08-13 23:16:541430 virtual_url != params.url) {
1431 // Hack on the "virtual" URL so that it will appear in history. For some
[email protected]420ae012009-04-24 05:16:321432 // types of URLs, we will display a magic URL that is different from where
1433 // the page is actually navigated. We want the user to see in history
[email protected]ebe89e062009-08-13 23:16:541434 // what they saw in the URL bar, so we add the virtual URL as a redirect.
1435 // This only applies to the main frame, as the virtual URL doesn't apply
[email protected]420ae012009-04-24 05:16:321436 // to sub-frames.
1437 std::vector<GURL> redirects = params.redirects;
1438 if (!redirects.empty())
[email protected]ebe89e062009-08-13 23:16:541439 redirects.back() = virtual_url;
1440 hs->AddPage(virtual_url, this, params.page_id, params.referrer,
[email protected]befd8d822009-07-01 04:51:471441 params.transition, redirects, details.did_replace_entry);
[email protected]420ae012009-04-24 05:16:321442 } else {
1443 hs->AddPage(params.url, this, params.page_id, params.referrer,
[email protected]befd8d822009-07-01 04:51:471444 params.transition, params.redirects,
1445 details.did_replace_entry);
[email protected]420ae012009-04-24 05:16:321446 }
1447 }
1448}
1449
1450bool TabContents::UpdateTitleForEntry(NavigationEntry* entry,
1451 const std::wstring& title) {
1452 // For file URLs without a title, use the pathname instead. In the case of a
1453 // synthesized title, we don't want the update to count toward the "one set
1454 // per page of the title to history."
1455 std::wstring final_title;
1456 bool explicit_set;
1457 if (entry->url().SchemeIsFile() && title.empty()) {
1458 final_title = UTF8ToWide(entry->url().ExtractFileName());
1459 explicit_set = false; // Don't count synthetic titles toward the set limit.
1460 } else {
1461 TrimWhitespace(title, TRIM_ALL, &final_title);
1462 explicit_set = true;
1463 }
1464
1465 if (final_title == UTF16ToWideHack(entry->title()))
1466 return false; // Nothing changed, don't bother.
1467
1468 entry->set_title(WideToUTF16Hack(final_title));
1469
1470 // Update the history system for this page.
1471 if (!profile()->IsOffTheRecord() && !received_page_title_) {
1472 HistoryService* hs =
1473 profile()->GetHistoryService(Profile::IMPLICIT_ACCESS);
1474 if (hs)
[email protected]ebe89e062009-08-13 23:16:541475 hs->SetPageTitle(entry->virtual_url(), final_title);
[email protected]420ae012009-04-24 05:16:321476
1477 // Don't allow the title to be saved again for explicitly set ones.
1478 received_page_title_ = explicit_set;
1479 }
1480
1481 // Lastly, set the title for the view.
1482 view_->SetPageTitle(final_title);
1483
1484 return true;
1485}
1486
1487void TabContents::NotifySwapped() {
1488 // After sending out a swap notification, we need to send a disconnect
1489 // notification so that clients that pick up a pointer to |this| can NULL the
1490 // pointer. See Bug 1230284.
1491 notify_disconnection_ = true;
1492 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341493 NotificationType::TAB_CONTENTS_SWAPPED,
[email protected]d82ed61e2009-06-16 02:46:221494 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321495 NotificationService::NoDetails());
1496}
1497
1498void TabContents::NotifyConnected() {
1499 notify_disconnection_ = true;
1500 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341501 NotificationType::TAB_CONTENTS_CONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221502 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321503 NotificationService::NoDetails());
1504}
1505
1506void TabContents::NotifyDisconnected() {
1507 if (!notify_disconnection_)
1508 return;
1509
1510 notify_disconnection_ = false;
1511 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341512 NotificationType::TAB_CONTENTS_DISCONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221513 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321514 NotificationService::NoDetails());
1515}
1516
1517void TabContents::GenerateKeywordIfNecessary(
1518 const ViewHostMsg_FrameNavigate_Params& params) {
1519 if (!params.searchable_form_url.is_valid())
1520 return;
1521
1522 if (profile()->IsOffTheRecord())
1523 return;
1524
1525 int last_index = controller_.last_committed_entry_index();
1526 // When there was no previous page, the last index will be 0. This is
1527 // normally due to a form submit that opened in a new tab.
1528 // TODO(brettw) bug 916126: we should support keywords when form submits
1529 // happen in new tabs.
1530 if (last_index <= 0)
1531 return;
1532 const NavigationEntry* previous_entry =
1533 controller_.GetEntryAtIndex(last_index - 1);
1534 if (IsFormSubmit(previous_entry)) {
1535 // Only generate a keyword if the previous page wasn't itself a form
1536 // submit.
1537 return;
1538 }
1539
1540 GURL keyword_url = previous_entry->user_typed_url().is_valid() ?
1541 previous_entry->user_typed_url() : previous_entry->url();
1542 std::wstring keyword =
1543 TemplateURLModel::GenerateKeyword(keyword_url, true); // autodetected
1544 if (keyword.empty())
1545 return;
1546
1547 TemplateURLModel* url_model = profile()->GetTemplateURLModel();
1548 if (!url_model)
1549 return;
1550
1551 if (!url_model->loaded()) {
1552 url_model->Load();
1553 return;
1554 }
1555
1556 const TemplateURL* current_url;
1557 std::wstring url = UTF8ToWide(params.searchable_form_url.spec());
1558 if (!url_model->CanReplaceKeyword(keyword, url, &current_url))
1559 return;
1560
1561 if (current_url) {
1562 if (current_url->originating_url().is_valid()) {
1563 // The existing keyword was generated from an OpenSearch description
1564 // document, don't regenerate.
1565 return;
1566 }
1567 url_model->Remove(current_url);
1568 }
1569 TemplateURL* new_url = new TemplateURL();
1570 new_url->set_keyword(keyword);
1571 new_url->set_short_name(keyword);
1572 new_url->SetURL(url, 0, 0);
1573 new_url->add_input_encoding(params.searchable_form_encoding);
1574 DCHECK(controller_.GetLastCommittedEntry());
1575 const GURL& favicon_url =
1576 controller_.GetLastCommittedEntry()->favicon().url();
1577 if (favicon_url.is_valid()) {
1578 new_url->SetFavIconURL(favicon_url);
1579 } else {
1580 // The favicon url isn't valid. This means there really isn't a favicon,
1581 // or the favicon url wasn't obtained before the load started. This assumes
1582 // the later.
1583 // TODO(sky): Need a way to set the favicon that doesn't involve generating
1584 // its url.
1585 new_url->SetFavIconURL(TemplateURL::GenerateFaviconURL(params.referrer));
1586 }
1587 new_url->set_safe_for_autoreplace(true);
1588 url_model->Add(new_url);
1589}
1590
[email protected]bf5c2ff392009-07-08 16:24:331591void TabContents::OnUserGesture() {
1592 // See comment in RenderViewHostDelegate::OnUserGesture as to why we do this.
1593 DownloadRequestManager* drm = g_browser_process->download_request_manager();
1594 if (drm)
1595 drm->OnUserGesture(this);
1596 controller_.OnUserGesture();
1597}
1598
1599void TabContents::OnFindReply(int request_id,
1600 int number_of_matches,
1601 const gfx::Rect& selection_rect,
1602 int active_match_ordinal,
1603 bool final_update) {
[email protected]3ce7ced2009-07-24 18:52:421604 // Ignore responses for requests that have been aborted.
1605 if (find_op_aborted_)
1606 return;
1607
[email protected]bf5c2ff392009-07-08 16:24:331608 // Ignore responses for requests other than the one we have most recently
1609 // issued. That way we won't act on stale results when the user has
1610 // already typed in another query.
1611 if (request_id != current_find_request_id_)
1612 return;
1613
1614 if (number_of_matches == -1)
1615 number_of_matches = last_search_result_.number_of_matches();
1616 if (active_match_ordinal == -1)
1617 active_match_ordinal = last_search_result_.active_match_ordinal();
1618
1619 gfx::Rect selection = selection_rect;
1620 if (selection.IsEmpty())
1621 selection = last_search_result_.selection_rect();
1622
1623 // Notify the UI, automation and any other observers that a find result was
1624 // found.
1625 last_search_result_ = FindNotificationDetails(request_id, number_of_matches,
1626 selection, active_match_ordinal,
1627 final_update);
1628 NotificationService::current()->Notify(
1629 NotificationType::FIND_RESULT_AVAILABLE,
1630 Source<TabContents>(this),
1631 Details<FindNotificationDetails>(&last_search_result_));
1632}
1633
1634void TabContents::GoToEntryAtOffset(int offset) {
1635 controller_.GoToOffset(offset);
1636}
1637
1638void TabContents::GetHistoryListCount(int* back_list_count,
1639 int* forward_list_count) {
1640 int current_index = controller_.last_committed_entry_index();
1641 *back_list_count = current_index;
1642 *forward_list_count = controller_.entry_count() - current_index - 1;
1643}
1644
1645void TabContents::OnMissingPluginStatus(int status) {
1646#if defined(OS_WIN)
1647// TODO(PORT): pull in when plug-ins work
1648 GetPluginInstaller()->OnMissingPluginStatus(status);
1649#endif
1650}
1651
1652void TabContents::OnCrashedPlugin(const FilePath& plugin_path) {
[email protected]bf5c2ff392009-07-08 16:24:331653 DCHECK(!plugin_path.value().empty());
1654
1655 std::wstring plugin_name = plugin_path.ToWStringHack();
[email protected]d32d46182009-07-23 00:09:351656#if defined(OS_WIN) || defined(OS_MACOSX)
[email protected]bf5c2ff392009-07-08 16:24:331657 scoped_ptr<FileVersionInfo> version_info(
1658 FileVersionInfo::CreateFileVersionInfo(plugin_path));
1659 if (version_info.get()) {
1660 const std::wstring& product_name = version_info->product_name();
[email protected]996fd702009-09-04 19:12:371661 if (!product_name.empty()) {
[email protected]bf5c2ff392009-07-08 16:24:331662 plugin_name = product_name;
[email protected]996fd702009-09-04 19:12:371663#if defined(OS_MACOSX)
1664 // Many plugins on the Mac have .plugin in the actual name, which looks
1665 // terrible, so look for that and strip it off if present.
1666 const std::wstring plugin_extension(L".plugin");
1667 if (EndsWith(plugin_name, plugin_extension, true))
1668 plugin_name.erase(plugin_name.length() - plugin_extension.length());
1669#endif // OS_MACOSX
1670 }
[email protected]bf5c2ff392009-07-08 16:24:331671 }
[email protected]4a560ed2009-07-13 23:35:261672#else
1673 NOTIMPLEMENTED() << " convert plugin path to plugin name";
1674#endif
[email protected]bf5c2ff392009-07-08 16:24:331675 AddInfoBar(new SimpleAlertInfoBarDelegate(
1676 this, l10n_util::GetStringF(IDS_PLUGIN_CRASHED_PROMPT, plugin_name),
1677 NULL));
[email protected]bf5c2ff392009-07-08 16:24:331678}
1679
1680void TabContents::OnCrashedWorker() {
1681 AddInfoBar(new SimpleAlertInfoBarDelegate(
1682 this, l10n_util::GetString(IDS_WEBWORKER_CRASHED_PROMPT),
1683 NULL));
1684}
1685
1686void TabContents::OnDidGetApplicationInfo(
1687 int32 page_id,
1688 const webkit_glue::WebApplicationInfo& info) {
1689 if (pending_install_.page_id != page_id)
1690 return; // The user clicked create on a separate page. Ignore this.
1691
1692 pending_install_.callback_functor =
1693 new GearsCreateShortcutCallbackFunctor(this);
1694 GearsCreateShortcut(
1695 info, pending_install_.title, pending_install_.url, pending_install_.icon,
1696 NewCallback(pending_install_.callback_functor,
1697 &GearsCreateShortcutCallbackFunctor::Run));
1698}
1699
1700void TabContents::DidStartProvisionalLoadForFrame(
1701 RenderViewHost* render_view_host,
1702 bool is_main_frame,
1703 const GURL& url) {
1704 ProvisionalLoadDetails details(is_main_frame,
1705 controller_.IsURLInPageNavigation(url),
1706 url, std::string(), false);
1707 NotificationService::current()->Notify(
1708 NotificationType::FRAME_PROVISIONAL_LOAD_START,
1709 Source<NavigationController>(&controller_),
1710 Details<ProvisionalLoadDetails>(&details));
1711}
1712
1713void TabContents::DidStartReceivingResourceResponse(
1714 ResourceRequestDetails* details) {
1715 NotificationService::current()->Notify(
1716 NotificationType::RESOURCE_RESPONSE_STARTED,
1717 Source<NavigationController>(&controller()),
1718 Details<ResourceRequestDetails>(details));
1719}
1720
1721void TabContents::DidRedirectResource(ResourceRequestDetails* details) {
1722 NotificationService::current()->Notify(
1723 NotificationType::RESOURCE_RECEIVED_REDIRECT,
1724 Source<NavigationController>(&controller()),
1725 Details<ResourceRequestDetails>(details));
1726}
1727
1728void TabContents::DidLoadResourceFromMemoryCache(
1729 const GURL& url,
1730 const std::string& frame_origin,
1731 const std::string& main_frame_origin,
1732 const std::string& security_info) {
1733 // Send out a notification that we loaded a resource from our memory cache.
1734 int cert_id = 0, cert_status = 0, security_bits = 0;
1735 SSLManager::DeserializeSecurityInfo(security_info,
1736 &cert_id, &cert_status,
1737 &security_bits);
1738 LoadFromMemoryCacheDetails details(url, frame_origin, main_frame_origin,
[email protected]76543b92009-08-31 17:27:451739 process()->id(), cert_id, cert_status);
[email protected]bf5c2ff392009-07-08 16:24:331740
1741 NotificationService::current()->Notify(
1742 NotificationType::LOAD_FROM_MEMORY_CACHE,
1743 Source<NavigationController>(&controller_),
1744 Details<LoadFromMemoryCacheDetails>(&details));
1745}
1746
1747void TabContents::DidFailProvisionalLoadWithError(
1748 RenderViewHost* render_view_host,
1749 bool is_main_frame,
1750 int error_code,
1751 const GURL& url,
1752 bool showing_repost_interstitial) {
1753 if (net::ERR_ABORTED == error_code) {
1754 // EVIL HACK ALERT! Ignore failed loads when we're showing interstitials.
1755 // This means that the interstitial won't be torn down properly, which is
1756 // bad. But if we have an interstitial, go back to another tab type, and
1757 // then load the same interstitial again, we could end up getting the first
1758 // interstitial's "failed" message (as a result of the cancel) when we're on
1759 // the second one.
1760 //
1761 // We can't tell this apart, so we think we're tearing down the current page
1762 // which will cause a crash later one. There is also some code in
1763 // RenderViewHostManager::RendererAbortedProvisionalLoad that is commented
1764 // out because of this problem.
1765 //
1766 // http://code.google.com/p/chromium/issues/detail?id=2855
1767 // Because this will not tear down the interstitial properly, if "back" is
1768 // back to another tab type, the interstitial will still be somewhat alive
1769 // in the previous tab type. If you navigate somewhere that activates the
1770 // tab with the interstitial again, you'll see a flash before the new load
1771 // commits of the interstitial page.
1772 if (showing_interstitial_page()) {
1773 LOG(WARNING) << "Discarding message during interstitial.";
1774 return;
1775 }
1776
1777 // This will discard our pending entry if we cancelled the load (e.g., if we
1778 // decided to download the file instead of load it). Only discard the
1779 // pending entry if the URLs match, otherwise the user initiated a navigate
1780 // before the page loaded so that the discard would discard the wrong entry.
1781 NavigationEntry* pending_entry = controller_.pending_entry();
1782 if (pending_entry && pending_entry->url() == url) {
1783 controller_.DiscardNonCommittedEntries();
1784 // Update the URL display.
1785 NotifyNavigationStateChanged(TabContents::INVALIDATE_URL);
1786 }
1787
1788 render_manager_.RendererAbortedProvisionalLoad(render_view_host);
1789 }
1790
1791 // Send out a notification that we failed a provisional load with an error.
1792 ProvisionalLoadDetails details(is_main_frame,
1793 controller_.IsURLInPageNavigation(url),
1794 url, std::string(), false);
1795 details.set_error_code(error_code);
1796
1797 NotificationService::current()->Notify(
1798 NotificationType::FAIL_PROVISIONAL_LOAD_WITH_ERROR,
1799 Source<NavigationController>(&controller_),
1800 Details<ProvisionalLoadDetails>(&details));
1801}
1802
1803void TabContents::DocumentLoadedInFrame() {
1804 controller_.DocumentLoadedInFrame();
1805}
1806
[email protected]8d3347f2009-07-09 22:00:211807RenderViewHostDelegate::View* TabContents::GetViewDelegate() {
[email protected]420ae012009-04-24 05:16:321808 return view_.get();
1809}
1810
[email protected]8d3347f2009-07-09 22:00:211811RenderViewHostDelegate::RendererManagement*
1812TabContents::GetRendererManagementDelegate() {
1813 return &render_manager_;
1814}
1815
[email protected]bf5c2ff392009-07-08 16:24:331816RenderViewHostDelegate::BrowserIntegration*
[email protected]8d3347f2009-07-09 22:00:211817 TabContents::GetBrowserIntegrationDelegate() {
1818 return this;
[email protected]bf5c2ff392009-07-08 16:24:331819}
1820
[email protected]8d3347f2009-07-09 22:00:211821RenderViewHostDelegate::Resource* TabContents::GetResourceDelegate() {
1822 return this;
[email protected]bf5c2ff392009-07-08 16:24:331823}
1824
[email protected]8d3347f2009-07-09 22:00:211825RenderViewHostDelegate::Save* TabContents::GetSaveDelegate() {
[email protected]420ae012009-04-24 05:16:321826 return save_package_.get(); // May be NULL, but we can return NULL.
1827}
1828
[email protected]8d3347f2009-07-09 22:00:211829RenderViewHostDelegate::Printing* TabContents::GetPrintingDelegate() {
1830 return &printing_;
1831}
1832
1833RenderViewHostDelegate::FavIcon* TabContents::GetFavIconDelegate() {
1834 return &fav_icon_helper_;
1835}
1836
1837RenderViewHostDelegate::Autofill* TabContents::GetAutofillDelegate() {
1838 if (autofill_manager_.get() == NULL)
1839 autofill_manager_.reset(new AutofillManager(this));
1840 return autofill_manager_.get();
[email protected]bf5c2ff392009-07-08 16:24:331841}
1842
[email protected]80d96fa2009-06-10 22:34:511843RendererPreferences TabContents::GetRendererPrefs() const {
[email protected]840b1512009-07-21 16:53:461844 return renderer_preferences_;
[email protected]80d96fa2009-06-10 22:34:511845}
1846
[email protected]57c6a652009-05-04 07:58:341847TabContents* TabContents::GetAsTabContents() {
1848 return this;
1849}
1850
[email protected]3e7cf1362009-08-21 17:47:081851void TabContents::AddBlockedNotice(const GURL& url, const string16& reason) {
1852 CreateBlockedPopupContainerIfNecessary();
1853 blocked_popups_->AddBlockedNotice(url, reason);
1854}
1855
[email protected]7b291f92009-08-14 05:43:531856ViewType::Type TabContents::GetRenderViewType() const {
1857 return ViewType::TAB_CONTENTS;
1858}
1859
1860int TabContents::GetBrowserWindowID() const {
1861 return controller().window_id().id();
1862}
1863
[email protected]420ae012009-04-24 05:16:321864void TabContents::RenderViewCreated(RenderViewHost* render_view_host) {
[email protected]2e4633c2009-07-09 16:58:061865 NotificationService::current()->Notify(
1866 NotificationType::RENDER_VIEW_HOST_CREATED_FOR_TAB,
1867 Source<TabContents>(this),
1868 Details<RenderViewHost>(render_view_host));
[email protected]420ae012009-04-24 05:16:321869 NavigationEntry* entry = controller_.GetActiveEntry();
1870 if (!entry)
1871 return;
1872
1873 // When we're creating views, we're still doing initial setup, so we always
1874 // use the pending DOM UI rather than any possibly existing committed one.
1875 if (render_manager_.pending_dom_ui()) {
1876 render_manager_.pending_dom_ui()->RenderViewCreated(render_view_host);
1877 }
1878
1879 if (entry->IsViewSourceMode()) {
1880 // Put the renderer in view source mode.
1881 render_view_host->Send(
1882 new ViewMsg_EnableViewSourceMode(render_view_host->routing_id()));
1883 }
[email protected]0666aef2009-05-13 19:48:081884
1885 view()->RenderViewCreated(render_view_host);
[email protected]420ae012009-04-24 05:16:321886}
1887
1888void TabContents::RenderViewReady(RenderViewHost* rvh) {
1889 if (rvh != render_view_host()) {
1890 // Don't notify the world, since this came from a renderer in the
1891 // background.
1892 return;
1893 }
1894
1895 NotifyConnected();
[email protected]9d8a4642009-07-29 17:25:301896 bool was_crashed = is_crashed();
[email protected]420ae012009-04-24 05:16:321897 SetIsCrashed(false);
[email protected]9d8a4642009-07-29 17:25:301898
1899 // Restore the focus to the tab (otherwise the focus will be on the top
1900 // window).
1901 if (was_crashed && !FocusLocationBarByDefault())
1902 Focus();
[email protected]420ae012009-04-24 05:16:321903}
1904
1905void TabContents::RenderViewGone(RenderViewHost* rvh) {
1906 // Ask the print preview if this renderer was valuable.
1907 if (!printing_.OnRenderViewGone(rvh))
1908 return;
1909 if (rvh != render_view_host()) {
1910 // The pending page's RenderViewHost is gone.
1911 return;
1912 }
1913
1914 SetIsLoading(false, NULL);
1915 NotifyDisconnected();
1916 SetIsCrashed(true);
1917
[email protected]8e537302009-06-10 21:19:581918 // Tell the view that we've crashed so it can prepare the sad tab page.
1919 view_->OnTabCrashed();
[email protected]420ae012009-04-24 05:16:321920
1921 // Hide any visible hung renderer warning for this web contents' process.
[email protected]d82ed61e2009-06-16 02:46:221922 HungRendererDialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:321923}
1924
[email protected]2e4633c2009-07-09 16:58:061925void TabContents::RenderViewDeleted(RenderViewHost* rvh) {
1926 NotificationService::current()->Notify(
1927 NotificationType::RENDER_VIEW_HOST_DELETED,
1928 Source<TabContents>(this),
1929 Details<RenderViewHost>(rvh));
1930}
1931
[email protected]420ae012009-04-24 05:16:321932void TabContents::DidNavigate(RenderViewHost* rvh,
1933 const ViewHostMsg_FrameNavigate_Params& params) {
1934 if (PageTransition::IsMainFrame(params.transition))
1935 render_manager_.DidNavigateMainFrame(rvh);
1936
1937 // Update the site of the SiteInstance if it doesn't have one yet.
1938 if (!GetSiteInstance()->has_site())
1939 GetSiteInstance()->SetSite(params.url);
1940
1941 // Need to update MIME type here because it's referred to in
1942 // UpdateNavigationCommands() called by RendererDidNavigate() to
1943 // determine whether or not to enable the encoding menu.
1944 // It's updated only for the main frame. For a subframe,
1945 // RenderView::UpdateURL does not set params.contents_mime_type.
1946 // (see http://code.google.com/p/chromium/issues/detail?id=2929 )
1947 // TODO(jungshik): Add a test for the encoding menu to avoid
1948 // regressing it again.
1949 if (PageTransition::IsMainFrame(params.transition))
1950 contents_mime_type_ = params.contents_mime_type;
1951
1952 NavigationController::LoadCommittedDetails details;
[email protected]3e90d4a2009-07-03 17:38:391953 bool did_navigate = controller_.RendererDidNavigate(params, &details);
1954
1955 // Update history. Note that this needs to happen after the entry is complete,
1956 // which WillNavigate[Main,Sub]Frame will do before this function is called.
1957 if (params.should_update_history) {
1958 // Most of the time, the displayURL matches the loaded URL, but for about:
1959 // URLs, we use a data: URL as the real value. We actually want to save
1960 // the about: URL to the history db and keep the data: URL hidden. This is
1961 // what the TabContents' URL getter does.
1962 UpdateHistoryForNavigation(GetURL(), details, params);
1963 }
1964
[email protected]76543b92009-08-31 17:27:451965 if (!did_navigate)
1966 return; // No navigation happened.
[email protected]420ae012009-04-24 05:16:321967
1968 // DO NOT ADD MORE STUFF TO THIS FUNCTION! Your component should either listen
1969 // for the appropriate notification (best) or you can add it to
1970 // DidNavigateMainFramePostCommit / DidNavigateAnyFramePostCommit (only if
1971 // necessary, please).
1972
1973 // Run post-commit tasks.
1974 if (details.is_main_frame)
1975 DidNavigateMainFramePostCommit(details, params);
1976 DidNavigateAnyFramePostCommit(rvh, details, params);
1977}
1978
1979void TabContents::UpdateState(RenderViewHost* rvh,
1980 int32 page_id,
1981 const std::string& state) {
1982 DCHECK(rvh == render_view_host());
1983
1984 // We must be prepared to handle state updates for any page, these occur
1985 // when the user is scrolling and entering form data, as well as when we're
1986 // leaving a page, in which case our state may have already been moved to
1987 // the next page. The navigation controller will look up the appropriate
1988 // NavigationEntry and update it when it is notified via the delegate.
1989
1990 int entry_index = controller_.GetEntryIndexWithPageID(
1991 GetSiteInstance(), page_id);
1992 if (entry_index < 0)
1993 return;
1994 NavigationEntry* entry = controller_.GetEntryAtIndex(entry_index);
1995
1996 if (state == entry->content_state())
1997 return; // Nothing to update.
1998 entry->set_content_state(state);
1999 controller_.NotifyEntryChanged(entry, entry_index);
2000}
2001
2002void TabContents::UpdateTitle(RenderViewHost* rvh,
2003 int32 page_id, const std::wstring& title) {
2004 // If we have a title, that's a pretty good indication that we've started
2005 // getting useful data.
2006 SetNotWaitingForResponse();
2007
2008 DCHECK(rvh == render_view_host());
2009 NavigationEntry* entry = controller_.GetEntryWithPageID(GetSiteInstance(),
2010 page_id);
2011 if (!entry || !UpdateTitleForEntry(entry, title))
2012 return;
2013
2014 // Broadcast notifications when the UI should be updated.
2015 if (entry == controller_.GetEntryAtOffset(0))
[email protected]c9cd2222009-05-06 05:16:502016 NotifyNavigationStateChanged(INVALIDATE_TAB);
[email protected]420ae012009-04-24 05:16:322017}
2018
[email protected]420ae012009-04-24 05:16:322019void TabContents::UpdateEncoding(RenderViewHost* render_view_host,
[email protected]41fc0322009-09-04 22:23:402020 const std::string& encoding) {
[email protected]420ae012009-04-24 05:16:322021 set_encoding(encoding);
2022}
2023
2024void TabContents::UpdateTargetURL(int32 page_id, const GURL& url) {
2025 if (delegate())
2026 delegate()->UpdateTargetURL(this, url);
2027}
2028
2029void TabContents::UpdateThumbnail(const GURL& url,
2030 const SkBitmap& bitmap,
2031 const ThumbnailScore& score) {
2032 // Tell History about this thumbnail
[email protected]449478302009-06-09 20:04:282033 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kThumbnailStore)) {
[email protected]72d0fc72009-07-09 17:30:492034 if (!profile()->IsOffTheRecord())
[email protected]42339402009-07-17 17:27:192035 profile()->GetThumbnailStore()->SetPageThumbnail(url, bitmap,
2036 score, true);
[email protected]449478302009-06-09 20:04:282037 } else {
2038 HistoryService* hs;
2039 if (!profile()->IsOffTheRecord() &&
2040 (hs = profile()->GetHistoryService(Profile::IMPLICIT_ACCESS))) {
2041 hs->SetPageThumbnail(url, bitmap, score);
2042 }
[email protected]420ae012009-04-24 05:16:322043 }
2044}
2045
[email protected]0df30122009-06-03 12:13:082046void TabContents::UpdateInspectorSettings(const std::wstring& raw_settings) {
2047 profile()->GetPrefs()->SetString(prefs::kWebKitInspectorSettings,
2048 raw_settings);
2049}
2050
[email protected]420ae012009-04-24 05:16:322051void TabContents::Close(RenderViewHost* rvh) {
[email protected]24a4d1062009-07-10 23:10:422052 // If we close the tab while we're in the middle of a drag, we'll crash.
2053 // Instead, cancel the drag and close it as soon as the drag ends.
2054 if (view()->IsDoingDrag()) {
2055 view()->CancelDragAndCloseTab();
2056 return;
2057 }
2058
[email protected]420ae012009-04-24 05:16:322059 // Ignore this if it comes from a RenderViewHost that we aren't showing.
2060 if (delegate() && rvh == render_view_host())
2061 delegate()->CloseContents(this);
2062}
2063
2064void TabContents::RequestMove(const gfx::Rect& new_bounds) {
2065 if (delegate() && delegate()->IsPopup(this))
2066 delegate()->MoveContents(this, new_bounds);
2067}
2068
[email protected]329581b2009-04-28 06:52:352069void TabContents::DidStartLoading(RenderViewHost* rvh) {
[email protected]420ae012009-04-24 05:16:322070 SetIsLoading(true, NULL);
2071}
2072
[email protected]329581b2009-04-28 06:52:352073void TabContents::DidStopLoading(RenderViewHost* rvh) {
[email protected]420ae012009-04-24 05:16:322074 scoped_ptr<LoadNotificationDetails> details;
2075
2076 NavigationEntry* entry = controller_.GetActiveEntry();
2077 // An entry may not exist for a stop when loading an initial blank page or
2078 // if an iframe injected by script into a blank page finishes loading.
2079 if (entry) {
2080 scoped_ptr<base::ProcessMetrics> metrics(
2081 base::ProcessMetrics::CreateProcessMetrics(
2082 process()->process().handle()));
2083
2084 base::TimeDelta elapsed = base::TimeTicks::Now() - current_load_start_;
2085
2086 details.reset(new LoadNotificationDetails(
[email protected]ebe89e062009-08-13 23:16:542087 entry->virtual_url(),
[email protected]420ae012009-04-24 05:16:322088 entry->transition_type(),
2089 elapsed,
2090 &controller_,
2091 controller_.GetCurrentEntryIndex()));
2092 }
2093
2094 // Tell PasswordManager we've finished a page load, which serves as a
2095 // green light to save pending passwords and reset itself.
2096 GetPasswordManager()->DidStopLoading();
2097
2098 SetIsLoading(false, details.get());
2099}
2100
[email protected]420ae012009-04-24 05:16:322101void TabContents::DidRedirectProvisionalLoad(int32 page_id,
2102 const GURL& source_url,
2103 const GURL& target_url) {
2104 NavigationEntry* entry;
2105 if (page_id == -1)
2106 entry = controller_.pending_entry();
2107 else
2108 entry = controller_.GetEntryWithPageID(GetSiteInstance(), page_id);
2109 if (!entry || entry->url() != source_url)
2110 return;
2111 entry->set_url(target_url);
2112}
2113
[email protected]420ae012009-04-24 05:16:322114void TabContents::RequestOpenURL(const GURL& url, const GURL& referrer,
2115 WindowOpenDisposition disposition) {
2116 if (render_manager_.dom_ui()) {
2117 // When we're a DOM UI, it will provide a page transition type for us (this
2118 // is so the new tab page can specify AUTO_BOOKMARK for automatically
2119 // generated suggestions).
2120 //
2121 // Note also that we hide the referrer for DOM UI pages. We don't really
[email protected]60e448982009-05-06 04:21:162122 // want web sites to see a referrer of "chrome://blah" (and some
2123 // chrome: URLs might have search terms or other stuff we don't want to
[email protected]420ae012009-04-24 05:16:322124 // send to the site), so we send no referrer.
2125 OpenURL(url, GURL(), disposition,
2126 render_manager_.dom_ui()->link_transition_type());
2127 } else {
2128 OpenURL(url, referrer, disposition, PageTransition::LINK);
2129 }
2130}
2131
[email protected]1ee614862009-07-06 20:29:182132void TabContents::DomOperationResponse(const std::string& json_string,
2133 int automation_id) {
2134}
2135
[email protected]420ae012009-04-24 05:16:322136void TabContents::ProcessDOMUIMessage(const std::string& message,
[email protected]811bfe32009-07-01 08:46:252137 const std::string& content,
2138 int request_id,
2139 bool has_callback) {
[email protected]420ae012009-04-24 05:16:322140 if (!render_manager_.dom_ui()) {
[email protected]c2e74fe82009-09-03 17:57:442141 // This can happen if someone uses window.open() to open an extension URL
2142 // from a non-extension context.
2143 render_view_host()->BlockExtensionRequest(request_id);
[email protected]420ae012009-04-24 05:16:322144 return;
2145 }
[email protected]811bfe32009-07-01 08:46:252146 render_manager_.dom_ui()->ProcessDOMUIMessage(message, content, request_id,
2147 has_callback);
[email protected]420ae012009-04-24 05:16:322148}
2149
2150void TabContents::ProcessExternalHostMessage(const std::string& message,
2151 const std::string& origin,
2152 const std::string& target) {
2153 if (delegate())
2154 delegate()->ForwardMessageToExternalHost(message, origin, target);
2155}
2156
[email protected]420ae012009-04-24 05:16:322157void TabContents::RunFileChooser(bool multiple_files,
2158 const string16& title,
2159 const FilePath& default_file) {
2160 if (!select_file_dialog_.get())
2161 select_file_dialog_ = SelectFileDialog::Create(this);
2162 SelectFileDialog::Type dialog_type =
2163 multiple_files ? SelectFileDialog::SELECT_OPEN_MULTI_FILE :
2164 SelectFileDialog::SELECT_OPEN_FILE;
2165 select_file_dialog_->SelectFile(dialog_type, title, default_file,
2166 NULL, 0, FILE_PATH_LITERAL(""),
2167 view_->GetTopLevelNativeWindow(), NULL);
2168}
2169
2170void TabContents::RunJavaScriptMessage(
2171 const std::wstring& message,
2172 const std::wstring& default_prompt,
2173 const GURL& frame_url,
2174 const int flags,
2175 IPC::Message* reply_msg,
2176 bool* did_suppress_message) {
2177 // Suppress javascript messages when requested and when inside a constrained
2178 // popup window (because that activates them and breaks them out of the
2179 // constrained window jail).
[email protected]91ed49a2009-07-07 21:29:552180 // Also suppress messages when showing an interstitial. The interstitial is
2181 // shown over the previous page, we don't want the hidden page dialogs to
2182 // interfere with the interstitial.
2183 bool suppress_this_message = suppress_javascript_messages_ ||
2184 showing_interstitial_page();
[email protected]420ae012009-04-24 05:16:322185 if (delegate())
2186 suppress_this_message |=
[email protected]b6c874582009-05-08 19:38:312187 (delegate()->GetConstrainingContents(this) != this);
[email protected]420ae012009-04-24 05:16:322188
2189 *did_suppress_message = suppress_this_message;
2190
2191 if (!suppress_this_message) {
2192 base::TimeDelta time_since_last_message(
2193 base::TimeTicks::Now() - last_javascript_message_dismissal_);
2194 bool show_suppress_checkbox = false;
2195 // Show a checkbox offering to suppress further messages if this message is
2196 // being displayed within kJavascriptMessageExpectedDelay of the last one.
2197 if (time_since_last_message <
2198 base::TimeDelta::FromMilliseconds(kJavascriptMessageExpectedDelay))
2199 show_suppress_checkbox = true;
2200
[email protected]d82ed61e2009-06-16 02:46:222201 RunJavascriptMessageBox(this, frame_url, flags, message, default_prompt,
[email protected]420ae012009-04-24 05:16:322202 show_suppress_checkbox, reply_msg);
2203 } else {
2204 // If we are suppressing messages, just reply as is if the user immediately
2205 // pressed "Cancel".
2206 OnJavaScriptMessageBoxClosed(reply_msg, false, std::wstring());
2207 }
2208}
2209
2210void TabContents::RunBeforeUnloadConfirm(const std::wstring& message,
2211 IPC::Message* reply_msg) {
[email protected]d82ed61e2009-06-16 02:46:222212 RunBeforeUnloadDialog(this, message, reply_msg);
[email protected]420ae012009-04-24 05:16:322213}
2214
2215void TabContents::ShowModalHTMLDialog(const GURL& url, int width, int height,
2216 const std::string& json_arguments,
2217 IPC::Message* reply_msg) {
2218 if (delegate()) {
2219 HtmlDialogUIDelegate* dialog_delegate =
2220 new ModalHtmlDialogDelegate(url, width, height, json_arguments,
[email protected]d82ed61e2009-06-16 02:46:222221 reply_msg, this);
[email protected]420ae012009-04-24 05:16:322222 delegate()->ShowHtmlDialog(dialog_delegate, NULL);
2223 }
2224}
2225
2226void TabContents::PasswordFormsSeen(
[email protected]ed3fb032009-06-16 19:50:562227 const std::vector<webkit_glue::PasswordForm>& forms) {
[email protected]420ae012009-04-24 05:16:322228 GetPasswordManager()->PasswordFormsSeen(forms);
2229}
2230
[email protected]420ae012009-04-24 05:16:322231// Checks to see if we should generate a keyword based on the OSDD, and if
2232// necessary uses TemplateURLFetcher to download the OSDD and create a keyword.
2233void TabContents::PageHasOSDD(RenderViewHost* render_view_host,
2234 int32 page_id, const GURL& url,
2235 bool autodetected) {
2236 // Make sure page_id is the current page, and the TemplateURLModel is loaded.
2237 DCHECK(url.is_valid());
2238 if (!IsActiveEntry(page_id))
2239 return;
2240 TemplateURLModel* url_model = profile()->GetTemplateURLModel();
2241 if (!url_model)
2242 return;
2243 if (!url_model->loaded()) {
2244 url_model->Load();
2245 return;
2246 }
2247 if (!profile()->GetTemplateURLFetcher())
2248 return;
2249
2250 if (profile()->IsOffTheRecord())
2251 return;
2252
2253 const NavigationEntry* entry = controller_.GetLastCommittedEntry();
2254 DCHECK(entry);
2255
2256 const NavigationEntry* base_entry = entry;
2257 if (IsFormSubmit(base_entry)) {
2258 // If the current page is a form submit, find the last page that was not
2259 // a form submit and use its url to generate the keyword from.
2260 int index = controller_.last_committed_entry_index() - 1;
2261 while (index >= 0 && IsFormSubmit(controller_.GetEntryAtIndex(index)))
2262 index--;
2263 if (index >= 0)
2264 base_entry = controller_.GetEntryAtIndex(index);
2265 else
2266 base_entry = NULL;
2267 }
2268
2269 // We want to use the user typed URL if available since that represents what
2270 // the user typed to get here, and fall back on the regular URL if not.
2271 if (!base_entry)
2272 return;
2273 GURL keyword_url = base_entry->user_typed_url().is_valid() ?
2274 base_entry->user_typed_url() : base_entry->url();
2275 if (!keyword_url.is_valid())
2276 return;
2277 std::wstring keyword = TemplateURLModel::GenerateKeyword(keyword_url,
2278 autodetected);
2279 if (keyword.empty())
2280 return;
2281 const TemplateURL* template_url =
2282 url_model->GetTemplateURLForKeyword(keyword);
2283 if (template_url && (!template_url->safe_for_autoreplace() ||
2284 template_url->originating_url() == url)) {
2285 // Either there is a user created TemplateURL for this keyword, or the
2286 // keyword has the same OSDD url and we've parsed it.
2287 return;
2288 }
2289
2290 // Download the OpenSearch description document. If this is successful a
2291 // new keyword will be created when done.
[email protected]420ae012009-04-24 05:16:322292 profile()->GetTemplateURLFetcher()->ScheduleDownload(
2293 keyword,
2294 url,
2295 base_entry->favicon().url(),
[email protected]d0cc9fb52009-06-23 02:26:162296 this,
[email protected]420ae012009-04-24 05:16:322297 autodetected);
2298}
2299
[email protected]420ae012009-04-24 05:16:322300GURL TabContents::GetAlternateErrorPageURL() const {
2301 GURL url;
2302 // Disable alternate error pages when in OffTheRecord/Incognito mode.
2303 if (profile()->IsOffTheRecord())
2304 return url;
2305
2306 PrefService* prefs = profile()->GetPrefs();
2307 DCHECK(prefs);
2308 if (prefs->GetBoolean(prefs::kAlternateErrorPagesEnabled)) {
[email protected]2bd6ca1c2009-08-27 16:42:202309 url = google_util::AppendGoogleLocaleParam(GURL(kLinkDoctorBaseURL));
[email protected]420ae012009-04-24 05:16:322310 url = google_util::AppendGoogleTLDParam(url);
2311 }
2312 return url;
2313}
2314
2315WebPreferences TabContents::GetWebkitPrefs() {
2316 PrefService* prefs = render_view_host()->process()->profile()->GetPrefs();
[email protected]7ccddb8c2009-08-04 17:36:552317 bool is_dom_ui = false;
2318 return RenderViewHostDelegateHelper::GetWebkitPrefs(prefs, is_dom_ui);
[email protected]420ae012009-04-24 05:16:322319}
2320
[email protected]420ae012009-04-24 05:16:322321void TabContents::OnJSOutOfMemory() {
2322 AddInfoBar(new SimpleAlertInfoBarDelegate(
2323 this, l10n_util::GetString(IDS_JS_OUT_OF_MEMORY_PROMPT), NULL));
2324}
2325
[email protected]420ae012009-04-24 05:16:322326void TabContents::OnCrossSiteResponse(int new_render_process_host_id,
2327 int new_request_id) {
[email protected]57c6a652009-05-04 07:58:342328 // Allows the TabContents to react when a cross-site response is ready to be
[email protected]420ae012009-04-24 05:16:322329 // delivered to a pending RenderViewHost. We must first run the onunload
2330 // handler of the old RenderViewHost before we can allow it to proceed.
2331 render_manager_.OnCrossSiteResponse(new_render_process_host_id,
2332 new_request_id);
2333}
2334
2335bool TabContents::CanBlur() const {
2336 return delegate() ? delegate()->CanBlur() : true;
2337}
2338
2339gfx::Rect TabContents::GetRootWindowResizerRect() const {
2340 if (delegate())
2341 return delegate()->GetRootWindowResizerRect();
2342 return gfx::Rect();
2343}
2344
2345void TabContents::RendererUnresponsive(RenderViewHost* rvh,
2346 bool is_during_unload) {
2347 if (is_during_unload) {
2348 // Hang occurred while firing the beforeunload/unload handler.
2349 // Pretend the handler fired so tab closing continues as if it had.
[email protected]829e7612009-04-25 01:15:112350 rvh->set_sudden_termination_allowed(true);
[email protected]420ae012009-04-24 05:16:322351
2352 if (!render_manager_.ShouldCloseTabOnUnresponsiveRenderer())
2353 return;
2354
2355 // If the tab hangs in the beforeunload/unload handler there's really
2356 // nothing we can do to recover. Pretend the unload listeners have
2357 // all fired and close the tab. If the hang is in the beforeunload handler
2358 // then the user will not have the option of cancelling the close.
2359 Close(rvh);
2360 return;
2361 }
2362
2363 if (render_view_host() && render_view_host()->IsRenderViewLive())
[email protected]d82ed61e2009-06-16 02:46:222364 HungRendererDialog::ShowForTabContents(this);
[email protected]420ae012009-04-24 05:16:322365}
2366
2367void TabContents::RendererResponsive(RenderViewHost* render_view_host) {
[email protected]d82ed61e2009-06-16 02:46:222368 HungRendererDialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:322369}
2370
2371void TabContents::LoadStateChanged(const GURL& url,
2372 net::LoadState load_state) {
2373 load_state_ = load_state;
[email protected]d686e812009-06-03 19:10:292374 std::wstring languages =
2375 profile()->GetPrefs()->GetString(prefs::kAcceptLanguages);
2376 load_state_host_.clear();
2377 std::string host = url.host();
2378 net::IDNToUnicode(host.c_str(), host.size(), languages, &load_state_host_);
[email protected]420ae012009-04-24 05:16:322379 if (load_state_ == net::LOAD_STATE_READING_RESPONSE)
2380 SetNotWaitingForResponse();
2381 if (is_loading())
[email protected]c9cd2222009-05-06 05:16:502382 NotifyNavigationStateChanged(INVALIDATE_LOAD | INVALIDATE_TAB);
[email protected]420ae012009-04-24 05:16:322383}
2384
[email protected]1ee614862009-07-06 20:29:182385bool TabContents::IsExternalTabContainer() const {
2386 if (!delegate())
2387 return false;
2388
2389 return delegate()->IsExternalTabContainer();
2390}
2391
[email protected]1ee614862009-07-06 20:29:182392void TabContents::DidInsertCSS() {
2393 // This RVHDelegate function is used for extensions and not us.
[email protected]420ae012009-04-24 05:16:322394}
2395
2396void TabContents::FileSelected(const FilePath& path,
2397 int index, void* params) {
2398 render_view_host()->FileSelected(path);
2399}
2400
2401void TabContents::MultiFilesSelected(const std::vector<FilePath>& files,
2402 void* params) {
2403 render_view_host()->MultiFilesSelected(files);
2404}
2405
2406void TabContents::FileSelectionCanceled(void* params) {
2407 // If the user cancels choosing a file to upload we pass back an
2408 // empty vector.
2409 render_view_host()->MultiFilesSelected(std::vector<FilePath>());
2410}
2411
2412void TabContents::BeforeUnloadFiredFromRenderManager(
2413 bool proceed,
2414 bool* proceed_to_fire_unload) {
2415 if (delegate())
2416 delegate()->BeforeUnloadFired(this, proceed, proceed_to_fire_unload);
2417}
2418
2419void TabContents::UpdateRenderViewSizeForRenderManager() {
2420 // TODO(brettw) this is a hack. See TabContentsView::SizeContents.
2421 view_->SizeContents(view_->GetContainerSize());
2422}
2423
2424DOMUI* TabContents::CreateDOMUIForRenderManager(const GURL& url) {
[email protected]d82ed61e2009-06-16 02:46:222425 return DOMUIFactory::CreateDOMUIForURL(this, url);
[email protected]420ae012009-04-24 05:16:322426}
2427
2428NavigationEntry*
2429TabContents::GetLastCommittedNavigationEntryForRenderManager() {
2430 return controller_.GetLastCommittedEntry();
2431}
2432
2433bool TabContents::CreateRenderViewForRenderManager(
2434 RenderViewHost* render_view_host) {
[email protected]811bfe32009-07-01 08:46:252435 // If the pending navigation is to a DOMUI, tell the RenderView about any
2436 // bindings it will need enabled.
[email protected]420ae012009-04-24 05:16:322437 if (render_manager_.pending_dom_ui())
[email protected]811bfe32009-07-01 08:46:252438 render_view_host->AllowBindings(
2439 render_manager_.pending_dom_ui()->bindings());
[email protected]1adff062009-06-02 18:39:552440
[email protected]420ae012009-04-24 05:16:322441 RenderWidgetHostView* rwh_view = view_->CreateViewForWidget(render_view_host);
2442 if (!render_view_host->CreateRenderView())
2443 return false;
2444
2445 // Now that the RenderView has been created, we need to tell it its size.
2446 rwh_view->SetSize(view_->GetContainerSize());
2447
2448 UpdateMaxPageIDIfNecessary(render_view_host->site_instance(),
2449 render_view_host);
2450 return true;
2451}
2452
2453void TabContents::Observe(NotificationType type,
2454 const NotificationSource& source,
2455 const NotificationDetails& details) {
2456 switch (type.value) {
2457 case NotificationType::BOOKMARK_MODEL_LOADED:
2458 // BookmarkModel finished loading, fall through to update starred state.
2459 case NotificationType::URLS_STARRED: {
2460 // Somewhere, a URL has been starred.
2461 // Ignore notifications for profiles other than our current one.
2462 Profile* source_profile = Source<Profile>(source).ptr();
2463 if (!source_profile || !source_profile->IsSameProfile(profile()))
2464 return;
2465
2466 UpdateStarredStateForCurrentURL();
2467 break;
2468 }
2469 case NotificationType::PREF_CHANGED: {
2470 std::wstring* pref_name_in = Details<std::wstring>(details).ptr();
2471 DCHECK(Source<PrefService>(source).ptr() == profile()->GetPrefs());
2472 if (*pref_name_in == prefs::kAlternateErrorPagesEnabled) {
2473 UpdateAlternateErrorPageURL();
2474 } else if (*pref_name_in == prefs::kDefaultCharset ||
2475 StartsWithASCII(WideToUTF8(*pref_name_in), "webkit.webprefs.", true)
2476 ) {
2477 UpdateWebPreferences();
2478 } else {
2479 NOTREACHED() << "unexpected pref change notification" << *pref_name_in;
2480 }
2481 break;
2482 }
2483 case NotificationType::RENDER_WIDGET_HOST_DESTROYED:
2484 view_->RenderWidgetHostDestroyed(Source<RenderWidgetHost>(source).ptr());
2485 break;
2486
2487 case NotificationType::NAV_ENTRY_COMMITTED: {
2488 DCHECK(&controller_ == Source<NavigationController>(source).ptr());
2489
2490 NavigationController::LoadCommittedDetails& committed_details =
2491 *(Details<NavigationController::LoadCommittedDetails>(details).ptr());
2492 ExpireInfoBars(committed_details);
2493 break;
2494 }
2495
[email protected]d3ba77272009-09-03 00:06:092496#if defined(OS_LINUX)
2497 case NotificationType::BROWSER_THEME_CHANGED: {
2498 render_view_host()->SyncRendererPrefs();
2499 break;
2500 }
2501#endif
2502
[email protected]420ae012009-04-24 05:16:322503 default:
2504 NOTREACHED();
2505 }
2506}
[email protected]aacd7cc2009-09-02 14:51:102507
[email protected]41fc0322009-09-04 22:23:402508void TabContents::set_encoding(const std::string& encoding) {
[email protected]aacd7cc2009-09-02 14:51:102509 encoding_ = CharacterEncoding::GetCanonicalEncodingNameByAliasName(encoding);
2510}
2511