blob: 938c9bf9a1cfa431cf7df545ef7ad6c6c0c5b94d [file] [log] [blame]
[email protected]61d68ef12011-01-13 14:02:561// Copyright (c) 2011 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]b75b8292010-10-01 07:28:257#include <cmath>
8
[email protected]8cc8d492010-02-02 10:40:499#include "base/auto_reset.h"
[email protected]835d7c82010-10-14 04:38:3810#include "base/metrics/histogram.h"
[email protected]724159a2010-12-30 01:11:1811#include "base/metrics/stats_counters.h"
[email protected]96d185d2009-04-24 03:28:5412#include "base/string16.h"
[email protected]996fd702009-09-04 19:12:3713#include "base/string_util.h"
[email protected]96d185d2009-04-24 03:28:5414#include "base/time.h"
[email protected]3c733bde2010-12-21 19:56:3115#include "base/utf_string_conversions.h"
[email protected]6db0def92010-03-02 19:55:4516#include "chrome/browser/autocomplete_history_manager.h"
[email protected]21674df2009-10-28 19:51:2117#include "chrome/browser/autofill/autofill_manager.h"
[email protected]c65e2f152010-10-14 15:30:4018#include "chrome/browser/blocked_content_container.h"
[email protected]420ae012009-04-24 05:16:3219#include "chrome/browser/bookmarks/bookmark_model.h"
[email protected]96d185d2009-04-24 03:28:5420#include "chrome/browser/browser_process.h"
[email protected]cf68b092010-01-15 20:05:5521#include "chrome/browser/browser_shutdown.h"
[email protected]aacd7cc2009-09-02 14:51:1022#include "chrome/browser/character_encoding.h"
[email protected]724159a2010-12-30 01:11:1823#include "chrome/browser/child_process_security_policy.h"
[email protected]5f24f212010-12-02 14:47:4524#include "chrome/browser/content_settings/content_settings_details.h"
[email protected]9d01a6a2010-11-30 12:03:3325#include "chrome/browser/content_settings/host_content_settings_map.h"
[email protected]96d185d2009-04-24 03:28:5426#include "chrome/browser/debugger/devtools_manager.h"
[email protected]c8f74942010-09-03 10:04:1427#include "chrome/browser/defaults.h"
[email protected]420ae012009-04-24 05:16:3228#include "chrome/browser/dom_operation_notification_details.h"
[email protected]96d185d2009-04-24 03:28:5429#include "chrome/browser/dom_ui/dom_ui.h"
[email protected]bcef0dc02009-02-28 00:35:0230#include "chrome/browser/download/download_item_model.h"
[email protected]420ae012009-04-24 05:16:3231#include "chrome/browser/download/download_manager.h"
[email protected]41291322010-07-15 17:09:0132#include "chrome/browser/download/download_request_limiter.h"
[email protected]eaa7dd182010-12-14 11:09:0033#include "chrome/browser/extensions/extension_service.h"
[email protected]3c733bde2010-12-21 19:56:3134#include "chrome/browser/external_protocol_handler.h"
35#include "chrome/browser/favicon_service.h"
36#include "chrome/browser/file_select_helper.h"
37#include "chrome/browser/google/google_util.h"
[email protected]ef0d04c2010-12-15 23:34:2138#include "chrome/browser/history/history.h"
[email protected]ec0b6c42010-08-26 03:16:5839#include "chrome/browser/history/history_types.h"
[email protected]523623c2010-05-19 16:03:4240#include "chrome/browser/history/top_sites.h"
[email protected]b75b8292010-10-01 07:28:2541#include "chrome/browser/host_zoom_map.h"
[email protected]8897c382009-05-06 17:53:2642#include "chrome/browser/hung_renderer_dialog.h"
[email protected]6ee12c42010-09-14 09:36:0743#include "chrome/browser/in_process_webkit/session_storage_namespace.h"
[email protected]420ae012009-04-24 05:16:3244#include "chrome/browser/load_from_memory_cache_details.h"
45#include "chrome/browser/load_notification_details.h"
[email protected]84abba62009-10-07 17:01:4446#include "chrome/browser/metrics/metric_event_duration_details.h"
[email protected]724159a2010-12-30 01:11:1847#include "chrome/browser/metrics/user_metrics.h"
[email protected]ea0e80892009-08-22 00:36:0548#include "chrome/browser/modal_html_dialog_delegate.h"
[email protected]c8865482009-07-23 20:40:1049#include "chrome/browser/omnibox_search_hint.h"
[email protected]c8f73ab2011-01-22 00:05:1750#include "chrome/browser/pdf_unsupported_feature.h"
[email protected]14a000d2010-04-29 21:44:2451#include "chrome/browser/platform_util.h"
[email protected]f4f50ef2011-01-21 19:01:1952#include "chrome/browser/plugin_installer_infobar_delegate.h"
[email protected]37858e52010-08-26 00:22:0253#include "chrome/browser/prefs/pref_service.h"
[email protected]33f74972010-12-08 16:40:3654#include "chrome/browser/prerender/prerender_manager.h"
[email protected]dbeebd52010-11-16 20:34:1655#include "chrome/browser/printing/print_preview_tab_controller.h"
[email protected]12802702010-07-09 19:43:0956#include "chrome/browser/printing/print_view_manager.h"
[email protected]8ecad5e2010-12-02 21:18:3357#include "chrome/browser/profiles/profile.h"
[email protected]1db6ff152009-10-12 15:32:0758#include "chrome/browser/renderer_host/render_process_host.h"
[email protected]8cb5d5b2010-02-09 11:36:1659#include "chrome/browser/renderer_host/render_view_host.h"
[email protected]96d185d2009-04-24 03:28:5460#include "chrome/browser/renderer_host/render_widget_host_view.h"
[email protected]fd694982009-07-03 17:32:3761#include "chrome/browser/renderer_host/resource_request_details.h"
[email protected]1db6ff152009-10-12 15:32:0762#include "chrome/browser/renderer_host/site_instance.h"
[email protected]96d185d2009-04-24 03:28:5463#include "chrome/browser/renderer_host/web_cache_manager.h"
[email protected]93623c5d2009-12-10 21:40:3264#include "chrome/browser/renderer_preferences_util.h"
[email protected]8b62334b2010-08-31 22:37:1165#include "chrome/browser/search_engines/template_url.h"
[email protected]152b8ba32010-03-29 19:40:1666#include "chrome/browser/search_engines/template_url_fetcher.h"
[email protected]0b5b85c2010-10-11 23:00:1267#include "chrome/browser/search_engines/template_url_fetcher_ui_callbacks.h"
[email protected]152b8ba32010-03-29 19:40:1668#include "chrome/browser/search_engines/template_url_model.h"
[email protected]c37e3b62010-01-22 00:15:1769#include "chrome/browser/sessions/session_types.h"
[email protected]bd1ad682009-05-15 22:19:1770#include "chrome/browser/tab_contents/infobar_delegate.h"
[email protected]7e383692009-06-12 19:14:5471#include "chrome/browser/tab_contents/interstitial_page.h"
[email protected]f3ec7742009-01-15 00:59:1672#include "chrome/browser/tab_contents/navigation_entry.h"
[email protected]de34a962009-06-26 23:22:4373#include "chrome/browser/tab_contents/provisional_load_details.h"
[email protected]f3ec7742009-01-15 00:59:1674#include "chrome/browser/tab_contents/tab_contents_delegate.h"
[email protected]1f18184a2010-07-21 19:34:4975#include "chrome/browser/tab_contents/tab_contents_ssl_helper.h"
[email protected]fdd61c62009-04-22 19:22:5776#include "chrome/browser/tab_contents/tab_contents_view.h"
[email protected]d54169e92011-01-21 09:19:5277#include "chrome/browser/tab_contents/thumbnail_generator.h"
[email protected]3c9e1872010-11-18 16:17:4978#include "chrome/browser/tab_contents/web_navigation_observer.h"
[email protected]152b8ba32010-03-29 19:40:1679#include "chrome/browser/translate/page_translated_details.h"
[email protected]1caa1522010-12-01 20:23:3580#include "chrome/browser/ui/app_modal_dialogs/message_box_handler.h"
[email protected]45300ad42010-12-02 15:51:1481#include "chrome/browser/ui/find_bar/find_bar_state.h"
[email protected]54ec6472010-04-09 19:39:5882#include "chrome/common/bindings_policy.h"
[email protected]35f7d212009-04-29 21:19:2783#include "chrome/common/chrome_switches.h"
[email protected]de4761b2010-12-07 21:05:2184#include "chrome/common/content_restriction.h"
[email protected]3b355642010-02-05 16:01:4985#include "chrome/common/extensions/extension.h"
[email protected]5d246db22009-10-27 06:17:5786#include "chrome/common/extensions/extension_action.h"
[email protected]807871f2010-09-16 01:04:4887#include "chrome/common/extensions/extension_icon_set.h"
[email protected]942690b132010-05-11 06:42:1488#include "chrome/common/extensions/extension_resource.h"
[email protected]12802702010-07-09 19:43:0989#include "chrome/common/extensions/url_pattern.h"
90#include "chrome/common/navigation_types.h"
91#include "chrome/common/net/url_request_context_getter.h"
[email protected]bfd04a62009-02-01 18:16:5692#include "chrome/common/notification_service.h"
initial.commit09911bf2008-07-26 23:55:2993#include "chrome/common/pref_names.h"
[email protected]420ae012009-04-24 05:16:3294#include "chrome/common/render_messages.h"
[email protected]939856a2010-08-24 20:29:0295#include "chrome/common/render_messages_params.h"
[email protected]96d185d2009-04-24 03:28:5496#include "chrome/common/url_constants.h"
[email protected]ef0d04c2010-12-15 23:34:2197#include "gfx/codec/png_codec.h"
[email protected]ea68c06a2010-04-23 17:25:3798#include "grit/chromium_strings.h"
[email protected]8897c382009-05-06 17:53:2699#include "grit/generated_resources.h"
[email protected]420ae012009-04-24 05:16:32100#include "grit/locale_settings.h"
[email protected]fd626782010-01-22 05:26:06101#include "grit/platform_locale_settings.h"
[email protected]ea68c06a2010-04-23 17:25:37102#include "grit/theme_resources.h"
[email protected]d686e812009-06-03 19:10:29103#include "net/base/net_util.h"
[email protected]420ae012009-04-24 05:16:32104#include "net/base/registry_controlled_domain.h"
[email protected]8bd0fe62011-01-17 06:44:37105#include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
[email protected]c051a1b2011-01-21 23:30:17106#include "ui/base/l10n/l10n_util.h"
[email protected]42ce29d2011-01-20 23:19:46107#include "ui/base/resource/resource_bundle.h"
[email protected]d5e311b2010-07-20 17:15:03108#include "webkit/glue/password_form.h"
[email protected]191eb3f72010-12-21 06:27:50109#include "webkit/plugins/npapi/plugin_list.h"
[email protected]3c733bde2010-12-21 19:56:31110#include "webkit/glue/webpreferences.h"
111
112#if defined(OS_MACOSX)
113#include "app/surface/io_surface_support_mac.h"
114#endif // defined(OS_MACOSX)
[email protected]3e45ba92009-02-20 21:09:00115
[email protected]61d68ef12011-01-13 14:02:56116#if defined(OS_CHROMEOS)
117#include "chrome/browser/chromeos/locale_change_guard.h"
118#endif // defined(OS_CHROMEOS)
119
[email protected]420ae012009-04-24 05:16:32120// Cross-Site Navigations
121//
122// If a TabContents is told to navigate to a different web site (as determined
123// by SiteInstance), it will replace its current RenderViewHost with a new
124// RenderViewHost dedicated to the new SiteInstance. This works as follows:
125//
126// - Navigate determines whether the destination is cross-site, and if so,
127// it creates a pending_render_view_host_ and moves into the PENDING
128// RendererState.
129// - The pending RVH is "suspended," so that no navigation messages are sent to
130// its renderer until the onbeforeunload JavaScript handler has a chance to
131// run in the current RVH.
132// - The pending RVH tells CrossSiteRequestManager (a thread-safe singleton)
133// that it has a pending cross-site request. ResourceDispatcherHost will
134// check for this when the response arrives.
135// - The current RVH runs its onbeforeunload handler. If it returns false, we
136// cancel all the pending logic and go back to NORMAL. Otherwise we allow
137// the pending RVH to send the navigation request to its renderer.
138// - ResourceDispatcherHost receives a ResourceRequest on the IO thread. It
139// checks CrossSiteRequestManager to see that the RVH responsible has a
140// pending cross-site request, and then installs a CrossSiteEventHandler.
141// - When RDH receives a response, the BufferedEventHandler determines whether
142// it is a download. If so, it sends a message to the new renderer causing
143// it to cancel the request, and the download proceeds in the download
144// thread. For now, we stay in a PENDING state (with a pending RVH) until
[email protected]57c6a652009-05-04 07:58:34145// the next DidNavigate event for this TabContents. This isn't ideal, but it
[email protected]420ae012009-04-24 05:16:32146// doesn't affect any functionality.
147// - After RDH receives a response and determines that it is safe and not a
148// download, it pauses the response to first run the old page's onunload
149// handler. It does this by asynchronously calling the OnCrossSiteResponse
[email protected]57c6a652009-05-04 07:58:34150// method of TabContents on the UI thread, which sends a ClosePage message
[email protected]420ae012009-04-24 05:16:32151// to the current RVH.
152// - Once the onunload handler is finished, a ClosePage_ACK message is sent to
153// the ResourceDispatcherHost, who unpauses the response. Data is then sent
154// to the pending RVH.
155// - The pending renderer sends a FrameNavigate message that invokes the
156// DidNavigate method. This replaces the current RVH with the
157// pending RVH and goes back to the NORMAL RendererState.
158
159namespace {
160
161// Amount of time we wait between when a key event is received and the renderer
162// is queried for its state and pushed to the NavigationEntry.
163const int kQueryStateDelay = 5000;
164
[email protected]6ebdc9b2010-09-27 16:55:57165const int kSyncWaitDelay = 40;
166
[email protected]420ae012009-04-24 05:16:32167// If another javascript message box is displayed within
168// kJavascriptMessageExpectedDelay of a previous javascript message box being
169// dismissed, display an option to suppress future message boxes from this
170// contents.
171const int kJavascriptMessageExpectedDelay = 1000;
172
[email protected]420ae012009-04-24 05:16:32173// The list of prefs we want to observe.
[email protected]57ecc4b2010-08-11 03:02:51174const char* kPrefsToObserve[] = {
[email protected]420ae012009-04-24 05:16:32175 prefs::kAlternateErrorPagesEnabled,
[email protected]d0b8d092010-10-25 04:05:17176 prefs::kDefaultZoomLevel,
[email protected]420ae012009-04-24 05:16:32177 prefs::kWebKitJavaEnabled,
178 prefs::kWebKitJavascriptEnabled,
179 prefs::kWebKitLoadsImagesAutomatically,
180 prefs::kWebKitPluginsEnabled,
181 prefs::kWebKitUsesUniversalDetector,
182 prefs::kWebKitSerifFontFamily,
183 prefs::kWebKitSansSerifFontFamily,
184 prefs::kWebKitFixedFontFamily,
185 prefs::kWebKitDefaultFontSize,
186 prefs::kWebKitDefaultFixedFontSize,
[email protected]6bdf05b2010-10-13 20:34:13187 prefs::kWebKitMinimumFontSize,
188 prefs::kWebKitMinimumLogicalFontSize,
[email protected]1661a3c02010-05-20 16:32:36189 prefs::kWebkitTabsToLinks,
[email protected]420ae012009-04-24 05:16:32190 prefs::kDefaultCharset
191 // kWebKitStandardFontIsSerif needs to be added
192 // if we let users pick which font to use, serif or sans-serif when
193 // no font is specified or a CSS generic family (serif or sans-serif)
194 // is not specified.
195};
196
197const int kPrefsToObserveLength = arraysize(kPrefsToObserve);
198
[email protected]420ae012009-04-24 05:16:32199// Returns true if the entry's transition type is FORM_SUBMIT.
200bool IsFormSubmit(const NavigationEntry* entry) {
201 return (PageTransition::StripQualifier(entry->transition_type()) ==
202 PageTransition::FORM_SUBMIT);
203}
204
205#if defined(OS_WIN)
206
207BOOL CALLBACK InvalidateWindow(HWND hwnd, LPARAM lparam) {
208 // Note: erase is required to properly paint some widgets borders. This can
209 // be seen with textfields.
210 InvalidateRect(hwnd, NULL, TRUE);
211 return TRUE;
212}
213#endif
214
[email protected]5e369672009-11-03 23:48:30215ViewMsg_Navigate_Params::NavigationType GetNavigationType(
[email protected]1ccb3568d2010-02-19 10:51:16216 Profile* profile, const NavigationEntry& entry,
217 NavigationController::ReloadType reload_type) {
218 switch (reload_type) {
219 case NavigationController::RELOAD:
220 return ViewMsg_Navigate_Params::RELOAD;
221 case NavigationController::RELOAD_IGNORING_CACHE:
222 return ViewMsg_Navigate_Params::RELOAD_IGNORING_CACHE;
223 case NavigationController::NO_RELOAD:
224 break; // Fall through to rest of function.
225 }
[email protected]5e369672009-11-03 23:48:30226
227 if (entry.restore_type() == NavigationEntry::RESTORE_LAST_SESSION &&
228 profile->DidLastSessionExitCleanly())
229 return ViewMsg_Navigate_Params::RESTORE;
230
231 return ViewMsg_Navigate_Params::NORMAL;
232}
233
[email protected]876bc832010-09-07 16:29:54234void MakeNavigateParams(const NavigationEntry& entry,
235 const NavigationController& controller,
[email protected]1ccb3568d2010-02-19 10:51:16236 NavigationController::ReloadType reload_type,
237 ViewMsg_Navigate_Params* params) {
[email protected]056de2d2009-06-26 16:41:34238 params->page_id = entry.page_id();
[email protected]876bc832010-09-07 16:29:54239 params->pending_history_list_offset = controller.GetIndexOfEntry(&entry);
[email protected]3cc72b12010-03-18 23:03:00240 params->current_history_list_offset = controller.last_committed_entry_index();
241 params->current_history_list_length = controller.entry_count();
[email protected]056de2d2009-06-26 16:41:34242 params->url = entry.url();
243 params->referrer = entry.referrer();
244 params->transition = entry.transition_type();
245 params->state = entry.content_state();
[email protected]3cc72b12010-03-18 23:03:00246 params->navigation_type =
247 GetNavigationType(controller.profile(), entry, reload_type);
[email protected]056de2d2009-06-26 16:41:34248 params->request_time = base::Time::Now();
249}
250
[email protected]f4f50ef2011-01-21 19:01:19251
252// OutdatedPluginInfoBar ------------------------------------------------------
253
[email protected]3c2826852010-12-16 19:09:14254class OutdatedPluginInfoBar : public ConfirmInfoBarDelegate {
[email protected]851b1eb2010-08-09 13:32:29255 public:
[email protected]3c2826852010-12-16 19:09:14256 OutdatedPluginInfoBar(TabContents* tab_contents,
[email protected]851b1eb2010-08-09 13:32:29257 const string16& name,
[email protected]f4f50ef2011-01-21 19:01:19258 const GURL& update_url);
[email protected]f5ec1ba2010-12-22 17:47:40259
[email protected]851b1eb2010-08-09 13:32:29260 private:
[email protected]f4f50ef2011-01-21 19:01:19261 virtual ~OutdatedPluginInfoBar();
262
263 // ConfirmInfoBarDelegate:
264 virtual void InfoBarClosed();
265 virtual SkBitmap* GetIcon() const;
266 virtual string16 GetMessageText() const;
267 virtual int GetButtons() const;
268 virtual string16 GetButtonLabel(InfoBarButton button) const;
269 virtual bool Accept();
270 virtual bool Cancel();
271 virtual string16 GetLinkText();
272 virtual bool LinkClicked(WindowOpenDisposition disposition);
273
[email protected]851b1eb2010-08-09 13:32:29274 TabContents* tab_contents_;
275 string16 name_;
276 GURL update_url_;
277};
278
[email protected]f4f50ef2011-01-21 19:01:19279OutdatedPluginInfoBar::OutdatedPluginInfoBar(TabContents* tab_contents,
280 const string16& name,
281 const GURL& update_url)
282 : ConfirmInfoBarDelegate(tab_contents),
283 tab_contents_(tab_contents),
284 name_(name),
285 update_url_(update_url) {
286 UserMetrics::RecordAction(UserMetricsAction("OutdatedPluginInfobar.Shown"));
287 tab_contents->AddInfoBar(this);
288}
289
290OutdatedPluginInfoBar::~OutdatedPluginInfoBar() {
291}
292
293void OutdatedPluginInfoBar::InfoBarClosed() {
294 UserMetrics::RecordAction(UserMetricsAction("OutdatedPluginInfobar.Closed"));
295 delete this;
296}
297
298SkBitmap* OutdatedPluginInfoBar::GetIcon() const {
299 return ResourceBundle::GetSharedInstance().GetBitmapNamed(
300 IDR_INFOBAR_PLUGIN_INSTALL);
301}
302
303string16 OutdatedPluginInfoBar::GetMessageText() const {
304 return l10n_util::GetStringFUTF16(IDS_PLUGIN_OUTDATED_PROMPT, name_);
305}
306
307int OutdatedPluginInfoBar::GetButtons() const {
308 return BUTTON_OK | BUTTON_CANCEL;
309}
310
311string16 OutdatedPluginInfoBar::GetButtonLabel(InfoBarButton button) const {
312 return l10n_util::GetStringUTF16((button == BUTTON_OK) ?
313 IDS_PLUGIN_UPDATE : IDS_PLUGIN_ENABLE_TEMPORARILY);
314}
315
316bool OutdatedPluginInfoBar::Accept() {
317 UserMetrics::RecordAction(UserMetricsAction("OutdatedPluginInfobar.Update"));
318 tab_contents_->OpenURL(update_url_, GURL(), NEW_FOREGROUND_TAB,
319 PageTransition::LINK);
320 return false;
321}
322
323bool OutdatedPluginInfoBar::Cancel() {
324 UserMetrics::RecordAction(
325 UserMetricsAction("OutdatedPluginInfobar.AllowThisTime"));
326 tab_contents_->render_view_host()->LoadBlockedPlugins();
327 return false;
328}
329
330string16 OutdatedPluginInfoBar::GetLinkText() {
331 return l10n_util::GetStringUTF16(IDS_LEARN_MORE);
332}
333
334bool OutdatedPluginInfoBar::LinkClicked(WindowOpenDisposition disposition) {
335 UserMetrics::RecordAction(
336 UserMetricsAction("OutdatedPluginInfobar.LearnMore"));
337 // TODO(bauerb): Navigate to a help page explaining why we disabled
338 // the plugin, once we have one.
339 return false;
340}
341
[email protected]420ae012009-04-24 05:16:32342} // namespace
343
[email protected]f4f50ef2011-01-21 19:01:19344
345// TabContents ----------------------------------------------------------------
[email protected]420ae012009-04-24 05:16:32346
[email protected]fdd61c62009-04-22 19:22:57347// static
348int TabContents::find_request_id_counter_ = -1;
349
[email protected]420ae012009-04-24 05:16:32350
[email protected]420ae012009-04-24 05:16:32351TabContents::TabContents(Profile* profile,
352 SiteInstance* site_instance,
353 int routing_id,
[email protected]6ee12c42010-09-14 09:36:07354 const TabContents* base_tab_contents,
355 SessionStorageNamespace* session_storage_namespace)
[email protected]b680ad22009-04-15 23:19:42356 : delegate_(NULL),
[email protected]6ee12c42010-09-14 09:36:07357 ALLOW_THIS_IN_INITIALIZER_LIST(controller_(
358 this, profile, session_storage_namespace)),
[email protected]66ba4932009-06-04 19:22:13359 ALLOW_THIS_IN_INITIALIZER_LIST(view_(
[email protected]d82ed61e2009-06-16 02:46:22360 TabContentsView::Create(this))),
361 ALLOW_THIS_IN_INITIALIZER_LIST(render_manager_(this, this)),
[email protected]fdd61c62009-04-22 19:22:57362 property_bag_(),
363 registrar_(),
[email protected]12802702010-07-09 19:43:09364 ALLOW_THIS_IN_INITIALIZER_LIST(printing_(
365 new printing::PrintViewManager(*this))),
[email protected]fdd61c62009-04-22 19:22:57366 save_package_(),
[email protected]6db0def92010-03-02 19:55:45367 autocomplete_history_manager_(),
[email protected]21674df2009-10-28 19:51:21368 autofill_manager_(),
[email protected]fdd61c62009-04-22 19:22:57369 plugin_installer_(),
[email protected]6f821dd2010-02-23 00:54:06370 bookmark_drag_(NULL),
[email protected]d82ed61e2009-06-16 02:46:22371 ALLOW_THIS_IN_INITIALIZER_LIST(fav_icon_helper_(this)),
[email protected]d5f942ba2008-09-26 19:30:34372 is_loading_(false),
[email protected]443b80e2010-12-14 00:42:23373 crashed_status_(base::TERMINATION_STATUS_STILL_RUNNING),
374 crashed_error_code_(0),
[email protected]d5f942ba2008-09-26 19:30:34375 waiting_for_response_(false),
[email protected]d5f942ba2008-09-26 19:30:34376 max_page_id_(-1),
[email protected]fdd61c62009-04-22 19:22:57377 current_load_start_(),
378 load_state_(net::LOAD_STATE_IDLE),
379 load_state_host_(),
[email protected]094e5b22009-09-25 04:23:56380 upload_size_(0),
381 upload_position_(0),
[email protected]fdd61c62009-04-22 19:22:57382 received_page_title_(false),
383 is_starred_(false),
384 contents_mime_type_(),
385 encoding_(),
[email protected]c65e2f152010-10-14 15:30:40386 blocked_contents_(NULL),
387 all_contents_blocked_(false),
[email protected]8cc8d492010-02-02 10:40:49388 dont_notify_render_view_(false),
[email protected]f17a0ee2010-05-17 17:38:47389 displayed_insecure_content_(false),
[email protected]fdd61c62009-04-22 19:22:57390 infobar_delegates_(),
[email protected]fdd61c62009-04-22 19:22:57391 find_ui_active_(false),
392 find_op_aborted_(false),
393 current_find_request_id_(find_request_id_counter_++),
[email protected]e491f1c2009-05-22 20:28:12394 last_search_case_sensitive_(false),
[email protected]e491f1c2009-05-22 20:28:12395 last_search_result_(),
[email protected]98aa0b52010-05-06 17:03:08396 extension_app_(NULL),
[email protected]fdd61c62009-04-22 19:22:57397 capturing_contents_(false),
398 is_being_destroyed_(false),
399 notify_disconnection_(false),
400 history_requests_(),
401#if defined(OS_WIN)
402 message_box_active_(CreateEvent(NULL, TRUE, FALSE, NULL)),
403#endif
404 last_javascript_message_dismissal_(),
[email protected]c2e74fe82009-09-03 17:57:44405 suppress_javascript_messages_(false),
[email protected]7ab1e7d62009-10-14 23:32:01406 is_showing_before_unload_dialog_(false),
[email protected]7ff431e2010-01-07 18:18:54407 renderer_preferences_(),
[email protected]1a242c32010-02-02 21:20:54408 opener_dom_ui_type_(DOMUIFactory::kNoDOMUI),
[email protected]777a7b62010-05-11 13:28:16409 language_state_(&controller_),
[email protected]ebf40a72010-07-22 01:46:38410 closed_by_user_gesture_(false),
[email protected]b75b8292010-10-01 07:28:25411 minimum_zoom_percent_(
412 static_cast<int>(WebKit::WebView::minTextSizeMultiplier * 100)),
413 maximum_zoom_percent_(
414 static_cast<int>(WebKit::WebView::maxTextSizeMultiplier * 100)),
[email protected]9e823662010-10-13 23:36:00415 temporary_zoom_settings_(false),
416 content_restrictions_(0) {
[email protected]7ff431e2010-01-07 18:18:54417 renderer_preferences_util::UpdateFromSystemSettings(
418 &renderer_preferences_, profile);
419
[email protected]9fb83e82010-07-02 18:24:55420 content_settings_delegate_.reset(
421 new TabSpecificContentSettings(this, profile));
422
[email protected]12636df2009-09-28 22:32:21423 render_manager_.Init(profile, site_instance, routing_id);
[email protected]420ae012009-04-24 05:16:32424
[email protected]34ac70502009-09-25 17:07:23425 // We have the initial size of the view be based on the size of the passed in
426 // tab contents (normally a tab from the same window).
427 view_->CreateView(base_tab_contents ?
428 base_tab_contents->view()->GetContainerSize() : gfx::Size());
[email protected]420ae012009-04-24 05:16:32429
430 // Register for notifications about all interested prefs change.
431 PrefService* prefs = profile->GetPrefs();
[email protected]2fb7dc982010-09-29 12:24:28432 pref_change_registrar_.Init(prefs);
[email protected]420ae012009-04-24 05:16:32433 if (prefs) {
434 for (int i = 0; i < kPrefsToObserveLength; ++i)
[email protected]2fb7dc982010-09-29 12:24:28435 pref_change_registrar_.Add(kPrefsToObserve[i], this);
[email protected]420ae012009-04-24 05:16:32436 }
437
438 // Register for notifications about URL starredness changing on any profile.
439 registrar_.Add(this, NotificationType::URLS_STARRED,
440 NotificationService::AllSources());
441 registrar_.Add(this, NotificationType::BOOKMARK_MODEL_LOADED,
442 NotificationService::AllSources());
443 registrar_.Add(this, NotificationType::RENDER_WIDGET_HOST_DESTROYED,
444 NotificationService::AllSources());
[email protected]d3ba77272009-09-03 00:06:09445#if defined(OS_LINUX)
446 registrar_.Add(this, NotificationType::BROWSER_THEME_CHANGED,
447 NotificationService::AllSources());
448#endif
[email protected]420ae012009-04-24 05:16:32449
[email protected]6c32ce72010-03-08 05:18:06450 registrar_.Add(this, NotificationType::USER_STYLE_SHEET_UPDATED,
451 NotificationService::AllSources());
452
[email protected]79580c62010-02-02 02:36:25453 // Register for notifications about content setting changes.
454 registrar_.Add(this, NotificationType::CONTENT_SETTINGS_CHANGED,
455 NotificationService::AllSources());
456
[email protected]a888b29e62010-04-01 13:38:57457 // Listen for extension changes so we can update extension_for_current_page_.
458 registrar_.Add(this, NotificationType::EXTENSION_LOADED,
459 NotificationService::AllSources());
460 registrar_.Add(this, NotificationType::EXTENSION_UNLOADED,
461 NotificationService::AllSources());
[email protected]a888b29e62010-04-01 13:38:57462
[email protected]88152052010-10-01 04:05:18463 // Listen for Google URL changes
464 registrar_.Add(this, NotificationType::GOOGLE_URL_UPDATED,
465 NotificationService::AllSources());
466
[email protected]c8865482009-07-23 20:40:10467 // Set-up the showing of the omnibox search infobar if applicable.
468 if (OmniboxSearchHint::IsEnabled(profile))
469 omnibox_search_hint_.reset(new OmniboxSearchHint(this));
[email protected]403415a2011-01-10 18:57:53470
471 autofill_manager_.reset(new AutoFillManager(this));
[email protected]0f180ee2011-01-21 18:23:10472 AddNavigationObserver(autofill_manager_.get());
[email protected]403415a2011-01-10 18:57:53473 autocomplete_history_manager_.reset(new AutocompleteHistoryManager(this));
[email protected]0f180ee2011-01-21 18:23:10474 AddNavigationObserver(autocomplete_history_manager_.get());
[email protected]332af7732009-03-11 13:21:35475}
initial.commit09911bf2008-07-26 23:55:29476
477TabContents::~TabContents() {
[email protected]420ae012009-04-24 05:16:32478 is_being_destroyed_ = true;
479
[email protected]bfe4c1582009-10-02 18:11:09480 // We don't want any notifications while we're running our destructor.
[email protected]420ae012009-04-24 05:16:32481 registrar_.RemoveAll();
[email protected]2fb7dc982010-09-29 12:24:28482 pref_change_registrar_.RemoveAll();
[email protected]420ae012009-04-24 05:16:32483
[email protected]420ae012009-04-24 05:16:32484 NotifyDisconnected();
[email protected]e7b8d832009-11-20 22:41:00485 hung_renderer_dialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:32486
[email protected]420ae012009-04-24 05:16:32487 // First cleanly close all child windows.
488 // TODO(mpcomplete): handle case if MaybeCloseChildWindows() already asked
489 // some of these to close. CloseWindows is async, so it might get called
490 // twice before it runs.
[email protected]8cc8d492010-02-02 10:40:49491 CloseConstrainedWindows();
[email protected]420ae012009-04-24 05:16:32492
[email protected]c65e2f152010-10-14 15:30:40493 // Close all blocked contents.
494 if (blocked_contents_)
495 blocked_contents_->Destroy();
[email protected]66ba4932009-06-04 19:22:13496
[email protected]420ae012009-04-24 05:16:32497 // Notify any observer that have a reference on this tab contents.
498 NotificationService::current()->Notify(
499 NotificationType::TAB_CONTENTS_DESTROYED,
500 Source<TabContents>(this),
501 NotificationService::NoDetails());
502
[email protected]1e5e74f2009-05-27 20:55:12503 // Notify any lasting InfobarDelegates that have not yet been removed that
504 // whatever infobar they were handling in this TabContents has closed,
505 // because the TabContents is going away entirely.
506 // This must happen after the TAB_CONTENTS_DESTROYED notification as the
507 // notification may trigger infobars calls that access their delegate. (and
508 // some implementations of InfoBarDelegate do delete themselves on
509 // InfoBarClosed()).
510 for (int i = 0; i < infobar_delegate_count(); ++i) {
511 InfoBarDelegate* delegate = GetInfoBarDelegateAt(i);
512 delegate->InfoBarClosed();
513 }
514 infobar_delegates_.clear();
515
[email protected]420ae012009-04-24 05:16:32516 // TODO(brettw) this should be moved to the view.
517#if defined(OS_WIN)
518 // If we still have a window handle, destroy it. GetNativeView can return
519 // NULL if this contents was part of a window that closed.
[email protected]50bd6452010-11-27 19:39:42520 if (GetNativeView()) {
521 RenderViewHost* host = render_view_host();
522 if (host && host->view()) {
523 host->view()->WillWmDestroy();
524 }
[email protected]420ae012009-04-24 05:16:32525 ::DestroyWindow(GetNativeView());
[email protected]50bd6452010-11-27 19:39:42526 }
[email protected]420ae012009-04-24 05:16:32527#endif
[email protected]7ab1e7d62009-10-14 23:32:01528
529 // OnCloseStarted isn't called in unit tests.
530 if (!tab_close_start_time_.is_null()) {
531 UMA_HISTOGRAM_TIMES("Tab.Close",
532 base::TimeTicks::Now() - tab_close_start_time_);
533 }
initial.commit09911bf2008-07-26 23:55:29534}
535
[email protected]d5f942ba2008-09-26 19:30:34536// static
537void TabContents::RegisterUserPrefs(PrefService* prefs) {
[email protected]420ae012009-04-24 05:16:32538 prefs->RegisterBooleanPref(prefs::kAlternateErrorPagesEnabled, true);
539
540 WebPreferences pref_defaults;
541 prefs->RegisterBooleanPref(prefs::kWebKitJavascriptEnabled,
542 pref_defaults.javascript_enabled);
543 prefs->RegisterBooleanPref(prefs::kWebKitWebSecurityEnabled,
544 pref_defaults.web_security_enabled);
545 prefs->RegisterBooleanPref(
546 prefs::kWebKitJavascriptCanOpenWindowsAutomatically, true);
547 prefs->RegisterBooleanPref(prefs::kWebKitLoadsImagesAutomatically,
548 pref_defaults.loads_images_automatically);
549 prefs->RegisterBooleanPref(prefs::kWebKitPluginsEnabled,
550 pref_defaults.plugins_enabled);
551 prefs->RegisterBooleanPref(prefs::kWebKitDomPasteEnabled,
552 pref_defaults.dom_paste_enabled);
553 prefs->RegisterBooleanPref(prefs::kWebKitShrinksStandaloneImagesToFit,
554 pref_defaults.shrinks_standalone_images_to_fit);
[email protected]c4e98902010-06-01 10:20:14555 prefs->RegisterDictionaryPref(prefs::kWebKitInspectorSettings);
[email protected]420ae012009-04-24 05:16:32556 prefs->RegisterBooleanPref(prefs::kWebKitTextAreasAreResizable,
557 pref_defaults.text_areas_are_resizable);
558 prefs->RegisterBooleanPref(prefs::kWebKitJavaEnabled,
559 pref_defaults.java_enabled);
[email protected]1661a3c02010-05-20 16:32:36560 prefs->RegisterBooleanPref(prefs::kWebkitTabsToLinks,
561 pref_defaults.tabs_to_links);
[email protected]420ae012009-04-24 05:16:32562
563 prefs->RegisterLocalizedStringPref(prefs::kAcceptLanguages,
564 IDS_ACCEPT_LANGUAGES);
565 prefs->RegisterLocalizedStringPref(prefs::kDefaultCharset,
566 IDS_DEFAULT_ENCODING);
567 prefs->RegisterLocalizedBooleanPref(prefs::kWebKitStandardFontIsSerif,
568 IDS_STANDARD_FONT_IS_SERIF);
569 prefs->RegisterLocalizedStringPref(prefs::kWebKitFixedFontFamily,
570 IDS_FIXED_FONT_FAMILY);
571 prefs->RegisterLocalizedStringPref(prefs::kWebKitSerifFontFamily,
572 IDS_SERIF_FONT_FAMILY);
573 prefs->RegisterLocalizedStringPref(prefs::kWebKitSansSerifFontFamily,
574 IDS_SANS_SERIF_FONT_FAMILY);
575 prefs->RegisterLocalizedStringPref(prefs::kWebKitCursiveFontFamily,
576 IDS_CURSIVE_FONT_FAMILY);
577 prefs->RegisterLocalizedStringPref(prefs::kWebKitFantasyFontFamily,
578 IDS_FANTASY_FONT_FAMILY);
579 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitDefaultFontSize,
580 IDS_DEFAULT_FONT_SIZE);
581 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitDefaultFixedFontSize,
582 IDS_DEFAULT_FIXED_FONT_SIZE);
583 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitMinimumFontSize,
584 IDS_MINIMUM_FONT_SIZE);
585 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitMinimumLogicalFontSize,
586 IDS_MINIMUM_LOGICAL_FONT_SIZE);
587 prefs->RegisterLocalizedBooleanPref(prefs::kWebKitUsesUniversalDetector,
588 IDS_USES_UNIVERSAL_DETECTOR);
589 prefs->RegisterLocalizedStringPref(prefs::kStaticEncodings,
590 IDS_STATIC_ENCODING_LIST);
initial.commit09911bf2008-07-26 23:55:29591}
592
[email protected]724159a2010-12-30 01:11:18593bool TabContents::OnMessageReceived(const IPC::Message& message) {
[email protected]0f180ee2011-01-21 18:23:10594 ObserverListBase<WebNavigationObserver>::Iterator it(
595 web_navigation_observers_);
596 WebNavigationObserver* observer;
597 while ((observer = it.GetNext()) != NULL)
598 if (observer->OnMessageReceived(message))
[email protected]403415a2011-01-10 18:57:53599 return true;
[email protected]403415a2011-01-10 18:57:53600
[email protected]724159a2010-12-30 01:11:18601 bool handled = true;
602 bool message_is_ok = true;
603 IPC_BEGIN_MESSAGE_MAP_EX(TabContents, message, message_is_ok)
604 IPC_MESSAGE_HANDLER(ViewHostMsg_DidStartProvisionalLoadForFrame,
605 OnDidStartProvisionalLoadForFrame)
606 IPC_MESSAGE_HANDLER(ViewHostMsg_DidRedirectProvisionalLoad,
607 OnDidRedirectProvisionalLoad)
608 IPC_MESSAGE_HANDLER(ViewHostMsg_DidFailProvisionalLoadWithError,
609 OnDidFailProvisionalLoadWithError)
610 IPC_MESSAGE_HANDLER(ViewHostMsg_DidLoadResourceFromMemoryCache,
611 OnDidLoadResourceFromMemoryCache)
612 IPC_MESSAGE_HANDLER(ViewHostMsg_DidDisplayInsecureContent,
613 OnDidDisplayInsecureContent)
614 IPC_MESSAGE_HANDLER(ViewHostMsg_DidRunInsecureContent,
615 OnDidRunInsecureContent)
616 IPC_MESSAGE_HANDLER(ViewHostMsg_DocumentLoadedInFrame,
617 OnDocumentLoadedInFrame)
618 IPC_MESSAGE_HANDLER(ViewHostMsg_DidFinishLoad, OnDidFinishLoad)
[email protected]c8f73ab2011-01-22 00:05:17619 IPC_MESSAGE_HANDLER(ViewHostMsg_UpdateContentRestrictions,
620 OnUpdateContentRestrictions)
621 IPC_MESSAGE_HANDLER(ViewHostMsg_PDFHasUnsupportedFeature,
622 OnPDFHasUnsupportedFeature)
[email protected]724159a2010-12-30 01:11:18623 IPC_MESSAGE_UNHANDLED(handled = false)
624 IPC_END_MESSAGE_MAP_EX()
625
626 if (!message_is_ok) {
627 UserMetrics::RecordAction(UserMetricsAction("BadMessageTerminate_RVD"));
628 GetRenderProcessHost()->ReceivedBadMessage();
629 }
630
631 return handled;
632}
633
[email protected]13367f72009-07-08 16:39:02634// Returns true if contains content rendered by an extension.
635bool TabContents::HostsExtension() const {
636 return GetURL().SchemeIs(chrome::kExtensionScheme);
[email protected]d5f942ba2008-09-26 19:30:34637}
638
[email protected]f4f50ef2011-01-21 19:01:19639PluginInstallerInfoBarDelegate* TabContents::GetPluginInstaller() {
[email protected]6e95706d2009-04-23 22:55:01640 if (plugin_installer_.get() == NULL)
[email protected]f4f50ef2011-01-21 19:01:19641 plugin_installer_.reset(new PluginInstallerInfoBarDelegate(this));
[email protected]6e95706d2009-04-23 22:55:01642 return plugin_installer_.get();
643}
644
[email protected]1f18184a2010-07-21 19:34:49645TabContentsSSLHelper* TabContents::GetSSLHelper() {
646 if (ssl_helper_.get() == NULL)
647 ssl_helper_.reset(new TabContentsSSLHelper(this));
648 return ssl_helper_.get();
649}
650
[email protected]8cb5d5b2010-02-09 11:36:16651RenderProcessHost* TabContents::GetRenderProcessHost() const {
652 return render_manager_.current_host()->process();
653}
654
[email protected]9adb9692010-10-29 23:14:02655void TabContents::SetExtensionApp(const Extension* extension) {
[email protected]867a73e12010-03-19 20:45:46656 DCHECK(!extension || extension->GetFullLaunchURL().is_valid());
[email protected]98aa0b52010-05-06 17:03:08657 extension_app_ = extension;
[email protected]fca656c2010-02-10 20:30:10658
[email protected]98aa0b52010-05-06 17:03:08659 UpdateExtensionAppIcon(extension_app_);
[email protected]a888b29e62010-04-01 13:38:57660
[email protected]fca656c2010-02-10 20:30:10661 NotificationService::current()->Notify(
662 NotificationType::TAB_CONTENTS_APPLICATION_EXTENSION_CHANGED,
663 Source<TabContents>(this),
664 NotificationService::NoDetails());
[email protected]3b355642010-02-05 16:01:49665}
666
[email protected]98aa0b52010-05-06 17:03:08667void TabContents::SetExtensionAppById(const std::string& extension_app_id) {
668 if (extension_app_id.empty())
[email protected]911f8152010-03-18 16:46:40669 return;
670
[email protected]eaa7dd182010-12-14 11:09:00671 ExtensionService* extension_service = profile()->GetExtensionService();
[email protected]fa6a9102010-11-22 15:38:50672 if (!extension_service || !extension_service->is_ready())
673 return;
674
675 const Extension* extension =
676 extension_service->GetExtensionById(extension_app_id, false);
677 if (extension)
678 SetExtensionApp(extension);
[email protected]911f8152010-03-18 16:46:40679}
680
[email protected]98aa0b52010-05-06 17:03:08681SkBitmap* TabContents::GetExtensionAppIcon() {
[email protected]98aa0b52010-05-06 17:03:08682 if (extension_app_icon_.empty())
[email protected]8eb043b2010-04-30 07:19:51683 return NULL;
684
[email protected]98aa0b52010-05-06 17:03:08685 return &extension_app_icon_;
[email protected]8eb043b2010-04-30 07:19:51686}
687
[email protected]d5f942ba2008-09-26 19:30:34688const GURL& TabContents::GetURL() const {
689 // We may not have a navigation entry yet
[email protected]ce3fa3c2009-04-20 19:55:57690 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]ebe89e062009-08-13 23:16:54691 return entry ? entry->virtual_url() : GURL::EmptyGURL();
[email protected]d5f942ba2008-09-26 19:30:34692}
693
[email protected]96d185d2009-04-24 03:28:54694const string16& TabContents::GetTitle() const {
[email protected]4c6092c5b2009-06-06 00:23:55695 // Transient entries take precedence. They are used for interstitial pages
696 // that are shown on top of existing pages.
697 NavigationEntry* entry = controller_.GetTransientEntry();
[email protected]45d0ef7f2011-01-05 13:46:23698 if (entry) {
699 return entry->GetTitleForDisplay(profile()->GetPrefs()->
700 GetString(prefs::kAcceptLanguages));
701 }
[email protected]96d185d2009-04-24 03:28:54702 DOMUI* our_dom_ui = render_manager_.pending_dom_ui() ?
703 render_manager_.pending_dom_ui() : render_manager_.dom_ui();
704 if (our_dom_ui) {
705 // Don't override the title in view source mode.
[email protected]4c6092c5b2009-06-06 00:23:55706 entry = controller_.GetActiveEntry();
[email protected]96d185d2009-04-24 03:28:54707 if (!(entry && entry->IsViewSourceMode())) {
708 // Give the DOM UI the chance to override our title.
709 const string16& title = our_dom_ui->overridden_title();
710 if (!title.empty())
711 return title;
712 }
713 }
714
715 // We use the title for the last committed entry rather than a pending
716 // navigation entry. For example, when the user types in a URL, we want to
717 // keep the old page's title until the new load has committed and we get a new
718 // title.
[email protected]96d185d2009-04-24 03:28:54719 entry = controller_.GetLastCommittedEntry();
[email protected]45d0ef7f2011-01-05 13:46:23720 if (entry) {
721 return entry->GetTitleForDisplay(profile()->GetPrefs()->
722 GetString(prefs::kAcceptLanguages));
723 }
[email protected]96d185d2009-04-24 03:28:54724 return EmptyString16();
725}
726
[email protected]bbbc1ef2010-02-12 18:03:41727// static
728string16 TabContents::GetDefaultTitle() {
729 return l10n_util::GetStringUTF16(IDS_DEFAULT_TAB_TITLE);
730}
731
[email protected]d5f942ba2008-09-26 19:30:34732int32 TabContents::GetMaxPageID() {
733 if (GetSiteInstance())
734 return GetSiteInstance()->max_page_id();
735 else
736 return max_page_id_;
737}
738
739void TabContents::UpdateMaxPageID(int32 page_id) {
740 // Ensure both the SiteInstance and RenderProcessHost update their max page
[email protected]57c6a652009-05-04 07:58:34741 // IDs in sync. Only TabContents will also have site instances, except during
[email protected]d5f942ba2008-09-26 19:30:34742 // testing.
743 if (GetSiteInstance())
744 GetSiteInstance()->UpdateMaxPageID(page_id);
[email protected]8cb5d5b2010-02-09 11:36:16745 GetRenderProcessHost()->UpdateMaxPageID(page_id);
[email protected]d5f942ba2008-09-26 19:30:34746}
747
[email protected]96d185d2009-04-24 03:28:54748SiteInstance* TabContents::GetSiteInstance() const {
749 return render_manager_.current_host()->site_instance();
750}
751
[email protected]96d185d2009-04-24 03:28:54752bool TabContents::ShouldDisplayURL() {
[email protected]8ab24cc2009-11-13 20:56:15753 // Don't hide the url in view source mode and with interstitials.
[email protected]96d185d2009-04-24 03:28:54754 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]8ab24cc2009-11-13 20:56:15755 if (entry && (entry->IsViewSourceMode() ||
[email protected]cccd3762010-11-12 18:40:01756 entry->page_type() == INTERSTITIAL_PAGE)) {
[email protected]96d185d2009-04-24 03:28:54757 return true;
[email protected]8ab24cc2009-11-13 20:56:15758 }
[email protected]83e3895b2009-06-11 00:07:16759
[email protected]96d185d2009-04-24 03:28:54760 DOMUI* dom_ui = GetDOMUIForCurrentState();
761 if (dom_ui)
762 return !dom_ui->should_hide_url();
763 return true;
764}
765
[email protected]d5f942ba2008-09-26 19:30:34766SkBitmap TabContents::GetFavIcon() const {
767 // Like GetTitle(), we also want to use the favicon for the last committed
768 // entry rather than a pending navigation entry.
[email protected]ce3fa3c2009-04-20 19:55:57769 NavigationEntry* entry = controller_.GetTransientEntry();
[email protected]cbab76d2008-10-13 22:42:47770 if (entry)
771 return entry->favicon().bitmap();
772
[email protected]ce3fa3c2009-04-20 19:55:57773 entry = controller_.GetLastCommittedEntry();
[email protected]d5f942ba2008-09-26 19:30:34774 if (entry)
775 return entry->favicon().bitmap();
[email protected]d5f942ba2008-09-26 19:30:34776 return SkBitmap();
777}
778
[email protected]0b303cc2009-09-28 22:35:15779bool TabContents::FavIconIsValid() const {
780 NavigationEntry* entry = controller_.GetTransientEntry();
781 if (entry)
782 return entry->favicon().is_valid();
783
784 entry = controller_.GetLastCommittedEntry();
785 if (entry)
786 return entry->favicon().is_valid();
787
788 return false;
789}
790
[email protected]96d185d2009-04-24 03:28:54791bool TabContents::ShouldDisplayFavIcon() {
792 // Always display a throbber during pending loads.
793 if (controller_.GetLastCommittedEntry() && controller_.pending_entry())
794 return true;
795
796 DOMUI* dom_ui = GetDOMUIForCurrentState();
797 if (dom_ui)
798 return !dom_ui->hide_favicon();
799 return true;
800}
801
[email protected]42197a22010-12-28 23:29:42802string16 TabContents::GetStatusText() const {
[email protected]96d185d2009-04-24 03:28:54803 if (!is_loading() || load_state_ == net::LOAD_STATE_IDLE)
[email protected]42197a22010-12-28 23:29:42804 return string16();
[email protected]96d185d2009-04-24 03:28:54805
806 switch (load_state_) {
807 case net::LOAD_STATE_WAITING_FOR_CACHE:
[email protected]42197a22010-12-28 23:29:42808 return l10n_util::GetStringUTF16(IDS_LOAD_STATE_WAITING_FOR_CACHE);
[email protected]8a1f3312010-05-25 19:25:04809 case net::LOAD_STATE_ESTABLISHING_PROXY_TUNNEL:
[email protected]42197a22010-12-28 23:29:42810 return
811 l10n_util::GetStringUTF16(IDS_LOAD_STATE_ESTABLISHING_PROXY_TUNNEL);
[email protected]96d185d2009-04-24 03:28:54812 case net::LOAD_STATE_RESOLVING_PROXY_FOR_URL:
[email protected]42197a22010-12-28 23:29:42813 return l10n_util::GetStringUTF16(IDS_LOAD_STATE_RESOLVING_PROXY_FOR_URL);
[email protected]96d185d2009-04-24 03:28:54814 case net::LOAD_STATE_RESOLVING_HOST:
[email protected]42197a22010-12-28 23:29:42815 return l10n_util::GetStringUTF16(IDS_LOAD_STATE_RESOLVING_HOST);
[email protected]96d185d2009-04-24 03:28:54816 case net::LOAD_STATE_CONNECTING:
[email protected]42197a22010-12-28 23:29:42817 return l10n_util::GetStringUTF16(IDS_LOAD_STATE_CONNECTING);
[email protected]375e3112010-05-27 20:32:06818 case net::LOAD_STATE_SSL_HANDSHAKE:
[email protected]42197a22010-12-28 23:29:42819 return l10n_util::GetStringUTF16(IDS_LOAD_STATE_SSL_HANDSHAKE);
[email protected]96d185d2009-04-24 03:28:54820 case net::LOAD_STATE_SENDING_REQUEST:
[email protected]094e5b22009-09-25 04:23:56821 if (upload_size_)
[email protected]42197a22010-12-28 23:29:42822 return l10n_util::GetStringFUTF16Int(
[email protected]094e5b22009-09-25 04:23:56823 IDS_LOAD_STATE_SENDING_REQUEST_WITH_PROGRESS,
824 static_cast<int>((100 * upload_position_) / upload_size_));
825 else
[email protected]42197a22010-12-28 23:29:42826 return l10n_util::GetStringUTF16(IDS_LOAD_STATE_SENDING_REQUEST);
[email protected]96d185d2009-04-24 03:28:54827 case net::LOAD_STATE_WAITING_FOR_RESPONSE:
[email protected]42197a22010-12-28 23:29:42828 return l10n_util::GetStringFUTF16(IDS_LOAD_STATE_WAITING_FOR_RESPONSE,
829 load_state_host_);
[email protected]96d185d2009-04-24 03:28:54830 // Ignore net::LOAD_STATE_READING_RESPONSE and net::LOAD_STATE_IDLE
831 case net::LOAD_STATE_IDLE:
832 case net::LOAD_STATE_READING_RESPONSE:
833 break;
834 }
835
[email protected]42197a22010-12-28 23:29:42836 return string16();
[email protected]96d185d2009-04-24 03:28:54837}
838
[email protected]3c9e1872010-11-18 16:17:49839void TabContents::AddNavigationObserver(WebNavigationObserver* observer) {
840 web_navigation_observers_.AddObserver(observer);
841}
842
843void TabContents::RemoveNavigationObserver(WebNavigationObserver* observer) {
844 web_navigation_observers_.RemoveObserver(observer);
845}
846
[email protected]443b80e2010-12-14 00:42:23847void TabContents::SetIsCrashed(base::TerminationStatus status, int error_code) {
848 if (status == crashed_status_)
[email protected]d5f942ba2008-09-26 19:30:34849 return;
850
[email protected]443b80e2010-12-14 00:42:23851 crashed_status_ = status;
852 crashed_error_code_ = error_code;
[email protected]c9cd2222009-05-06 05:16:50853 NotifyNavigationStateChanged(INVALIDATE_TAB);
[email protected]d5f942ba2008-09-26 19:30:34854}
855
[email protected]744ef172009-10-16 21:53:46856void TabContents::PageActionStateChanged() {
857 NotifyNavigationStateChanged(TabContents::INVALIDATE_PAGE_ACTIONS);
[email protected]d7eaf572009-07-01 21:57:00858}
[email protected]ce5c4502009-05-06 16:46:11859
[email protected]d5f942ba2008-09-26 19:30:34860void TabContents::NotifyNavigationStateChanged(unsigned changed_flags) {
861 if (delegate_)
862 delegate_->NavigationStateChanged(this, changed_flags);
863}
864
[email protected]96d185d2009-04-24 03:28:54865void TabContents::DidBecomeSelected() {
866 controller_.SetActive(true);
[email protected]8cb5d5b2010-02-09 11:36:16867 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
868 if (rwhv) {
869 rwhv->DidBecomeSelected();
[email protected]789e9152009-08-04 21:59:43870#if defined(OS_MACOSX)
[email protected]8cb5d5b2010-02-09 11:36:16871 rwhv->SetActive(true);
[email protected]789e9152009-08-04 21:59:43872#endif
873 }
[email protected]96d185d2009-04-24 03:28:54874
[email protected]8cb5d5b2010-02-09 11:36:16875 WebCacheManager::GetInstance()->ObserveActivity(GetRenderProcessHost()->id());
[email protected]5ac20162010-11-24 23:33:11876 last_selected_time_ = base::TimeTicks::Now();
[email protected]61d68ef12011-01-13 14:02:56877#if defined(OS_CHROMEOS)
878 chromeos::LocaleChangeGuard::Check(this);
879#endif
[email protected]96d185d2009-04-24 03:28:54880}
881
[email protected]a94454b2010-12-16 04:16:03882void TabContents::FadeForInstant(bool animate) {
[email protected]1b072562010-12-13 19:37:45883 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
884 SkColor whitish = SkColorSetARGB(192, 255, 255, 255);
885 if (rwhv)
[email protected]a94454b2010-12-16 04:16:03886 rwhv->SetVisuallyDeemphasized(&whitish, animate);
[email protected]1b072562010-12-13 19:37:45887}
888
889void TabContents::CancelInstantFade() {
890 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
891 if (rwhv)
892 rwhv->SetVisuallyDeemphasized(NULL, false);
893}
894
[email protected]96d185d2009-04-24 03:28:54895void TabContents::WasHidden() {
896 if (!capturing_contents()) {
897 // |render_view_host()| can be NULL if the user middle clicks a link to open
898 // a tab in then background, then closes the tab before selecting it. This
899 // is because closing the tab calls TabContents::Destroy(), which removes
900 // the |render_view_host()|; then when we actually destroy the window,
901 // OnWindowPosChanged() notices and calls HideContents() (which calls us).
[email protected]8cb5d5b2010-02-09 11:36:16902 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
903 if (rwhv)
904 rwhv->WasHidden();
[email protected]96d185d2009-04-24 03:28:54905 }
906
907 NotificationService::current()->Notify(
908 NotificationType::TAB_CONTENTS_HIDDEN,
909 Source<TabContents>(this),
910 NotificationService::NoDetails());
911}
912
[email protected]d5f942ba2008-09-26 19:30:34913void TabContents::Activate() {
914 if (delegate_)
915 delegate_->ActivateContents(this);
916}
917
[email protected]ea42e7782010-08-23 23:58:12918void TabContents::Deactivate() {
919 if (delegate_)
920 delegate_->DeactivateContents(this);
921}
922
[email protected]96d185d2009-04-24 03:28:54923void TabContents::ShowContents() {
[email protected]8cb5d5b2010-02-09 11:36:16924 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
925 if (rwhv)
926 rwhv->DidBecomeSelected();
[email protected]96d185d2009-04-24 03:28:54927}
928
929void TabContents::HideContents() {
930 // TODO(pkasting): http://b/1239839 Right now we purposefully don't call
931 // our superclass HideContents(), because some callers want to be very picky
932 // about the order in which these get called. In addition to making the code
933 // here practically impossible to understand, this also means we end up
934 // calling TabContents::WasHidden() twice if callers call both versions of
[email protected]57c6a652009-05-04 07:58:34935 // HideContents() on a TabContents.
[email protected]96d185d2009-04-24 03:28:54936 WasHidden();
937}
938
[email protected]5aab5e22010-12-08 22:13:29939bool TabContents::NeedToFireBeforeUnload() {
940 // TODO(creis): Should we fire even for interstitial pages?
941 return notify_disconnection() &&
942 !showing_interstitial_page() &&
943 !render_view_host()->SuddenTerminationAllowed();
944}
945
[email protected]c0588052008-10-27 23:01:50946void TabContents::OpenURL(const GURL& url, const GURL& referrer,
[email protected]d5f942ba2008-09-26 19:30:34947 WindowOpenDisposition disposition,
948 PageTransition::Type transition) {
949 if (delegate_)
[email protected]c0588052008-10-27 23:01:50950 delegate_->OpenURLFromTab(this, url, referrer, disposition, transition);
[email protected]d5f942ba2008-09-26 19:30:34951}
952
[email protected]1ccb3568d2010-02-19 10:51:16953bool TabContents::NavigateToPendingEntry(
954 NavigationController::ReloadType reload_type) {
[email protected]876bc832010-09-07 16:29:54955 return NavigateToEntry(*controller_.pending_entry(), reload_type);
956}
[email protected]96d185d2009-04-24 03:28:54957
[email protected]876bc832010-09-07 16:29:54958bool TabContents::NavigateToEntry(
959 const NavigationEntry& entry,
960 NavigationController::ReloadType reload_type) {
[email protected]96d185d2009-04-24 03:28:54961 RenderViewHost* dest_render_view_host = render_manager_.Navigate(entry);
962 if (!dest_render_view_host)
963 return false; // Unable to create the desired render view host.
964
[email protected]770dd8b2010-05-24 18:11:39965 if (delegate_ && delegate_->ShouldEnablePreferredSizeNotifications()) {
966 dest_render_view_host->EnablePreferredSizeChangedMode(
967 kPreferredSizeWidth | kPreferredSizeHeightThisIsSlow);
968 }
[email protected]9fb325e2010-05-06 18:23:24969
[email protected]05fdd492010-11-15 17:52:07970 // For security, we should never send non-DOM-UI URLs to a DOM UI renderer.
971 // Double check that here.
[email protected]54ec6472010-04-09 19:39:58972 int enabled_bindings = dest_render_view_host->enabled_bindings();
[email protected]0352e5472010-04-29 00:11:40973 bool is_allowed_in_dom_ui_renderer =
[email protected]05fdd492010-11-15 17:52:07974 DOMUIFactory::IsURLAcceptableForDOMUI(profile(), entry.url());
[email protected]54ec6472010-04-09 19:39:58975 CHECK(!BindingsPolicy::is_dom_ui_enabled(enabled_bindings) ||
[email protected]0352e5472010-04-29 00:11:40976 is_allowed_in_dom_ui_renderer);
[email protected]54ec6472010-04-09 19:39:58977
[email protected]96d185d2009-04-24 03:28:54978 // Tell DevTools agent that it is attached prior to the navigation.
[email protected]d9f9b792009-06-24 13:17:12979 DevToolsManager* devtools_manager = DevToolsManager::GetInstance();
[email protected]7e8e6b62009-05-08 11:28:32980 if (devtools_manager) { // NULL in unit tests.
[email protected]3cc72b12010-03-18 23:03:00981 devtools_manager->OnNavigatingToPendingEntry(render_view_host(),
982 dest_render_view_host,
983 entry.url());
[email protected]7e8e6b62009-05-08 11:28:32984 }
[email protected]96d185d2009-04-24 03:28:54985
986 // Used for page load time metrics.
987 current_load_start_ = base::TimeTicks::Now();
988
989 // Navigate in the desired RenderViewHost.
[email protected]056de2d2009-06-26 16:41:34990 ViewMsg_Navigate_Params navigate_params;
[email protected]876bc832010-09-07 16:29:54991 MakeNavigateParams(entry, controller_, reload_type, &navigate_params);
[email protected]52c68652010-12-07 17:47:04992 if (delegate_) {
993 navigate_params.extra_headers =
994 delegate_->GetNavigationHeaders(navigate_params.url);
995 }
[email protected]056de2d2009-06-26 16:41:34996 dest_render_view_host->Navigate(navigate_params);
[email protected]96d185d2009-04-24 03:28:54997
998 if (entry.page_id() == -1) {
999 // HACK!! This code suppresses javascript: URLs from being added to
1000 // session history, which is what we want to do for javascript: URLs that
1001 // do not generate content. What we really need is a message from the
1002 // renderer telling us that a new page was not created. The same message
1003 // could be used for mailto: URLs and the like.
1004 if (entry.url().SchemeIs(chrome::kJavaScriptScheme))
1005 return false;
1006 }
1007
[email protected]3c9e1872010-11-18 16:17:491008 // Notify observers about navigation.
1009 FOR_EACH_OBSERVER(WebNavigationObserver, web_navigation_observers_,
1010 NavigateToPendingEntry());
[email protected]96d185d2009-04-24 03:28:541011
[email protected]1ccb3568d2010-02-19 10:51:161012 if (reload_type != NavigationController::NO_RELOAD &&
1013 !profile()->IsOffTheRecord()) {
[email protected]0189bc722009-08-28 21:56:481014 FaviconService* favicon_service =
1015 profile()->GetFaviconService(Profile::IMPLICIT_ACCESS);
1016 if (favicon_service)
1017 favicon_service->SetFaviconOutOfDateForPage(entry.url());
[email protected]96d185d2009-04-24 03:28:541018 }
1019
[email protected]d5f942ba2008-09-26 19:30:341020 return true;
1021}
1022
[email protected]96d185d2009-04-24 03:28:541023void TabContents::Stop() {
1024 render_manager_.Stop();
[email protected]12802702010-07-09 19:43:091025 printing_->Stop();
[email protected]96d185d2009-04-24 03:28:541026}
1027
[email protected]96d185d2009-04-24 03:28:541028void TabContents::DisassociateFromPopupCount() {
1029 render_view_host()->DisassociateFromPopupCount();
1030}
1031
1032TabContents* TabContents::Clone() {
1033 // We create a new SiteInstance so that the new tab won't share processes
1034 // with the old one. This can be changed in the future if we need it to share
1035 // processes for some reason.
[email protected]420ae012009-04-24 05:16:321036 TabContents* tc = new TabContents(profile(),
[email protected]96d185d2009-04-24 03:28:541037 SiteInstance::CreateSiteInstance(profile()),
[email protected]6ee12c42010-09-14 09:36:071038 MSG_ROUTING_NONE, this, NULL);
[email protected]96d185d2009-04-24 03:28:541039 tc->controller().CopyStateFrom(controller_);
[email protected]98aa0b52010-05-06 17:03:081040 tc->extension_app_ = extension_app_;
[email protected]98aa0b52010-05-06 17:03:081041 tc->extension_app_icon_ = extension_app_icon_;
[email protected]96d185d2009-04-24 03:28:541042 return tc;
1043}
1044
[email protected]4d677202009-07-19 07:37:121045void TabContents::ShowPageInfo(const GURL& url,
1046 const NavigationEntry::SSLStatus& ssl,
1047 bool show_history) {
1048 if (!delegate_)
1049 return;
1050
[email protected]bb678332009-07-21 00:15:501051 delegate_->ShowPageInfo(profile(), url, ssl, show_history);
[email protected]4d677202009-07-19 07:37:121052}
1053
[email protected]ef0d04c2010-12-15 23:34:211054void TabContents::SaveFavicon() {
1055 NavigationEntry* entry = controller_.GetActiveEntry();
1056 if (!entry || entry->url().is_empty())
1057 return;
1058
1059 // Make sure the page is in history, otherwise adding the favicon does
1060 // nothing.
1061 HistoryService* history = profile()->GetOriginalProfile()->GetHistoryService(
1062 Profile::IMPLICIT_ACCESS);
1063 if (!history)
1064 return;
1065 history->AddPageNoVisitForBookmark(entry->url());
1066
1067 FaviconService* service = profile()->GetOriginalProfile()->GetFaviconService(
1068 Profile::IMPLICIT_ACCESS);
1069 if (!service)
1070 return;
1071 const NavigationEntry::FaviconStatus& favicon(entry->favicon());
1072 if (!favicon.is_valid() || favicon.url().is_empty() ||
1073 favicon.bitmap().empty()) {
1074 return;
1075 }
1076 std::vector<unsigned char> image_data;
1077 gfx::PNGCodec::EncodeBGRASkBitmap(favicon.bitmap(), false, &image_data);
1078 service->SetFavicon(entry->url(), favicon.url(), image_data);
1079}
1080
initial.commit09911bf2008-07-26 23:55:291081ConstrainedWindow* TabContents::CreateConstrainedDialog(
[email protected]e8382172009-06-19 22:16:281082 ConstrainedWindowDelegate* delegate) {
initial.commit09911bf2008-07-26 23:55:291083 ConstrainedWindow* window =
[email protected]e8382172009-06-19 22:16:281084 ConstrainedWindow::CreateConstrainedDialog(this, delegate);
initial.commit09911bf2008-07-26 23:55:291085 child_windows_.push_back(window);
[email protected]fa1cf0b82010-01-15 21:49:441086
1087 if (child_windows_.size() == 1) {
1088 window->ShowConstrainedWindow();
1089 BlockTabContent(true);
1090 }
1091
initial.commit09911bf2008-07-26 23:55:291092 return window;
1093}
1094
[email protected]fa1cf0b82010-01-15 21:49:441095void TabContents::BlockTabContent(bool blocked) {
[email protected]8dccd7cb2010-02-25 05:19:551096 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
[email protected]1b072562010-12-13 19:37:451097 // 70% opaque grey.
1098 SkColor greyish = SkColorSetARGB(178, 0, 0, 0);
[email protected]8dccd7cb2010-02-25 05:19:551099 if (rwhv)
[email protected]1b072562010-12-13 19:37:451100 rwhv->SetVisuallyDeemphasized(blocked ? &greyish : NULL, false);
[email protected]fa1cf0b82010-01-15 21:49:441101 render_view_host()->set_ignore_input_events(blocked);
1102 if (delegate_)
1103 delegate_->SetTabContentBlocked(this, blocked);
1104}
1105
initial.commit09911bf2008-07-26 23:55:291106void TabContents::AddNewContents(TabContents* new_contents,
1107 WindowOpenDisposition disposition,
1108 const gfx::Rect& initial_pos,
[email protected]7e7414ae2010-01-26 20:19:291109 bool user_gesture) {
[email protected]c65e2f152010-10-14 15:30:401110 if (all_contents_blocked_) {
1111 if (!blocked_contents_)
1112 blocked_contents_ = new BlockedContentContainer(this);
1113 blocked_contents_->AddTabContents(
1114 new_contents, disposition, initial_pos, user_gesture);
1115 return;
1116 }
1117
initial.commit09911bf2008-07-26 23:55:291118 if (!delegate_)
1119 return;
1120
[email protected]35f7d212009-04-29 21:19:271121 if ((disposition == NEW_POPUP) && !user_gesture &&
1122 !CommandLine::ForCurrentProcess()->HasSwitch(
1123 switches::kDisablePopupBlocking)) {
[email protected]2c4410d2009-05-06 23:46:221124 // Unrequested popups from normal pages are constrained unless they're in
[email protected]b6c874582009-05-08 19:38:311125 // the whitelist. The popup owner will handle checking this.
[email protected]7e7414ae2010-01-26 20:19:291126 delegate_->GetConstrainingContents(this)->AddPopup(
1127 new_contents, initial_pos);
[email protected]b6c874582009-05-08 19:38:311128 } else {
[email protected]0aa55312008-10-17 21:53:081129 new_contents->DisassociateFromPopupCount();
initial.commit09911bf2008-07-26 23:55:291130 delegate_->AddNewContents(this, new_contents, disposition, initial_pos,
1131 user_gesture);
[email protected]7f37dd92009-09-15 16:35:491132 NotificationService::current()->Notify(
1133 NotificationType::TAB_ADDED,
1134 Source<TabContentsDelegate>(delegate_),
1135 Details<TabContents>(this));
initial.commit09911bf2008-07-26 23:55:291136 }
initial.commit09911bf2008-07-26 23:55:291137
[email protected]8cc8d492010-02-02 10:40:491138 // TODO(pkasting): Why is this necessary?
[email protected]c65e2f152010-10-14 15:30:401139 PopupNotificationVisibilityChanged(blocked_contents_ != NULL);
initial.commit09911bf2008-07-26 23:55:291140}
1141
[email protected]bcd9dc42010-01-23 16:38:181142bool TabContents::ExecuteCode(int request_id, const std::string& extension_id,
[email protected]20ad2692009-11-20 18:27:201143 bool is_js_code, const std::string& code_string,
1144 bool all_frames) {
[email protected]ebd0eae72010-02-16 23:50:511145 RenderViewHost* host = render_view_host();
1146 if (!host)
1147 return false;
1148
1149 return host->Send(new ViewMsg_ExecuteCode(host->routing_id(),
[email protected]be7e5cb2010-10-04 12:53:171150 ViewMsg_ExecuteCode_Params(request_id, extension_id,
[email protected]ebd0eae72010-02-16 23:50:511151 is_js_code, code_string, all_frames)));
[email protected]912256b32009-09-18 09:47:351152}
1153
[email protected]96d185d2009-04-24 03:28:541154void TabContents::PopupNotificationVisibilityChanged(bool visible) {
[email protected]8cc8d492010-02-02 10:40:491155 if (is_being_destroyed_)
1156 return;
[email protected]9fb83e82010-07-02 18:24:551157 content_settings_delegate_->SetPopupsBlocked(visible);
[email protected]8cc8d492010-02-02 10:40:491158 if (!dont_notify_render_view_)
[email protected]e8345242010-05-06 03:00:401159 render_view_host()->AllowScriptToClose(!visible);
[email protected]96d185d2009-04-24 03:28:541160}
1161
[email protected]5c9e97a2009-09-09 23:48:301162gfx::NativeView TabContents::GetContentNativeView() const {
[email protected]96d185d2009-04-24 03:28:541163 return view_->GetContentNativeView();
1164}
1165
1166gfx::NativeView TabContents::GetNativeView() const {
1167 return view_->GetNativeView();
1168}
1169
1170void TabContents::GetContainerBounds(gfx::Rect *out) const {
1171 view_->GetContainerBounds(out);
1172}
1173
1174void TabContents::Focus() {
1175 view_->Focus();
1176}
1177
[email protected]90daadb42009-06-08 21:27:281178void TabContents::FocusThroughTabTraversal(bool reverse) {
[email protected]7e383692009-06-12 19:14:541179 if (showing_interstitial_page()) {
1180 render_manager_.interstitial_page()->FocusThroughTabTraversal(reverse);
1181 return;
1182 }
[email protected]96d185d2009-04-24 03:28:541183 render_view_host()->SetInitialFocus(reverse);
1184}
1185
1186bool TabContents::FocusLocationBarByDefault() {
1187 DOMUI* dom_ui = GetDOMUIForCurrentState();
1188 if (dom_ui)
1189 return dom_ui->focus_location_bar_by_default();
[email protected]3e3f0eb2009-06-22 18:33:431190 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]e0d481582009-09-15 21:06:251191 if (entry && entry->url() == GURL(chrome::kAboutBlankURL))
[email protected]3e3f0eb2009-06-22 18:33:431192 return true;
[email protected]96d185d2009-04-24 03:28:541193 return false;
1194}
1195
[email protected]a26dc362010-04-23 01:48:581196void TabContents::SetFocusToLocationBar(bool select_all) {
[email protected]a11aaf22010-03-30 00:03:381197 if (delegate())
[email protected]a26dc362010-04-23 01:48:581198 delegate()->SetFocusToLocationBar(select_all);
[email protected]a11aaf22010-03-30 00:03:381199}
1200
[email protected]616ed5a2008-11-21 22:27:241201void TabContents::AddInfoBar(InfoBarDelegate* delegate) {
[email protected]f7019302010-03-26 19:58:321202 if (delegate_ && !delegate_->infobars_enabled()) {
1203 delegate->InfoBarClosed();
1204 return;
1205 }
1206
[email protected]616ed5a2008-11-21 22:27:241207 // Look through the existing InfoBarDelegates we have for a match. If we've
1208 // already got one that matches, then we don't add the new one.
[email protected]f86a07022008-11-25 01:06:051209 for (int i = 0; i < infobar_delegate_count(); ++i) {
[email protected]6d27a7b2009-12-18 23:25:451210 if (GetInfoBarDelegateAt(i)->EqualsDelegate(delegate)) {
1211 // Tell the new infobar to close so that it can clean itself up.
1212 delegate->InfoBarClosed();
[email protected]616ed5a2008-11-21 22:27:241213 return;
[email protected]6d27a7b2009-12-18 23:25:451214 }
[email protected]616ed5a2008-11-21 22:27:241215 }
1216
1217 infobar_delegates_.push_back(delegate);
[email protected]bfd04a62009-02-01 18:16:561218 NotificationService::current()->Notify(
[email protected]f4f50ef2011-01-21 19:01:191219 NotificationType::TAB_CONTENTS_INFOBAR_ADDED, Source<TabContents>(this),
[email protected]bfd04a62009-02-01 18:16:561220 Details<InfoBarDelegate>(delegate));
[email protected]616ed5a2008-11-21 22:27:241221
1222 // Add ourselves as an observer for navigations the first time a delegate is
1223 // added. We use this notification to expire InfoBars that need to expire on
1224 // page transitions.
1225 if (infobar_delegates_.size() == 1) {
[email protected]bfd04a62009-02-01 18:16:561226 registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED,
[email protected]ce3fa3c2009-04-20 19:55:571227 Source<NavigationController>(&controller_));
[email protected]616ed5a2008-11-21 22:27:241228 }
1229}
1230
1231void TabContents::RemoveInfoBar(InfoBarDelegate* delegate) {
[email protected]f7019302010-03-26 19:58:321232 if (delegate_ && !delegate_->infobars_enabled()) {
1233 return;
1234 }
1235
[email protected]616ed5a2008-11-21 22:27:241236 std::vector<InfoBarDelegate*>::iterator it =
1237 find(infobar_delegates_.begin(), infobar_delegates_.end(), delegate);
1238 if (it != infobar_delegates_.end()) {
1239 InfoBarDelegate* delegate = *it;
[email protected]bfd04a62009-02-01 18:16:561240 NotificationService::current()->Notify(
1241 NotificationType::TAB_CONTENTS_INFOBAR_REMOVED,
1242 Source<TabContents>(this),
1243 Details<InfoBarDelegate>(delegate));
[email protected]616ed5a2008-11-21 22:27:241244
[email protected]f4f50ef2011-01-21 19:01:191245 infobar_delegates_.erase(it);
1246 // Remove ourselves as an observer if we are tracking no more InfoBars.
1247 if (infobar_delegates_.empty()) {
1248 registrar_.Remove(this, NotificationType::NAV_ENTRY_COMMITTED,
1249 Source<NavigationController>(&controller_));
[email protected]6a02963e2009-01-06 16:58:031250 }
[email protected]616ed5a2008-11-21 22:27:241251 }
1252}
[email protected]3edd9522009-03-04 22:19:411253
[email protected]f81821c2009-08-07 01:29:471254void TabContents::ReplaceInfoBar(InfoBarDelegate* old_delegate,
1255 InfoBarDelegate* new_delegate) {
[email protected]f7019302010-03-26 19:58:321256 if (delegate_ && !delegate_->infobars_enabled()) {
1257 new_delegate->InfoBarClosed();
1258 return;
1259 }
1260
[email protected]f81821c2009-08-07 01:29:471261 std::vector<InfoBarDelegate*>::iterator it =
1262 find(infobar_delegates_.begin(), infobar_delegates_.end(), old_delegate);
1263 DCHECK(it != infobar_delegates_.end());
1264
1265 // Notify the container about the change of plans.
1266 scoped_ptr<std::pair<InfoBarDelegate*, InfoBarDelegate*> > details(
1267 new std::pair<InfoBarDelegate*, InfoBarDelegate*>(
1268 old_delegate, new_delegate));
1269 NotificationService::current()->Notify(
1270 NotificationType::TAB_CONTENTS_INFOBAR_REPLACED,
1271 Source<TabContents>(this),
1272 Details<std::pair<InfoBarDelegate*, InfoBarDelegate*> >(details.get()));
1273
[email protected]f4f50ef2011-01-21 19:01:191274 // Remove the old one.
1275 infobar_delegates_.erase(it);
[email protected]f81821c2009-08-07 01:29:471276
1277 // Add the new one.
[email protected]cdadf6f2010-08-20 16:36:081278 DCHECK(find(infobar_delegates_.begin(),
1279 infobar_delegates_.end(), new_delegate) ==
1280 infobar_delegates_.end());
[email protected]f81821c2009-08-07 01:29:471281 infobar_delegates_.push_back(new_delegate);
1282}
1283
[email protected]20ede072009-10-06 00:28:351284bool TabContents::ShouldShowBookmarkBar() {
[email protected]aa9196f2009-10-22 16:20:271285 if (showing_interstitial_page())
1286 return false;
1287
[email protected]c8f74942010-09-03 10:04:141288 // Do not show bookmarks bar if bookmarks aren't enabled.
1289 if (!browser_defaults::bookmarks_enabled)
1290 return false;
1291
[email protected]96d185d2009-04-24 03:28:541292 // See GetDOMUIForCurrentState() comment for more info. This case is very
1293 // similar, but for non-first loads, we want to use the committed entry. This
1294 // is so the bookmarks bar disappears at the same time the page does.
1295 if (controller_.GetLastCommittedEntry()) {
1296 // Not the first load, always use the committed DOM UI.
[email protected]d6219912010-11-17 19:47:371297 return (render_manager_.dom_ui() == NULL) ?
1298 false : render_manager_.dom_ui()->force_bookmark_bar_visible();
[email protected]96d185d2009-04-24 03:28:541299 }
1300
1301 // When it's the first load, we know either the pending one or the committed
1302 // one will have the DOM UI in it (see GetDOMUIForCurrentState), and only one
1303 // of them will be valid, so we can just check both.
1304 if (render_manager_.pending_dom_ui())
1305 return render_manager_.pending_dom_ui()->force_bookmark_bar_visible();
[email protected]d6219912010-11-17 19:47:371306 return (render_manager_.dom_ui() == NULL) ?
1307 false : render_manager_.dom_ui()->force_bookmark_bar_visible();
[email protected]96d185d2009-04-24 03:28:541308}
1309
[email protected]3edd9522009-03-04 22:19:411310void TabContents::ToolbarSizeChanged(bool is_animating) {
1311 TabContentsDelegate* d = delegate();
1312 if (d)
1313 d->ToolbarSizeChanged(this, is_animating);
1314}
[email protected]616ed5a2008-11-21 22:27:241315
[email protected]fc6fb7fb2009-11-07 02:35:041316bool TabContents::CanDownload(int request_id) {
1317 TabContentsDelegate* d = delegate();
1318 if (d)
1319 return d->CanDownload(request_id);
1320 return true;
1321}
1322
[email protected]d5f942ba2008-09-26 19:30:341323void TabContents::OnStartDownload(DownloadItem* download) {
1324 DCHECK(download);
[email protected]d5f942ba2008-09-26 19:30:341325
1326 // Download in a constrained popup is shown in the tab that opened it.
[email protected]b6c874582009-05-08 19:38:311327 TabContents* tab_contents = delegate()->GetConstrainingContents(this);
[email protected]d5f942ba2008-09-26 19:30:341328
[email protected]5b928b62010-08-03 11:46:511329 if (tab_contents && tab_contents->delegate())
1330 tab_contents->delegate()->OnStartDownload(download, this);
[email protected]3edd9522009-03-04 22:19:411331}
[email protected]3edd9522009-03-04 22:19:411332
[email protected]d5f942ba2008-09-26 19:30:341333void TabContents::WillClose(ConstrainedWindow* window) {
[email protected]d6219912010-11-17 19:47:371334 ConstrainedWindowList::iterator i(
1335 std::find(child_windows_.begin(), child_windows_.end(), window));
1336 bool removed_topmost_window = i == child_windows_.begin();
1337 if (i != child_windows_.end())
1338 child_windows_.erase(i);
1339 if (child_windows_.empty()) {
[email protected]fa1cf0b82010-01-15 21:49:441340 BlockTabContent(false);
[email protected]d6219912010-11-17 19:47:371341 } else {
1342 if (removed_topmost_window)
1343 child_windows_[0]->ShowConstrainedWindow();
1344 BlockTabContent(true);
[email protected]fa1cf0b82010-01-15 21:49:441345 }
[email protected]66ba4932009-06-04 19:22:131346}
[email protected]d5f942ba2008-09-26 19:30:341347
[email protected]c65e2f152010-10-14 15:30:401348void TabContents::WillCloseBlockedContentContainer(
1349 BlockedContentContainer* container) {
1350 DCHECK(blocked_contents_ == container);
1351 blocked_contents_ = NULL;
[email protected]8cc8d492010-02-02 10:40:491352 PopupNotificationVisibilityChanged(false);
[email protected]d5f942ba2008-09-26 19:30:341353}
1354
[email protected]d5f942ba2008-09-26 19:30:341355void TabContents::DidMoveOrResize(ConstrainedWindow* window) {
[email protected]332af7732009-03-11 13:21:351356#if defined(OS_WIN)
[email protected]92edc472009-02-10 20:32:061357 UpdateWindow(GetNativeView());
[email protected]332af7732009-03-11 13:21:351358#endif
[email protected]d5f942ba2008-09-26 19:30:341359}
1360
[email protected]c1dc9f132009-11-20 23:13:211361void TabContents::StartFinding(string16 search_string,
[email protected]e491f1c2009-05-22 20:28:121362 bool forward_direction,
1363 bool case_sensitive) {
[email protected]c1dc9f132009-11-20 23:13:211364 // If search_string is empty, it means FindNext was pressed with a keyboard
[email protected]6e95706d2009-04-23 22:55:011365 // shortcut so unless we have something to search for we return early.
[email protected]c1dc9f132009-11-20 23:13:211366 if (search_string.empty() && find_text_.empty()) {
[email protected]e5d2f7132010-04-09 18:13:531367 string16 last_search_prepopulate_text =
1368 FindBarState::GetLastPrepopulateText(profile());
1369
[email protected]87fae512009-12-11 02:20:211370 // Try the last thing we searched for on this tab, then the last thing
1371 // searched for on any tab.
1372 if (!previous_find_text_.empty())
1373 search_string = previous_find_text_;
[email protected]e5d2f7132010-04-09 18:13:531374 else if (!last_search_prepopulate_text.empty())
1375 search_string = last_search_prepopulate_text;
[email protected]87fae512009-12-11 02:20:211376 else
[email protected]e491f1c2009-05-22 20:28:121377 return;
[email protected]e491f1c2009-05-22 20:28:121378 }
[email protected]6e95706d2009-04-23 22:55:011379
[email protected]101c90e2009-10-26 21:14:441380 // Keep track of the previous search.
1381 previous_find_text_ = find_text_;
1382
[email protected]6e95706d2009-04-23 22:55:011383 // This is a FindNext operation if we are searching for the same text again,
1384 // or if the passed in search text is empty (FindNext keyboard shortcut). The
1385 // exception to this is if the Find was aborted (then we don't want FindNext
1386 // because the highlighting has been cleared and we need it to reappear). We
1387 // therefore treat FindNext after an aborted Find operation as a full fledged
1388 // Find.
[email protected]c1dc9f132009-11-20 23:13:211389 bool find_next = (find_text_ == search_string || search_string.empty()) &&
[email protected]e491f1c2009-05-22 20:28:121390 (last_search_case_sensitive_ == case_sensitive) &&
[email protected]6e95706d2009-04-23 22:55:011391 !find_op_aborted_;
1392 if (!find_next)
1393 current_find_request_id_ = find_request_id_counter_++;
1394
[email protected]c1dc9f132009-11-20 23:13:211395 if (!search_string.empty())
1396 find_text_ = search_string;
[email protected]e491f1c2009-05-22 20:28:121397 last_search_case_sensitive_ = case_sensitive;
[email protected]6e95706d2009-04-23 22:55:011398
1399 find_op_aborted_ = false;
1400
1401 // Keep track of what the last search was across the tabs.
[email protected]e5d2f7132010-04-09 18:13:531402 FindBarState* find_bar_state = profile()->GetFindBarState();
1403 find_bar_state->set_last_prepopulate_text(find_text_);
[email protected]6e95706d2009-04-23 22:55:011404 render_view_host()->StartFinding(current_find_request_id_,
1405 find_text_,
1406 forward_direction,
[email protected]e491f1c2009-05-22 20:28:121407 case_sensitive,
[email protected]6e95706d2009-04-23 22:55:011408 find_next);
1409}
1410
[email protected]5bc8fe92010-03-11 18:19:001411void TabContents::StopFinding(
1412 FindBarController::SelectionAction selection_action) {
[email protected]d6e8d332010-06-04 22:26:011413 if (selection_action == FindBarController::kClearSelection) {
1414 // kClearSelection means the find string has been cleared by the user, but
1415 // the UI has not been dismissed. In that case we want to clear the
1416 // previously remembered search (http://crbug.com/42639).
1417 previous_find_text_ = string16();
1418 } else {
[email protected]2d2dffc2009-06-09 21:41:001419 find_ui_active_ = false;
[email protected]d6e8d332010-06-04 22:26:011420 if (!find_text_.empty())
1421 previous_find_text_ = find_text_;
1422 }
[email protected]101c90e2009-10-26 21:14:441423 find_text_.clear();
[email protected]6e95706d2009-04-23 22:55:011424 find_op_aborted_ = true;
[email protected]e491f1c2009-05-22 20:28:121425 last_search_result_ = FindNotificationDetails();
[email protected]5bc8fe92010-03-11 18:19:001426 render_view_host()->StopFinding(selection_action);
[email protected]6e95706d2009-04-23 22:55:011427}
1428
[email protected]420ae012009-04-24 05:16:321429void TabContents::OnSavePage() {
1430 // If we can not save the page, try to download it.
1431 if (!SavePackage::IsSavableContents(contents_mime_type())) {
1432 DownloadManager* dlm = profile()->GetDownloadManager();
1433 const GURL& current_page_url = GetURL();
1434 if (dlm && current_page_url.is_valid())
[email protected]d82ed61e2009-06-16 02:46:221435 dlm->DownloadUrl(current_page_url, GURL(), "", this);
[email protected]420ae012009-04-24 05:16:321436 return;
1437 }
1438
1439 Stop();
1440
1441 // Create the save package and possibly prompt the user for the name to save
1442 // the page as. The user prompt is an asynchronous operation that runs on
1443 // another thread.
[email protected]d82ed61e2009-06-16 02:46:221444 save_package_ = new SavePackage(this);
[email protected]420ae012009-04-24 05:16:321445 save_package_->GetSaveInfo();
1446}
1447
1448// Used in automated testing to bypass prompting the user for file names.
1449// Instead, the names and paths are hard coded rather than running them through
1450// file name sanitation and extension / mime checking.
[email protected]828cabe2009-09-26 22:47:111451bool TabContents::SavePage(const FilePath& main_file, const FilePath& dir_path,
[email protected]420ae012009-04-24 05:16:321452 SavePackage::SavePackageType save_type) {
1453 // Stop the page from navigating.
1454 Stop();
1455
[email protected]828cabe2009-09-26 22:47:111456 save_package_ = new SavePackage(this, save_type, main_file, dir_path);
[email protected]cdf6cc82009-08-04 21:48:591457 return save_package_->Init();
[email protected]420ae012009-04-24 05:16:321458}
1459
[email protected]89d6e6e2009-12-03 23:06:511460void TabContents::EmailPageLocation() {
1461 std::string title = EscapeQueryParamValue(UTF16ToUTF8(GetTitle()), false);
1462 std::string page_url = EscapeQueryParamValue(GetURL().spec(), false);
1463 std::string mailto = std::string("mailto:?subject=Fwd:%20") +
1464 title + "&body=%0A%0A" + page_url;
1465 platform_util::OpenExternal(GURL(mailto));
1466}
1467
[email protected]420ae012009-04-24 05:16:321468void TabContents::PrintPreview() {
[email protected]dbeebd52010-11-16 20:34:161469 if (CommandLine::ForCurrentProcess()->HasSwitch(
1470 switches::kEnablePrintPreview)) {
1471 if (showing_interstitial_page())
1472 return;
1473 render_view_host()->PrintPreview();
1474 } else {
1475 PrintNow();
1476 }
[email protected]420ae012009-04-24 05:16:321477}
1478
1479bool TabContents::PrintNow() {
1480 // We can't print interstitial page for now.
1481 if (showing_interstitial_page())
1482 return false;
1483
1484 return render_view_host()->PrintPages();
1485}
1486
[email protected]82270452009-06-19 15:58:011487void TabContents::PrintingDone(int document_cookie, bool success) {
1488 render_view_host()->PrintingDone(document_cookie, success);
1489}
1490
[email protected]420ae012009-04-24 05:16:321491bool TabContents::IsActiveEntry(int32 page_id) {
1492 NavigationEntry* active_entry = controller_.GetActiveEntry();
1493 return (active_entry != NULL &&
1494 active_entry->site_instance() == GetSiteInstance() &&
1495 active_entry->page_id() == page_id);
1496}
1497
[email protected]b2fe07d12010-02-09 14:38:081498void TabContents::SetOverrideEncoding(const std::string& encoding) {
[email protected]8cb5d5b2010-02-09 11:36:161499 set_encoding(encoding);
1500 render_view_host()->SetPageEncoding(encoding);
1501}
1502
[email protected]b2fe07d12010-02-09 14:38:081503void TabContents::ResetOverrideEncoding() {
[email protected]8cb5d5b2010-02-09 11:36:161504 reset_encoding();
1505 render_view_host()->ResetPageEncodingToDefault();
1506}
1507
1508void TabContents::WindowMoveOrResizeStarted() {
1509 render_view_host()->WindowMoveOrResizeStarted();
1510}
1511
[email protected]c65e2f152010-10-14 15:30:401512void TabContents::SetAllContentsBlocked(bool value) {
1513 if (all_contents_blocked_ == value)
1514 return;
1515
1516 all_contents_blocked_ = value;
1517 if (!all_contents_blocked_ && blocked_contents_) {
1518 std::vector<TabContents*> blocked;
1519 blocked_contents_->GetBlockedContents(&blocked);
1520 for (size_t i = 0; i < blocked.size(); ++i)
1521 blocked_contents_->LaunchForContents(blocked[i]);
1522 }
1523}
1524
[email protected]3f8eb7f2009-10-02 23:58:051525void TabContents::LogNewTabTime(const std::string& event_name) {
1526 // Not all new tab pages get timed. In those cases, we don't have a
1527 // new_tab_start_time_.
1528 if (new_tab_start_time_.is_null())
1529 return;
1530
1531 base::TimeDelta duration = base::TimeTicks::Now() - new_tab_start_time_;
[email protected]84abba62009-10-07 17:01:441532 MetricEventDurationDetails details(event_name,
1533 static_cast<int>(duration.InMilliseconds()));
1534
[email protected]7ab1e7d62009-10-14 23:32:011535 if (event_name == "Tab.NewTabScriptStart") {
1536 UMA_HISTOGRAM_TIMES("Tab.NewTabScriptStart", duration);
1537 } else if (event_name == "Tab.NewTabDOMContentLoaded") {
1538 UMA_HISTOGRAM_TIMES("Tab.NewTabDOMContentLoaded", duration);
1539 } else if (event_name == "Tab.NewTabOnload") {
1540 UMA_HISTOGRAM_TIMES("Tab.NewTabOnload", duration);
[email protected]3f8eb7f2009-10-02 23:58:051541 // The new tab page has finished loading; reset it.
1542 new_tab_start_time_ = base::TimeTicks();
[email protected]84abba62009-10-07 17:01:441543 } else {
1544 NOTREACHED();
[email protected]3f8eb7f2009-10-02 23:58:051545 }
[email protected]84abba62009-10-07 17:01:441546 NotificationService::current()->Notify(
1547 NotificationType::METRIC_EVENT_DURATION,
1548 Source<TabContents>(this),
1549 Details<MetricEventDurationDetails>(&details));
[email protected]3f8eb7f2009-10-02 23:58:051550}
1551
[email protected]7ab1e7d62009-10-14 23:32:011552void TabContents::OnCloseStarted() {
1553 if (tab_close_start_time_.is_null())
1554 tab_close_start_time_ = base::TimeTicks::Now();
1555}
1556
[email protected]46624bf2010-06-09 16:04:191557bool TabContents::ShouldAcceptDragAndDrop() const {
1558#if defined(OS_CHROMEOS)
1559 // ChromeOS panels (pop-ups) do not take drag-n-drop.
1560 // See http://crosbug.com/2413
[email protected]b83af492010-10-09 03:21:201561 if (delegate() && delegate()->IsPopup(this))
1562 return false;
1563 return true;
[email protected]46624bf2010-06-09 16:04:191564#else
1565 return true;
1566#endif
1567}
1568
[email protected]ec0b6c42010-08-26 03:16:581569void TabContents::UpdateHistoryForNavigation(
1570 scoped_refptr<history::HistoryAddPageArgs> add_page_args) {
1571 if (profile()->IsOffTheRecord())
1572 return;
1573
1574 // Add to history service.
1575 HistoryService* hs = profile()->GetHistoryService(Profile::IMPLICIT_ACCESS);
1576 if (hs)
1577 hs->AddPage(*add_page_args);
1578}
1579
1580void TabContents::UpdateHistoryPageTitle(const NavigationEntry& entry) {
1581 if (profile()->IsOffTheRecord())
1582 return;
1583
1584 HistoryService* hs = profile()->GetHistoryService(Profile::IMPLICIT_ACCESS);
1585 if (hs)
1586 hs->SetPageTitle(entry.virtual_url(), entry.title());
1587}
1588
[email protected]d0b8d092010-10-25 04:05:171589double TabContents::GetZoomLevel() const {
[email protected]b75b8292010-10-01 07:28:251590 HostZoomMap* zoom_map = profile()->GetHostZoomMap();
1591 if (!zoom_map)
[email protected]d0b8d092010-10-25 04:05:171592 return 0;
[email protected]b75b8292010-10-01 07:28:251593
1594 double zoom_level;
1595 if (temporary_zoom_settings_) {
1596 zoom_level = zoom_map->GetTemporaryZoomLevel(
1597 GetRenderProcessHost()->id(), render_view_host()->routing_id());
1598 } else {
1599 zoom_level = zoom_map->GetZoomLevel(GetURL());
1600 }
[email protected]d0b8d092010-10-25 04:05:171601 return zoom_level;
1602}
[email protected]b75b8292010-10-01 07:28:251603
[email protected]d0b8d092010-10-25 04:05:171604int TabContents::GetZoomPercent(bool* enable_increment,
1605 bool* enable_decrement) {
1606 *enable_decrement = *enable_increment = false;
[email protected]b75b8292010-10-01 07:28:251607 int percent = static_cast<int>(
[email protected]d0b8d092010-10-25 04:05:171608 WebKit::WebView::zoomLevelToZoomFactor(GetZoomLevel()) * 100);
[email protected]b75b8292010-10-01 07:28:251609 *enable_decrement = percent > minimum_zoom_percent_;
1610 *enable_increment = percent < maximum_zoom_percent_;
1611 return percent;
1612}
1613
[email protected]3c733bde2010-12-21 19:56:311614void TabContents::ViewSource() {
[email protected]1788e772010-12-15 16:40:501615 if (!delegate_)
1616 return;
1617
1618 NavigationEntry* active_entry = controller().GetActiveEntry();
1619 if (!active_entry)
1620 return;
1621
1622 delegate_->ViewSourceForTab(this, active_entry->url());
[email protected]77d8d622010-12-15 10:30:121623}
1624
[email protected]724159a2010-12-30 01:11:181625void TabContents::OnDidStartProvisionalLoadForFrame(int64 frame_id,
1626 bool is_main_frame,
1627 const GURL& url) {
1628 bool is_error_page = (url.spec() == chrome::kUnreachableWebDataURL);
1629 GURL validated_url(url);
1630 render_view_host()->FilterURL(ChildProcessSecurityPolicy::GetInstance(),
1631 GetRenderProcessHost()->id(), &validated_url);
1632
1633 ProvisionalLoadDetails details(
1634 is_main_frame,
1635 controller_.IsURLInPageNavigation(validated_url),
1636 validated_url, std::string(), false, is_error_page, frame_id);
1637 NotificationService::current()->Notify(
1638 NotificationType::FRAME_PROVISIONAL_LOAD_START,
1639 Source<NavigationController>(&controller_),
1640 Details<ProvisionalLoadDetails>(&details));
1641 if (is_main_frame) {
1642 // If we're displaying a network error page do not reset the content
1643 // settings delegate's cookies so the user has a chance to modify cookie
1644 // settings.
1645 if (!is_error_page)
1646 content_settings_delegate_->ClearCookieSpecificContentSettings();
1647 content_settings_delegate_->ClearGeolocationContentSettings();
[email protected]e7d50892011-01-19 21:47:381648
1649 // Check if the URL we are about to load has been prerendered by any chance,
1650 // and use it if possible.
1651 MaybeUsePreloadedPage(url);
[email protected]724159a2010-12-30 01:11:181652 }
1653}
1654
1655void TabContents::OnDidRedirectProvisionalLoad(int32 page_id,
1656 const GURL& source_url,
1657 const GURL& target_url) {
1658 NavigationEntry* entry;
1659 if (page_id == -1)
1660 entry = controller_.pending_entry();
1661 else
1662 entry = controller_.GetEntryWithPageID(GetSiteInstance(), page_id);
1663 if (!entry || entry->url() != source_url)
1664 return;
1665 entry->set_url(target_url);
[email protected]e7d50892011-01-19 21:47:381666
1667 // Check if the URL we are about to load has been prerendered by any chance,
1668 // and use it if possible.
1669 MaybeUsePreloadedPage(target_url);
[email protected]724159a2010-12-30 01:11:181670}
1671
1672void TabContents::OnDidFailProvisionalLoadWithError(
1673 int64 frame_id,
1674 bool is_main_frame,
1675 int error_code,
1676 const GURL& url,
1677 bool showing_repost_interstitial) {
1678 VLOG(1) << "Failed Provisional Load: " << url.possibly_invalid_spec()
1679 << ", error_code: " << error_code
1680 << " is_main_frame: " << is_main_frame
1681 << " showing_repost_interstitial: " << showing_repost_interstitial
1682 << " frame_id: " << frame_id;
1683 GURL validated_url(url);
1684 render_view_host()->FilterURL(ChildProcessSecurityPolicy::GetInstance(),
1685 GetRenderProcessHost()->id(), &validated_url);
1686
1687 if (net::ERR_ABORTED == error_code) {
1688 // EVIL HACK ALERT! Ignore failed loads when we're showing interstitials.
1689 // This means that the interstitial won't be torn down properly, which is
1690 // bad. But if we have an interstitial, go back to another tab type, and
1691 // then load the same interstitial again, we could end up getting the first
1692 // interstitial's "failed" message (as a result of the cancel) when we're on
1693 // the second one.
1694 //
1695 // We can't tell this apart, so we think we're tearing down the current page
1696 // which will cause a crash later one. There is also some code in
1697 // RenderViewHostManager::RendererAbortedProvisionalLoad that is commented
1698 // out because of this problem.
1699 //
1700 // http://code.google.com/p/chromium/issues/detail?id=2855
1701 // Because this will not tear down the interstitial properly, if "back" is
1702 // back to another tab type, the interstitial will still be somewhat alive
1703 // in the previous tab type. If you navigate somewhere that activates the
1704 // tab with the interstitial again, you'll see a flash before the new load
1705 // commits of the interstitial page.
1706 if (showing_interstitial_page()) {
1707 LOG(WARNING) << "Discarding message during interstitial.";
1708 return;
1709 }
1710
1711 // This will discard our pending entry if we cancelled the load (e.g., if we
1712 // decided to download the file instead of load it). Only discard the
1713 // pending entry if the URLs match, otherwise the user initiated a navigate
1714 // before the page loaded so that the discard would discard the wrong entry.
1715 NavigationEntry* pending_entry = controller_.pending_entry();
1716 if (pending_entry && pending_entry->url() == validated_url) {
1717 controller_.DiscardNonCommittedEntries();
1718 // Update the URL display.
1719 NotifyNavigationStateChanged(TabContents::INVALIDATE_URL);
1720 }
1721
1722 render_manager_.RendererAbortedProvisionalLoad(render_view_host());
1723 }
1724
1725 // Send out a notification that we failed a provisional load with an error.
1726 ProvisionalLoadDetails details(
1727 is_main_frame, controller_.IsURLInPageNavigation(validated_url),
1728 validated_url, std::string(), false, false, frame_id);
1729 details.set_error_code(error_code);
1730
1731 NotificationService::current()->Notify(
1732 NotificationType::FAIL_PROVISIONAL_LOAD_WITH_ERROR,
1733 Source<NavigationController>(&controller_),
1734 Details<ProvisionalLoadDetails>(&details));
1735}
1736
1737void TabContents::OnDidLoadResourceFromMemoryCache(
1738 const GURL& url,
1739 const std::string& frame_origin,
1740 const std::string& main_frame_origin,
1741 const std::string& security_info) {
1742 static base::StatsCounter cache("WebKit.CacheHit");
1743 cache.Increment();
1744
1745 // Send out a notification that we loaded a resource from our memory cache.
1746 int cert_id = 0, cert_status = 0, security_bits = -1, connection_status = 0;
1747 SSLManager::DeserializeSecurityInfo(security_info,
1748 &cert_id, &cert_status,
1749 &security_bits,
1750 &connection_status);
1751 LoadFromMemoryCacheDetails details(url, frame_origin, main_frame_origin,
1752 GetRenderProcessHost()->id(), cert_id,
1753 cert_status);
1754
1755 NotificationService::current()->Notify(
1756 NotificationType::LOAD_FROM_MEMORY_CACHE,
1757 Source<NavigationController>(&controller_),
1758 Details<LoadFromMemoryCacheDetails>(&details));
1759}
1760
1761void TabContents::OnDidDisplayInsecureContent() {
1762 displayed_insecure_content_ = true;
1763 SSLManager::NotifySSLInternalStateChanged();
1764}
1765
1766void TabContents::OnDidRunInsecureContent(
[email protected]92771112011-01-20 00:13:021767 const std::string& security_origin, const GURL& target_url) {
[email protected]724159a2010-12-30 01:11:181768 controller_.ssl_manager()->DidRunInsecureContent(security_origin);
1769}
1770
1771void TabContents::OnDocumentLoadedInFrame(int64 frame_id) {
1772 controller_.DocumentLoadedInFrame();
1773 NotificationService::current()->Notify(
1774 NotificationType::FRAME_DOM_CONTENT_LOADED,
1775 Source<NavigationController>(&controller_),
1776 Details<int64>(&frame_id));
1777}
1778
1779void TabContents::OnDidFinishLoad(int64 frame_id) {
1780 NotificationService::current()->Notify(
1781 NotificationType::FRAME_DID_FINISH_LOAD,
1782 Source<NavigationController>(&controller_),
1783 Details<int64>(&frame_id));
1784}
1785
[email protected]c8f73ab2011-01-22 00:05:171786void TabContents::OnUpdateContentRestrictions(int restrictions) {
1787 content_restrictions_ = restrictions;
1788 delegate()->ContentRestrictionsChanged(this);
1789}
1790
1791void TabContents::OnPDFHasUnsupportedFeature() {
1792 PDFHasUnsupportedFeature(this);
1793}
1794
[email protected]96d185d2009-04-24 03:28:541795// Notifies the RenderWidgetHost instance about the fact that the page is
1796// loading, or done loading and calls the base implementation.
1797void TabContents::SetIsLoading(bool is_loading,
1798 LoadNotificationDetails* details) {
1799 if (is_loading == is_loading_)
1800 return;
1801
1802 if (!is_loading) {
1803 load_state_ = net::LOAD_STATE_IDLE;
1804 load_state_host_.clear();
[email protected]094e5b22009-09-25 04:23:561805 upload_size_ = 0;
1806 upload_position_ = 0;
[email protected]96d185d2009-04-24 03:28:541807 }
1808
1809 render_manager_.SetIsLoading(is_loading);
1810
1811 is_loading_ = is_loading;
1812 waiting_for_response_ = is_loading;
1813
[email protected]6ebdc9b2010-09-27 16:55:571814 if (delegate_)
1815 delegate_->LoadingStateChanged(this);
[email protected]329581b2009-04-28 06:52:351816 NotifyNavigationStateChanged(INVALIDATE_LOAD);
[email protected]96d185d2009-04-24 03:28:541817
1818 NotificationType type = is_loading ? NotificationType::LOAD_START :
1819 NotificationType::LOAD_STOP;
[email protected]ce5c4502009-05-06 16:46:111820 NotificationDetails det = NotificationService::NoDetails();
[email protected]96d185d2009-04-24 03:28:541821 if (details)
1822 det = Details<LoadNotificationDetails>(details);
1823 NotificationService::current()->Notify(type,
1824 Source<NavigationController>(&controller_),
1825 det);
1826}
1827
[email protected]b6c874582009-05-08 19:38:311828void TabContents::AddPopup(TabContents* new_contents,
[email protected]7e7414ae2010-01-26 20:19:291829 const gfx::Rect& initial_pos) {
[email protected]57eda82a2010-05-05 21:28:061830 // A page can't spawn popups (or do anything else, either) until its load
1831 // commits, so when we reach here, the popup was spawned by the
1832 // NavigationController's last committed entry, not the active entry. For
1833 // example, if a page opens a popup in an onunload() handler, then the active
1834 // entry is the page to be loaded as we navigate away from the unloading
1835 // page. For this reason, we can't use GetURL() to get the opener URL,
1836 // because it returns the active entry.
1837 NavigationEntry* entry = controller_.GetLastCommittedEntry();
1838 GURL creator = entry ? entry->virtual_url() : GURL::EmptyGURL();
1839
1840 if (creator.is_valid() &&
[email protected]8cc8d492010-02-02 10:40:491841 profile()->GetHostContentSettingsMap()->GetContentSetting(
[email protected]ca352452010-08-06 11:14:091842 creator, CONTENT_SETTINGS_TYPE_POPUPS, "") == CONTENT_SETTING_ALLOW) {
[email protected]8cc8d492010-02-02 10:40:491843 AddNewContents(new_contents, NEW_POPUP, initial_pos, true);
1844 } else {
[email protected]c65e2f152010-10-14 15:30:401845 if (!blocked_contents_)
1846 blocked_contents_ = new BlockedContentContainer(this);
1847 blocked_contents_->AddTabContents(new_contents, NEW_POPUP, initial_pos,
1848 true);
[email protected]55126132010-08-19 14:53:281849 content_settings_delegate_->OnContentBlocked(CONTENT_SETTINGS_TYPE_POPUPS,
1850 std::string());
[email protected]8cc8d492010-02-02 10:40:491851 }
[email protected]b9681312008-11-07 00:08:261852}
[email protected]616ed5a2008-11-21 22:27:241853
[email protected]616ed5a2008-11-21 22:27:241854void TabContents::ExpireInfoBars(
1855 const NavigationController::LoadCommittedDetails& details) {
1856 // Only hide InfoBars when the user has done something that makes the main
1857 // frame load. We don't want various automatic or subframe navigations making
1858 // it disappear.
1859 if (!details.is_user_initiated_main_frame_load())
1860 return;
1861
[email protected]f86a07022008-11-25 01:06:051862 for (int i = infobar_delegate_count() - 1; i >= 0; --i) {
[email protected]616ed5a2008-11-21 22:27:241863 InfoBarDelegate* delegate = GetInfoBarDelegateAt(i);
[email protected]f86a07022008-11-25 01:06:051864 if (delegate->ShouldExpire(details))
[email protected]616ed5a2008-11-21 22:27:241865 RemoveInfoBar(delegate);
[email protected]616ed5a2008-11-21 22:27:241866 }
1867}
[email protected]fdd61c62009-04-22 19:22:571868
[email protected]96d185d2009-04-24 03:28:541869DOMUI* TabContents::GetDOMUIForCurrentState() {
1870 // When there is a pending navigation entry, we want to use the pending DOMUI
1871 // that goes along with it to control the basic flags. For example, we want to
1872 // show the pending URL in the URL bar, so we want the display_url flag to
1873 // be from the pending entry.
1874 //
1875 // The confusion comes because there are multiple possibilities for the
1876 // initial load in a tab as a side effect of the way the RenderViewHostManager
1877 // works.
1878 //
1879 // - For the very first tab the load looks "normal". The new tab DOM UI is
1880 // the pending one, and we want it to apply here.
1881 //
1882 // - For subsequent new tabs, they'll get a new SiteInstance which will then
1883 // get switched to the one previously associated with the new tab pages.
1884 // This switching will cause the manager to commit the RVH/DOMUI. So we'll
1885 // have a committed DOM UI in this case.
1886 //
1887 // This condition handles all of these cases:
1888 //
1889 // - First load in first tab: no committed nav entry + pending nav entry +
1890 // pending dom ui:
1891 // -> Use pending DOM UI if any.
1892 //
1893 // - First load in second tab: no committed nav entry + pending nav entry +
1894 // no pending DOM UI:
1895 // -> Use the committed DOM UI if any.
1896 //
1897 // - Second navigation in any tab: committed nav entry + pending nav entry:
1898 // -> Use pending DOM UI if any.
1899 //
1900 // - Normal state with no load: committed nav entry + no pending nav entry:
1901 // -> Use committed DOM UI.
1902 if (controller_.pending_entry() &&
1903 (controller_.GetLastCommittedEntry() ||
1904 render_manager_.pending_dom_ui()))
1905 return render_manager_.pending_dom_ui();
1906 return render_manager_.dom_ui();
1907}
[email protected]420ae012009-04-24 05:16:321908
1909void TabContents::DidNavigateMainFramePostCommit(
1910 const NavigationController::LoadCommittedDetails& details,
1911 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]c2e74fe82009-09-03 17:57:441912 if (opener_dom_ui_type_ != DOMUIFactory::kNoDOMUI) {
1913 // If this is a window.open navigation, use the same DOMUI as the renderer
1914 // that opened the window, as long as both renderers have the same
1915 // privileges.
[email protected]583d45c12010-08-31 02:48:121916 if (opener_dom_ui_type_ ==
1917 DOMUIFactory::GetDOMUIType(profile(), GetURL())) {
[email protected]c2e74fe82009-09-03 17:57:441918 DOMUI* dom_ui = DOMUIFactory::CreateDOMUIForURL(this, GetURL());
1919 // dom_ui might be NULL if the URL refers to a non-existent extension.
1920 if (dom_ui) {
1921 render_manager_.SetDOMUIPostCommit(dom_ui);
1922 dom_ui->RenderViewCreated(render_view_host());
1923 }
1924 }
1925 opener_dom_ui_type_ = DOMUIFactory::kNoDOMUI;
1926 }
1927
[email protected]420ae012009-04-24 05:16:321928 if (details.is_user_initiated_main_frame_load()) {
1929 // Clear the status bubble. This is a workaround for a bug where WebKit
1930 // doesn't let us know that the cursor left an element during a
1931 // transition (this is also why the mouse cursor remains as a hand after
1932 // clicking on a link); see bugs 1184641 and 980803. We don't want to
1933 // clear the bubble when a user navigates to a named anchor in the same
1934 // page.
1935 UpdateTargetURL(details.entry->page_id(), GURL());
[email protected]420ae012009-04-24 05:16:321936 }
1937
1938 // The keyword generator uses the navigation entries, so must be called after
1939 // the commit.
1940 GenerateKeywordIfNecessary(params);
1941
1942 // Allow the new page to set the title again.
1943 received_page_title_ = false;
1944
1945 // Get the favicon, either from history or request it from the net.
1946 fav_icon_helper_.FetchFavIcon(details.entry->url());
1947
[email protected]0de80162010-02-03 04:52:351948 // Clear all page actions, blocked content notifications and browser actions
1949 // for this tab, unless this is an in-page navigation.
[email protected]a6e82fc2010-02-24 22:28:141950 if (!details.is_in_page) {
[email protected]eaa7dd182010-12-14 11:09:001951 ExtensionService* service = profile()->GetExtensionService();
[email protected]92c6f9b92009-10-24 04:35:081952 if (service) {
1953 for (size_t i = 0; i < service->extensions()->size(); ++i) {
[email protected]5d246db22009-10-27 06:17:571954 ExtensionAction* browser_action =
[email protected]92c6f9b92009-10-24 04:35:081955 service->extensions()->at(i)->browser_action();
[email protected]56ce6e52009-10-27 00:10:521956 if (browser_action) {
1957 browser_action->ClearAllValuesForTab(controller().session_id().id());
1958 NotificationService::current()->Notify(
1959 NotificationType::EXTENSION_BROWSER_ACTION_UPDATED,
[email protected]5d246db22009-10-27 06:17:571960 Source<ExtensionAction>(browser_action),
[email protected]56ce6e52009-10-27 00:10:521961 NotificationService::NoDetails());
1962 }
[email protected]92c6f9b92009-10-24 04:35:081963
[email protected]5d246db22009-10-27 06:17:571964 ExtensionAction* page_action =
[email protected]56ce6e52009-10-27 00:10:521965 service->extensions()->at(i)->page_action();
1966 if (page_action) {
1967 page_action->ClearAllValuesForTab(controller().session_id().id());
1968 PageActionStateChanged();
1969 }
[email protected]92c6f9b92009-10-24 04:35:081970 }
[email protected]aece2c7f2009-08-27 20:43:171971 }
[email protected]0de80162010-02-03 04:52:351972
1973 // Close blocked popups.
[email protected]c65e2f152010-10-14 15:30:401974 if (blocked_contents_) {
[email protected]0fbd70332010-06-01 19:28:341975 AutoReset<bool> auto_reset(&dont_notify_render_view_, true);
[email protected]c65e2f152010-10-14 15:30:401976 blocked_contents_->Destroy();
1977 blocked_contents_ = NULL;
[email protected]0de80162010-02-03 04:52:351978 }
1979
1980 // Clear "blocked" flags.
[email protected]da4f8a12010-08-19 08:33:411981 content_settings_delegate_->ClearBlockedContentSettingsExceptForCookies();
[email protected]9fb83e82010-07-02 18:24:551982 content_settings_delegate_->GeolocationDidNavigate(details);
[email protected]f17a0ee2010-05-17 17:38:471983
1984 // Once the main frame is navigated, we're no longer considered to have
1985 // displayed insecure content.
1986 displayed_insecure_content_ = false;
[email protected]aece2c7f2009-08-27 20:43:171987 }
[email protected]ce5c4502009-05-06 16:46:111988
[email protected]8cc8d492010-02-02 10:40:491989 // Close constrained windows if necessary.
1990 if (!net::RegistryControlledDomainService::SameDomainOrHost(
1991 details.previous_url, details.entry->url()))
1992 CloseConstrainedWindows();
1993
[email protected]420ae012009-04-24 05:16:321994 // Update the starred state.
1995 UpdateStarredStateForCurrentURL();
[email protected]c07c9752010-03-12 05:57:531996
[email protected]3c9e1872010-11-18 16:17:491997 // Notify observers about navigation.
1998 FOR_EACH_OBSERVER(WebNavigationObserver, web_navigation_observers_,
1999 DidNavigateMainFramePostCommit(details, params));
[email protected]420ae012009-04-24 05:16:322000}
2001
2002void TabContents::DidNavigateAnyFramePostCommit(
2003 RenderViewHost* render_view_host,
2004 const NavigationController::LoadCommittedDetails& details,
2005 const ViewHostMsg_FrameNavigate_Params& params) {
2006 // If we navigate, start showing messages again. This does nothing to prevent
2007 // a malicious script from spamming messages, since the script could just
2008 // reload the page to stop blocking.
2009 suppress_javascript_messages_ = false;
2010
[email protected]3c9e1872010-11-18 16:17:492011 // Notify observers about navigation.
2012 FOR_EACH_OBSERVER(WebNavigationObserver, web_navigation_observers_,
2013 DidNavigateAnyFramePostCommit(details, params));
[email protected]3b1c1812010-02-12 21:53:552014
2015 // Let the LanguageState clear its state.
[email protected]80fc6112010-08-13 20:10:582016 language_state_.DidNavigate(details);
[email protected]420ae012009-04-24 05:16:322017}
2018
[email protected]8cc8d492010-02-02 10:40:492019void TabContents::CloseConstrainedWindows() {
2020 // Clear out any constrained windows since we are leaving this page entirely.
[email protected]420ae012009-04-24 05:16:322021 // We use indices instead of iterators in case CloseWindow does something
2022 // that may invalidate an iterator.
[email protected]61d68ef12011-01-13 14:02:562023 for (size_t i = 0; i < child_windows_.size(); ++i) {
2024 ConstrainedWindow* window = child_windows_[child_windows_.size() - 1 - i];
[email protected]fa1cf0b82010-01-15 21:49:442025 if (window) {
[email protected]420ae012009-04-24 05:16:322026 window->CloseConstrainedWindow();
[email protected]fa1cf0b82010-01-15 21:49:442027 BlockTabContent(false);
2028 }
[email protected]420ae012009-04-24 05:16:322029 }
2030}
2031
2032void TabContents::UpdateStarredStateForCurrentURL() {
2033 BookmarkModel* model = profile()->GetBookmarkModel();
2034 const bool old_state = is_starred_;
2035 is_starred_ = (model && model->IsBookmarked(GetURL()));
2036
2037 if (is_starred_ != old_state && delegate())
2038 delegate()->URLStarredChanged(this, is_starred_);
2039}
2040
2041void TabContents::UpdateAlternateErrorPageURL() {
2042 GURL url = GetAlternateErrorPageURL();
2043 render_view_host()->SetAlternateErrorPageURL(url);
2044}
2045
2046void TabContents::UpdateWebPreferences() {
2047 render_view_host()->UpdateWebPreferences(GetWebkitPrefs());
2048}
2049
[email protected]d0b8d092010-10-25 04:05:172050void TabContents::UpdateZoomLevel() {
2051 render_view_host()->SetZoomLevel(GetZoomLevel());
2052}
2053
[email protected]420ae012009-04-24 05:16:322054void TabContents::UpdateMaxPageIDIfNecessary(SiteInstance* site_instance,
2055 RenderViewHost* rvh) {
2056 // If we are creating a RVH for a restored controller, then we might
2057 // have more page IDs than the SiteInstance's current max page ID. We must
2058 // make sure that the max page ID is larger than any restored page ID.
2059 // Note that it is ok for conflicting page IDs to exist in another tab
2060 // (i.e., NavigationController), but if any page ID is larger than the max,
2061 // the back/forward list will get confused.
2062 int max_restored_page_id = controller_.max_restored_page_id();
2063 if (max_restored_page_id > 0) {
2064 int curr_max_page_id = site_instance->max_page_id();
2065 if (max_restored_page_id > curr_max_page_id) {
2066 // Need to update the site instance immediately.
2067 site_instance->UpdateMaxPageID(max_restored_page_id);
2068
2069 // Also tell the renderer to update its internal representation. We
2070 // need to reserve enough IDs to make all restored page IDs less than
2071 // the max.
2072 if (curr_max_page_id < 0)
2073 curr_max_page_id = 0;
2074 rvh->ReservePageIDRange(max_restored_page_id - curr_max_page_id);
2075 }
2076 }
2077}
2078
[email protected]ec0b6c42010-08-26 03:16:582079scoped_refptr<history::HistoryAddPageArgs>
2080TabContents::CreateHistoryAddPageArgs(
[email protected]ebe89e062009-08-13 23:16:542081 const GURL& virtual_url,
[email protected]befd8d822009-07-01 04:51:472082 const NavigationController::LoadCommittedDetails& details,
[email protected]420ae012009-04-24 05:16:322083 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]ec0b6c42010-08-26 03:16:582084 scoped_refptr<history::HistoryAddPageArgs> add_page_args(
2085 new history::HistoryAddPageArgs(
2086 params.url, base::Time::Now(), this, params.page_id, params.referrer,
2087 params.redirects, params.transition, history::SOURCE_BROWSED,
2088 details.did_replace_entry));
2089 if (PageTransition::IsMainFrame(params.transition) &&
2090 virtual_url != params.url) {
2091 // Hack on the "virtual" URL so that it will appear in history. For some
2092 // types of URLs, we will display a magic URL that is different from where
2093 // the page is actually navigated. We want the user to see in history what
2094 // they saw in the URL bar, so we add the virtual URL as a redirect. This
2095 // only applies to the main frame, as the virtual URL doesn't apply to
2096 // sub-frames.
2097 add_page_args->url = virtual_url;
2098 if (!add_page_args->redirects.empty())
2099 add_page_args->redirects.back() = virtual_url;
[email protected]420ae012009-04-24 05:16:322100 }
[email protected]ec0b6c42010-08-26 03:16:582101 return add_page_args;
[email protected]420ae012009-04-24 05:16:322102}
2103
2104bool TabContents::UpdateTitleForEntry(NavigationEntry* entry,
2105 const std::wstring& title) {
2106 // For file URLs without a title, use the pathname instead. In the case of a
2107 // synthesized title, we don't want the update to count toward the "one set
2108 // per page of the title to history."
[email protected]e53668962010-06-23 15:35:252109 string16 final_title;
[email protected]420ae012009-04-24 05:16:322110 bool explicit_set;
2111 if (entry->url().SchemeIsFile() && title.empty()) {
[email protected]e53668962010-06-23 15:35:252112 final_title = UTF8ToUTF16(entry->url().ExtractFileName());
[email protected]420ae012009-04-24 05:16:322113 explicit_set = false; // Don't count synthetic titles toward the set limit.
2114 } else {
[email protected]e53668962010-06-23 15:35:252115 TrimWhitespace(WideToUTF16Hack(title), TRIM_ALL, &final_title);
[email protected]420ae012009-04-24 05:16:322116 explicit_set = true;
2117 }
2118
[email protected]e53668962010-06-23 15:35:252119 if (final_title == entry->title())
[email protected]420ae012009-04-24 05:16:322120 return false; // Nothing changed, don't bother.
2121
[email protected]e53668962010-06-23 15:35:252122 entry->set_title(final_title);
[email protected]420ae012009-04-24 05:16:322123
[email protected]ec0b6c42010-08-26 03:16:582124 if (!received_page_title_) {
2125 UpdateHistoryPageTitle(*entry);
[email protected]420ae012009-04-24 05:16:322126 received_page_title_ = explicit_set;
2127 }
2128
2129 // Lastly, set the title for the view.
[email protected]e53668962010-06-23 15:35:252130 view_->SetPageTitle(UTF16ToWideHack(final_title));
[email protected]420ae012009-04-24 05:16:322131
[email protected]cbc0e1b2010-04-12 18:33:042132 NotificationService::current()->Notify(
2133 NotificationType::TAB_CONTENTS_TITLE_UPDATED,
2134 Source<TabContents>(this),
2135 NotificationService::NoDetails());
2136
[email protected]420ae012009-04-24 05:16:322137 return true;
2138}
2139
2140void TabContents::NotifySwapped() {
2141 // After sending out a swap notification, we need to send a disconnect
2142 // notification so that clients that pick up a pointer to |this| can NULL the
2143 // pointer. See Bug 1230284.
2144 notify_disconnection_ = true;
2145 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:342146 NotificationType::TAB_CONTENTS_SWAPPED,
[email protected]d82ed61e2009-06-16 02:46:222147 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:322148 NotificationService::NoDetails());
2149}
2150
2151void TabContents::NotifyConnected() {
2152 notify_disconnection_ = true;
2153 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:342154 NotificationType::TAB_CONTENTS_CONNECTED,
[email protected]d82ed61e2009-06-16 02:46:222155 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:322156 NotificationService::NoDetails());
2157}
2158
2159void TabContents::NotifyDisconnected() {
2160 if (!notify_disconnection_)
2161 return;
2162
2163 notify_disconnection_ = false;
2164 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:342165 NotificationType::TAB_CONTENTS_DISCONNECTED,
[email protected]d82ed61e2009-06-16 02:46:222166 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:322167 NotificationService::NoDetails());
2168}
2169
2170void TabContents::GenerateKeywordIfNecessary(
2171 const ViewHostMsg_FrameNavigate_Params& params) {
2172 if (!params.searchable_form_url.is_valid())
2173 return;
2174
2175 if (profile()->IsOffTheRecord())
2176 return;
2177
2178 int last_index = controller_.last_committed_entry_index();
2179 // When there was no previous page, the last index will be 0. This is
2180 // normally due to a form submit that opened in a new tab.
2181 // TODO(brettw) bug 916126: we should support keywords when form submits
2182 // happen in new tabs.
2183 if (last_index <= 0)
2184 return;
2185 const NavigationEntry* previous_entry =
2186 controller_.GetEntryAtIndex(last_index - 1);
2187 if (IsFormSubmit(previous_entry)) {
2188 // Only generate a keyword if the previous page wasn't itself a form
2189 // submit.
2190 return;
2191 }
2192
2193 GURL keyword_url = previous_entry->user_typed_url().is_valid() ?
2194 previous_entry->user_typed_url() : previous_entry->url();
[email protected]400b133f2011-01-19 18:32:302195 string16 keyword =
[email protected]420ae012009-04-24 05:16:322196 TemplateURLModel::GenerateKeyword(keyword_url, true); // autodetected
2197 if (keyword.empty())
2198 return;
2199
2200 TemplateURLModel* url_model = profile()->GetTemplateURLModel();
2201 if (!url_model)
2202 return;
2203
2204 if (!url_model->loaded()) {
2205 url_model->Load();
2206 return;
2207 }
2208
2209 const TemplateURL* current_url;
[email protected]ddd231e2010-06-29 20:35:192210 GURL url = params.searchable_form_url;
[email protected]420ae012009-04-24 05:16:322211 if (!url_model->CanReplaceKeyword(keyword, url, &current_url))
2212 return;
2213
2214 if (current_url) {
2215 if (current_url->originating_url().is_valid()) {
2216 // The existing keyword was generated from an OpenSearch description
2217 // document, don't regenerate.
2218 return;
2219 }
2220 url_model->Remove(current_url);
2221 }
2222 TemplateURL* new_url = new TemplateURL();
2223 new_url->set_keyword(keyword);
2224 new_url->set_short_name(keyword);
[email protected]ddd231e2010-06-29 20:35:192225 new_url->SetURL(url.spec(), 0, 0);
[email protected]420ae012009-04-24 05:16:322226 new_url->add_input_encoding(params.searchable_form_encoding);
2227 DCHECK(controller_.GetLastCommittedEntry());
2228 const GURL& favicon_url =
2229 controller_.GetLastCommittedEntry()->favicon().url();
2230 if (favicon_url.is_valid()) {
2231 new_url->SetFavIconURL(favicon_url);
2232 } else {
2233 // The favicon url isn't valid. This means there really isn't a favicon,
2234 // or the favicon url wasn't obtained before the load started. This assumes
2235 // the later.
2236 // TODO(sky): Need a way to set the favicon that doesn't involve generating
2237 // its url.
2238 new_url->SetFavIconURL(TemplateURL::GenerateFaviconURL(params.referrer));
2239 }
2240 new_url->set_safe_for_autoreplace(true);
2241 url_model->Add(new_url);
2242}
2243
[email protected]bf5c2ff392009-07-08 16:24:332244void TabContents::OnUserGesture() {
2245 // See comment in RenderViewHostDelegate::OnUserGesture as to why we do this.
[email protected]41291322010-07-15 17:09:012246 DownloadRequestLimiter* limiter =
2247 g_browser_process->download_request_limiter();
2248 if (limiter)
2249 limiter->OnUserGesture(this);
[email protected]d070ec62010-07-27 21:28:262250 ExternalProtocolHandler::PermitLaunchUrl();
[email protected]bf5c2ff392009-07-08 16:24:332251}
2252
2253void TabContents::OnFindReply(int request_id,
2254 int number_of_matches,
2255 const gfx::Rect& selection_rect,
2256 int active_match_ordinal,
2257 bool final_update) {
[email protected]3ce7ced2009-07-24 18:52:422258 // Ignore responses for requests that have been aborted.
2259 if (find_op_aborted_)
2260 return;
2261
[email protected]bf5c2ff392009-07-08 16:24:332262 // Ignore responses for requests other than the one we have most recently
2263 // issued. That way we won't act on stale results when the user has
2264 // already typed in another query.
2265 if (request_id != current_find_request_id_)
2266 return;
2267
2268 if (number_of_matches == -1)
2269 number_of_matches = last_search_result_.number_of_matches();
2270 if (active_match_ordinal == -1)
2271 active_match_ordinal = last_search_result_.active_match_ordinal();
2272
2273 gfx::Rect selection = selection_rect;
2274 if (selection.IsEmpty())
2275 selection = last_search_result_.selection_rect();
2276
2277 // Notify the UI, automation and any other observers that a find result was
2278 // found.
2279 last_search_result_ = FindNotificationDetails(request_id, number_of_matches,
2280 selection, active_match_ordinal,
2281 final_update);
2282 NotificationService::current()->Notify(
2283 NotificationType::FIND_RESULT_AVAILABLE,
2284 Source<TabContents>(this),
2285 Details<FindNotificationDetails>(&last_search_result_));
2286}
2287
2288void TabContents::GoToEntryAtOffset(int offset) {
[email protected]876bc832010-09-07 16:29:542289 if (!delegate_ || delegate_->OnGoToEntryOffset(offset)) {
2290 NavigationEntry* entry = controller_.GetEntryAtOffset(offset);
2291 if (!entry)
2292 return;
2293 // Note that we don't call NavigationController::GotToOffset() as we don't
2294 // want to create a pending navigation entry (it might end up lingering
2295 // http://crbug.com/51680).
[email protected]784688a62010-09-13 07:06:522296 entry->set_transition_type(entry->transition_type() |
2297 PageTransition::FORWARD_BACK);
[email protected]876bc832010-09-07 16:29:542298 NavigateToEntry(*entry, NavigationController::NO_RELOAD);
2299 }
[email protected]bf5c2ff392009-07-08 16:24:332300}
2301
[email protected]bf5c2ff392009-07-08 16:24:332302void TabContents::OnMissingPluginStatus(int status) {
2303#if defined(OS_WIN)
2304// TODO(PORT): pull in when plug-ins work
2305 GetPluginInstaller()->OnMissingPluginStatus(status);
2306#endif
2307}
2308
2309void TabContents::OnCrashedPlugin(const FilePath& plugin_path) {
[email protected]bf5c2ff392009-07-08 16:24:332310 DCHECK(!plugin_path.value().empty());
2311
2312 std::wstring plugin_name = plugin_path.ToWStringHack();
[email protected]191eb3f72010-12-21 06:27:502313 webkit::npapi::WebPluginInfo plugin_info;
2314 if (webkit::npapi::PluginList::Singleton()->GetPluginInfoByPath(
[email protected]45446a52010-11-04 17:41:002315 plugin_path, &plugin_info) &&
2316 !plugin_info.name.empty()) {
2317 plugin_name = UTF16ToWide(plugin_info.name);
[email protected]996fd702009-09-04 19:12:372318#if defined(OS_MACOSX)
[email protected]45446a52010-11-04 17:41:002319 // Many plugins on the Mac have .plugin in the actual name, which looks
2320 // terrible, so look for that and strip it off if present.
2321 const std::wstring plugin_extension(L".plugin");
2322 if (EndsWith(plugin_name, plugin_extension, true))
2323 plugin_name.erase(plugin_name.length() - plugin_extension.length());
[email protected]996fd702009-09-04 19:12:372324#endif // OS_MACOSX
[email protected]bf5c2ff392009-07-08 16:24:332325 }
[email protected]2ae8a922010-06-28 20:27:532326 SkBitmap* crash_icon = ResourceBundle::GetSharedInstance().GetBitmapNamed(
2327 IDR_INFOBAR_PLUGIN_CRASHED);
[email protected]f4f50ef2011-01-21 19:01:192328 AddInfoBar(new SimpleAlertInfoBarDelegate(this, crash_icon,
2329 l10n_util::GetStringFUTF16(IDS_PLUGIN_CRASHED_PROMPT,
2330 WideToUTF16Hack(plugin_name)), true));
[email protected]bf5c2ff392009-07-08 16:24:332331}
2332
2333void TabContents::OnCrashedWorker() {
[email protected]f4f50ef2011-01-21 19:01:192334 AddInfoBar(new SimpleAlertInfoBarDelegate(this, NULL,
2335 l10n_util::GetStringUTF16(IDS_WEBWORKER_CRASHED_PROMPT), true));
[email protected]bf5c2ff392009-07-08 16:24:332336}
2337
[email protected]38789d82010-11-17 06:03:442338void TabContents::OnDidGetApplicationInfo(int32 page_id,
2339 const WebApplicationInfo& info) {
[email protected]ed5431872009-11-17 08:39:512340 web_app_info_ = info;
[email protected]bf5c2ff392009-07-08 16:24:332341
[email protected]ed5431872009-11-17 08:39:512342 if (delegate())
2343 delegate()->OnDidGetApplicationInfo(this, page_id);
[email protected]bf5c2ff392009-07-08 16:24:332344}
2345
[email protected]bb461532010-11-26 21:50:232346void TabContents::OnInstallApplication(const WebApplicationInfo& info) {
2347 if (delegate())
2348 delegate()->OnInstallApplication(this, info);
2349}
2350
[email protected]3c2826852010-12-16 19:09:142351void TabContents::OnBlockedOutdatedPlugin(const string16& name,
2352 const GURL& update_url) {
2353 new OutdatedPluginInfoBar(this, name, update_url);
[email protected]851b1eb2010-08-09 13:32:292354}
2355
[email protected]a2cf65eb2010-01-18 08:20:382356void TabContents::OnPageContents(const GURL& url,
2357 int renderer_process_id,
2358 int32 page_id,
[email protected]e5106202010-06-11 21:12:362359 const string16& contents,
[email protected]1c57b7b2010-07-12 17:38:102360 const std::string& language,
2361 bool page_translatable) {
[email protected]a2cf65eb2010-01-18 08:20:382362 // Don't index any https pages. People generally don't want their bank
2363 // accounts, etc. indexed on their computer, especially since some of these
2364 // things are not marked cachable.
2365 // TODO(brettw) we may want to consider more elaborate heuristics such as
2366 // the cachability of the page. We may also want to consider subframes (this
2367 // test will still index subframes if the subframe is SSL).
[email protected]ccd40a52010-05-11 16:15:272368 // TODO(zelidrag) bug chromium-os:2808 - figure out if we want to reenable
2369 // content indexing for chromeos in some future releases.
2370#if !defined(OS_CHROMEOS)
[email protected]a2cf65eb2010-01-18 08:20:382371 if (!url.SchemeIsSecure()) {
2372 Profile* p = profile();
2373 if (p && !p->IsOffTheRecord()) {
2374 HistoryService* hs = p->GetHistoryService(Profile::IMPLICIT_ACCESS);
2375 if (hs)
2376 hs->SetPageContents(url, contents);
2377 }
2378 }
[email protected]ccd40a52010-05-11 16:15:272379#endif
[email protected]a8a81292010-01-21 00:32:452380
[email protected]1c57b7b2010-07-12 17:38:102381 language_state_.LanguageDetermined(language, page_translatable);
[email protected]a8a81292010-01-21 00:32:452382
2383 std::string lang = language;
2384 NotificationService::current()->Notify(
2385 NotificationType::TAB_LANGUAGE_DETERMINED,
[email protected]7e0831b2010-02-01 08:45:152386 Source<TabContents>(this),
[email protected]a8a81292010-01-21 00:32:452387 Details<std::string>(&lang));
[email protected]d54169e92011-01-21 09:19:522388
2389 // Generate the thumbnail here if the in-browser thumbnailing is enabled.
2390 if (CommandLine::ForCurrentProcess()->HasSwitch(
2391 switches::kEnableInBrowserThumbnailing)) {
2392 ThumbnailGenerator::UpdateThumbnailIfNecessary(this, url);
2393 }
[email protected]a2cf65eb2010-01-18 08:20:382394}
2395
[email protected]7e0831b2010-02-01 08:45:152396void TabContents::OnPageTranslated(int32 page_id,
2397 const std::string& original_lang,
[email protected]152b8ba32010-03-29 19:40:162398 const std::string& translated_lang,
2399 TranslateErrors::Type error_type) {
[email protected]3b1c1812010-02-12 21:53:552400 language_state_.set_current_language(translated_lang);
2401 language_state_.set_translation_pending(false);
[email protected]152b8ba32010-03-29 19:40:162402 PageTranslatedDetails details(original_lang, translated_lang, error_type);
[email protected]7e0831b2010-02-01 08:45:152403 NotificationService::current()->Notify(
2404 NotificationType::PAGE_TRANSLATED,
2405 Source<TabContents>(this),
[email protected]152b8ba32010-03-29 19:40:162406 Details<PageTranslatedDetails>(&details));
[email protected]7e0831b2010-02-01 08:45:152407}
2408
[email protected]ce833282010-11-04 15:48:392409void TabContents::OnSetSuggestions(
2410 int32 page_id,
2411 const std::vector<std::string>& suggestions) {
[email protected]15d9c0c2010-09-10 22:36:262412 if (delegate())
[email protected]ce833282010-11-04 15:48:392413 delegate()->OnSetSuggestions(page_id, suggestions);
2414}
2415
2416void TabContents::OnInstantSupportDetermined(int32 page_id, bool result) {
2417 if (delegate())
2418 delegate()->OnInstantSupportDetermined(page_id, result);
[email protected]15d9c0c2010-09-10 22:36:262419}
2420
[email protected]dcd5b332010-08-11 08:55:182421void TabContents::OnContentSettingsAccessed(bool content_was_blocked) {
[email protected]614c0e12010-03-25 10:12:562422 if (delegate_)
[email protected]20ad6a92010-04-21 14:07:242423 delegate_->OnContentSettingsChange(this);
[email protected]614c0e12010-03-25 10:12:562424}
2425
[email protected]8d3347f2009-07-09 22:00:212426RenderViewHostDelegate::View* TabContents::GetViewDelegate() {
[email protected]420ae012009-04-24 05:16:322427 return view_.get();
2428}
2429
[email protected]8d3347f2009-07-09 22:00:212430RenderViewHostDelegate::RendererManagement*
2431TabContents::GetRendererManagementDelegate() {
2432 return &render_manager_;
2433}
2434
[email protected]bf5c2ff392009-07-08 16:24:332435RenderViewHostDelegate::BrowserIntegration*
[email protected]8d3347f2009-07-09 22:00:212436 TabContents::GetBrowserIntegrationDelegate() {
2437 return this;
[email protected]bf5c2ff392009-07-08 16:24:332438}
2439
[email protected]46ba9082010-06-22 10:00:582440RenderViewHostDelegate::ContentSettings*
2441TabContents::GetContentSettingsDelegate() {
[email protected]9fb83e82010-07-02 18:24:552442 return content_settings_delegate_.get();
[email protected]46ba9082010-06-22 10:00:582443}
2444
[email protected]8d3347f2009-07-09 22:00:212445RenderViewHostDelegate::Save* TabContents::GetSaveDelegate() {
[email protected]420ae012009-04-24 05:16:322446 return save_package_.get(); // May be NULL, but we can return NULL.
2447}
2448
[email protected]8d3347f2009-07-09 22:00:212449RenderViewHostDelegate::Printing* TabContents::GetPrintingDelegate() {
[email protected]12802702010-07-09 19:43:092450 return printing_.get();
[email protected]8d3347f2009-07-09 22:00:212451}
2452
2453RenderViewHostDelegate::FavIcon* TabContents::GetFavIconDelegate() {
2454 return &fav_icon_helper_;
2455}
2456
[email protected]1f18184a2010-07-21 19:34:492457RenderViewHostDelegate::SSL* TabContents::GetSSLDelegate() {
2458 return GetSSLHelper();
2459}
2460
[email protected]b5977a0c2010-08-24 19:46:262461RenderViewHostDelegate::FileSelect* TabContents::GetFileSelectDelegate() {
2462 if (file_select_helper_.get() == NULL)
[email protected]ba70d082010-09-10 16:54:492463 file_select_helper_.reset(new FileSelectHelper(profile()));
[email protected]b5977a0c2010-08-24 19:46:262464 return file_select_helper_.get();
2465}
2466
[email protected]fd571bb2010-03-19 01:23:542467AutomationResourceRoutingDelegate*
2468TabContents::GetAutomationResourceRoutingDelegate() {
2469 return delegate();
2470}
2471
[email protected]ced90ae12010-02-20 02:06:162472RenderViewHostDelegate::BookmarkDrag* TabContents::GetBookmarkDragDelegate() {
2473 return bookmark_drag_;
2474}
2475
2476void TabContents::SetBookmarkDragDelegate(
2477 RenderViewHostDelegate::BookmarkDrag* bookmark_drag) {
2478 bookmark_drag_ = bookmark_drag;
2479}
2480
[email protected]9fb83e82010-07-02 18:24:552481TabSpecificContentSettings* TabContents::GetTabSpecificContentSettings() const {
2482 return content_settings_delegate_.get();
2483}
2484
[email protected]93623c5d2009-12-10 21:40:322485RendererPreferences TabContents::GetRendererPrefs(Profile* profile) const {
[email protected]840b1512009-07-21 16:53:462486 return renderer_preferences_;
[email protected]80d96fa2009-06-10 22:34:512487}
2488
[email protected]57c6a652009-05-04 07:58:342489TabContents* TabContents::GetAsTabContents() {
2490 return this;
2491}
2492
[email protected]7b291f92009-08-14 05:43:532493ViewType::Type TabContents::GetRenderViewType() const {
2494 return ViewType::TAB_CONTENTS;
2495}
2496
2497int TabContents::GetBrowserWindowID() const {
2498 return controller().window_id().id();
2499}
2500
[email protected]420ae012009-04-24 05:16:322501void TabContents::RenderViewCreated(RenderViewHost* render_view_host) {
[email protected]2e4633c2009-07-09 16:58:062502 NotificationService::current()->Notify(
2503 NotificationType::RENDER_VIEW_HOST_CREATED_FOR_TAB,
2504 Source<TabContents>(this),
2505 Details<RenderViewHost>(render_view_host));
[email protected]420ae012009-04-24 05:16:322506 NavigationEntry* entry = controller_.GetActiveEntry();
2507 if (!entry)
2508 return;
2509
2510 // When we're creating views, we're still doing initial setup, so we always
2511 // use the pending DOM UI rather than any possibly existing committed one.
2512 if (render_manager_.pending_dom_ui()) {
2513 render_manager_.pending_dom_ui()->RenderViewCreated(render_view_host);
2514 }
2515
2516 if (entry->IsViewSourceMode()) {
2517 // Put the renderer in view source mode.
2518 render_view_host->Send(
2519 new ViewMsg_EnableViewSourceMode(render_view_host->routing_id()));
2520 }
[email protected]0666aef2009-05-13 19:48:082521
2522 view()->RenderViewCreated(render_view_host);
[email protected]420ae012009-04-24 05:16:322523}
2524
2525void TabContents::RenderViewReady(RenderViewHost* rvh) {
2526 if (rvh != render_view_host()) {
2527 // Don't notify the world, since this came from a renderer in the
2528 // background.
2529 return;
2530 }
2531
2532 NotifyConnected();
[email protected]9d8a4642009-07-29 17:25:302533 bool was_crashed = is_crashed();
[email protected]443b80e2010-12-14 00:42:232534 SetIsCrashed(base::TERMINATION_STATUS_STILL_RUNNING, 0);
[email protected]9d8a4642009-07-29 17:25:302535
2536 // Restore the focus to the tab (otherwise the focus will be on the top
2537 // window).
[email protected]484ae5912010-09-29 19:16:142538 if (was_crashed && !FocusLocationBarByDefault() &&
2539 (!delegate_ || delegate_->ShouldFocusPageAfterCrash())) {
[email protected]9d8a4642009-07-29 17:25:302540 Focus();
[email protected]484ae5912010-09-29 19:16:142541 }
[email protected]420ae012009-04-24 05:16:322542}
2543
[email protected]443b80e2010-12-14 00:42:232544void TabContents::RenderViewGone(RenderViewHost* rvh,
2545 base::TerminationStatus status,
2546 int error_code) {
[email protected]420ae012009-04-24 05:16:322547 // Ask the print preview if this renderer was valuable.
[email protected]12802702010-07-09 19:43:092548 if (!printing_->OnRenderViewGone(rvh))
[email protected]420ae012009-04-24 05:16:322549 return;
2550 if (rvh != render_view_host()) {
2551 // The pending page's RenderViewHost is gone.
2552 return;
2553 }
2554
2555 SetIsLoading(false, NULL);
2556 NotifyDisconnected();
[email protected]443b80e2010-12-14 00:42:232557 SetIsCrashed(status, error_code);
[email protected]420ae012009-04-24 05:16:322558
[email protected]ff9efc62010-02-18 22:10:542559 // Remove all infobars.
[email protected]e5430c92010-02-18 22:39:212560 for (int i = infobar_delegate_count() - 1; i >=0 ; --i)
[email protected]ff9efc62010-02-18 22:10:542561 RemoveInfoBar(GetInfoBarDelegateAt(i));
2562
[email protected]8e537302009-06-10 21:19:582563 // Tell the view that we've crashed so it can prepare the sad tab page.
[email protected]cf68b092010-01-15 20:05:552564 // Only do this if we're not in browser shutdown, so that TabContents
2565 // objects that are not in a browser (e.g., HTML dialogs) and thus are
2566 // visible do not flash a sad tab page.
2567 if (browser_shutdown::GetShutdownType() == browser_shutdown::NOT_VALID)
[email protected]a8f024392011-01-13 21:50:162568 view_->OnTabCrashed(status, error_code);
[email protected]420ae012009-04-24 05:16:322569
2570 // Hide any visible hung renderer warning for this web contents' process.
[email protected]e7b8d832009-11-20 22:41:002571 hung_renderer_dialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:322572}
2573
[email protected]2e4633c2009-07-09 16:58:062574void TabContents::RenderViewDeleted(RenderViewHost* rvh) {
2575 NotificationService::current()->Notify(
2576 NotificationType::RENDER_VIEW_HOST_DELETED,
2577 Source<TabContents>(this),
2578 Details<RenderViewHost>(rvh));
[email protected]fb7b79f72009-11-06 18:00:482579 render_manager_.RenderViewDeleted(rvh);
[email protected]2e4633c2009-07-09 16:58:062580}
2581
[email protected]420ae012009-04-24 05:16:322582void TabContents::DidNavigate(RenderViewHost* rvh,
2583 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]8030f012009-09-25 18:09:372584 int extra_invalidate_flags = 0;
2585
2586 if (PageTransition::IsMainFrame(params.transition)) {
[email protected]e7d50892011-01-19 21:47:382587 if (MaybeUsePreloadedPage(params.url)) {
2588 return;
[email protected]33f74972010-12-08 16:40:362589 }
2590
[email protected]20ede072009-10-06 00:28:352591 bool was_bookmark_bar_visible = ShouldShowBookmarkBar();
[email protected]8030f012009-09-25 18:09:372592
[email protected]420ae012009-04-24 05:16:322593 render_manager_.DidNavigateMainFrame(rvh);
2594
[email protected]20ede072009-10-06 00:28:352595 if (was_bookmark_bar_visible != ShouldShowBookmarkBar())
[email protected]8030f012009-09-25 18:09:372596 extra_invalidate_flags |= INVALIDATE_BOOKMARK_BAR;
[email protected]8030f012009-09-25 18:09:372597 }
2598
[email protected]420ae012009-04-24 05:16:322599 // Update the site of the SiteInstance if it doesn't have one yet.
2600 if (!GetSiteInstance()->has_site())
2601 GetSiteInstance()->SetSite(params.url);
2602
2603 // Need to update MIME type here because it's referred to in
2604 // UpdateNavigationCommands() called by RendererDidNavigate() to
2605 // determine whether or not to enable the encoding menu.
2606 // It's updated only for the main frame. For a subframe,
2607 // RenderView::UpdateURL does not set params.contents_mime_type.
2608 // (see http://code.google.com/p/chromium/issues/detail?id=2929 )
2609 // TODO(jungshik): Add a test for the encoding menu to avoid
2610 // regressing it again.
2611 if (PageTransition::IsMainFrame(params.transition))
2612 contents_mime_type_ = params.contents_mime_type;
2613
2614 NavigationController::LoadCommittedDetails details;
[email protected]8030f012009-09-25 18:09:372615 bool did_navigate = controller_.RendererDidNavigate(
2616 params, extra_invalidate_flags, &details);
[email protected]3e90d4a2009-07-03 17:38:392617
[email protected]a9c0bfe2010-09-17 08:35:222618 // Send notification about committed provisional loads. This notification is
2619 // different from the NAV_ENTRY_COMMITTED notification which doesn't include
2620 // the actual URL navigated to and isn't sent for AUTO_SUBFRAME navigations.
2621 if (details.type != NavigationType::NAV_IGNORE) {
[email protected]dabb0d12010-10-05 12:50:072622 // For AUTO_SUBFRAME navigations, an event for the main frame is generated
2623 // that is not recorded in the navigation history. For the purpose of
2624 // tracking navigation events, we treat this event as a sub frame navigation
2625 // event.
2626 bool is_main_frame = did_navigate ? details.is_main_frame : false;
2627 ProvisionalLoadDetails load_details(
2628 is_main_frame, details.is_in_page, params.url, std::string(), false,
[email protected]dd0aafd2010-12-02 13:13:412629 false, params.frame_id);
[email protected]a9c0bfe2010-09-17 08:35:222630 load_details.set_transition_type(params.transition);
2631 // Whether or not a page transition was triggered by going backward or
2632 // forward in the history is only stored in the navigation controller's
2633 // entry list.
2634 if (did_navigate &&
2635 (controller_.GetActiveEntry()->transition_type() &
2636 PageTransition::FORWARD_BACK)) {
2637 load_details.set_transition_type(
2638 params.transition | PageTransition::FORWARD_BACK);
2639 }
2640 NotificationService::current()->Notify(
2641 NotificationType::FRAME_PROVISIONAL_LOAD_COMMITTED,
2642 Source<NavigationController>(&controller_),
2643 Details<ProvisionalLoadDetails>(&load_details));
[email protected]a9c0bfe2010-09-17 08:35:222644 }
2645
[email protected]3e90d4a2009-07-03 17:38:392646 // Update history. Note that this needs to happen after the entry is complete,
2647 // which WillNavigate[Main,Sub]Frame will do before this function is called.
[email protected]ec0b6c42010-08-26 03:16:582648 if (params.should_update_history) {
[email protected]3e90d4a2009-07-03 17:38:392649 // Most of the time, the displayURL matches the loaded URL, but for about:
[email protected]ec0b6c42010-08-26 03:16:582650 // URLs, we use a data: URL as the real value. We actually want to save the
2651 // about: URL to the history db and keep the data: URL hidden. This is what
2652 // the TabContents' URL getter does.
2653 scoped_refptr<history::HistoryAddPageArgs> add_page_args(
2654 CreateHistoryAddPageArgs(GetURL(), details, params));
2655 if (!delegate() ||
2656 delegate()->ShouldAddNavigationToHistory(*add_page_args,
2657 details.type)) {
2658 UpdateHistoryForNavigation(add_page_args);
2659 }
[email protected]3e90d4a2009-07-03 17:38:392660 }
2661
[email protected]76543b92009-08-31 17:27:452662 if (!did_navigate)
2663 return; // No navigation happened.
[email protected]420ae012009-04-24 05:16:322664
2665 // DO NOT ADD MORE STUFF TO THIS FUNCTION! Your component should either listen
2666 // for the appropriate notification (best) or you can add it to
2667 // DidNavigateMainFramePostCommit / DidNavigateAnyFramePostCommit (only if
2668 // necessary, please).
2669
2670 // Run post-commit tasks.
2671 if (details.is_main_frame)
2672 DidNavigateMainFramePostCommit(details, params);
2673 DidNavigateAnyFramePostCommit(rvh, details, params);
2674}
2675
2676void TabContents::UpdateState(RenderViewHost* rvh,
2677 int32 page_id,
2678 const std::string& state) {
2679 DCHECK(rvh == render_view_host());
2680
2681 // We must be prepared to handle state updates for any page, these occur
2682 // when the user is scrolling and entering form data, as well as when we're
2683 // leaving a page, in which case our state may have already been moved to
2684 // the next page. The navigation controller will look up the appropriate
2685 // NavigationEntry and update it when it is notified via the delegate.
2686
2687 int entry_index = controller_.GetEntryIndexWithPageID(
2688 GetSiteInstance(), page_id);
2689 if (entry_index < 0)
2690 return;
2691 NavigationEntry* entry = controller_.GetEntryAtIndex(entry_index);
2692
2693 if (state == entry->content_state())
2694 return; // Nothing to update.
2695 entry->set_content_state(state);
2696 controller_.NotifyEntryChanged(entry, entry_index);
2697}
2698
2699void TabContents::UpdateTitle(RenderViewHost* rvh,
2700 int32 page_id, const std::wstring& title) {
2701 // If we have a title, that's a pretty good indication that we've started
2702 // getting useful data.
2703 SetNotWaitingForResponse();
2704
2705 DCHECK(rvh == render_view_host());
[email protected]371cc8b2010-09-01 16:03:482706 NavigationEntry* entry = controller_.GetEntryWithPageID(rvh->site_instance(),
[email protected]ec0b6c42010-08-26 03:16:582707 page_id);
[email protected]420ae012009-04-24 05:16:322708 if (!entry || !UpdateTitleForEntry(entry, title))
2709 return;
2710
2711 // Broadcast notifications when the UI should be updated.
2712 if (entry == controller_.GetEntryAtOffset(0))
[email protected]f1cd5e82009-10-23 17:19:032713 NotifyNavigationStateChanged(INVALIDATE_TITLE);
[email protected]420ae012009-04-24 05:16:322714}
2715
[email protected]420ae012009-04-24 05:16:322716void TabContents::UpdateEncoding(RenderViewHost* render_view_host,
[email protected]41fc0322009-09-04 22:23:402717 const std::string& encoding) {
[email protected]420ae012009-04-24 05:16:322718 set_encoding(encoding);
2719}
2720
2721void TabContents::UpdateTargetURL(int32 page_id, const GURL& url) {
2722 if (delegate())
2723 delegate()->UpdateTargetURL(this, url);
2724}
2725
2726void TabContents::UpdateThumbnail(const GURL& url,
2727 const SkBitmap& bitmap,
2728 const ThumbnailScore& score) {
[email protected]e6baed562010-11-02 22:33:442729 if (profile()->IsOffTheRecord())
2730 return;
2731
[email protected]420ae012009-04-24 05:16:322732 // Tell History about this thumbnail
[email protected]54097492f2011-01-18 08:32:072733 history::TopSites* ts = profile()->GetTopSites();
2734 if (ts)
2735 ts->SetPageThumbnail(url, bitmap, score);
[email protected]420ae012009-04-24 05:16:322736}
2737
[email protected]c4e98902010-06-01 10:20:142738void TabContents::UpdateInspectorSetting(const std::string& key,
2739 const std::string& value) {
[email protected]24d27d72010-10-12 16:30:052740 RenderViewHostDelegateHelper::UpdateInspectorSetting(profile(), key, value);
[email protected]0df30122009-06-03 12:13:082741}
2742
[email protected]45aafc92010-06-07 14:18:532743void TabContents::ClearInspectorSettings() {
[email protected]24d27d72010-10-12 16:30:052744 RenderViewHostDelegateHelper::ClearInspectorSettings(profile());
[email protected]45aafc92010-06-07 14:18:532745}
2746
[email protected]420ae012009-04-24 05:16:322747void TabContents::Close(RenderViewHost* rvh) {
[email protected]07707302009-11-06 00:50:292748 // The UI may be in an event-tracking loop, such as between the
2749 // mouse-down and mouse-up in text selection or a button click.
2750 // Defer the close until after tracking is complete, so that we
2751 // don't free objects out from under the UI.
2752 // TODO(shess): This could probably be integrated with the
2753 // IsDoingDrag() test below. Punting for now because I need more
2754 // research to understand how this impacts platforms other than Mac.
2755 // TODO(shess): This could get more fine-grained. For instance,
2756 // closing a tab in another window while selecting text in the
2757 // current window's Omnibox should be just fine.
2758 if (view()->IsEventTracking()) {
2759 view()->CloseTabAfterEventTracking();
2760 return;
2761 }
2762
[email protected]24a4d1062009-07-10 23:10:422763 // If we close the tab while we're in the middle of a drag, we'll crash.
2764 // Instead, cancel the drag and close it as soon as the drag ends.
2765 if (view()->IsDoingDrag()) {
2766 view()->CancelDragAndCloseTab();
2767 return;
2768 }
2769
[email protected]420ae012009-04-24 05:16:322770 // Ignore this if it comes from a RenderViewHost that we aren't showing.
2771 if (delegate() && rvh == render_view_host())
2772 delegate()->CloseContents(this);
2773}
2774
2775void TabContents::RequestMove(const gfx::Rect& new_bounds) {
2776 if (delegate() && delegate()->IsPopup(this))
2777 delegate()->MoveContents(this, new_bounds);
2778}
2779
[email protected]7ab1e7d62009-10-14 23:32:012780void TabContents::DidStartLoading() {
[email protected]420ae012009-04-24 05:16:322781 SetIsLoading(true, NULL);
[email protected]3c9e1872010-11-18 16:17:492782
[email protected]de4761b2010-12-07 21:05:212783 if (delegate()) {
2784 bool is_print_preview_tab =
2785 printing::PrintPreviewTabController::IsPrintPreviewTab(this);
2786 if (content_restrictions_ || is_print_preview_tab) {
2787 content_restrictions_= is_print_preview_tab ?
2788 CONTENT_RESTRICTION_PRINT : 0;
2789 delegate()->ContentRestrictionsChanged(this);
2790 }
[email protected]9e823662010-10-13 23:36:002791 }
[email protected]3c9e1872010-11-18 16:17:492792
2793 // Notify observers about navigation.
2794 FOR_EACH_OBSERVER(WebNavigationObserver, web_navigation_observers_,
2795 DidStartLoading());
[email protected]420ae012009-04-24 05:16:322796}
2797
[email protected]7ab1e7d62009-10-14 23:32:012798void TabContents::DidStopLoading() {
[email protected]420ae012009-04-24 05:16:322799 scoped_ptr<LoadNotificationDetails> details;
2800
2801 NavigationEntry* entry = controller_.GetActiveEntry();
2802 // An entry may not exist for a stop when loading an initial blank page or
2803 // if an iframe injected by script into a blank page finishes loading.
2804 if (entry) {
[email protected]420ae012009-04-24 05:16:322805 base::TimeDelta elapsed = base::TimeTicks::Now() - current_load_start_;
2806
2807 details.reset(new LoadNotificationDetails(
[email protected]ebe89e062009-08-13 23:16:542808 entry->virtual_url(),
[email protected]420ae012009-04-24 05:16:322809 entry->transition_type(),
2810 elapsed,
2811 &controller_,
2812 controller_.GetCurrentEntryIndex()));
2813 }
2814
[email protected]420ae012009-04-24 05:16:322815 SetIsLoading(false, details.get());
[email protected]3c9e1872010-11-18 16:17:492816
2817 // Notify observers about navigation.
2818 FOR_EACH_OBSERVER(WebNavigationObserver, web_navigation_observers_,
2819 DidStopLoading());
[email protected]420ae012009-04-24 05:16:322820}
2821
[email protected]1a3c3cb2010-12-16 21:03:402822void TabContents::DidChangeLoadProgress(double progress) {
2823 if (delegate())
2824 delegate()->LoadProgressChanged(progress);
2825}
2826
[email protected]25497492010-09-11 15:15:082827void TabContents::DocumentOnLoadCompletedInMainFrame(
2828 RenderViewHost* render_view_host,
2829 int32 page_id) {
2830 NotificationService::current()->Notify(
2831 NotificationType::LOAD_COMPLETED_MAIN_FRAME,
2832 Source<TabContents>(this),
2833 Details<int>(&page_id));
2834}
2835
[email protected]420ae012009-04-24 05:16:322836void TabContents::RequestOpenURL(const GURL& url, const GURL& referrer,
2837 WindowOpenDisposition disposition) {
2838 if (render_manager_.dom_ui()) {
2839 // When we're a DOM UI, it will provide a page transition type for us (this
2840 // is so the new tab page can specify AUTO_BOOKMARK for automatically
2841 // generated suggestions).
2842 //
2843 // Note also that we hide the referrer for DOM UI pages. We don't really
[email protected]60e448982009-05-06 04:21:162844 // want web sites to see a referrer of "chrome://blah" (and some
2845 // chrome: URLs might have search terms or other stuff we don't want to
[email protected]420ae012009-04-24 05:16:322846 // send to the site), so we send no referrer.
2847 OpenURL(url, GURL(), disposition,
2848 render_manager_.dom_ui()->link_transition_type());
2849 } else {
2850 OpenURL(url, referrer, disposition, PageTransition::LINK);
2851 }
2852}
2853
[email protected]1ee614862009-07-06 20:29:182854void TabContents::DomOperationResponse(const std::string& json_string,
2855 int automation_id) {
2856}
2857
[email protected]8b8e7c92010-08-19 18:05:562858void TabContents::ProcessDOMUIMessage(
2859 const ViewHostMsg_DomMessage_Params& params) {
[email protected]420ae012009-04-24 05:16:322860 if (!render_manager_.dom_ui()) {
[email protected]c2e74fe82009-09-03 17:57:442861 // This can happen if someone uses window.open() to open an extension URL
2862 // from a non-extension context.
[email protected]8b8e7c92010-08-19 18:05:562863 render_view_host()->BlockExtensionRequest(params.request_id);
[email protected]420ae012009-04-24 05:16:322864 return;
2865 }
[email protected]8b8e7c92010-08-19 18:05:562866 render_manager_.dom_ui()->ProcessDOMUIMessage(params);
[email protected]420ae012009-04-24 05:16:322867}
2868
2869void TabContents::ProcessExternalHostMessage(const std::string& message,
2870 const std::string& origin,
2871 const std::string& target) {
2872 if (delegate())
2873 delegate()->ForwardMessageToExternalHost(message, origin, target);
2874}
2875
[email protected]420ae012009-04-24 05:16:322876void TabContents::RunJavaScriptMessage(
2877 const std::wstring& message,
2878 const std::wstring& default_prompt,
2879 const GURL& frame_url,
2880 const int flags,
2881 IPC::Message* reply_msg,
2882 bool* did_suppress_message) {
2883 // Suppress javascript messages when requested and when inside a constrained
2884 // popup window (because that activates them and breaks them out of the
2885 // constrained window jail).
[email protected]91ed49a2009-07-07 21:29:552886 // Also suppress messages when showing an interstitial. The interstitial is
2887 // shown over the previous page, we don't want the hidden page dialogs to
2888 // interfere with the interstitial.
[email protected]8f55e802010-12-06 18:11:502889 bool suppress_this_message =
2890 suppress_javascript_messages_ ||
2891 showing_interstitial_page() ||
2892 (delegate() && delegate()->ShouldSuppressDialogs());
[email protected]420ae012009-04-24 05:16:322893 if (delegate())
2894 suppress_this_message |=
[email protected]b6c874582009-05-08 19:38:312895 (delegate()->GetConstrainingContents(this) != this);
[email protected]420ae012009-04-24 05:16:322896
2897 *did_suppress_message = suppress_this_message;
2898
2899 if (!suppress_this_message) {
2900 base::TimeDelta time_since_last_message(
2901 base::TimeTicks::Now() - last_javascript_message_dismissal_);
2902 bool show_suppress_checkbox = false;
2903 // Show a checkbox offering to suppress further messages if this message is
2904 // being displayed within kJavascriptMessageExpectedDelay of the last one.
2905 if (time_since_last_message <
2906 base::TimeDelta::FromMilliseconds(kJavascriptMessageExpectedDelay))
2907 show_suppress_checkbox = true;
2908
[email protected]d6ad7442010-09-05 19:06:522909 RunJavascriptMessageBox(profile(), this, frame_url, flags, message,
2910 default_prompt, show_suppress_checkbox, reply_msg);
[email protected]420ae012009-04-24 05:16:322911 } else {
2912 // If we are suppressing messages, just reply as is if the user immediately
2913 // pressed "Cancel".
[email protected]beb440c2009-11-06 04:08:542914 OnMessageBoxClosed(reply_msg, false, std::wstring());
[email protected]420ae012009-04-24 05:16:322915 }
2916}
2917
2918void TabContents::RunBeforeUnloadConfirm(const std::wstring& message,
2919 IPC::Message* reply_msg) {
[email protected]5aab5e22010-12-08 22:13:292920 if (delegate())
2921 delegate()->WillRunBeforeUnloadConfirm();
[email protected]8f55e802010-12-06 18:11:502922 if (delegate() && delegate()->ShouldSuppressDialogs()) {
2923 render_view_host()->JavaScriptMessageBoxClosed(reply_msg, true,
2924 std::wstring());
2925 return;
2926 }
[email protected]7ab1e7d62009-10-14 23:32:012927 is_showing_before_unload_dialog_ = true;
[email protected]d82ed61e2009-06-16 02:46:222928 RunBeforeUnloadDialog(this, message, reply_msg);
[email protected]420ae012009-04-24 05:16:322929}
2930
2931void TabContents::ShowModalHTMLDialog(const GURL& url, int width, int height,
2932 const std::string& json_arguments,
2933 IPC::Message* reply_msg) {
2934 if (delegate()) {
2935 HtmlDialogUIDelegate* dialog_delegate =
2936 new ModalHtmlDialogDelegate(url, width, height, json_arguments,
[email protected]d82ed61e2009-06-16 02:46:222937 reply_msg, this);
[email protected]420ae012009-04-24 05:16:322938 delegate()->ShowHtmlDialog(dialog_delegate, NULL);
2939 }
2940}
2941
[email protected]420ae012009-04-24 05:16:322942// Checks to see if we should generate a keyword based on the OSDD, and if
2943// necessary uses TemplateURLFetcher to download the OSDD and create a keyword.
[email protected]2de7e002010-10-11 22:58:202944void TabContents::PageHasOSDD(
2945 RenderViewHost* render_view_host,
2946 int32 page_id,
2947 const GURL& url,
2948 const ViewHostMsg_PageHasOSDD_Type& msg_provider_type) {
[email protected]0b5b85c2010-10-11 23:00:122949 // Make sure page_id is the current page and other basic checks.
[email protected]420ae012009-04-24 05:16:322950 DCHECK(url.is_valid());
2951 if (!IsActiveEntry(page_id))
2952 return;
[email protected]420ae012009-04-24 05:16:322953 if (!profile()->GetTemplateURLFetcher())
2954 return;
[email protected]420ae012009-04-24 05:16:322955 if (profile()->IsOffTheRecord())
2956 return;
2957
[email protected]2de7e002010-10-11 22:58:202958 TemplateURLFetcher::ProviderType provider_type;
2959 switch (msg_provider_type.type) {
2960 case ViewHostMsg_PageHasOSDD_Type::AUTODETECTED_PROVIDER:
2961 provider_type = TemplateURLFetcher::AUTODETECTED_PROVIDER;
2962 break;
2963
2964 case ViewHostMsg_PageHasOSDD_Type::EXPLICIT_DEFAULT_PROVIDER:
2965 provider_type = TemplateURLFetcher::EXPLICIT_DEFAULT_PROVIDER;
2966 break;
2967
2968 case ViewHostMsg_PageHasOSDD_Type::EXPLICIT_PROVIDER:
2969 provider_type = TemplateURLFetcher::EXPLICIT_PROVIDER;
2970 break;
2971
2972 default:
2973 NOTREACHED();
2974 return;
2975 }
2976
[email protected]420ae012009-04-24 05:16:322977 const NavigationEntry* entry = controller_.GetLastCommittedEntry();
2978 DCHECK(entry);
2979
2980 const NavigationEntry* base_entry = entry;
2981 if (IsFormSubmit(base_entry)) {
2982 // If the current page is a form submit, find the last page that was not
2983 // a form submit and use its url to generate the keyword from.
2984 int index = controller_.last_committed_entry_index() - 1;
2985 while (index >= 0 && IsFormSubmit(controller_.GetEntryAtIndex(index)))
2986 index--;
2987 if (index >= 0)
2988 base_entry = controller_.GetEntryAtIndex(index);
2989 else
2990 base_entry = NULL;
2991 }
2992
2993 // We want to use the user typed URL if available since that represents what
2994 // the user typed to get here, and fall back on the regular URL if not.
2995 if (!base_entry)
2996 return;
2997 GURL keyword_url = base_entry->user_typed_url().is_valid() ?
2998 base_entry->user_typed_url() : base_entry->url();
2999 if (!keyword_url.is_valid())
3000 return;
[email protected]2de7e002010-10-11 22:58:203001
[email protected]400b133f2011-01-19 18:32:303002 string16 keyword = TemplateURLModel::GenerateKeyword(
[email protected]0b5b85c2010-10-11 23:00:123003 keyword_url,
3004 provider_type == TemplateURLFetcher::AUTODETECTED_PROVIDER);
[email protected]a88dbdd52010-01-15 19:25:533005
[email protected]0b5b85c2010-10-11 23:00:123006 // Download the OpenSearch description document. If this is successful, a
[email protected]420ae012009-04-24 05:16:323007 // new keyword will be created when done.
[email protected]420ae012009-04-24 05:16:323008 profile()->GetTemplateURLFetcher()->ScheduleDownload(
3009 keyword,
3010 url,
3011 base_entry->favicon().url(),
[email protected]0b5b85c2010-10-11 23:00:123012 new TemplateURLFetcherUICallbacks(this),
[email protected]2de7e002010-10-11 22:58:203013 provider_type);
[email protected]420ae012009-04-24 05:16:323014}
3015
[email protected]420ae012009-04-24 05:16:323016GURL TabContents::GetAlternateErrorPageURL() const {
3017 GURL url;
3018 // Disable alternate error pages when in OffTheRecord/Incognito mode.
3019 if (profile()->IsOffTheRecord())
3020 return url;
3021
3022 PrefService* prefs = profile()->GetPrefs();
3023 DCHECK(prefs);
3024 if (prefs->GetBoolean(prefs::kAlternateErrorPagesEnabled)) {
[email protected]a33457b82009-09-21 23:07:153025 url = google_util::AppendGoogleLocaleParam(
3026 GURL(google_util::kLinkDoctorBaseURL));
[email protected]420ae012009-04-24 05:16:323027 url = google_util::AppendGoogleTLDParam(url);
3028 }
3029 return url;
3030}
3031
3032WebPreferences TabContents::GetWebkitPrefs() {
[email protected]6c32ce72010-03-08 05:18:063033 Profile* profile = render_view_host()->process()->profile();
[email protected]7ccddb8c2009-08-04 17:36:553034 bool is_dom_ui = false;
[email protected]447021c2010-09-08 21:29:083035 WebPreferences web_prefs =
3036 RenderViewHostDelegateHelper::GetWebkitPrefs(profile, is_dom_ui);
3037
[email protected]9beff752010-09-22 19:35:433038 // Force accelerated compositing and 2d canvas off for chrome: and
3039 // chrome-extension: pages.
[email protected]27c790d42010-10-22 09:28:433040 if (GetURL().SchemeIs(chrome::kChromeDevToolsScheme) ||
3041 GetURL().SchemeIs(chrome::kChromeUIScheme)) {
[email protected]447021c2010-09-08 21:29:083042 web_prefs.accelerated_compositing_enabled = false;
[email protected]9beff752010-09-22 19:35:433043 web_prefs.accelerated_2d_canvas_enabled = false;
[email protected]447021c2010-09-08 21:29:083044 }
3045
[email protected]39553442010-09-09 00:01:553046#if defined(OS_MACOSX)
3047 // Disable accelerated compositing if IOSurface's are not supported,
3048 // as is the case in 10.5.
3049 if (!IOSurfaceSupport::Initialize())
3050 web_prefs.accelerated_compositing_enabled = false;
3051#endif
3052
[email protected]447021c2010-09-08 21:29:083053 return web_prefs;
[email protected]420ae012009-04-24 05:16:323054}
3055
[email protected]fa1cf0b82010-01-15 21:49:443056void TabContents::OnIgnoredUIEvent() {
3057 if (constrained_window_count()) {
3058 ConstrainedWindow* window = *constrained_window_begin();
3059 window->FocusConstrainedWindow();
3060 }
3061}
3062
[email protected]420ae012009-04-24 05:16:323063void TabContents::OnJSOutOfMemory() {
[email protected]f4f50ef2011-01-21 19:01:193064 AddInfoBar(new SimpleAlertInfoBarDelegate(this, NULL,
3065 l10n_util::GetStringUTF16(IDS_JS_OUT_OF_MEMORY_PROMPT), true));
[email protected]420ae012009-04-24 05:16:323066}
3067
[email protected]420ae012009-04-24 05:16:323068void TabContents::OnCrossSiteResponse(int new_render_process_host_id,
3069 int new_request_id) {
[email protected]57c6a652009-05-04 07:58:343070 // Allows the TabContents to react when a cross-site response is ready to be
[email protected]420ae012009-04-24 05:16:323071 // delivered to a pending RenderViewHost. We must first run the onunload
3072 // handler of the old RenderViewHost before we can allow it to proceed.
3073 render_manager_.OnCrossSiteResponse(new_render_process_host_id,
3074 new_request_id);
3075}
3076
[email protected]420ae012009-04-24 05:16:323077void TabContents::RendererUnresponsive(RenderViewHost* rvh,
3078 bool is_during_unload) {
3079 if (is_during_unload) {
3080 // Hang occurred while firing the beforeunload/unload handler.
3081 // Pretend the handler fired so tab closing continues as if it had.
[email protected]829e7612009-04-25 01:15:113082 rvh->set_sudden_termination_allowed(true);
[email protected]420ae012009-04-24 05:16:323083
3084 if (!render_manager_.ShouldCloseTabOnUnresponsiveRenderer())
3085 return;
3086
3087 // If the tab hangs in the beforeunload/unload handler there's really
3088 // nothing we can do to recover. Pretend the unload listeners have
3089 // all fired and close the tab. If the hang is in the beforeunload handler
3090 // then the user will not have the option of cancelling the close.
3091 Close(rvh);
3092 return;
3093 }
3094
3095 if (render_view_host() && render_view_host()->IsRenderViewLive())
[email protected]e7b8d832009-11-20 22:41:003096 hung_renderer_dialog::ShowForTabContents(this);
[email protected]420ae012009-04-24 05:16:323097}
3098
3099void TabContents::RendererResponsive(RenderViewHost* render_view_host) {
[email protected]e7b8d832009-11-20 22:41:003100 hung_renderer_dialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:323101}
3102
3103void TabContents::LoadStateChanged(const GURL& url,
[email protected]094e5b22009-09-25 04:23:563104 net::LoadState load_state,
3105 uint64 upload_position,
3106 uint64 upload_size) {
[email protected]420ae012009-04-24 05:16:323107 load_state_ = load_state;
[email protected]094e5b22009-09-25 04:23:563108 upload_position_ = upload_position;
3109 upload_size_ = upload_size;
[email protected]d686e812009-06-03 19:10:293110 std::wstring languages =
[email protected]ddd231e2010-06-29 20:35:193111 UTF8ToWide(profile()->GetPrefs()->GetString(prefs::kAcceptLanguages));
[email protected]d686e812009-06-03 19:10:293112 std::string host = url.host();
[email protected]42197a22010-12-28 23:29:423113 load_state_host_ = WideToUTF16Hack(
3114 net::IDNToUnicode(host.c_str(), host.size(), languages, NULL));
[email protected]420ae012009-04-24 05:16:323115 if (load_state_ == net::LOAD_STATE_READING_RESPONSE)
3116 SetNotWaitingForResponse();
3117 if (is_loading())
[email protected]c9cd2222009-05-06 05:16:503118 NotifyNavigationStateChanged(INVALIDATE_LOAD | INVALIDATE_TAB);
[email protected]420ae012009-04-24 05:16:323119}
3120
[email protected]1ee614862009-07-06 20:29:183121bool TabContents::IsExternalTabContainer() const {
3122 if (!delegate())
3123 return false;
3124
3125 return delegate()->IsExternalTabContainer();
3126}
3127
[email protected]1ee614862009-07-06 20:29:183128void TabContents::DidInsertCSS() {
3129 // This RVHDelegate function is used for extensions and not us.
[email protected]420ae012009-04-24 05:16:323130}
3131
[email protected]546ae4e02010-12-08 14:57:193132void TabContents::FocusedNodeChanged(bool is_editable_node) {
[email protected]130efb02009-09-18 18:54:353133 NotificationService::current()->Notify(
3134 NotificationType::FOCUS_CHANGED_IN_PAGE,
3135 Source<RenderViewHost>(render_view_host()),
[email protected]546ae4e02010-12-08 14:57:193136 Details<const bool>(&is_editable_node));
[email protected]130efb02009-09-18 18:54:353137}
3138
[email protected]b75b8292010-10-01 07:28:253139void TabContents::UpdateZoomLimits(int minimum_percent,
3140 int maximum_percent,
3141 bool remember) {
3142 minimum_zoom_percent_ = minimum_percent;
3143 maximum_zoom_percent_ = maximum_percent;
3144 temporary_zoom_settings_ = !remember;
[email protected]ebf40a72010-07-22 01:46:383145}
3146
[email protected]420ae012009-04-24 05:16:323147void TabContents::BeforeUnloadFiredFromRenderManager(
3148 bool proceed,
3149 bool* proceed_to_fire_unload) {
3150 if (delegate())
3151 delegate()->BeforeUnloadFired(this, proceed, proceed_to_fire_unload);
3152}
3153
[email protected]3a3d47472010-07-15 21:03:543154void TabContents::DidStartLoadingFromRenderManager(
3155 RenderViewHost* render_view_host) {
3156 DidStartLoading();
3157}
3158
3159void TabContents::RenderViewGoneFromRenderManager(
3160 RenderViewHost* render_view_host) {
[email protected]443b80e2010-12-14 00:42:233161 DCHECK(crashed_status_ != base::TERMINATION_STATUS_STILL_RUNNING);
3162 RenderViewGone(render_view_host, crashed_status_, crashed_error_code_);
[email protected]3a3d47472010-07-15 21:03:543163}
3164
[email protected]420ae012009-04-24 05:16:323165void TabContents::UpdateRenderViewSizeForRenderManager() {
3166 // TODO(brettw) this is a hack. See TabContentsView::SizeContents.
[email protected]0323ee42010-02-17 22:03:263167 gfx::Size size = view_->GetContainerSize();
3168 // 0x0 isn't a valid window size (minimal window size is 1x1) but it may be
3169 // here during container initialization and normal window size will be set
3170 // later. In case of tab duplication this resizing to 0x0 prevents setting
3171 // normal size later so just ignore it.
3172 if (!size.IsEmpty())
3173 view_->SizeContents(size);
[email protected]420ae012009-04-24 05:16:323174}
3175
[email protected]3a3d47472010-07-15 21:03:543176void TabContents::NotifySwappedFromRenderManager() {
3177 NotifySwapped();
3178}
3179
3180NavigationController& TabContents::GetControllerForRenderManager() {
3181 return controller();
3182}
3183
[email protected]420ae012009-04-24 05:16:323184DOMUI* TabContents::CreateDOMUIForRenderManager(const GURL& url) {
[email protected]d82ed61e2009-06-16 02:46:223185 return DOMUIFactory::CreateDOMUIForURL(this, url);
[email protected]420ae012009-04-24 05:16:323186}
3187
3188NavigationEntry*
3189TabContents::GetLastCommittedNavigationEntryForRenderManager() {
3190 return controller_.GetLastCommittedEntry();
3191}
3192
3193bool TabContents::CreateRenderViewForRenderManager(
3194 RenderViewHost* render_view_host) {
[email protected]420ae012009-04-24 05:16:323195 RenderWidgetHostView* rwh_view = view_->CreateViewForWidget(render_view_host);
[email protected]1a98a932009-11-17 00:12:523196
[email protected]674b3822010-08-04 04:02:513197 if (!render_view_host->CreateRenderView(string16()))
[email protected]420ae012009-04-24 05:16:323198 return false;
3199
3200 // Now that the RenderView has been created, we need to tell it its size.
3201 rwh_view->SetSize(view_->GetContainerSize());
3202
3203 UpdateMaxPageIDIfNecessary(render_view_host->site_instance(),
3204 render_view_host);
3205 return true;
3206}
3207
3208void TabContents::Observe(NotificationType type,
3209 const NotificationSource& source,
3210 const NotificationDetails& details) {
3211 switch (type.value) {
3212 case NotificationType::BOOKMARK_MODEL_LOADED:
3213 // BookmarkModel finished loading, fall through to update starred state.
3214 case NotificationType::URLS_STARRED: {
3215 // Somewhere, a URL has been starred.
3216 // Ignore notifications for profiles other than our current one.
3217 Profile* source_profile = Source<Profile>(source).ptr();
3218 if (!source_profile || !source_profile->IsSameProfile(profile()))
3219 return;
3220
3221 UpdateStarredStateForCurrentURL();
3222 break;
3223 }
3224 case NotificationType::PREF_CHANGED: {
[email protected]57ecc4b2010-08-11 03:02:513225 std::string* pref_name_in = Details<std::string>(details).ptr();
[email protected]420ae012009-04-24 05:16:323226 DCHECK(Source<PrefService>(source).ptr() == profile()->GetPrefs());
3227 if (*pref_name_in == prefs::kAlternateErrorPagesEnabled) {
3228 UpdateAlternateErrorPageURL();
[email protected]88e14382010-10-14 21:45:533229 } else if ((*pref_name_in == prefs::kDefaultCharset) ||
3230 StartsWithASCII(*pref_name_in, "webkit.webprefs.", true)) {
[email protected]420ae012009-04-24 05:16:323231 UpdateWebPreferences();
[email protected]d0b8d092010-10-25 04:05:173232 } else if (*pref_name_in == prefs::kDefaultZoomLevel) {
3233 UpdateZoomLevel();
[email protected]420ae012009-04-24 05:16:323234 } else {
3235 NOTREACHED() << "unexpected pref change notification" << *pref_name_in;
3236 }
3237 break;
3238 }
3239 case NotificationType::RENDER_WIDGET_HOST_DESTROYED:
3240 view_->RenderWidgetHostDestroyed(Source<RenderWidgetHost>(source).ptr());
3241 break;
3242
3243 case NotificationType::NAV_ENTRY_COMMITTED: {
3244 DCHECK(&controller_ == Source<NavigationController>(source).ptr());
3245
3246 NavigationController::LoadCommittedDetails& committed_details =
3247 *(Details<NavigationController::LoadCommittedDetails>(details).ptr());
3248 ExpireInfoBars(committed_details);
3249 break;
3250 }
3251
[email protected]d3ba77272009-09-03 00:06:093252#if defined(OS_LINUX)
3253 case NotificationType::BROWSER_THEME_CHANGED: {
[email protected]7ff431e2010-01-07 18:18:543254 renderer_preferences_util::UpdateFromSystemSettings(
3255 &renderer_preferences_, profile());
[email protected]d3ba77272009-09-03 00:06:093256 render_view_host()->SyncRendererPrefs();
3257 break;
3258 }
3259#endif
3260
[email protected]6c32ce72010-03-08 05:18:063261 case NotificationType::USER_STYLE_SHEET_UPDATED:
3262 UpdateWebPreferences();
3263 break;
3264
[email protected]79580c62010-02-02 02:36:253265 case NotificationType::CONTENT_SETTINGS_CHANGED: {
[email protected]5f24f212010-12-02 14:47:453266 Details<const ContentSettingsDetails> settings_details(details);
[email protected]79580c62010-02-02 02:36:253267 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]da0c8e92010-02-09 23:25:493268 GURL entry_url;
[email protected]0314ae02010-04-08 09:18:293269 if (entry)
[email protected]da0c8e92010-02-09 23:25:493270 entry_url = entry->url();
[email protected]0314ae02010-04-08 09:18:293271 if (settings_details.ptr()->update_all() ||
3272 settings_details.ptr()->pattern().Matches(entry_url)) {
3273 render_view_host()->SendContentSettings(entry_url,
[email protected]b9a4ea7f2010-06-25 07:11:233274 profile()->GetHostContentSettingsMap()->
3275 GetContentSettings(entry_url));
[email protected]79580c62010-02-02 02:36:253276 }
3277 break;
3278 }
3279
[email protected]e5adff72010-06-14 18:13:063280 case NotificationType::EXTENSION_LOADED:
[email protected]a888b29e62010-04-01 13:38:573281 break;
[email protected]a888b29e62010-04-01 13:38:573282
3283 case NotificationType::EXTENSION_UNLOADED:
[email protected]a888b29e62010-04-01 13:38:573284 break;
[email protected]a888b29e62010-04-01 13:38:573285
[email protected]88152052010-10-01 04:05:183286 case NotificationType::GOOGLE_URL_UPDATED:
3287 UpdateAlternateErrorPageURL();
3288 break;
3289
[email protected]420ae012009-04-24 05:16:323290 default:
3291 NOTREACHED();
3292 }
3293}
[email protected]aacd7cc2009-09-02 14:51:103294
[email protected]9adb9692010-10-29 23:14:023295void TabContents::UpdateExtensionAppIcon(const Extension* extension) {
[email protected]98aa0b52010-05-06 17:03:083296 extension_app_icon_.reset();
[email protected]f075fe0a22010-04-26 04:06:433297
[email protected]a888b29e62010-04-01 13:38:573298 if (extension) {
[email protected]98aa0b52010-05-06 17:03:083299 extension_app_image_loader_.reset(new ImageLoadingTracker(this));
3300 extension_app_image_loader_->LoadImage(
[email protected]a888b29e62010-04-01 13:38:573301 extension,
[email protected]807871f2010-09-16 01:04:483302 extension->GetIconResource(Extension::EXTENSION_ICON_SMALLISH,
3303 ExtensionIconSet::MATCH_EXACTLY),
[email protected]a888b29e62010-04-01 13:38:573304 gfx::Size(Extension::EXTENSION_ICON_SMALLISH,
3305 Extension::EXTENSION_ICON_SMALLISH),
3306 ImageLoadingTracker::CACHE);
3307 } else {
[email protected]98aa0b52010-05-06 17:03:083308 extension_app_image_loader_.reset(NULL);
[email protected]a888b29e62010-04-01 13:38:573309 }
3310}
3311
[email protected]9adb9692010-10-29 23:14:023312const Extension* TabContents::GetExtensionContaining(const GURL& url) {
[email protected]eaa7dd182010-12-14 11:09:003313 ExtensionService* extensions_service = profile()->GetExtensionService();
[email protected]a888b29e62010-04-01 13:38:573314 if (!extensions_service)
3315 return NULL;
3316
[email protected]9adb9692010-10-29 23:14:023317 const Extension* extension = extensions_service->GetExtensionByURL(url);
[email protected]a888b29e62010-04-01 13:38:573318 return extension ?
3319 extension : extensions_service->GetExtensionByWebExtent(url);
3320}
3321
[email protected]a888b29e62010-04-01 13:38:573322void TabContents::OnImageLoaded(SkBitmap* image, ExtensionResource resource,
3323 int index) {
3324 if (image) {
[email protected]98aa0b52010-05-06 17:03:083325 extension_app_icon_ = *image;
[email protected]a888b29e62010-04-01 13:38:573326 NotifyNavigationStateChanged(INVALIDATE_TAB);
3327 }
3328}
3329
[email protected]beb440c2009-11-06 04:08:543330gfx::NativeWindow TabContents::GetMessageBoxRootWindow() {
3331 return view_->GetTopLevelNativeWindow();
3332}
3333
3334void TabContents::OnMessageBoxClosed(IPC::Message* reply_msg,
3335 bool success,
3336 const std::wstring& prompt) {
3337 last_javascript_message_dismissal_ = base::TimeTicks::Now();
3338 if (is_showing_before_unload_dialog_ && !success) {
3339 // If a beforeunload dialog is canceled, we need to stop the throbber from
3340 // spinning, since we forced it to start spinning in Navigate.
3341 DidStopLoading();
3342
3343 tab_close_start_time_ = base::TimeTicks();
3344 }
3345 is_showing_before_unload_dialog_ = false;
3346 render_view_host()->JavaScriptMessageBoxClosed(reply_msg, success, prompt);
3347}
3348
3349void TabContents::SetSuppressMessageBoxes(bool suppress_message_boxes) {
3350 set_suppress_javascript_messages(suppress_message_boxes);
3351}
3352
[email protected]3a3d47472010-07-15 21:03:543353TabContents* TabContents::AsTabContents() {
3354 return this;
3355}
3356
3357ExtensionHost* TabContents::AsExtensionHost() {
3358 return NULL;
3359}
3360
[email protected]41fc0322009-09-04 22:23:403361void TabContents::set_encoding(const std::string& encoding) {
[email protected]aacd7cc2009-09-02 14:51:103362 encoding_ = CharacterEncoding::GetCanonicalEncodingNameByAliasName(encoding);
3363}
[email protected]f45d2a72010-03-08 23:28:353364
3365void TabContents::SetAppIcon(const SkBitmap& app_icon) {
3366 app_icon_ = app_icon;
3367 NotifyNavigationStateChanged(INVALIDATE_TITLE);
3368}
[email protected]33f74972010-12-08 16:40:363369
3370void TabContents::SwapInRenderViewHost(RenderViewHost* rvh) {
3371 render_manager_.SwapInRenderViewHost(rvh);
3372}
3373
3374void TabContents::CreateViewAndSetSizeForRVH(RenderViewHost* rvh) {
3375 RenderWidgetHostView* rwh_view = view()->CreateViewForWidget(rvh);
3376 rwh_view->SetSize(view()->GetContainerSize());
3377}
[email protected]e7d50892011-01-19 21:47:383378
3379bool TabContents::MaybeUsePreloadedPage(const GURL& url) {
3380 PrerenderManager* pm = profile()->GetPrerenderManager();
3381 if (pm != NULL) {
3382 if (pm->MaybeUsePreloadedPage(this, url)) {
3383 // TODO(tburkard): If the preloaded page has not finished preloading
3384 // yet, we should not do this.
3385 DidStopLoading();
3386 return true;
3387 }
3388 }
3389 return false;
3390}