blob: 2483d47bf028b747d02fad9ba3daa002d6550126 [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]420ae012009-04-24 05:16:32139// The list of prefs we want to observe.
140const wchar_t* kPrefsToObserve[] = {
141 prefs::kAlternateErrorPagesEnabled,
142 prefs::kWebKitJavaEnabled,
143 prefs::kWebKitJavascriptEnabled,
144 prefs::kWebKitLoadsImagesAutomatically,
145 prefs::kWebKitPluginsEnabled,
146 prefs::kWebKitUsesUniversalDetector,
147 prefs::kWebKitSerifFontFamily,
148 prefs::kWebKitSansSerifFontFamily,
149 prefs::kWebKitFixedFontFamily,
150 prefs::kWebKitDefaultFontSize,
151 prefs::kWebKitDefaultFixedFontSize,
152 prefs::kDefaultCharset
153 // kWebKitStandardFontIsSerif needs to be added
154 // if we let users pick which font to use, serif or sans-serif when
155 // no font is specified or a CSS generic family (serif or sans-serif)
156 // is not specified.
157};
158
159const int kPrefsToObserveLength = arraysize(kPrefsToObserve);
160
[email protected]420ae012009-04-24 05:16:32161// Returns true if the entry's transition type is FORM_SUBMIT.
162bool IsFormSubmit(const NavigationEntry* entry) {
163 return (PageTransition::StripQualifier(entry->transition_type()) ==
164 PageTransition::FORM_SUBMIT);
165}
166
167#if defined(OS_WIN)
168
169BOOL CALLBACK InvalidateWindow(HWND hwnd, LPARAM lparam) {
170 // Note: erase is required to properly paint some widgets borders. This can
171 // be seen with textfields.
172 InvalidateRect(hwnd, NULL, TRUE);
173 return TRUE;
174}
175#endif
176
[email protected]056de2d2009-06-26 16:41:34177void MakeNavigateParams(const NavigationEntry& entry, bool reload,
178 ViewMsg_Navigate_Params* params) {
179 params->page_id = entry.page_id();
180 params->url = entry.url();
181 params->referrer = entry.referrer();
182 params->transition = entry.transition_type();
183 params->state = entry.content_state();
184 params->reload = reload;
185 params->request_time = base::Time::Now();
186}
187
[email protected]420ae012009-04-24 05:16:32188} // namespace
189
190// -----------------------------------------------------------------------------
191
[email protected]fdd61c62009-04-22 19:22:57192// static
193int TabContents::find_request_id_counter_ = -1;
194
[email protected]420ae012009-04-24 05:16:32195class TabContents::GearsCreateShortcutCallbackFunctor {
196 public:
197 explicit GearsCreateShortcutCallbackFunctor(TabContents* contents)
198 : contents_(contents) {}
199
200 void Run(const GearsShortcutData2& shortcut_data, bool success) {
201 if (contents_)
202 contents_->OnGearsCreateShortcutDone(shortcut_data, success);
203 delete this;
204 }
205 void Cancel() {
206 contents_ = NULL;
207 }
208
209 private:
210 TabContents* contents_;
211};
212
[email protected]420ae012009-04-24 05:16:32213TabContents::TabContents(Profile* profile,
214 SiteInstance* site_instance,
215 int routing_id,
216 base::WaitableEvent* modal_dialog_event)
[email protected]b680ad22009-04-15 23:19:42217 : delegate_(NULL),
[email protected]66ba4932009-06-04 19:22:13218 ALLOW_THIS_IN_INITIALIZER_LIST(controller_(this, profile)),
219 ALLOW_THIS_IN_INITIALIZER_LIST(view_(
[email protected]d82ed61e2009-06-16 02:46:22220 TabContentsView::Create(this))),
221 ALLOW_THIS_IN_INITIALIZER_LIST(render_manager_(this, this)),
[email protected]fdd61c62009-04-22 19:22:57222 property_bag_(),
223 registrar_(),
[email protected]d82ed61e2009-06-16 02:46:22224 ALLOW_THIS_IN_INITIALIZER_LIST(printing_(*this)),
[email protected]fdd61c62009-04-22 19:22:57225 save_package_(),
226 cancelable_consumer_(),
227 autofill_manager_(),
228 password_manager_(),
229 plugin_installer_(),
[email protected]d82ed61e2009-06-16 02:46:22230 ALLOW_THIS_IN_INITIALIZER_LIST(fav_icon_helper_(this)),
[email protected]fdd61c62009-04-22 19:22:57231 select_file_dialog_(),
232 pending_install_(),
[email protected]d5f942ba2008-09-26 19:30:34233 is_loading_(false),
initial.commit09911bf2008-07-26 23:55:29234 is_crashed_(false),
[email protected]d5f942ba2008-09-26 19:30:34235 waiting_for_response_(false),
[email protected]d5f942ba2008-09-26 19:30:34236 max_page_id_(-1),
[email protected]fdd61c62009-04-22 19:22:57237 current_load_start_(),
238 load_state_(net::LOAD_STATE_IDLE),
239 load_state_host_(),
[email protected]094e5b22009-09-25 04:23:56240 upload_size_(0),
241 upload_position_(0),
[email protected]fdd61c62009-04-22 19:22:57242 received_page_title_(false),
243 is_starred_(false),
244 contents_mime_type_(),
245 encoding_(),
[email protected]332af7732009-03-11 13:21:35246 blocked_popups_(NULL),
[email protected]fdd61c62009-04-22 19:22:57247 infobar_delegates_(),
[email protected]fdd61c62009-04-22 19:22:57248 find_ui_active_(false),
249 find_op_aborted_(false),
250 current_find_request_id_(find_request_id_counter_++),
251 find_text_(),
[email protected]e491f1c2009-05-22 20:28:12252 last_search_case_sensitive_(false),
253 last_search_prepopulate_text_(NULL),
254 last_search_result_(),
[email protected]fdd61c62009-04-22 19:22:57255 capturing_contents_(false),
256 is_being_destroyed_(false),
257 notify_disconnection_(false),
258 history_requests_(),
259#if defined(OS_WIN)
260 message_box_active_(CreateEvent(NULL, TRUE, FALSE, NULL)),
261#endif
262 last_javascript_message_dismissal_(),
[email protected]c2e74fe82009-09-03 17:57:44263 suppress_javascript_messages_(false),
264 opener_dom_ui_type_(DOMUIFactory::kNoDOMUI) {
[email protected]420ae012009-04-24 05:16:32265 pending_install_.page_id = 0;
266 pending_install_.callback_functor = NULL;
267
[email protected]16779842009-07-08 23:45:29268#if defined(OS_CHROMEOS)
[email protected]58dca552009-06-17 00:35:02269 // Make sure the thumbnailer is started before starting the render manager.
270 // The thumbnailer will want to listen for RVH creations, one of which will
271 // happen in RVHManager::Init.
272 ThumbnailGenerator* generator = g_browser_process->GetThumbnailGenerator();
273 if (generator)
274 generator->StartThumbnailing();
275#endif
276
[email protected]420ae012009-04-24 05:16:32277 render_manager_.Init(profile, site_instance, routing_id, modal_dialog_event);
278
279 view_->CreateView();
280
281 // Register for notifications about all interested prefs change.
282 PrefService* prefs = profile->GetPrefs();
283 if (prefs) {
284 for (int i = 0; i < kPrefsToObserveLength; ++i)
285 prefs->AddPrefObserver(kPrefsToObserve[i], this);
286 }
287
288 // Register for notifications about URL starredness changing on any profile.
289 registrar_.Add(this, NotificationType::URLS_STARRED,
290 NotificationService::AllSources());
291 registrar_.Add(this, NotificationType::BOOKMARK_MODEL_LOADED,
292 NotificationService::AllSources());
293 registrar_.Add(this, NotificationType::RENDER_WIDGET_HOST_DESTROYED,
294 NotificationService::AllSources());
[email protected]d3ba77272009-09-03 00:06:09295#if defined(OS_LINUX)
296 registrar_.Add(this, NotificationType::BROWSER_THEME_CHANGED,
297 NotificationService::AllSources());
298#endif
[email protected]420ae012009-04-24 05:16:32299
300 // Keep a global copy of the previous search string (if any).
301 static string16 global_last_search = string16();
[email protected]e491f1c2009-05-22 20:28:12302 last_search_prepopulate_text_ = &global_last_search;
[email protected]840b1512009-07-21 16:53:46303
[email protected]c8865482009-07-23 20:40:10304 // Set-up the showing of the omnibox search infobar if applicable.
305 if (OmniboxSearchHint::IsEnabled(profile))
306 omnibox_search_hint_.reset(new OmniboxSearchHint(this));
307
[email protected]840b1512009-07-21 16:53:46308 view_->InitRendererPrefs(&renderer_preferences_);
[email protected]332af7732009-03-11 13:21:35309}
initial.commit09911bf2008-07-26 23:55:29310
311TabContents::~TabContents() {
[email protected]420ae012009-04-24 05:16:32312 is_being_destroyed_ = true;
313
314 // We don't want any notifications while we're runnign our destructor.
315 registrar_.RemoveAll();
316
317 // Unregister the notifications of all observed prefs change.
318 PrefService* prefs = profile()->GetPrefs();
319 if (prefs) {
320 for (int i = 0; i < kPrefsToObserveLength; ++i)
321 prefs->RemovePrefObserver(kPrefsToObserve[i], this);
322 }
323
324 // Clean up subwindows like plugins and the find in page bar.
325 view_->OnContentsDestroy();
326
327 NotifyDisconnected();
[email protected]d82ed61e2009-06-16 02:46:22328 HungRendererDialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:32329
330 if (pending_install_.callback_functor)
331 pending_install_.callback_functor->Cancel();
332
333 // First cleanly close all child windows.
334 // TODO(mpcomplete): handle case if MaybeCloseChildWindows() already asked
335 // some of these to close. CloseWindows is async, so it might get called
336 // twice before it runs.
337 int size = static_cast<int>(child_windows_.size());
338 for (int i = size - 1; i >= 0; --i) {
339 ConstrainedWindow* window = child_windows_[i];
340 if (window)
341 window->CloseConstrainedWindow();
342 }
343
[email protected]66ba4932009-06-04 19:22:13344 if (blocked_popups_)
345 blocked_popups_->Destroy();
346
[email protected]420ae012009-04-24 05:16:32347 // Notify any observer that have a reference on this tab contents.
348 NotificationService::current()->Notify(
349 NotificationType::TAB_CONTENTS_DESTROYED,
350 Source<TabContents>(this),
351 NotificationService::NoDetails());
352
[email protected]1e5e74f2009-05-27 20:55:12353 // Notify any lasting InfobarDelegates that have not yet been removed that
354 // whatever infobar they were handling in this TabContents has closed,
355 // because the TabContents is going away entirely.
356 // This must happen after the TAB_CONTENTS_DESTROYED notification as the
357 // notification may trigger infobars calls that access their delegate. (and
358 // some implementations of InfoBarDelegate do delete themselves on
359 // InfoBarClosed()).
360 for (int i = 0; i < infobar_delegate_count(); ++i) {
361 InfoBarDelegate* delegate = GetInfoBarDelegateAt(i);
362 delegate->InfoBarClosed();
363 }
364 infobar_delegates_.clear();
365
[email protected]420ae012009-04-24 05:16:32366 // TODO(brettw) this should be moved to the view.
367#if defined(OS_WIN)
368 // If we still have a window handle, destroy it. GetNativeView can return
369 // NULL if this contents was part of a window that closed.
370 if (GetNativeView())
371 ::DestroyWindow(GetNativeView());
372#endif
initial.commit09911bf2008-07-26 23:55:29373}
374
[email protected]d5f942ba2008-09-26 19:30:34375// static
376void TabContents::RegisterUserPrefs(PrefService* prefs) {
[email protected]420ae012009-04-24 05:16:32377 prefs->RegisterBooleanPref(prefs::kAlternateErrorPagesEnabled, true);
378
379 WebPreferences pref_defaults;
380 prefs->RegisterBooleanPref(prefs::kWebKitJavascriptEnabled,
381 pref_defaults.javascript_enabled);
382 prefs->RegisterBooleanPref(prefs::kWebKitWebSecurityEnabled,
383 pref_defaults.web_security_enabled);
384 prefs->RegisterBooleanPref(
385 prefs::kWebKitJavascriptCanOpenWindowsAutomatically, true);
386 prefs->RegisterBooleanPref(prefs::kWebKitLoadsImagesAutomatically,
387 pref_defaults.loads_images_automatically);
388 prefs->RegisterBooleanPref(prefs::kWebKitPluginsEnabled,
389 pref_defaults.plugins_enabled);
390 prefs->RegisterBooleanPref(prefs::kWebKitDomPasteEnabled,
391 pref_defaults.dom_paste_enabled);
392 prefs->RegisterBooleanPref(prefs::kWebKitShrinksStandaloneImagesToFit,
393 pref_defaults.shrinks_standalone_images_to_fit);
[email protected]0df30122009-06-03 12:13:08394 prefs->RegisterStringPref(prefs::kWebKitInspectorSettings,
395 pref_defaults.inspector_settings);
[email protected]420ae012009-04-24 05:16:32396 prefs->RegisterBooleanPref(prefs::kWebKitTextAreasAreResizable,
397 pref_defaults.text_areas_are_resizable);
398 prefs->RegisterBooleanPref(prefs::kWebKitJavaEnabled,
399 pref_defaults.java_enabled);
400
401 prefs->RegisterLocalizedStringPref(prefs::kAcceptLanguages,
402 IDS_ACCEPT_LANGUAGES);
403 prefs->RegisterLocalizedStringPref(prefs::kDefaultCharset,
404 IDS_DEFAULT_ENCODING);
405 prefs->RegisterLocalizedBooleanPref(prefs::kWebKitStandardFontIsSerif,
406 IDS_STANDARD_FONT_IS_SERIF);
407 prefs->RegisterLocalizedStringPref(prefs::kWebKitFixedFontFamily,
408 IDS_FIXED_FONT_FAMILY);
409 prefs->RegisterLocalizedStringPref(prefs::kWebKitSerifFontFamily,
410 IDS_SERIF_FONT_FAMILY);
411 prefs->RegisterLocalizedStringPref(prefs::kWebKitSansSerifFontFamily,
412 IDS_SANS_SERIF_FONT_FAMILY);
413 prefs->RegisterLocalizedStringPref(prefs::kWebKitCursiveFontFamily,
414 IDS_CURSIVE_FONT_FAMILY);
415 prefs->RegisterLocalizedStringPref(prefs::kWebKitFantasyFontFamily,
416 IDS_FANTASY_FONT_FAMILY);
417 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitDefaultFontSize,
418 IDS_DEFAULT_FONT_SIZE);
419 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitDefaultFixedFontSize,
420 IDS_DEFAULT_FIXED_FONT_SIZE);
421 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitMinimumFontSize,
422 IDS_MINIMUM_FONT_SIZE);
423 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitMinimumLogicalFontSize,
424 IDS_MINIMUM_LOGICAL_FONT_SIZE);
425 prefs->RegisterLocalizedBooleanPref(prefs::kWebKitUsesUniversalDetector,
426 IDS_USES_UNIVERSAL_DETECTOR);
427 prefs->RegisterLocalizedStringPref(prefs::kStaticEncodings,
428 IDS_STATIC_ENCODING_LIST);
initial.commit09911bf2008-07-26 23:55:29429}
430
[email protected]13367f72009-07-08 16:39:02431// Returns true if contains content rendered by an extension.
432bool TabContents::HostsExtension() const {
433 return GetURL().SchemeIs(chrome::kExtensionScheme);
[email protected]d5f942ba2008-09-26 19:30:34434}
435
[email protected]6e95706d2009-04-23 22:55:01436PasswordManager* TabContents::GetPasswordManager() {
437 if (password_manager_.get() == NULL)
438 password_manager_.reset(new PasswordManager(this));
439 return password_manager_.get();
440}
441
442PluginInstaller* TabContents::GetPluginInstaller() {
443 if (plugin_installer_.get() == NULL)
444 plugin_installer_.reset(new PluginInstaller(this));
445 return plugin_installer_.get();
446}
447
[email protected]d5f942ba2008-09-26 19:30:34448const GURL& TabContents::GetURL() const {
449 // We may not have a navigation entry yet
[email protected]ce3fa3c2009-04-20 19:55:57450 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]ebe89e062009-08-13 23:16:54451 return entry ? entry->virtual_url() : GURL::EmptyGURL();
[email protected]d5f942ba2008-09-26 19:30:34452}
453
[email protected]96d185d2009-04-24 03:28:54454const string16& TabContents::GetTitle() const {
[email protected]4c6092c5b2009-06-06 00:23:55455 // Transient entries take precedence. They are used for interstitial pages
456 // that are shown on top of existing pages.
457 NavigationEntry* entry = controller_.GetTransientEntry();
458 if (entry)
459 return entry->GetTitleForDisplay(&controller_);
460
[email protected]96d185d2009-04-24 03:28:54461 DOMUI* our_dom_ui = render_manager_.pending_dom_ui() ?
462 render_manager_.pending_dom_ui() : render_manager_.dom_ui();
463 if (our_dom_ui) {
464 // Don't override the title in view source mode.
[email protected]4c6092c5b2009-06-06 00:23:55465 entry = controller_.GetActiveEntry();
[email protected]96d185d2009-04-24 03:28:54466 if (!(entry && entry->IsViewSourceMode())) {
467 // Give the DOM UI the chance to override our title.
468 const string16& title = our_dom_ui->overridden_title();
469 if (!title.empty())
470 return title;
471 }
472 }
473
474 // We use the title for the last committed entry rather than a pending
475 // navigation entry. For example, when the user types in a URL, we want to
476 // keep the old page's title until the new load has committed and we get a new
477 // title.
[email protected]96d185d2009-04-24 03:28:54478 entry = controller_.GetLastCommittedEntry();
479 if (entry)
480 return entry->GetTitleForDisplay(&controller_);
[email protected]96d185d2009-04-24 03:28:54481 return EmptyString16();
482}
483
[email protected]d5f942ba2008-09-26 19:30:34484int32 TabContents::GetMaxPageID() {
485 if (GetSiteInstance())
486 return GetSiteInstance()->max_page_id();
487 else
488 return max_page_id_;
489}
490
491void TabContents::UpdateMaxPageID(int32 page_id) {
492 // Ensure both the SiteInstance and RenderProcessHost update their max page
[email protected]57c6a652009-05-04 07:58:34493 // IDs in sync. Only TabContents will also have site instances, except during
[email protected]d5f942ba2008-09-26 19:30:34494 // testing.
495 if (GetSiteInstance())
496 GetSiteInstance()->UpdateMaxPageID(page_id);
[email protected]57c6a652009-05-04 07:58:34497 process()->UpdateMaxPageID(page_id);
[email protected]d5f942ba2008-09-26 19:30:34498}
499
[email protected]96d185d2009-04-24 03:28:54500SiteInstance* TabContents::GetSiteInstance() const {
501 return render_manager_.current_host()->site_instance();
502}
503
[email protected]d5f942ba2008-09-26 19:30:34504const std::wstring TabContents::GetDefaultTitle() const {
505 return l10n_util::GetString(IDS_DEFAULT_TAB_TITLE);
506}
507
[email protected]96d185d2009-04-24 03:28:54508bool TabContents::ShouldDisplayURL() {
509 // Don't hide the url in view source mode.
510 NavigationEntry* entry = controller_.GetActiveEntry();
511 if (entry && entry->IsViewSourceMode())
512 return true;
[email protected]83e3895b2009-06-11 00:07:16513
[email protected]96d185d2009-04-24 03:28:54514 DOMUI* dom_ui = GetDOMUIForCurrentState();
515 if (dom_ui)
516 return !dom_ui->should_hide_url();
517 return true;
518}
519
[email protected]d5f942ba2008-09-26 19:30:34520SkBitmap TabContents::GetFavIcon() const {
521 // Like GetTitle(), we also want to use the favicon for the last committed
522 // entry rather than a pending navigation entry.
[email protected]ce3fa3c2009-04-20 19:55:57523 NavigationEntry* entry = controller_.GetTransientEntry();
[email protected]cbab76d2008-10-13 22:42:47524 if (entry)
525 return entry->favicon().bitmap();
526
[email protected]ce3fa3c2009-04-20 19:55:57527 entry = controller_.GetLastCommittedEntry();
[email protected]d5f942ba2008-09-26 19:30:34528 if (entry)
529 return entry->favicon().bitmap();
[email protected]d5f942ba2008-09-26 19:30:34530 return SkBitmap();
531}
532
[email protected]96d185d2009-04-24 03:28:54533bool TabContents::ShouldDisplayFavIcon() {
534 // Always display a throbber during pending loads.
535 if (controller_.GetLastCommittedEntry() && controller_.pending_entry())
536 return true;
537
538 DOMUI* dom_ui = GetDOMUIForCurrentState();
539 if (dom_ui)
540 return !dom_ui->hide_favicon();
541 return true;
542}
543
[email protected]96d185d2009-04-24 03:28:54544std::wstring TabContents::GetStatusText() const {
545 if (!is_loading() || load_state_ == net::LOAD_STATE_IDLE)
546 return std::wstring();
547
548 switch (load_state_) {
549 case net::LOAD_STATE_WAITING_FOR_CACHE:
550 return l10n_util::GetString(IDS_LOAD_STATE_WAITING_FOR_CACHE);
551 case net::LOAD_STATE_RESOLVING_PROXY_FOR_URL:
552 return l10n_util::GetString(IDS_LOAD_STATE_RESOLVING_PROXY_FOR_URL);
553 case net::LOAD_STATE_RESOLVING_HOST:
554 return l10n_util::GetString(IDS_LOAD_STATE_RESOLVING_HOST);
555 case net::LOAD_STATE_CONNECTING:
556 return l10n_util::GetString(IDS_LOAD_STATE_CONNECTING);
557 case net::LOAD_STATE_SENDING_REQUEST:
[email protected]094e5b22009-09-25 04:23:56558 if (upload_size_)
559 return l10n_util::GetStringF(
560 IDS_LOAD_STATE_SENDING_REQUEST_WITH_PROGRESS,
561 static_cast<int>((100 * upload_position_) / upload_size_));
562 else
563 return l10n_util::GetString(IDS_LOAD_STATE_SENDING_REQUEST);
[email protected]96d185d2009-04-24 03:28:54564 case net::LOAD_STATE_WAITING_FOR_RESPONSE:
565 return l10n_util::GetStringF(IDS_LOAD_STATE_WAITING_FOR_RESPONSE,
566 load_state_host_);
567 // Ignore net::LOAD_STATE_READING_RESPONSE and net::LOAD_STATE_IDLE
568 case net::LOAD_STATE_IDLE:
569 case net::LOAD_STATE_READING_RESPONSE:
570 break;
571 }
572
573 return std::wstring();
574}
575
[email protected]d5f942ba2008-09-26 19:30:34576void TabContents::SetIsCrashed(bool state) {
577 if (state == is_crashed_)
578 return;
579
580 is_crashed_ = state;
[email protected]c9cd2222009-05-06 05:16:50581 NotifyNavigationStateChanged(INVALIDATE_TAB);
[email protected]d5f942ba2008-09-26 19:30:34582}
583
[email protected]f9b1a7b2009-06-22 17:26:38584void TabContents::SetPageActionEnabled(const PageAction* page_action,
[email protected]d7eaf572009-07-01 21:57:00585 bool enable,
586 const std::string& title,
587 int icon_id) {
[email protected]ce5c4502009-05-06 16:46:11588 DCHECK(page_action);
589
[email protected]d7eaf572009-07-01 21:57:00590 if (!enable &&
591 enabled_page_actions_.end() == enabled_page_actions_.find(page_action)) {
592 return; // Don't need to disable twice.
593 }
[email protected]ce5c4502009-05-06 16:46:11594
[email protected]d7eaf572009-07-01 21:57:00595 if (enable) {
596 enabled_page_actions_[page_action].reset(
597 new PageActionState(title, icon_id));
598 } else {
[email protected]f9b1a7b2009-06-22 17:26:38599 enabled_page_actions_.erase(page_action);
[email protected]d7eaf572009-07-01 21:57:00600 }
[email protected]ce5c4502009-05-06 16:46:11601}
602
[email protected]d7eaf572009-07-01 21:57:00603const PageActionState* TabContents::GetPageActionState(
604 const PageAction* page_action) {
605 if (enabled_page_actions_.end() == enabled_page_actions_.find(page_action))
606 return NULL;
[email protected]ce5c4502009-05-06 16:46:11607
[email protected]d7eaf572009-07-01 21:57:00608 return enabled_page_actions_[page_action].get();
609}
[email protected]ce5c4502009-05-06 16:46:11610
[email protected]d5f942ba2008-09-26 19:30:34611void TabContents::NotifyNavigationStateChanged(unsigned changed_flags) {
612 if (delegate_)
613 delegate_->NavigationStateChanged(this, changed_flags);
614}
615
[email protected]96d185d2009-04-24 03:28:54616void TabContents::DidBecomeSelected() {
617 controller_.SetActive(true);
[email protected]789e9152009-08-04 21:59:43618 if (render_widget_host_view()) {
[email protected]96d185d2009-04-24 03:28:54619 render_widget_host_view()->DidBecomeSelected();
[email protected]789e9152009-08-04 21:59:43620#if defined(OS_MACOSX)
621 render_widget_host_view()->SetActive(true);
622#endif
623 }
[email protected]96d185d2009-04-24 03:28:54624
[email protected]76543b92009-08-31 17:27:45625 WebCacheManager::GetInstance()->ObserveActivity(process()->id());
[email protected]96d185d2009-04-24 03:28:54626}
627
628void TabContents::WasHidden() {
629 if (!capturing_contents()) {
630 // |render_view_host()| can be NULL if the user middle clicks a link to open
631 // a tab in then background, then closes the tab before selecting it. This
632 // is because closing the tab calls TabContents::Destroy(), which removes
633 // the |render_view_host()|; then when we actually destroy the window,
634 // OnWindowPosChanged() notices and calls HideContents() (which calls us).
635 if (render_widget_host_view())
636 render_widget_host_view()->WasHidden();
[email protected]96d185d2009-04-24 03:28:54637 }
638
639 NotificationService::current()->Notify(
640 NotificationType::TAB_CONTENTS_HIDDEN,
641 Source<TabContents>(this),
642 NotificationService::NoDetails());
643}
644
[email protected]d5f942ba2008-09-26 19:30:34645void TabContents::Activate() {
646 if (delegate_)
647 delegate_->ActivateContents(this);
648}
649
[email protected]96d185d2009-04-24 03:28:54650void TabContents::ShowContents() {
651 if (render_widget_host_view())
652 render_widget_host_view()->DidBecomeSelected();
[email protected]96d185d2009-04-24 03:28:54653}
654
655void TabContents::HideContents() {
656 // TODO(pkasting): http://b/1239839 Right now we purposefully don't call
657 // our superclass HideContents(), because some callers want to be very picky
658 // about the order in which these get called. In addition to making the code
659 // here practically impossible to understand, this also means we end up
660 // calling TabContents::WasHidden() twice if callers call both versions of
[email protected]57c6a652009-05-04 07:58:34661 // HideContents() on a TabContents.
[email protected]96d185d2009-04-24 03:28:54662 WasHidden();
663}
664
[email protected]c0588052008-10-27 23:01:50665void TabContents::OpenURL(const GURL& url, const GURL& referrer,
[email protected]d5f942ba2008-09-26 19:30:34666 WindowOpenDisposition disposition,
667 PageTransition::Type transition) {
668 if (delegate_)
[email protected]c0588052008-10-27 23:01:50669 delegate_->OpenURLFromTab(this, url, referrer, disposition, transition);
[email protected]d5f942ba2008-09-26 19:30:34670}
671
672bool TabContents::NavigateToPendingEntry(bool reload) {
[email protected]96d185d2009-04-24 03:28:54673 const NavigationEntry& entry = *controller_.pending_entry();
674
675 RenderViewHost* dest_render_view_host = render_manager_.Navigate(entry);
676 if (!dest_render_view_host)
677 return false; // Unable to create the desired render view host.
678
679 // Tell DevTools agent that it is attached prior to the navigation.
[email protected]d9f9b792009-06-24 13:17:12680 DevToolsManager* devtools_manager = DevToolsManager::GetInstance();
[email protected]7e8e6b62009-05-08 11:28:32681 if (devtools_manager) { // NULL in unit tests.
[email protected]684b4ba22009-05-07 06:27:41682 devtools_manager->OnNavigatingToPendingEntry(
683 render_view_host(),
684 dest_render_view_host,
685 controller_.pending_entry()->url());
[email protected]7e8e6b62009-05-08 11:28:32686 }
[email protected]96d185d2009-04-24 03:28:54687
688 // Used for page load time metrics.
689 current_load_start_ = base::TimeTicks::Now();
690
691 // Navigate in the desired RenderViewHost.
[email protected]056de2d2009-06-26 16:41:34692 ViewMsg_Navigate_Params navigate_params;
693 MakeNavigateParams(entry, reload, &navigate_params);
694 dest_render_view_host->Navigate(navigate_params);
[email protected]96d185d2009-04-24 03:28:54695
696 if (entry.page_id() == -1) {
697 // HACK!! This code suppresses javascript: URLs from being added to
698 // session history, which is what we want to do for javascript: URLs that
699 // do not generate content. What we really need is a message from the
700 // renderer telling us that a new page was not created. The same message
701 // could be used for mailto: URLs and the like.
702 if (entry.url().SchemeIs(chrome::kJavaScriptScheme))
703 return false;
704 }
705
706 // Clear any provisional password saves - this stops password infobars
707 // showing up on pages the user navigates to while the right page is
708 // loading.
709 GetPasswordManager()->ClearProvisionalSave();
710
711 if (reload && !profile()->IsOffTheRecord()) {
[email protected]0189bc722009-08-28 21:56:48712 FaviconService* favicon_service =
713 profile()->GetFaviconService(Profile::IMPLICIT_ACCESS);
714 if (favicon_service)
715 favicon_service->SetFaviconOutOfDateForPage(entry.url());
[email protected]96d185d2009-04-24 03:28:54716 }
717
[email protected]d5f942ba2008-09-26 19:30:34718 return true;
719}
720
[email protected]96d185d2009-04-24 03:28:54721void TabContents::Stop() {
722 render_manager_.Stop();
723 printing_.Stop();
724}
725
726void TabContents::Cut() {
727 render_view_host()->Cut();
728}
729
730void TabContents::Copy() {
731 render_view_host()->Copy();
732}
733
[email protected]c0cc3092009-09-12 08:27:27734#if defined(OS_MACOSX)
[email protected]a954bf72009-09-12 07:30:35735void TabContents::CopyToFindPboard() {
736 render_view_host()->CopyToFindPboard();
737}
[email protected]c0cc3092009-09-12 08:27:27738#endif
[email protected]a954bf72009-09-12 07:30:35739
[email protected]96d185d2009-04-24 03:28:54740void TabContents::Paste() {
741 render_view_host()->Paste();
742}
743
744void TabContents::DisassociateFromPopupCount() {
745 render_view_host()->DisassociateFromPopupCount();
746}
747
748TabContents* TabContents::Clone() {
749 // We create a new SiteInstance so that the new tab won't share processes
750 // with the old one. This can be changed in the future if we need it to share
751 // processes for some reason.
[email protected]420ae012009-04-24 05:16:32752 TabContents* tc = new TabContents(profile(),
[email protected]96d185d2009-04-24 03:28:54753 SiteInstance::CreateSiteInstance(profile()),
754 MSG_ROUTING_NONE, NULL);
755 tc->controller().CopyStateFrom(controller_);
756 return tc;
757}
758
[email protected]420ae012009-04-24 05:16:32759void TabContents::CreateShortcut() {
760 NavigationEntry* entry = controller_.GetLastCommittedEntry();
761 if (!entry)
762 return;
763
[email protected]70b8d6b2009-08-20 16:56:24764#if defined(OS_LINUX) && !defined(TOOLKIT_VIEWS)
[email protected]0ba5c5e2009-08-20 16:47:13765 CreateApplicationShortcutsDialogGtk::Show(view()->GetTopLevelNativeWindow(),
766 GetURL(), GetTitle());
[email protected]b96aa932009-08-12 21:34:49767#else
[email protected]420ae012009-04-24 05:16:32768 // We only allow one pending install request. By resetting the page id we
769 // effectively cancel the pending install request.
770 pending_install_.page_id = entry->page_id();
771 pending_install_.icon = GetFavIcon();
772 pending_install_.title = UTF16ToWideHack(GetTitle());
773 pending_install_.url = GetURL();
774 if (pending_install_.callback_functor) {
775 pending_install_.callback_functor->Cancel();
776 pending_install_.callback_functor = NULL;
777 }
778 DCHECK(!pending_install_.icon.isNull()) << "Menu item should be disabled.";
779 if (pending_install_.title.empty())
780 pending_install_.title = UTF8ToWide(GetURL().spec());
781
782 // Request the application info. When done OnDidGetApplicationInfo is invoked
783 // and we'll create the shortcut.
784 render_view_host()->GetApplicationInfo(pending_install_.page_id);
[email protected]b96aa932009-08-12 21:34:49785#endif
[email protected]420ae012009-04-24 05:16:32786}
787
[email protected]4d677202009-07-19 07:37:12788void TabContents::ShowPageInfo(const GURL& url,
789 const NavigationEntry::SSLStatus& ssl,
790 bool show_history) {
791 if (!delegate_)
792 return;
793
[email protected]bb678332009-07-21 00:15:50794 delegate_->ShowPageInfo(profile(), url, ssl, show_history);
[email protected]4d677202009-07-19 07:37:12795}
796
initial.commit09911bf2008-07-26 23:55:29797ConstrainedWindow* TabContents::CreateConstrainedDialog(
[email protected]e8382172009-06-19 22:16:28798 ConstrainedWindowDelegate* delegate) {
initial.commit09911bf2008-07-26 23:55:29799 ConstrainedWindow* window =
[email protected]e8382172009-06-19 22:16:28800 ConstrainedWindow::CreateConstrainedDialog(this, delegate);
initial.commit09911bf2008-07-26 23:55:29801 child_windows_.push_back(window);
802 return window;
803}
804
805void TabContents::AddNewContents(TabContents* new_contents,
806 WindowOpenDisposition disposition,
807 const gfx::Rect& initial_pos,
[email protected]c88a70fe2009-05-05 20:00:22808 bool user_gesture,
809 const GURL& creator_url) {
initial.commit09911bf2008-07-26 23:55:29810 if (!delegate_)
811 return;
812
[email protected]35f7d212009-04-29 21:19:27813 if ((disposition == NEW_POPUP) && !user_gesture &&
814 !CommandLine::ForCurrentProcess()->HasSwitch(
815 switches::kDisablePopupBlocking)) {
[email protected]2c4410d2009-05-06 23:46:22816 // Unrequested popups from normal pages are constrained unless they're in
[email protected]b6c874582009-05-08 19:38:31817 // the whitelist. The popup owner will handle checking this.
818 delegate_->GetConstrainingContents(this)->AddPopup(new_contents,
819 initial_pos,
820 creator_url.is_valid() ? creator_url.host() : std::string());
821 } else {
[email protected]304f8792009-08-19 21:28:09822#if defined(OS_CHROMEOS)
823 if (disposition == NEW_POPUP) {
824 // If the popup is bigger than a given factor of the screen, then
825 // turn it into a foreground tab (on chrome os only)
826 GdkScreen* screen = gdk_screen_get_default();
827 int max_width = gdk_screen_get_width(screen) * kMaxWidthFactor;
828 int max_height = gdk_screen_get_height(screen) * kMaxHeightFactor;
829 if (initial_pos.width() > max_width ||
830 initial_pos.height() > max_height) {
831 disposition = NEW_FOREGROUND_TAB;
832 }
833 }
834#endif
835
[email protected]0aa55312008-10-17 21:53:08836 new_contents->DisassociateFromPopupCount();
initial.commit09911bf2008-07-26 23:55:29837 delegate_->AddNewContents(this, new_contents, disposition, initial_pos,
838 user_gesture);
[email protected]7f37dd92009-09-15 16:35:49839 NotificationService::current()->Notify(
840 NotificationType::TAB_ADDED,
841 Source<TabContentsDelegate>(delegate_),
842 Details<TabContents>(this));
initial.commit09911bf2008-07-26 23:55:29843 }
[email protected]b6c874582009-05-08 19:38:31844 PopupNotificationVisibilityChanged(ShowingBlockedPopupNotification());
initial.commit09911bf2008-07-26 23:55:29845}
846
initial.commit09911bf2008-07-26 23:55:29847void TabContents::CloseAllSuppressedPopups() {
[email protected]d6598c052008-11-05 19:03:25848 if (blocked_popups_)
[email protected]2c4410d2009-05-06 23:46:22849 blocked_popups_->CloseAll();
initial.commit09911bf2008-07-26 23:55:29850}
851
[email protected]912256b32009-09-18 09:47:35852void TabContents::ExecuteCode(int request_id, const std::string& extension_id,
853 bool is_js_code, const std::string& code_string) {
854 render_view_host()->Send(
855 new ViewMsg_ExecuteCode(render_view_host()->routing_id(), request_id,
856 extension_id, is_js_code, code_string));
857}
858
[email protected]96d185d2009-04-24 03:28:54859void TabContents::PopupNotificationVisibilityChanged(bool visible) {
860 render_view_host()->PopupNotificationVisibilityChanged(visible);
861}
862
[email protected]5c9e97a2009-09-09 23:48:30863gfx::NativeView TabContents::GetContentNativeView() const {
[email protected]96d185d2009-04-24 03:28:54864 return view_->GetContentNativeView();
865}
866
867gfx::NativeView TabContents::GetNativeView() const {
868 return view_->GetNativeView();
869}
870
871void TabContents::GetContainerBounds(gfx::Rect *out) const {
872 view_->GetContainerBounds(out);
873}
874
875void TabContents::Focus() {
876 view_->Focus();
877}
878
[email protected]90daadb42009-06-08 21:27:28879void TabContents::FocusThroughTabTraversal(bool reverse) {
[email protected]7e383692009-06-12 19:14:54880 if (showing_interstitial_page()) {
881 render_manager_.interstitial_page()->FocusThroughTabTraversal(reverse);
882 return;
883 }
[email protected]96d185d2009-04-24 03:28:54884 render_view_host()->SetInitialFocus(reverse);
885}
886
887bool TabContents::FocusLocationBarByDefault() {
888 DOMUI* dom_ui = GetDOMUIForCurrentState();
889 if (dom_ui)
890 return dom_ui->focus_location_bar_by_default();
[email protected]3e3f0eb2009-06-22 18:33:43891 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]e0d481582009-09-15 21:06:25892 if (entry && entry->url() == GURL(chrome::kAboutBlankURL))
[email protected]3e3f0eb2009-06-22 18:33:43893 return true;
[email protected]96d185d2009-04-24 03:28:54894 return false;
895}
896
[email protected]616ed5a2008-11-21 22:27:24897void TabContents::AddInfoBar(InfoBarDelegate* delegate) {
898 // Look through the existing InfoBarDelegates we have for a match. If we've
899 // already got one that matches, then we don't add the new one.
[email protected]f86a07022008-11-25 01:06:05900 for (int i = 0; i < infobar_delegate_count(); ++i) {
[email protected]616ed5a2008-11-21 22:27:24901 if (GetInfoBarDelegateAt(i)->EqualsDelegate(delegate))
902 return;
903 }
904
905 infobar_delegates_.push_back(delegate);
[email protected]bfd04a62009-02-01 18:16:56906 NotificationService::current()->Notify(
907 NotificationType::TAB_CONTENTS_INFOBAR_ADDED,
908 Source<TabContents>(this),
909 Details<InfoBarDelegate>(delegate));
[email protected]616ed5a2008-11-21 22:27:24910
911 // Add ourselves as an observer for navigations the first time a delegate is
912 // added. We use this notification to expire InfoBars that need to expire on
913 // page transitions.
914 if (infobar_delegates_.size() == 1) {
[email protected]bfd04a62009-02-01 18:16:56915 registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED,
[email protected]ce3fa3c2009-04-20 19:55:57916 Source<NavigationController>(&controller_));
[email protected]616ed5a2008-11-21 22:27:24917 }
918}
919
920void TabContents::RemoveInfoBar(InfoBarDelegate* delegate) {
921 std::vector<InfoBarDelegate*>::iterator it =
922 find(infobar_delegates_.begin(), infobar_delegates_.end(), delegate);
923 if (it != infobar_delegates_.end()) {
924 InfoBarDelegate* delegate = *it;
[email protected]bfd04a62009-02-01 18:16:56925 NotificationService::current()->Notify(
926 NotificationType::TAB_CONTENTS_INFOBAR_REMOVED,
927 Source<TabContents>(this),
928 Details<InfoBarDelegate>(delegate));
[email protected]f86a07022008-11-25 01:06:05929 infobar_delegates_.erase(it);
[email protected]616ed5a2008-11-21 22:27:24930
[email protected]6a02963e2009-01-06 16:58:03931 // Remove ourselves as an observer if we are tracking no more InfoBars.
932 if (infobar_delegates_.empty()) {
[email protected]bfd04a62009-02-01 18:16:56933 registrar_.Remove(this, NotificationType::NAV_ENTRY_COMMITTED,
[email protected]ce3fa3c2009-04-20 19:55:57934 Source<NavigationController>(&controller_));
[email protected]6a02963e2009-01-06 16:58:03935 }
[email protected]616ed5a2008-11-21 22:27:24936 }
937}
[email protected]3edd9522009-03-04 22:19:41938
[email protected]f81821c2009-08-07 01:29:47939void TabContents::ReplaceInfoBar(InfoBarDelegate* old_delegate,
940 InfoBarDelegate* new_delegate) {
941 std::vector<InfoBarDelegate*>::iterator it =
942 find(infobar_delegates_.begin(), infobar_delegates_.end(), old_delegate);
943 DCHECK(it != infobar_delegates_.end());
944
945 // Notify the container about the change of plans.
946 scoped_ptr<std::pair<InfoBarDelegate*, InfoBarDelegate*> > details(
947 new std::pair<InfoBarDelegate*, InfoBarDelegate*>(
948 old_delegate, new_delegate));
949 NotificationService::current()->Notify(
950 NotificationType::TAB_CONTENTS_INFOBAR_REPLACED,
951 Source<TabContents>(this),
952 Details<std::pair<InfoBarDelegate*, InfoBarDelegate*> >(details.get()));
953
954 // Remove the old one.
955 infobar_delegates_.erase(it);
956
957 // Add the new one.
958 infobar_delegates_.push_back(new_delegate);
959}
960
[email protected]96d185d2009-04-24 03:28:54961bool TabContents::IsBookmarkBarAlwaysVisible() {
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_bookmark_bar_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_bookmark_bar_visible();
977 if (render_manager_.dom_ui())
978 return render_manager_.dom_ui()->force_bookmark_bar_visible();
979 return false; // Default.
980}
981
[email protected]d8f16ae2009-08-30 03:47:26982bool TabContents::IsExtensionShelfAlwaysVisible() {
983 // See GetDOMUIForCurrentState() comment for more info. This case is very
984 // similar, but for non-first loads, we want to use the committed entry. This
985 // is so the bookmarks bar disappears at the same time the page does.
986 if (controller_.GetLastCommittedEntry()) {
987 // Not the first load, always use the committed DOM UI.
988 if (render_manager_.dom_ui())
989 return render_manager_.dom_ui()->force_extension_shelf_visible();
990 return false; // Default.
991 }
992
993 // When it's the first load, we know either the pending one or the committed
994 // one will have the DOM UI in it (see GetDOMUIForCurrentState), and only one
995 // of them will be valid, so we can just check both.
996 if (render_manager_.pending_dom_ui())
997 return render_manager_.pending_dom_ui()->force_extension_shelf_visible();
998 if (render_manager_.dom_ui())
999 return render_manager_.dom_ui()->force_extension_shelf_visible();
1000 return false; // Default.
1001}
1002
[email protected]3edd9522009-03-04 22:19:411003void TabContents::ToolbarSizeChanged(bool is_animating) {
1004 TabContentsDelegate* d = delegate();
1005 if (d)
1006 d->ToolbarSizeChanged(this, is_animating);
1007}
[email protected]616ed5a2008-11-21 22:27:241008
[email protected]d5f942ba2008-09-26 19:30:341009void TabContents::OnStartDownload(DownloadItem* download) {
1010 DCHECK(download);
[email protected]d5f942ba2008-09-26 19:30:341011
1012 // Download in a constrained popup is shown in the tab that opened it.
[email protected]b6c874582009-05-08 19:38:311013 TabContents* tab_contents = delegate()->GetConstrainingContents(this);
[email protected]d5f942ba2008-09-26 19:30:341014
[email protected]59560e0b2009-06-04 03:30:221015 if (tab_contents && tab_contents->delegate())
1016 tab_contents->delegate()->OnStartDownload(download);
[email protected]3edd9522009-03-04 22:19:411017}
[email protected]3edd9522009-03-04 22:19:411018
[email protected]d5f942ba2008-09-26 19:30:341019void TabContents::WillClose(ConstrainedWindow* window) {
1020 ConstrainedWindowList::iterator it =
1021 find(child_windows_.begin(), child_windows_.end(), window);
1022 if (it != child_windows_.end())
1023 child_windows_.erase(it);
[email protected]66ba4932009-06-04 19:22:131024}
[email protected]d5f942ba2008-09-26 19:30:341025
[email protected]66ba4932009-06-04 19:22:131026void TabContents::WillCloseBlockedPopupContainer(
1027 BlockedPopupContainer* container) {
1028 DCHECK(blocked_popups_ == container);
1029 blocked_popups_ = NULL;
[email protected]d5f942ba2008-09-26 19:30:341030}
1031
[email protected]d5f942ba2008-09-26 19:30:341032void TabContents::DidMoveOrResize(ConstrainedWindow* window) {
[email protected]332af7732009-03-11 13:21:351033#if defined(OS_WIN)
[email protected]92edc472009-02-10 20:32:061034 UpdateWindow(GetNativeView());
[email protected]332af7732009-03-11 13:21:351035#endif
[email protected]d5f942ba2008-09-26 19:30:341036}
1037
[email protected]e491f1c2009-05-22 20:28:121038void TabContents::StartFinding(string16 find_text,
1039 bool forward_direction,
1040 bool case_sensitive) {
[email protected]6e95706d2009-04-23 22:55:011041 // If find_text is empty, it means FindNext was pressed with a keyboard
1042 // shortcut so unless we have something to search for we return early.
[email protected]e491f1c2009-05-22 20:28:121043 if (find_text.empty() && find_text_.empty()) {
1044 if (last_search_prepopulate_text_->empty())
1045 return;
1046 // Try whatever we searched for last in any tab.
1047 find_text = *last_search_prepopulate_text_;
1048 }
[email protected]6e95706d2009-04-23 22:55:011049
1050 // This is a FindNext operation if we are searching for the same text again,
1051 // or if the passed in search text is empty (FindNext keyboard shortcut). The
1052 // exception to this is if the Find was aborted (then we don't want FindNext
1053 // because the highlighting has been cleared and we need it to reappear). We
1054 // therefore treat FindNext after an aborted Find operation as a full fledged
1055 // Find.
1056 bool find_next = (find_text_ == find_text || find_text.empty()) &&
[email protected]e491f1c2009-05-22 20:28:121057 (last_search_case_sensitive_ == case_sensitive) &&
[email protected]6e95706d2009-04-23 22:55:011058 !find_op_aborted_;
1059 if (!find_next)
1060 current_find_request_id_ = find_request_id_counter_++;
1061
1062 if (!find_text.empty())
1063 find_text_ = find_text;
[email protected]e491f1c2009-05-22 20:28:121064 last_search_case_sensitive_ = case_sensitive;
[email protected]6e95706d2009-04-23 22:55:011065
1066 find_op_aborted_ = false;
1067
1068 // Keep track of what the last search was across the tabs.
[email protected]e491f1c2009-05-22 20:28:121069 *last_search_prepopulate_text_ = find_text;
[email protected]6e95706d2009-04-23 22:55:011070
1071 render_view_host()->StartFinding(current_find_request_id_,
1072 find_text_,
1073 forward_direction,
[email protected]e491f1c2009-05-22 20:28:121074 case_sensitive,
[email protected]6e95706d2009-04-23 22:55:011075 find_next);
1076}
1077
1078void TabContents::StopFinding(bool clear_selection) {
[email protected]2d2dffc2009-06-09 21:41:001079 // When |clear_selection| is true, it means the find string has been cleared
1080 // by the user, but the UI has not been dismissed.
1081 if (!clear_selection)
1082 find_ui_active_ = false;
[email protected]6e95706d2009-04-23 22:55:011083 find_op_aborted_ = true;
[email protected]e491f1c2009-05-22 20:28:121084 last_search_result_ = FindNotificationDetails();
[email protected]6e95706d2009-04-23 22:55:011085 render_view_host()->StopFinding(clear_selection);
1086}
1087
[email protected]5c4266922009-07-10 16:41:271088void TabContents::GetPageLanguage() {
1089 render_view_host()->GetPageLanguage();
1090}
1091
[email protected]420ae012009-04-24 05:16:321092void TabContents::OnJavaScriptMessageBoxClosed(IPC::Message* reply_msg,
1093 bool success,
1094 const std::wstring& prompt) {
1095 last_javascript_message_dismissal_ = base::TimeTicks::Now();
1096 render_manager_.OnJavaScriptMessageBoxClosed(reply_msg, success, prompt);
1097}
1098
[email protected]571e31152009-05-15 19:34:021099void TabContents::OnJavaScriptMessageBoxWindowDestroyed() {
1100 render_manager_.OnJavaScriptMessageBoxWindowDestroyed();
1101}
1102
[email protected]420ae012009-04-24 05:16:321103void TabContents::OnSavePage() {
1104 // If we can not save the page, try to download it.
1105 if (!SavePackage::IsSavableContents(contents_mime_type())) {
1106 DownloadManager* dlm = profile()->GetDownloadManager();
1107 const GURL& current_page_url = GetURL();
1108 if (dlm && current_page_url.is_valid())
[email protected]d82ed61e2009-06-16 02:46:221109 dlm->DownloadUrl(current_page_url, GURL(), "", this);
[email protected]420ae012009-04-24 05:16:321110 return;
1111 }
1112
1113 Stop();
1114
1115 // Create the save package and possibly prompt the user for the name to save
1116 // the page as. The user prompt is an asynchronous operation that runs on
1117 // another thread.
[email protected]d82ed61e2009-06-16 02:46:221118 save_package_ = new SavePackage(this);
[email protected]420ae012009-04-24 05:16:321119 save_package_->GetSaveInfo();
1120}
1121
1122// Used in automated testing to bypass prompting the user for file names.
1123// Instead, the names and paths are hard coded rather than running them through
1124// file name sanitation and extension / mime checking.
[email protected]cdf6cc82009-08-04 21:48:591125bool TabContents::SavePage(const std::wstring& main_file,
[email protected]420ae012009-04-24 05:16:321126 const std::wstring& dir_path,
1127 SavePackage::SavePackageType save_type) {
1128 // Stop the page from navigating.
1129 Stop();
1130
[email protected]d82ed61e2009-06-16 02:46:221131 save_package_ = new SavePackage(this, save_type,
[email protected]420ae012009-04-24 05:16:321132 FilePath::FromWStringHack(main_file),
1133 FilePath::FromWStringHack(dir_path));
[email protected]cdf6cc82009-08-04 21:48:591134 return save_package_->Init();
[email protected]420ae012009-04-24 05:16:321135}
1136
1137void TabContents::PrintPreview() {
1138 // We don't show the print preview yet, only the print dialog.
1139 PrintNow();
1140}
1141
1142bool TabContents::PrintNow() {
1143 // We can't print interstitial page for now.
1144 if (showing_interstitial_page())
1145 return false;
1146
1147 return render_view_host()->PrintPages();
1148}
1149
[email protected]82270452009-06-19 15:58:011150void TabContents::PrintingDone(int document_cookie, bool success) {
1151 render_view_host()->PrintingDone(document_cookie, success);
1152}
1153
[email protected]420ae012009-04-24 05:16:321154bool TabContents::IsActiveEntry(int32 page_id) {
1155 NavigationEntry* active_entry = controller_.GetActiveEntry();
1156 return (active_entry != NULL &&
1157 active_entry->site_instance() == GetSiteInstance() &&
1158 active_entry->page_id() == page_id);
1159}
1160
[email protected]96d185d2009-04-24 03:28:541161// Notifies the RenderWidgetHost instance about the fact that the page is
1162// loading, or done loading and calls the base implementation.
1163void TabContents::SetIsLoading(bool is_loading,
1164 LoadNotificationDetails* details) {
1165 if (is_loading == is_loading_)
1166 return;
1167
1168 if (!is_loading) {
1169 load_state_ = net::LOAD_STATE_IDLE;
1170 load_state_host_.clear();
[email protected]094e5b22009-09-25 04:23:561171 upload_size_ = 0;
1172 upload_position_ = 0;
[email protected]96d185d2009-04-24 03:28:541173 }
1174
1175 render_manager_.SetIsLoading(is_loading);
1176
1177 is_loading_ = is_loading;
1178 waiting_for_response_ = is_loading;
1179
1180 if (delegate_)
1181 delegate_->LoadingStateChanged(this);
[email protected]329581b2009-04-28 06:52:351182 NotifyNavigationStateChanged(INVALIDATE_LOAD);
[email protected]96d185d2009-04-24 03:28:541183
1184 NotificationType type = is_loading ? NotificationType::LOAD_START :
1185 NotificationType::LOAD_STOP;
[email protected]ce5c4502009-05-06 16:46:111186 NotificationDetails det = NotificationService::NoDetails();
[email protected]96d185d2009-04-24 03:28:541187 if (details)
1188 det = Details<LoadNotificationDetails>(details);
1189 NotificationService::current()->Notify(type,
1190 Source<NavigationController>(&controller_),
1191 det);
1192}
1193
[email protected]2c4410d2009-05-06 23:46:221194void TabContents::CreateBlockedPopupContainerIfNecessary() {
1195 if (blocked_popups_)
1196 return;
1197
[email protected]4d4c0b92009-06-08 23:13:011198 blocked_popups_ = BlockedPopupContainer::Create(this, profile());
[email protected]2c4410d2009-05-06 23:46:221199}
1200
[email protected]b6c874582009-05-08 19:38:311201void TabContents::AddPopup(TabContents* new_contents,
1202 const gfx::Rect& initial_pos,
1203 const std::string& host) {
[email protected]2c4410d2009-05-06 23:46:221204 CreateBlockedPopupContainerIfNecessary();
1205 blocked_popups_->AddTabContents(new_contents, initial_pos, host);
[email protected]2c4410d2009-05-06 23:46:221206}
1207
[email protected]e69bce3dd2009-04-20 22:05:121208// TODO(brettw) This should be on the TabContentsView.
[email protected]66ba4932009-06-04 19:22:131209void TabContents::RepositionSupressedPopupsToFit() {
[email protected]d6598c052008-11-05 19:03:251210 if (blocked_popups_)
[email protected]4d4c0b92009-06-08 23:13:011211 blocked_popups_->RepositionBlockedPopupContainer();
[email protected]d5f942ba2008-09-26 19:30:341212}
1213
[email protected]b9681312008-11-07 00:08:261214bool TabContents::ShowingBlockedPopupNotification() const {
1215 return blocked_popups_ != NULL &&
[email protected]2c4410d2009-05-06 23:46:221216 blocked_popups_->GetBlockedPopupCount() != 0;
[email protected]b9681312008-11-07 00:08:261217}
[email protected]616ed5a2008-11-21 22:27:241218
1219namespace {
1220bool TransitionIsReload(PageTransition::Type transition) {
1221 return PageTransition::StripQualifier(transition) == PageTransition::RELOAD;
1222}
1223}
1224
1225void TabContents::ExpireInfoBars(
1226 const NavigationController::LoadCommittedDetails& details) {
1227 // Only hide InfoBars when the user has done something that makes the main
1228 // frame load. We don't want various automatic or subframe navigations making
1229 // it disappear.
1230 if (!details.is_user_initiated_main_frame_load())
1231 return;
1232
[email protected]f86a07022008-11-25 01:06:051233 for (int i = infobar_delegate_count() - 1; i >= 0; --i) {
[email protected]616ed5a2008-11-21 22:27:241234 InfoBarDelegate* delegate = GetInfoBarDelegateAt(i);
[email protected]f86a07022008-11-25 01:06:051235 if (delegate->ShouldExpire(details))
[email protected]616ed5a2008-11-21 22:27:241236 RemoveInfoBar(delegate);
[email protected]616ed5a2008-11-21 22:27:241237 }
1238}
[email protected]fdd61c62009-04-22 19:22:571239
1240void TabContents::OnGearsCreateShortcutDone(
1241 const GearsShortcutData2& shortcut_data, bool success) {
1242 NavigationEntry* current_entry = controller_.GetLastCommittedEntry();
1243 bool same_page =
1244 current_entry && pending_install_.page_id == current_entry->page_id();
1245
1246 if (success && same_page) {
1247 // Only switch to app mode if the user chose to create a shortcut and
1248 // we're still on the same page that it corresponded to.
1249 if (delegate())
1250 delegate()->ConvertContentsToApplication(this);
1251 }
1252
1253 // Reset the page id to indicate no requests are pending.
1254 pending_install_.page_id = 0;
1255 pending_install_.callback_functor = NULL;
1256}
[email protected]96d185d2009-04-24 03:28:541257
1258DOMUI* TabContents::GetDOMUIForCurrentState() {
1259 // When there is a pending navigation entry, we want to use the pending DOMUI
1260 // that goes along with it to control the basic flags. For example, we want to
1261 // show the pending URL in the URL bar, so we want the display_url flag to
1262 // be from the pending entry.
1263 //
1264 // The confusion comes because there are multiple possibilities for the
1265 // initial load in a tab as a side effect of the way the RenderViewHostManager
1266 // works.
1267 //
1268 // - For the very first tab the load looks "normal". The new tab DOM UI is
1269 // the pending one, and we want it to apply here.
1270 //
1271 // - For subsequent new tabs, they'll get a new SiteInstance which will then
1272 // get switched to the one previously associated with the new tab pages.
1273 // This switching will cause the manager to commit the RVH/DOMUI. So we'll
1274 // have a committed DOM UI in this case.
1275 //
1276 // This condition handles all of these cases:
1277 //
1278 // - First load in first tab: no committed nav entry + pending nav entry +
1279 // pending dom ui:
1280 // -> Use pending DOM UI if any.
1281 //
1282 // - First load in second tab: no committed nav entry + pending nav entry +
1283 // no pending DOM UI:
1284 // -> Use the committed DOM UI if any.
1285 //
1286 // - Second navigation in any tab: committed nav entry + pending nav entry:
1287 // -> Use pending DOM UI if any.
1288 //
1289 // - Normal state with no load: committed nav entry + no pending nav entry:
1290 // -> Use committed DOM UI.
1291 if (controller_.pending_entry() &&
1292 (controller_.GetLastCommittedEntry() ||
1293 render_manager_.pending_dom_ui()))
1294 return render_manager_.pending_dom_ui();
1295 return render_manager_.dom_ui();
1296}
[email protected]420ae012009-04-24 05:16:321297
1298void TabContents::DidNavigateMainFramePostCommit(
1299 const NavigationController::LoadCommittedDetails& details,
1300 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]c2e74fe82009-09-03 17:57:441301 if (opener_dom_ui_type_ != DOMUIFactory::kNoDOMUI) {
1302 // If this is a window.open navigation, use the same DOMUI as the renderer
1303 // that opened the window, as long as both renderers have the same
1304 // privileges.
1305 if (opener_dom_ui_type_ == DOMUIFactory::GetDOMUIType(GetURL())) {
1306 DOMUI* dom_ui = DOMUIFactory::CreateDOMUIForURL(this, GetURL());
1307 // dom_ui might be NULL if the URL refers to a non-existent extension.
1308 if (dom_ui) {
1309 render_manager_.SetDOMUIPostCommit(dom_ui);
1310 dom_ui->RenderViewCreated(render_view_host());
1311 }
1312 }
1313 opener_dom_ui_type_ = DOMUIFactory::kNoDOMUI;
1314 }
1315
[email protected]420ae012009-04-24 05:16:321316 if (details.is_user_initiated_main_frame_load()) {
1317 // Clear the status bubble. This is a workaround for a bug where WebKit
1318 // doesn't let us know that the cursor left an element during a
1319 // transition (this is also why the mouse cursor remains as a hand after
1320 // clicking on a link); see bugs 1184641 and 980803. We don't want to
1321 // clear the bubble when a user navigates to a named anchor in the same
1322 // page.
1323 UpdateTargetURL(details.entry->page_id(), GURL());
1324
1325 // UpdateHelpersForDidNavigate will handle the case where the password_form
1326 // origin is valid.
1327 // TODO(brettw) bug 1343111: Password manager stuff in here needs to be
1328 // cleaned up and covered by tests.
1329 if (!params.password_form.origin.is_valid())
1330 GetPasswordManager()->DidNavigate();
1331 }
1332
1333 // The keyword generator uses the navigation entries, so must be called after
1334 // the commit.
1335 GenerateKeywordIfNecessary(params);
1336
1337 // Allow the new page to set the title again.
1338 received_page_title_ = false;
1339
1340 // Get the favicon, either from history or request it from the net.
1341 fav_icon_helper_.FetchFavIcon(details.entry->url());
1342
[email protected]aece2c7f2009-08-27 20:43:171343 // Disable all page actions, unless this is an in-page navigation.
1344 if (!enabled_page_actions_.empty()) {
1345 url_canon::Replacements<char> replacements;
1346 replacements.ClearRef();
1347 if (params.url.ReplaceComponents(replacements) !=
1348 params.referrer.ReplaceComponents(replacements)) {
1349 enabled_page_actions_.clear();
1350 }
1351 }
[email protected]ce5c4502009-05-06 16:46:111352
[email protected]420ae012009-04-24 05:16:321353 // Close constrained popups if necessary.
1354 MaybeCloseChildWindows(details.previous_url, details.entry->url());
1355
1356 // Update the starred state.
1357 UpdateStarredStateForCurrentURL();
1358}
1359
1360void TabContents::DidNavigateAnyFramePostCommit(
1361 RenderViewHost* render_view_host,
1362 const NavigationController::LoadCommittedDetails& details,
1363 const ViewHostMsg_FrameNavigate_Params& params) {
1364 // If we navigate, start showing messages again. This does nothing to prevent
1365 // a malicious script from spamming messages, since the script could just
1366 // reload the page to stop blocking.
1367 suppress_javascript_messages_ = false;
1368
[email protected]420ae012009-04-24 05:16:321369 // Notify the password manager of the navigation or form submit.
1370 // TODO(brettw) bug 1343111: Password manager stuff in here needs to be
1371 // cleaned up and covered by tests.
1372 if (params.password_form.origin.is_valid())
1373 GetPasswordManager()->ProvisionallySavePassword(params.password_form);
1374}
1375
1376void TabContents::MaybeCloseChildWindows(const GURL& previous_url,
1377 const GURL& current_url) {
1378 if (net::RegistryControlledDomainService::SameDomainOrHost(
1379 previous_url, current_url))
1380 return;
1381
1382 // Clear out any child windows since we are leaving this page entirely.
1383 // We use indices instead of iterators in case CloseWindow does something
1384 // that may invalidate an iterator.
1385 int size = static_cast<int>(child_windows_.size());
1386 for (int i = size - 1; i >= 0; --i) {
1387 ConstrainedWindow* window = child_windows_[i];
1388 if (window)
1389 window->CloseConstrainedWindow();
1390 }
[email protected]e29300cd2009-07-23 21:14:591391
1392 // Close the popup container.
1393 if (blocked_popups_) {
1394 blocked_popups_->Destroy();
1395 blocked_popups_ = NULL;
1396 }
[email protected]420ae012009-04-24 05:16:321397}
1398
1399void TabContents::UpdateStarredStateForCurrentURL() {
1400 BookmarkModel* model = profile()->GetBookmarkModel();
1401 const bool old_state = is_starred_;
1402 is_starred_ = (model && model->IsBookmarked(GetURL()));
1403
1404 if (is_starred_ != old_state && delegate())
1405 delegate()->URLStarredChanged(this, is_starred_);
1406}
1407
1408void TabContents::UpdateAlternateErrorPageURL() {
1409 GURL url = GetAlternateErrorPageURL();
1410 render_view_host()->SetAlternateErrorPageURL(url);
1411}
1412
1413void TabContents::UpdateWebPreferences() {
1414 render_view_host()->UpdateWebPreferences(GetWebkitPrefs());
1415}
1416
1417void TabContents::UpdateMaxPageIDIfNecessary(SiteInstance* site_instance,
1418 RenderViewHost* rvh) {
1419 // If we are creating a RVH for a restored controller, then we might
1420 // have more page IDs than the SiteInstance's current max page ID. We must
1421 // make sure that the max page ID is larger than any restored page ID.
1422 // Note that it is ok for conflicting page IDs to exist in another tab
1423 // (i.e., NavigationController), but if any page ID is larger than the max,
1424 // the back/forward list will get confused.
1425 int max_restored_page_id = controller_.max_restored_page_id();
1426 if (max_restored_page_id > 0) {
1427 int curr_max_page_id = site_instance->max_page_id();
1428 if (max_restored_page_id > curr_max_page_id) {
1429 // Need to update the site instance immediately.
1430 site_instance->UpdateMaxPageID(max_restored_page_id);
1431
1432 // Also tell the renderer to update its internal representation. We
1433 // need to reserve enough IDs to make all restored page IDs less than
1434 // the max.
1435 if (curr_max_page_id < 0)
1436 curr_max_page_id = 0;
1437 rvh->ReservePageIDRange(max_restored_page_id - curr_max_page_id);
1438 }
1439 }
1440}
1441
1442void TabContents::UpdateHistoryForNavigation(
[email protected]ebe89e062009-08-13 23:16:541443 const GURL& virtual_url,
[email protected]befd8d822009-07-01 04:51:471444 const NavigationController::LoadCommittedDetails& details,
[email protected]420ae012009-04-24 05:16:321445 const ViewHostMsg_FrameNavigate_Params& params) {
1446 if (profile()->IsOffTheRecord())
1447 return;
1448
1449 // Add to history service.
1450 HistoryService* hs = profile()->GetHistoryService(Profile::IMPLICIT_ACCESS);
1451 if (hs) {
1452 if (PageTransition::IsMainFrame(params.transition) &&
[email protected]ebe89e062009-08-13 23:16:541453 virtual_url != params.url) {
1454 // Hack on the "virtual" URL so that it will appear in history. For some
[email protected]420ae012009-04-24 05:16:321455 // types of URLs, we will display a magic URL that is different from where
1456 // the page is actually navigated. We want the user to see in history
[email protected]ebe89e062009-08-13 23:16:541457 // what they saw in the URL bar, so we add the virtual URL as a redirect.
1458 // This only applies to the main frame, as the virtual URL doesn't apply
[email protected]420ae012009-04-24 05:16:321459 // to sub-frames.
1460 std::vector<GURL> redirects = params.redirects;
1461 if (!redirects.empty())
[email protected]ebe89e062009-08-13 23:16:541462 redirects.back() = virtual_url;
1463 hs->AddPage(virtual_url, this, params.page_id, params.referrer,
[email protected]befd8d822009-07-01 04:51:471464 params.transition, redirects, details.did_replace_entry);
[email protected]420ae012009-04-24 05:16:321465 } else {
1466 hs->AddPage(params.url, this, params.page_id, params.referrer,
[email protected]befd8d822009-07-01 04:51:471467 params.transition, params.redirects,
1468 details.did_replace_entry);
[email protected]420ae012009-04-24 05:16:321469 }
1470 }
1471}
1472
1473bool TabContents::UpdateTitleForEntry(NavigationEntry* entry,
1474 const std::wstring& title) {
1475 // For file URLs without a title, use the pathname instead. In the case of a
1476 // synthesized title, we don't want the update to count toward the "one set
1477 // per page of the title to history."
1478 std::wstring final_title;
1479 bool explicit_set;
1480 if (entry->url().SchemeIsFile() && title.empty()) {
1481 final_title = UTF8ToWide(entry->url().ExtractFileName());
1482 explicit_set = false; // Don't count synthetic titles toward the set limit.
1483 } else {
1484 TrimWhitespace(title, TRIM_ALL, &final_title);
1485 explicit_set = true;
1486 }
1487
1488 if (final_title == UTF16ToWideHack(entry->title()))
1489 return false; // Nothing changed, don't bother.
1490
1491 entry->set_title(WideToUTF16Hack(final_title));
1492
1493 // Update the history system for this page.
1494 if (!profile()->IsOffTheRecord() && !received_page_title_) {
1495 HistoryService* hs =
1496 profile()->GetHistoryService(Profile::IMPLICIT_ACCESS);
1497 if (hs)
[email protected]ebe89e062009-08-13 23:16:541498 hs->SetPageTitle(entry->virtual_url(), final_title);
[email protected]420ae012009-04-24 05:16:321499
1500 // Don't allow the title to be saved again for explicitly set ones.
1501 received_page_title_ = explicit_set;
1502 }
1503
1504 // Lastly, set the title for the view.
1505 view_->SetPageTitle(final_title);
1506
1507 return true;
1508}
1509
1510void TabContents::NotifySwapped() {
1511 // After sending out a swap notification, we need to send a disconnect
1512 // notification so that clients that pick up a pointer to |this| can NULL the
1513 // pointer. See Bug 1230284.
1514 notify_disconnection_ = true;
1515 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341516 NotificationType::TAB_CONTENTS_SWAPPED,
[email protected]d82ed61e2009-06-16 02:46:221517 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321518 NotificationService::NoDetails());
1519}
1520
1521void TabContents::NotifyConnected() {
1522 notify_disconnection_ = true;
1523 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341524 NotificationType::TAB_CONTENTS_CONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221525 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321526 NotificationService::NoDetails());
1527}
1528
1529void TabContents::NotifyDisconnected() {
1530 if (!notify_disconnection_)
1531 return;
1532
1533 notify_disconnection_ = false;
1534 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341535 NotificationType::TAB_CONTENTS_DISCONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221536 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321537 NotificationService::NoDetails());
1538}
1539
1540void TabContents::GenerateKeywordIfNecessary(
1541 const ViewHostMsg_FrameNavigate_Params& params) {
1542 if (!params.searchable_form_url.is_valid())
1543 return;
1544
1545 if (profile()->IsOffTheRecord())
1546 return;
1547
1548 int last_index = controller_.last_committed_entry_index();
1549 // When there was no previous page, the last index will be 0. This is
1550 // normally due to a form submit that opened in a new tab.
1551 // TODO(brettw) bug 916126: we should support keywords when form submits
1552 // happen in new tabs.
1553 if (last_index <= 0)
1554 return;
1555 const NavigationEntry* previous_entry =
1556 controller_.GetEntryAtIndex(last_index - 1);
1557 if (IsFormSubmit(previous_entry)) {
1558 // Only generate a keyword if the previous page wasn't itself a form
1559 // submit.
1560 return;
1561 }
1562
1563 GURL keyword_url = previous_entry->user_typed_url().is_valid() ?
1564 previous_entry->user_typed_url() : previous_entry->url();
1565 std::wstring keyword =
1566 TemplateURLModel::GenerateKeyword(keyword_url, true); // autodetected
1567 if (keyword.empty())
1568 return;
1569
1570 TemplateURLModel* url_model = profile()->GetTemplateURLModel();
1571 if (!url_model)
1572 return;
1573
1574 if (!url_model->loaded()) {
1575 url_model->Load();
1576 return;
1577 }
1578
1579 const TemplateURL* current_url;
1580 std::wstring url = UTF8ToWide(params.searchable_form_url.spec());
1581 if (!url_model->CanReplaceKeyword(keyword, url, &current_url))
1582 return;
1583
1584 if (current_url) {
1585 if (current_url->originating_url().is_valid()) {
1586 // The existing keyword was generated from an OpenSearch description
1587 // document, don't regenerate.
1588 return;
1589 }
1590 url_model->Remove(current_url);
1591 }
1592 TemplateURL* new_url = new TemplateURL();
1593 new_url->set_keyword(keyword);
1594 new_url->set_short_name(keyword);
1595 new_url->SetURL(url, 0, 0);
1596 new_url->add_input_encoding(params.searchable_form_encoding);
1597 DCHECK(controller_.GetLastCommittedEntry());
1598 const GURL& favicon_url =
1599 controller_.GetLastCommittedEntry()->favicon().url();
1600 if (favicon_url.is_valid()) {
1601 new_url->SetFavIconURL(favicon_url);
1602 } else {
1603 // The favicon url isn't valid. This means there really isn't a favicon,
1604 // or the favicon url wasn't obtained before the load started. This assumes
1605 // the later.
1606 // TODO(sky): Need a way to set the favicon that doesn't involve generating
1607 // its url.
1608 new_url->SetFavIconURL(TemplateURL::GenerateFaviconURL(params.referrer));
1609 }
1610 new_url->set_safe_for_autoreplace(true);
1611 url_model->Add(new_url);
1612}
1613
[email protected]bf5c2ff392009-07-08 16:24:331614void TabContents::OnUserGesture() {
1615 // See comment in RenderViewHostDelegate::OnUserGesture as to why we do this.
1616 DownloadRequestManager* drm = g_browser_process->download_request_manager();
1617 if (drm)
1618 drm->OnUserGesture(this);
1619 controller_.OnUserGesture();
1620}
1621
1622void TabContents::OnFindReply(int request_id,
1623 int number_of_matches,
1624 const gfx::Rect& selection_rect,
1625 int active_match_ordinal,
1626 bool final_update) {
[email protected]3ce7ced2009-07-24 18:52:421627 // Ignore responses for requests that have been aborted.
1628 if (find_op_aborted_)
1629 return;
1630
[email protected]bf5c2ff392009-07-08 16:24:331631 // Ignore responses for requests other than the one we have most recently
1632 // issued. That way we won't act on stale results when the user has
1633 // already typed in another query.
1634 if (request_id != current_find_request_id_)
1635 return;
1636
1637 if (number_of_matches == -1)
1638 number_of_matches = last_search_result_.number_of_matches();
1639 if (active_match_ordinal == -1)
1640 active_match_ordinal = last_search_result_.active_match_ordinal();
1641
1642 gfx::Rect selection = selection_rect;
1643 if (selection.IsEmpty())
1644 selection = last_search_result_.selection_rect();
1645
1646 // Notify the UI, automation and any other observers that a find result was
1647 // found.
1648 last_search_result_ = FindNotificationDetails(request_id, number_of_matches,
1649 selection, active_match_ordinal,
1650 final_update);
1651 NotificationService::current()->Notify(
1652 NotificationType::FIND_RESULT_AVAILABLE,
1653 Source<TabContents>(this),
1654 Details<FindNotificationDetails>(&last_search_result_));
1655}
1656
1657void TabContents::GoToEntryAtOffset(int offset) {
1658 controller_.GoToOffset(offset);
1659}
1660
1661void TabContents::GetHistoryListCount(int* back_list_count,
1662 int* forward_list_count) {
1663 int current_index = controller_.last_committed_entry_index();
1664 *back_list_count = current_index;
1665 *forward_list_count = controller_.entry_count() - current_index - 1;
1666}
1667
1668void TabContents::OnMissingPluginStatus(int status) {
1669#if defined(OS_WIN)
1670// TODO(PORT): pull in when plug-ins work
1671 GetPluginInstaller()->OnMissingPluginStatus(status);
1672#endif
1673}
1674
1675void TabContents::OnCrashedPlugin(const FilePath& plugin_path) {
[email protected]bf5c2ff392009-07-08 16:24:331676 DCHECK(!plugin_path.value().empty());
1677
1678 std::wstring plugin_name = plugin_path.ToWStringHack();
[email protected]d32d46182009-07-23 00:09:351679#if defined(OS_WIN) || defined(OS_MACOSX)
[email protected]bf5c2ff392009-07-08 16:24:331680 scoped_ptr<FileVersionInfo> version_info(
1681 FileVersionInfo::CreateFileVersionInfo(plugin_path));
1682 if (version_info.get()) {
1683 const std::wstring& product_name = version_info->product_name();
[email protected]996fd702009-09-04 19:12:371684 if (!product_name.empty()) {
[email protected]bf5c2ff392009-07-08 16:24:331685 plugin_name = product_name;
[email protected]996fd702009-09-04 19:12:371686#if defined(OS_MACOSX)
1687 // Many plugins on the Mac have .plugin in the actual name, which looks
1688 // terrible, so look for that and strip it off if present.
1689 const std::wstring plugin_extension(L".plugin");
1690 if (EndsWith(plugin_name, plugin_extension, true))
1691 plugin_name.erase(plugin_name.length() - plugin_extension.length());
1692#endif // OS_MACOSX
1693 }
[email protected]bf5c2ff392009-07-08 16:24:331694 }
[email protected]4a560ed2009-07-13 23:35:261695#else
1696 NOTIMPLEMENTED() << " convert plugin path to plugin name";
1697#endif
[email protected]bf5c2ff392009-07-08 16:24:331698 AddInfoBar(new SimpleAlertInfoBarDelegate(
1699 this, l10n_util::GetStringF(IDS_PLUGIN_CRASHED_PROMPT, plugin_name),
1700 NULL));
[email protected]bf5c2ff392009-07-08 16:24:331701}
1702
1703void TabContents::OnCrashedWorker() {
1704 AddInfoBar(new SimpleAlertInfoBarDelegate(
1705 this, l10n_util::GetString(IDS_WEBWORKER_CRASHED_PROMPT),
1706 NULL));
1707}
1708
1709void TabContents::OnDidGetApplicationInfo(
1710 int32 page_id,
1711 const webkit_glue::WebApplicationInfo& info) {
1712 if (pending_install_.page_id != page_id)
1713 return; // The user clicked create on a separate page. Ignore this.
1714
1715 pending_install_.callback_functor =
1716 new GearsCreateShortcutCallbackFunctor(this);
1717 GearsCreateShortcut(
1718 info, pending_install_.title, pending_install_.url, pending_install_.icon,
1719 NewCallback(pending_install_.callback_functor,
1720 &GearsCreateShortcutCallbackFunctor::Run));
1721}
1722
1723void TabContents::DidStartProvisionalLoadForFrame(
1724 RenderViewHost* render_view_host,
1725 bool is_main_frame,
1726 const GURL& url) {
1727 ProvisionalLoadDetails details(is_main_frame,
1728 controller_.IsURLInPageNavigation(url),
1729 url, std::string(), false);
1730 NotificationService::current()->Notify(
1731 NotificationType::FRAME_PROVISIONAL_LOAD_START,
1732 Source<NavigationController>(&controller_),
1733 Details<ProvisionalLoadDetails>(&details));
1734}
1735
1736void TabContents::DidStartReceivingResourceResponse(
1737 ResourceRequestDetails* details) {
1738 NotificationService::current()->Notify(
1739 NotificationType::RESOURCE_RESPONSE_STARTED,
1740 Source<NavigationController>(&controller()),
1741 Details<ResourceRequestDetails>(details));
1742}
1743
1744void TabContents::DidRedirectResource(ResourceRequestDetails* details) {
1745 NotificationService::current()->Notify(
1746 NotificationType::RESOURCE_RECEIVED_REDIRECT,
1747 Source<NavigationController>(&controller()),
1748 Details<ResourceRequestDetails>(details));
1749}
1750
1751void TabContents::DidLoadResourceFromMemoryCache(
1752 const GURL& url,
1753 const std::string& frame_origin,
1754 const std::string& main_frame_origin,
1755 const std::string& security_info) {
1756 // Send out a notification that we loaded a resource from our memory cache.
1757 int cert_id = 0, cert_status = 0, security_bits = 0;
1758 SSLManager::DeserializeSecurityInfo(security_info,
1759 &cert_id, &cert_status,
1760 &security_bits);
1761 LoadFromMemoryCacheDetails details(url, frame_origin, main_frame_origin,
[email protected]76543b92009-08-31 17:27:451762 process()->id(), cert_id, cert_status);
[email protected]bf5c2ff392009-07-08 16:24:331763
1764 NotificationService::current()->Notify(
1765 NotificationType::LOAD_FROM_MEMORY_CACHE,
1766 Source<NavigationController>(&controller_),
1767 Details<LoadFromMemoryCacheDetails>(&details));
1768}
1769
1770void TabContents::DidFailProvisionalLoadWithError(
1771 RenderViewHost* render_view_host,
1772 bool is_main_frame,
1773 int error_code,
1774 const GURL& url,
1775 bool showing_repost_interstitial) {
1776 if (net::ERR_ABORTED == error_code) {
1777 // EVIL HACK ALERT! Ignore failed loads when we're showing interstitials.
1778 // This means that the interstitial won't be torn down properly, which is
1779 // bad. But if we have an interstitial, go back to another tab type, and
1780 // then load the same interstitial again, we could end up getting the first
1781 // interstitial's "failed" message (as a result of the cancel) when we're on
1782 // the second one.
1783 //
1784 // We can't tell this apart, so we think we're tearing down the current page
1785 // which will cause a crash later one. There is also some code in
1786 // RenderViewHostManager::RendererAbortedProvisionalLoad that is commented
1787 // out because of this problem.
1788 //
1789 // http://code.google.com/p/chromium/issues/detail?id=2855
1790 // Because this will not tear down the interstitial properly, if "back" is
1791 // back to another tab type, the interstitial will still be somewhat alive
1792 // in the previous tab type. If you navigate somewhere that activates the
1793 // tab with the interstitial again, you'll see a flash before the new load
1794 // commits of the interstitial page.
1795 if (showing_interstitial_page()) {
1796 LOG(WARNING) << "Discarding message during interstitial.";
1797 return;
1798 }
1799
1800 // This will discard our pending entry if we cancelled the load (e.g., if we
1801 // decided to download the file instead of load it). Only discard the
1802 // pending entry if the URLs match, otherwise the user initiated a navigate
1803 // before the page loaded so that the discard would discard the wrong entry.
1804 NavigationEntry* pending_entry = controller_.pending_entry();
1805 if (pending_entry && pending_entry->url() == url) {
1806 controller_.DiscardNonCommittedEntries();
1807 // Update the URL display.
1808 NotifyNavigationStateChanged(TabContents::INVALIDATE_URL);
1809 }
1810
1811 render_manager_.RendererAbortedProvisionalLoad(render_view_host);
1812 }
1813
1814 // Send out a notification that we failed a provisional load with an error.
1815 ProvisionalLoadDetails details(is_main_frame,
1816 controller_.IsURLInPageNavigation(url),
1817 url, std::string(), false);
1818 details.set_error_code(error_code);
1819
1820 NotificationService::current()->Notify(
1821 NotificationType::FAIL_PROVISIONAL_LOAD_WITH_ERROR,
1822 Source<NavigationController>(&controller_),
1823 Details<ProvisionalLoadDetails>(&details));
1824}
1825
1826void TabContents::DocumentLoadedInFrame() {
1827 controller_.DocumentLoadedInFrame();
1828}
1829
[email protected]8d3347f2009-07-09 22:00:211830RenderViewHostDelegate::View* TabContents::GetViewDelegate() {
[email protected]420ae012009-04-24 05:16:321831 return view_.get();
1832}
1833
[email protected]8d3347f2009-07-09 22:00:211834RenderViewHostDelegate::RendererManagement*
1835TabContents::GetRendererManagementDelegate() {
1836 return &render_manager_;
1837}
1838
[email protected]bf5c2ff392009-07-08 16:24:331839RenderViewHostDelegate::BrowserIntegration*
[email protected]8d3347f2009-07-09 22:00:211840 TabContents::GetBrowserIntegrationDelegate() {
1841 return this;
[email protected]bf5c2ff392009-07-08 16:24:331842}
1843
[email protected]8d3347f2009-07-09 22:00:211844RenderViewHostDelegate::Resource* TabContents::GetResourceDelegate() {
1845 return this;
[email protected]bf5c2ff392009-07-08 16:24:331846}
1847
[email protected]8d3347f2009-07-09 22:00:211848RenderViewHostDelegate::Save* TabContents::GetSaveDelegate() {
[email protected]420ae012009-04-24 05:16:321849 return save_package_.get(); // May be NULL, but we can return NULL.
1850}
1851
[email protected]8d3347f2009-07-09 22:00:211852RenderViewHostDelegate::Printing* TabContents::GetPrintingDelegate() {
1853 return &printing_;
1854}
1855
1856RenderViewHostDelegate::FavIcon* TabContents::GetFavIconDelegate() {
1857 return &fav_icon_helper_;
1858}
1859
1860RenderViewHostDelegate::Autofill* TabContents::GetAutofillDelegate() {
1861 if (autofill_manager_.get() == NULL)
1862 autofill_manager_.reset(new AutofillManager(this));
1863 return autofill_manager_.get();
[email protected]bf5c2ff392009-07-08 16:24:331864}
1865
[email protected]80d96fa2009-06-10 22:34:511866RendererPreferences TabContents::GetRendererPrefs() const {
[email protected]840b1512009-07-21 16:53:461867 return renderer_preferences_;
[email protected]80d96fa2009-06-10 22:34:511868}
1869
[email protected]57c6a652009-05-04 07:58:341870TabContents* TabContents::GetAsTabContents() {
1871 return this;
1872}
1873
[email protected]3e7cf1362009-08-21 17:47:081874void TabContents::AddBlockedNotice(const GURL& url, const string16& reason) {
1875 CreateBlockedPopupContainerIfNecessary();
1876 blocked_popups_->AddBlockedNotice(url, reason);
1877}
1878
[email protected]7b291f92009-08-14 05:43:531879ViewType::Type TabContents::GetRenderViewType() const {
1880 return ViewType::TAB_CONTENTS;
1881}
1882
1883int TabContents::GetBrowserWindowID() const {
1884 return controller().window_id().id();
1885}
1886
[email protected]420ae012009-04-24 05:16:321887void TabContents::RenderViewCreated(RenderViewHost* render_view_host) {
[email protected]2e4633c2009-07-09 16:58:061888 NotificationService::current()->Notify(
1889 NotificationType::RENDER_VIEW_HOST_CREATED_FOR_TAB,
1890 Source<TabContents>(this),
1891 Details<RenderViewHost>(render_view_host));
[email protected]420ae012009-04-24 05:16:321892 NavigationEntry* entry = controller_.GetActiveEntry();
1893 if (!entry)
1894 return;
1895
1896 // When we're creating views, we're still doing initial setup, so we always
1897 // use the pending DOM UI rather than any possibly existing committed one.
1898 if (render_manager_.pending_dom_ui()) {
1899 render_manager_.pending_dom_ui()->RenderViewCreated(render_view_host);
1900 }
1901
1902 if (entry->IsViewSourceMode()) {
1903 // Put the renderer in view source mode.
1904 render_view_host->Send(
1905 new ViewMsg_EnableViewSourceMode(render_view_host->routing_id()));
1906 }
[email protected]0666aef2009-05-13 19:48:081907
1908 view()->RenderViewCreated(render_view_host);
[email protected]420ae012009-04-24 05:16:321909}
1910
1911void TabContents::RenderViewReady(RenderViewHost* rvh) {
1912 if (rvh != render_view_host()) {
1913 // Don't notify the world, since this came from a renderer in the
1914 // background.
1915 return;
1916 }
1917
1918 NotifyConnected();
[email protected]9d8a4642009-07-29 17:25:301919 bool was_crashed = is_crashed();
[email protected]420ae012009-04-24 05:16:321920 SetIsCrashed(false);
[email protected]9d8a4642009-07-29 17:25:301921
1922 // Restore the focus to the tab (otherwise the focus will be on the top
1923 // window).
1924 if (was_crashed && !FocusLocationBarByDefault())
1925 Focus();
[email protected]420ae012009-04-24 05:16:321926}
1927
1928void TabContents::RenderViewGone(RenderViewHost* rvh) {
1929 // Ask the print preview if this renderer was valuable.
1930 if (!printing_.OnRenderViewGone(rvh))
1931 return;
1932 if (rvh != render_view_host()) {
1933 // The pending page's RenderViewHost is gone.
1934 return;
1935 }
1936
1937 SetIsLoading(false, NULL);
1938 NotifyDisconnected();
1939 SetIsCrashed(true);
1940
[email protected]8e537302009-06-10 21:19:581941 // Tell the view that we've crashed so it can prepare the sad tab page.
1942 view_->OnTabCrashed();
[email protected]420ae012009-04-24 05:16:321943
1944 // Hide any visible hung renderer warning for this web contents' process.
[email protected]d82ed61e2009-06-16 02:46:221945 HungRendererDialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:321946}
1947
[email protected]2e4633c2009-07-09 16:58:061948void TabContents::RenderViewDeleted(RenderViewHost* rvh) {
1949 NotificationService::current()->Notify(
1950 NotificationType::RENDER_VIEW_HOST_DELETED,
1951 Source<TabContents>(this),
1952 Details<RenderViewHost>(rvh));
1953}
1954
[email protected]420ae012009-04-24 05:16:321955void TabContents::DidNavigate(RenderViewHost* rvh,
1956 const ViewHostMsg_FrameNavigate_Params& params) {
1957 if (PageTransition::IsMainFrame(params.transition))
1958 render_manager_.DidNavigateMainFrame(rvh);
1959
1960 // Update the site of the SiteInstance if it doesn't have one yet.
1961 if (!GetSiteInstance()->has_site())
1962 GetSiteInstance()->SetSite(params.url);
1963
1964 // Need to update MIME type here because it's referred to in
1965 // UpdateNavigationCommands() called by RendererDidNavigate() to
1966 // determine whether or not to enable the encoding menu.
1967 // It's updated only for the main frame. For a subframe,
1968 // RenderView::UpdateURL does not set params.contents_mime_type.
1969 // (see http://code.google.com/p/chromium/issues/detail?id=2929 )
1970 // TODO(jungshik): Add a test for the encoding menu to avoid
1971 // regressing it again.
1972 if (PageTransition::IsMainFrame(params.transition))
1973 contents_mime_type_ = params.contents_mime_type;
1974
1975 NavigationController::LoadCommittedDetails details;
[email protected]3e90d4a2009-07-03 17:38:391976 bool did_navigate = controller_.RendererDidNavigate(params, &details);
1977
1978 // Update history. Note that this needs to happen after the entry is complete,
1979 // which WillNavigate[Main,Sub]Frame will do before this function is called.
1980 if (params.should_update_history) {
1981 // Most of the time, the displayURL matches the loaded URL, but for about:
1982 // URLs, we use a data: URL as the real value. We actually want to save
1983 // the about: URL to the history db and keep the data: URL hidden. This is
1984 // what the TabContents' URL getter does.
1985 UpdateHistoryForNavigation(GetURL(), details, params);
1986 }
1987
[email protected]76543b92009-08-31 17:27:451988 if (!did_navigate)
1989 return; // No navigation happened.
[email protected]420ae012009-04-24 05:16:321990
1991 // DO NOT ADD MORE STUFF TO THIS FUNCTION! Your component should either listen
1992 // for the appropriate notification (best) or you can add it to
1993 // DidNavigateMainFramePostCommit / DidNavigateAnyFramePostCommit (only if
1994 // necessary, please).
1995
1996 // Run post-commit tasks.
1997 if (details.is_main_frame)
1998 DidNavigateMainFramePostCommit(details, params);
1999 DidNavigateAnyFramePostCommit(rvh, details, params);
2000}
2001
2002void TabContents::UpdateState(RenderViewHost* rvh,
2003 int32 page_id,
2004 const std::string& state) {
2005 DCHECK(rvh == render_view_host());
2006
2007 // We must be prepared to handle state updates for any page, these occur
2008 // when the user is scrolling and entering form data, as well as when we're
2009 // leaving a page, in which case our state may have already been moved to
2010 // the next page. The navigation controller will look up the appropriate
2011 // NavigationEntry and update it when it is notified via the delegate.
2012
2013 int entry_index = controller_.GetEntryIndexWithPageID(
2014 GetSiteInstance(), page_id);
2015 if (entry_index < 0)
2016 return;
2017 NavigationEntry* entry = controller_.GetEntryAtIndex(entry_index);
2018
2019 if (state == entry->content_state())
2020 return; // Nothing to update.
2021 entry->set_content_state(state);
2022 controller_.NotifyEntryChanged(entry, entry_index);
2023}
2024
2025void TabContents::UpdateTitle(RenderViewHost* rvh,
2026 int32 page_id, const std::wstring& title) {
2027 // If we have a title, that's a pretty good indication that we've started
2028 // getting useful data.
2029 SetNotWaitingForResponse();
2030
2031 DCHECK(rvh == render_view_host());
2032 NavigationEntry* entry = controller_.GetEntryWithPageID(GetSiteInstance(),
2033 page_id);
2034 if (!entry || !UpdateTitleForEntry(entry, title))
2035 return;
2036
2037 // Broadcast notifications when the UI should be updated.
2038 if (entry == controller_.GetEntryAtOffset(0))
[email protected]c9cd2222009-05-06 05:16:502039 NotifyNavigationStateChanged(INVALIDATE_TAB);
[email protected]420ae012009-04-24 05:16:322040}
2041
[email protected]420ae012009-04-24 05:16:322042void TabContents::UpdateEncoding(RenderViewHost* render_view_host,
[email protected]41fc0322009-09-04 22:23:402043 const std::string& encoding) {
[email protected]420ae012009-04-24 05:16:322044 set_encoding(encoding);
2045}
2046
2047void TabContents::UpdateTargetURL(int32 page_id, const GURL& url) {
2048 if (delegate())
2049 delegate()->UpdateTargetURL(this, url);
2050}
2051
2052void TabContents::UpdateThumbnail(const GURL& url,
2053 const SkBitmap& bitmap,
2054 const ThumbnailScore& score) {
2055 // Tell History about this thumbnail
[email protected]449478302009-06-09 20:04:282056 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kThumbnailStore)) {
[email protected]72d0fc72009-07-09 17:30:492057 if (!profile()->IsOffTheRecord())
[email protected]42339402009-07-17 17:27:192058 profile()->GetThumbnailStore()->SetPageThumbnail(url, bitmap,
2059 score, true);
[email protected]449478302009-06-09 20:04:282060 } else {
2061 HistoryService* hs;
2062 if (!profile()->IsOffTheRecord() &&
2063 (hs = profile()->GetHistoryService(Profile::IMPLICIT_ACCESS))) {
2064 hs->SetPageThumbnail(url, bitmap, score);
2065 }
[email protected]420ae012009-04-24 05:16:322066 }
2067}
2068
[email protected]0df30122009-06-03 12:13:082069void TabContents::UpdateInspectorSettings(const std::wstring& raw_settings) {
2070 profile()->GetPrefs()->SetString(prefs::kWebKitInspectorSettings,
2071 raw_settings);
2072}
2073
[email protected]420ae012009-04-24 05:16:322074void TabContents::Close(RenderViewHost* rvh) {
[email protected]24a4d1062009-07-10 23:10:422075 // If we close the tab while we're in the middle of a drag, we'll crash.
2076 // Instead, cancel the drag and close it as soon as the drag ends.
2077 if (view()->IsDoingDrag()) {
2078 view()->CancelDragAndCloseTab();
2079 return;
2080 }
2081
[email protected]420ae012009-04-24 05:16:322082 // Ignore this if it comes from a RenderViewHost that we aren't showing.
2083 if (delegate() && rvh == render_view_host())
2084 delegate()->CloseContents(this);
2085}
2086
2087void TabContents::RequestMove(const gfx::Rect& new_bounds) {
2088 if (delegate() && delegate()->IsPopup(this))
2089 delegate()->MoveContents(this, new_bounds);
2090}
2091
[email protected]329581b2009-04-28 06:52:352092void TabContents::DidStartLoading(RenderViewHost* rvh) {
[email protected]420ae012009-04-24 05:16:322093 SetIsLoading(true, NULL);
2094}
2095
[email protected]329581b2009-04-28 06:52:352096void TabContents::DidStopLoading(RenderViewHost* rvh) {
[email protected]420ae012009-04-24 05:16:322097 scoped_ptr<LoadNotificationDetails> details;
2098
2099 NavigationEntry* entry = controller_.GetActiveEntry();
2100 // An entry may not exist for a stop when loading an initial blank page or
2101 // if an iframe injected by script into a blank page finishes loading.
2102 if (entry) {
2103 scoped_ptr<base::ProcessMetrics> metrics(
2104 base::ProcessMetrics::CreateProcessMetrics(
2105 process()->process().handle()));
2106
2107 base::TimeDelta elapsed = base::TimeTicks::Now() - current_load_start_;
2108
2109 details.reset(new LoadNotificationDetails(
[email protected]ebe89e062009-08-13 23:16:542110 entry->virtual_url(),
[email protected]420ae012009-04-24 05:16:322111 entry->transition_type(),
2112 elapsed,
2113 &controller_,
2114 controller_.GetCurrentEntryIndex()));
2115 }
2116
2117 // Tell PasswordManager we've finished a page load, which serves as a
2118 // green light to save pending passwords and reset itself.
2119 GetPasswordManager()->DidStopLoading();
2120
2121 SetIsLoading(false, details.get());
2122}
2123
[email protected]420ae012009-04-24 05:16:322124void TabContents::DidRedirectProvisionalLoad(int32 page_id,
2125 const GURL& source_url,
2126 const GURL& target_url) {
2127 NavigationEntry* entry;
2128 if (page_id == -1)
2129 entry = controller_.pending_entry();
2130 else
2131 entry = controller_.GetEntryWithPageID(GetSiteInstance(), page_id);
2132 if (!entry || entry->url() != source_url)
2133 return;
2134 entry->set_url(target_url);
2135}
2136
[email protected]420ae012009-04-24 05:16:322137void TabContents::RequestOpenURL(const GURL& url, const GURL& referrer,
2138 WindowOpenDisposition disposition) {
2139 if (render_manager_.dom_ui()) {
2140 // When we're a DOM UI, it will provide a page transition type for us (this
2141 // is so the new tab page can specify AUTO_BOOKMARK for automatically
2142 // generated suggestions).
2143 //
2144 // Note also that we hide the referrer for DOM UI pages. We don't really
[email protected]60e448982009-05-06 04:21:162145 // want web sites to see a referrer of "chrome://blah" (and some
2146 // chrome: URLs might have search terms or other stuff we don't want to
[email protected]420ae012009-04-24 05:16:322147 // send to the site), so we send no referrer.
2148 OpenURL(url, GURL(), disposition,
2149 render_manager_.dom_ui()->link_transition_type());
2150 } else {
2151 OpenURL(url, referrer, disposition, PageTransition::LINK);
2152 }
2153}
2154
[email protected]1ee614862009-07-06 20:29:182155void TabContents::DomOperationResponse(const std::string& json_string,
2156 int automation_id) {
2157}
2158
[email protected]420ae012009-04-24 05:16:322159void TabContents::ProcessDOMUIMessage(const std::string& message,
[email protected]811bfe32009-07-01 08:46:252160 const std::string& content,
2161 int request_id,
2162 bool has_callback) {
[email protected]420ae012009-04-24 05:16:322163 if (!render_manager_.dom_ui()) {
[email protected]c2e74fe82009-09-03 17:57:442164 // This can happen if someone uses window.open() to open an extension URL
2165 // from a non-extension context.
2166 render_view_host()->BlockExtensionRequest(request_id);
[email protected]420ae012009-04-24 05:16:322167 return;
2168 }
[email protected]811bfe32009-07-01 08:46:252169 render_manager_.dom_ui()->ProcessDOMUIMessage(message, content, request_id,
2170 has_callback);
[email protected]420ae012009-04-24 05:16:322171}
2172
2173void TabContents::ProcessExternalHostMessage(const std::string& message,
2174 const std::string& origin,
2175 const std::string& target) {
2176 if (delegate())
2177 delegate()->ForwardMessageToExternalHost(message, origin, target);
2178}
2179
[email protected]420ae012009-04-24 05:16:322180void TabContents::RunFileChooser(bool multiple_files,
2181 const string16& title,
2182 const FilePath& default_file) {
2183 if (!select_file_dialog_.get())
2184 select_file_dialog_ = SelectFileDialog::Create(this);
2185 SelectFileDialog::Type dialog_type =
2186 multiple_files ? SelectFileDialog::SELECT_OPEN_MULTI_FILE :
2187 SelectFileDialog::SELECT_OPEN_FILE;
2188 select_file_dialog_->SelectFile(dialog_type, title, default_file,
2189 NULL, 0, FILE_PATH_LITERAL(""),
2190 view_->GetTopLevelNativeWindow(), NULL);
2191}
2192
2193void TabContents::RunJavaScriptMessage(
2194 const std::wstring& message,
2195 const std::wstring& default_prompt,
2196 const GURL& frame_url,
2197 const int flags,
2198 IPC::Message* reply_msg,
2199 bool* did_suppress_message) {
2200 // Suppress javascript messages when requested and when inside a constrained
2201 // popup window (because that activates them and breaks them out of the
2202 // constrained window jail).
[email protected]91ed49a2009-07-07 21:29:552203 // Also suppress messages when showing an interstitial. The interstitial is
2204 // shown over the previous page, we don't want the hidden page dialogs to
2205 // interfere with the interstitial.
2206 bool suppress_this_message = suppress_javascript_messages_ ||
2207 showing_interstitial_page();
[email protected]420ae012009-04-24 05:16:322208 if (delegate())
2209 suppress_this_message |=
[email protected]b6c874582009-05-08 19:38:312210 (delegate()->GetConstrainingContents(this) != this);
[email protected]420ae012009-04-24 05:16:322211
2212 *did_suppress_message = suppress_this_message;
2213
2214 if (!suppress_this_message) {
2215 base::TimeDelta time_since_last_message(
2216 base::TimeTicks::Now() - last_javascript_message_dismissal_);
2217 bool show_suppress_checkbox = false;
2218 // Show a checkbox offering to suppress further messages if this message is
2219 // being displayed within kJavascriptMessageExpectedDelay of the last one.
2220 if (time_since_last_message <
2221 base::TimeDelta::FromMilliseconds(kJavascriptMessageExpectedDelay))
2222 show_suppress_checkbox = true;
2223
[email protected]d82ed61e2009-06-16 02:46:222224 RunJavascriptMessageBox(this, frame_url, flags, message, default_prompt,
[email protected]420ae012009-04-24 05:16:322225 show_suppress_checkbox, reply_msg);
2226 } else {
2227 // If we are suppressing messages, just reply as is if the user immediately
2228 // pressed "Cancel".
2229 OnJavaScriptMessageBoxClosed(reply_msg, false, std::wstring());
2230 }
2231}
2232
2233void TabContents::RunBeforeUnloadConfirm(const std::wstring& message,
2234 IPC::Message* reply_msg) {
[email protected]d82ed61e2009-06-16 02:46:222235 RunBeforeUnloadDialog(this, message, reply_msg);
[email protected]420ae012009-04-24 05:16:322236}
2237
2238void TabContents::ShowModalHTMLDialog(const GURL& url, int width, int height,
2239 const std::string& json_arguments,
2240 IPC::Message* reply_msg) {
2241 if (delegate()) {
2242 HtmlDialogUIDelegate* dialog_delegate =
2243 new ModalHtmlDialogDelegate(url, width, height, json_arguments,
[email protected]d82ed61e2009-06-16 02:46:222244 reply_msg, this);
[email protected]420ae012009-04-24 05:16:322245 delegate()->ShowHtmlDialog(dialog_delegate, NULL);
2246 }
2247}
2248
2249void TabContents::PasswordFormsSeen(
[email protected]ed3fb032009-06-16 19:50:562250 const std::vector<webkit_glue::PasswordForm>& forms) {
[email protected]420ae012009-04-24 05:16:322251 GetPasswordManager()->PasswordFormsSeen(forms);
2252}
2253
[email protected]420ae012009-04-24 05:16:322254// Checks to see if we should generate a keyword based on the OSDD, and if
2255// necessary uses TemplateURLFetcher to download the OSDD and create a keyword.
2256void TabContents::PageHasOSDD(RenderViewHost* render_view_host,
2257 int32 page_id, const GURL& url,
2258 bool autodetected) {
2259 // Make sure page_id is the current page, and the TemplateURLModel is loaded.
2260 DCHECK(url.is_valid());
2261 if (!IsActiveEntry(page_id))
2262 return;
2263 TemplateURLModel* url_model = profile()->GetTemplateURLModel();
2264 if (!url_model)
2265 return;
2266 if (!url_model->loaded()) {
2267 url_model->Load();
2268 return;
2269 }
2270 if (!profile()->GetTemplateURLFetcher())
2271 return;
2272
2273 if (profile()->IsOffTheRecord())
2274 return;
2275
2276 const NavigationEntry* entry = controller_.GetLastCommittedEntry();
2277 DCHECK(entry);
2278
2279 const NavigationEntry* base_entry = entry;
2280 if (IsFormSubmit(base_entry)) {
2281 // If the current page is a form submit, find the last page that was not
2282 // a form submit and use its url to generate the keyword from.
2283 int index = controller_.last_committed_entry_index() - 1;
2284 while (index >= 0 && IsFormSubmit(controller_.GetEntryAtIndex(index)))
2285 index--;
2286 if (index >= 0)
2287 base_entry = controller_.GetEntryAtIndex(index);
2288 else
2289 base_entry = NULL;
2290 }
2291
2292 // We want to use the user typed URL if available since that represents what
2293 // the user typed to get here, and fall back on the regular URL if not.
2294 if (!base_entry)
2295 return;
2296 GURL keyword_url = base_entry->user_typed_url().is_valid() ?
2297 base_entry->user_typed_url() : base_entry->url();
2298 if (!keyword_url.is_valid())
2299 return;
2300 std::wstring keyword = TemplateURLModel::GenerateKeyword(keyword_url,
2301 autodetected);
2302 if (keyword.empty())
2303 return;
2304 const TemplateURL* template_url =
2305 url_model->GetTemplateURLForKeyword(keyword);
2306 if (template_url && (!template_url->safe_for_autoreplace() ||
2307 template_url->originating_url() == url)) {
2308 // Either there is a user created TemplateURL for this keyword, or the
2309 // keyword has the same OSDD url and we've parsed it.
2310 return;
2311 }
2312
2313 // Download the OpenSearch description document. If this is successful a
2314 // new keyword will be created when done.
[email protected]420ae012009-04-24 05:16:322315 profile()->GetTemplateURLFetcher()->ScheduleDownload(
2316 keyword,
2317 url,
2318 base_entry->favicon().url(),
[email protected]d0cc9fb52009-06-23 02:26:162319 this,
[email protected]420ae012009-04-24 05:16:322320 autodetected);
2321}
2322
[email protected]420ae012009-04-24 05:16:322323GURL TabContents::GetAlternateErrorPageURL() const {
2324 GURL url;
2325 // Disable alternate error pages when in OffTheRecord/Incognito mode.
2326 if (profile()->IsOffTheRecord())
2327 return url;
2328
2329 PrefService* prefs = profile()->GetPrefs();
2330 DCHECK(prefs);
2331 if (prefs->GetBoolean(prefs::kAlternateErrorPagesEnabled)) {
[email protected]a33457b82009-09-21 23:07:152332 url = google_util::AppendGoogleLocaleParam(
2333 GURL(google_util::kLinkDoctorBaseURL));
[email protected]420ae012009-04-24 05:16:322334 url = google_util::AppendGoogleTLDParam(url);
2335 }
2336 return url;
2337}
2338
2339WebPreferences TabContents::GetWebkitPrefs() {
2340 PrefService* prefs = render_view_host()->process()->profile()->GetPrefs();
[email protected]7ccddb8c2009-08-04 17:36:552341 bool is_dom_ui = false;
2342 return RenderViewHostDelegateHelper::GetWebkitPrefs(prefs, is_dom_ui);
[email protected]420ae012009-04-24 05:16:322343}
2344
[email protected]420ae012009-04-24 05:16:322345void TabContents::OnJSOutOfMemory() {
2346 AddInfoBar(new SimpleAlertInfoBarDelegate(
2347 this, l10n_util::GetString(IDS_JS_OUT_OF_MEMORY_PROMPT), NULL));
2348}
2349
[email protected]420ae012009-04-24 05:16:322350void TabContents::OnCrossSiteResponse(int new_render_process_host_id,
2351 int new_request_id) {
[email protected]57c6a652009-05-04 07:58:342352 // Allows the TabContents to react when a cross-site response is ready to be
[email protected]420ae012009-04-24 05:16:322353 // delivered to a pending RenderViewHost. We must first run the onunload
2354 // handler of the old RenderViewHost before we can allow it to proceed.
2355 render_manager_.OnCrossSiteResponse(new_render_process_host_id,
2356 new_request_id);
2357}
2358
2359bool TabContents::CanBlur() const {
2360 return delegate() ? delegate()->CanBlur() : true;
2361}
2362
2363gfx::Rect TabContents::GetRootWindowResizerRect() const {
2364 if (delegate())
2365 return delegate()->GetRootWindowResizerRect();
2366 return gfx::Rect();
2367}
2368
2369void TabContents::RendererUnresponsive(RenderViewHost* rvh,
2370 bool is_during_unload) {
2371 if (is_during_unload) {
2372 // Hang occurred while firing the beforeunload/unload handler.
2373 // Pretend the handler fired so tab closing continues as if it had.
[email protected]829e7612009-04-25 01:15:112374 rvh->set_sudden_termination_allowed(true);
[email protected]420ae012009-04-24 05:16:322375
2376 if (!render_manager_.ShouldCloseTabOnUnresponsiveRenderer())
2377 return;
2378
2379 // If the tab hangs in the beforeunload/unload handler there's really
2380 // nothing we can do to recover. Pretend the unload listeners have
2381 // all fired and close the tab. If the hang is in the beforeunload handler
2382 // then the user will not have the option of cancelling the close.
2383 Close(rvh);
2384 return;
2385 }
2386
2387 if (render_view_host() && render_view_host()->IsRenderViewLive())
[email protected]d82ed61e2009-06-16 02:46:222388 HungRendererDialog::ShowForTabContents(this);
[email protected]420ae012009-04-24 05:16:322389}
2390
2391void TabContents::RendererResponsive(RenderViewHost* render_view_host) {
[email protected]d82ed61e2009-06-16 02:46:222392 HungRendererDialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:322393}
2394
2395void TabContents::LoadStateChanged(const GURL& url,
[email protected]094e5b22009-09-25 04:23:562396 net::LoadState load_state,
2397 uint64 upload_position,
2398 uint64 upload_size) {
[email protected]420ae012009-04-24 05:16:322399 load_state_ = load_state;
[email protected]094e5b22009-09-25 04:23:562400 upload_position_ = upload_position;
2401 upload_size_ = upload_size;
[email protected]d686e812009-06-03 19:10:292402 std::wstring languages =
2403 profile()->GetPrefs()->GetString(prefs::kAcceptLanguages);
2404 load_state_host_.clear();
2405 std::string host = url.host();
2406 net::IDNToUnicode(host.c_str(), host.size(), languages, &load_state_host_);
[email protected]420ae012009-04-24 05:16:322407 if (load_state_ == net::LOAD_STATE_READING_RESPONSE)
2408 SetNotWaitingForResponse();
2409 if (is_loading())
[email protected]c9cd2222009-05-06 05:16:502410 NotifyNavigationStateChanged(INVALIDATE_LOAD | INVALIDATE_TAB);
[email protected]420ae012009-04-24 05:16:322411}
2412
[email protected]1ee614862009-07-06 20:29:182413bool TabContents::IsExternalTabContainer() const {
2414 if (!delegate())
2415 return false;
2416
2417 return delegate()->IsExternalTabContainer();
2418}
2419
[email protected]1ee614862009-07-06 20:29:182420void TabContents::DidInsertCSS() {
2421 // This RVHDelegate function is used for extensions and not us.
[email protected]420ae012009-04-24 05:16:322422}
2423
[email protected]130efb02009-09-18 18:54:352424void TabContents::FocusedNodeChanged() {
2425 NotificationService::current()->Notify(
2426 NotificationType::FOCUS_CHANGED_IN_PAGE,
2427 Source<RenderViewHost>(render_view_host()),
2428 NotificationService::NoDetails());
2429}
2430
[email protected]420ae012009-04-24 05:16:322431void TabContents::FileSelected(const FilePath& path,
2432 int index, void* params) {
2433 render_view_host()->FileSelected(path);
2434}
2435
2436void TabContents::MultiFilesSelected(const std::vector<FilePath>& files,
2437 void* params) {
2438 render_view_host()->MultiFilesSelected(files);
2439}
2440
2441void TabContents::FileSelectionCanceled(void* params) {
2442 // If the user cancels choosing a file to upload we pass back an
2443 // empty vector.
2444 render_view_host()->MultiFilesSelected(std::vector<FilePath>());
2445}
2446
2447void TabContents::BeforeUnloadFiredFromRenderManager(
2448 bool proceed,
2449 bool* proceed_to_fire_unload) {
2450 if (delegate())
2451 delegate()->BeforeUnloadFired(this, proceed, proceed_to_fire_unload);
2452}
2453
2454void TabContents::UpdateRenderViewSizeForRenderManager() {
2455 // TODO(brettw) this is a hack. See TabContentsView::SizeContents.
2456 view_->SizeContents(view_->GetContainerSize());
2457}
2458
2459DOMUI* TabContents::CreateDOMUIForRenderManager(const GURL& url) {
[email protected]d82ed61e2009-06-16 02:46:222460 return DOMUIFactory::CreateDOMUIForURL(this, url);
[email protected]420ae012009-04-24 05:16:322461}
2462
2463NavigationEntry*
2464TabContents::GetLastCommittedNavigationEntryForRenderManager() {
2465 return controller_.GetLastCommittedEntry();
2466}
2467
2468bool TabContents::CreateRenderViewForRenderManager(
2469 RenderViewHost* render_view_host) {
[email protected]811bfe32009-07-01 08:46:252470 // If the pending navigation is to a DOMUI, tell the RenderView about any
2471 // bindings it will need enabled.
[email protected]420ae012009-04-24 05:16:322472 if (render_manager_.pending_dom_ui())
[email protected]811bfe32009-07-01 08:46:252473 render_view_host->AllowBindings(
2474 render_manager_.pending_dom_ui()->bindings());
[email protected]1adff062009-06-02 18:39:552475
[email protected]420ae012009-04-24 05:16:322476 RenderWidgetHostView* rwh_view = view_->CreateViewForWidget(render_view_host);
2477 if (!render_view_host->CreateRenderView())
2478 return false;
2479
2480 // Now that the RenderView has been created, we need to tell it its size.
2481 rwh_view->SetSize(view_->GetContainerSize());
2482
2483 UpdateMaxPageIDIfNecessary(render_view_host->site_instance(),
2484 render_view_host);
2485 return true;
2486}
2487
2488void TabContents::Observe(NotificationType type,
2489 const NotificationSource& source,
2490 const NotificationDetails& details) {
2491 switch (type.value) {
2492 case NotificationType::BOOKMARK_MODEL_LOADED:
2493 // BookmarkModel finished loading, fall through to update starred state.
2494 case NotificationType::URLS_STARRED: {
2495 // Somewhere, a URL has been starred.
2496 // Ignore notifications for profiles other than our current one.
2497 Profile* source_profile = Source<Profile>(source).ptr();
2498 if (!source_profile || !source_profile->IsSameProfile(profile()))
2499 return;
2500
2501 UpdateStarredStateForCurrentURL();
2502 break;
2503 }
2504 case NotificationType::PREF_CHANGED: {
2505 std::wstring* pref_name_in = Details<std::wstring>(details).ptr();
2506 DCHECK(Source<PrefService>(source).ptr() == profile()->GetPrefs());
2507 if (*pref_name_in == prefs::kAlternateErrorPagesEnabled) {
2508 UpdateAlternateErrorPageURL();
2509 } else if (*pref_name_in == prefs::kDefaultCharset ||
2510 StartsWithASCII(WideToUTF8(*pref_name_in), "webkit.webprefs.", true)
2511 ) {
2512 UpdateWebPreferences();
2513 } else {
2514 NOTREACHED() << "unexpected pref change notification" << *pref_name_in;
2515 }
2516 break;
2517 }
2518 case NotificationType::RENDER_WIDGET_HOST_DESTROYED:
2519 view_->RenderWidgetHostDestroyed(Source<RenderWidgetHost>(source).ptr());
2520 break;
2521
2522 case NotificationType::NAV_ENTRY_COMMITTED: {
2523 DCHECK(&controller_ == Source<NavigationController>(source).ptr());
2524
2525 NavigationController::LoadCommittedDetails& committed_details =
2526 *(Details<NavigationController::LoadCommittedDetails>(details).ptr());
2527 ExpireInfoBars(committed_details);
2528 break;
2529 }
2530
[email protected]d3ba77272009-09-03 00:06:092531#if defined(OS_LINUX)
2532 case NotificationType::BROWSER_THEME_CHANGED: {
2533 render_view_host()->SyncRendererPrefs();
2534 break;
2535 }
2536#endif
2537
[email protected]420ae012009-04-24 05:16:322538 default:
2539 NOTREACHED();
2540 }
2541}
[email protected]aacd7cc2009-09-02 14:51:102542
[email protected]41fc0322009-09-04 22:23:402543void TabContents::set_encoding(const std::string& encoding) {
[email protected]aacd7cc2009-09-02 14:51:102544 encoding_ = CharacterEncoding::GetCanonicalEncodingNameByAliasName(encoding);
2545}
2546