blob: 0de178493669b3255d5984fa4fac9190a44cbba4 [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]a92b8642009-05-05 23:38:569#include "app/l10n_util.h"
[email protected]9929da92009-05-05 02:05:1110#include "app/resource_bundle.h"
[email protected]8cc8d492010-02-02 10:40:4911#include "base/auto_reset.h"
[email protected]835d7c82010-10-14 04:38:3812#include "base/metrics/histogram.h"
[email protected]724159a2010-12-30 01:11:1813#include "base/metrics/stats_counters.h"
[email protected]96d185d2009-04-24 03:28:5414#include "base/string16.h"
[email protected]996fd702009-09-04 19:12:3715#include "base/string_util.h"
[email protected]96d185d2009-04-24 03:28:5416#include "base/time.h"
[email protected]3c733bde2010-12-21 19:56:3117#include "base/utf_string_conversions.h"
[email protected]6db0def92010-03-02 19:55:4518#include "chrome/browser/autocomplete_history_manager.h"
[email protected]21674df2009-10-28 19:51:2119#include "chrome/browser/autofill/autofill_manager.h"
[email protected]c65e2f152010-10-14 15:30:4020#include "chrome/browser/blocked_content_container.h"
[email protected]420ae012009-04-24 05:16:3221#include "chrome/browser/bookmarks/bookmark_model.h"
[email protected]96d185d2009-04-24 03:28:5422#include "chrome/browser/browser_process.h"
[email protected]cf68b092010-01-15 20:05:5523#include "chrome/browser/browser_shutdown.h"
[email protected]aacd7cc2009-09-02 14:51:1024#include "chrome/browser/character_encoding.h"
[email protected]724159a2010-12-30 01:11:1825#include "chrome/browser/child_process_security_policy.h"
[email protected]5f24f212010-12-02 14:47:4526#include "chrome/browser/content_settings/content_settings_details.h"
[email protected]9d01a6a2010-11-30 12:03:3327#include "chrome/browser/content_settings/host_content_settings_map.h"
[email protected]96d185d2009-04-24 03:28:5428#include "chrome/browser/debugger/devtools_manager.h"
[email protected]c8f74942010-09-03 10:04:1429#include "chrome/browser/defaults.h"
[email protected]420ae012009-04-24 05:16:3230#include "chrome/browser/dom_operation_notification_details.h"
[email protected]96d185d2009-04-24 03:28:5431#include "chrome/browser/dom_ui/dom_ui.h"
[email protected]bcef0dc02009-02-28 00:35:0232#include "chrome/browser/download/download_item_model.h"
[email protected]420ae012009-04-24 05:16:3233#include "chrome/browser/download/download_manager.h"
[email protected]41291322010-07-15 17:09:0134#include "chrome/browser/download/download_request_limiter.h"
[email protected]eaa7dd182010-12-14 11:09:0035#include "chrome/browser/extensions/extension_service.h"
[email protected]3c733bde2010-12-21 19:56:3136#include "chrome/browser/external_protocol_handler.h"
37#include "chrome/browser/favicon_service.h"
38#include "chrome/browser/file_select_helper.h"
39#include "chrome/browser/google/google_util.h"
[email protected]ef0d04c2010-12-15 23:34:2140#include "chrome/browser/history/history.h"
[email protected]ec0b6c42010-08-26 03:16:5841#include "chrome/browser/history/history_types.h"
[email protected]523623c2010-05-19 16:03:4242#include "chrome/browser/history/top_sites.h"
[email protected]b75b8292010-10-01 07:28:2543#include "chrome/browser/host_zoom_map.h"
[email protected]8897c382009-05-06 17:53:2644#include "chrome/browser/hung_renderer_dialog.h"
[email protected]6ee12c42010-09-14 09:36:0745#include "chrome/browser/in_process_webkit/session_storage_namespace.h"
[email protected]420ae012009-04-24 05:16:3246#include "chrome/browser/load_from_memory_cache_details.h"
47#include "chrome/browser/load_notification_details.h"
[email protected]84abba62009-10-07 17:01:4448#include "chrome/browser/metrics/metric_event_duration_details.h"
[email protected]724159a2010-12-30 01:11:1849#include "chrome/browser/metrics/user_metrics.h"
[email protected]ea0e80892009-08-22 00:36:0550#include "chrome/browser/modal_html_dialog_delegate.h"
[email protected]c8865482009-07-23 20:40:1051#include "chrome/browser/omnibox_search_hint.h"
[email protected]14a000d2010-04-29 21:44:2452#include "chrome/browser/platform_util.h"
[email protected]fdd61c62009-04-22 19:22:5753#include "chrome/browser/plugin_installer.h"
[email protected]37858e52010-08-26 00:22:0254#include "chrome/browser/prefs/pref_service.h"
[email protected]33f74972010-12-08 16:40:3655#include "chrome/browser/prerender/prerender_manager.h"
[email protected]dbeebd52010-11-16 20:34:1656#include "chrome/browser/printing/print_preview_tab_controller.h"
[email protected]12802702010-07-09 19:43:0957#include "chrome/browser/printing/print_view_manager.h"
[email protected]8ecad5e2010-12-02 21:18:3358#include "chrome/browser/profiles/profile.h"
[email protected]1db6ff152009-10-12 15:32:0759#include "chrome/browser/renderer_host/render_process_host.h"
[email protected]8cb5d5b2010-02-09 11:36:1660#include "chrome/browser/renderer_host/render_view_host.h"
[email protected]96d185d2009-04-24 03:28:5461#include "chrome/browser/renderer_host/render_widget_host_view.h"
[email protected]fd694982009-07-03 17:32:3762#include "chrome/browser/renderer_host/resource_request_details.h"
[email protected]1db6ff152009-10-12 15:32:0763#include "chrome/browser/renderer_host/site_instance.h"
[email protected]96d185d2009-04-24 03:28:5464#include "chrome/browser/renderer_host/web_cache_manager.h"
[email protected]93623c5d2009-12-10 21:40:3265#include "chrome/browser/renderer_preferences_util.h"
[email protected]8b62334b2010-08-31 22:37:1166#include "chrome/browser/search_engines/template_url.h"
[email protected]152b8ba32010-03-29 19:40:1667#include "chrome/browser/search_engines/template_url_fetcher.h"
[email protected]0b5b85c2010-10-11 23:00:1268#include "chrome/browser/search_engines/template_url_fetcher_ui_callbacks.h"
[email protected]152b8ba32010-03-29 19:40:1669#include "chrome/browser/search_engines/template_url_model.h"
[email protected]c37e3b62010-01-22 00:15:1770#include "chrome/browser/sessions/session_types.h"
[email protected]bd1ad682009-05-15 22:19:1771#include "chrome/browser/tab_contents/infobar_delegate.h"
[email protected]7e383692009-06-12 19:14:5472#include "chrome/browser/tab_contents/interstitial_page.h"
[email protected]f3ec7742009-01-15 00:59:1673#include "chrome/browser/tab_contents/navigation_entry.h"
[email protected]de34a962009-06-26 23:22:4374#include "chrome/browser/tab_contents/provisional_load_details.h"
[email protected]f3ec7742009-01-15 00:59:1675#include "chrome/browser/tab_contents/tab_contents_delegate.h"
[email protected]1f18184a2010-07-21 19:34:4976#include "chrome/browser/tab_contents/tab_contents_ssl_helper.h"
[email protected]fdd61c62009-04-22 19:22:5777#include "chrome/browser/tab_contents/tab_contents_view.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]d5e311b2010-07-20 17:15:03106#include "webkit/glue/password_form.h"
[email protected]191eb3f72010-12-21 06:27:50107#include "webkit/plugins/npapi/plugin_list.h"
[email protected]3c733bde2010-12-21 19:56:31108#include "webkit/glue/webpreferences.h"
109
110#if defined(OS_MACOSX)
111#include "app/surface/io_surface_support_mac.h"
112#endif // defined(OS_MACOSX)
[email protected]3e45ba92009-02-20 21:09:00113
[email protected]61d68ef12011-01-13 14:02:56114#if defined(OS_CHROMEOS)
115#include "chrome/browser/chromeos/locale_change_guard.h"
116#endif // defined(OS_CHROMEOS)
117
[email protected]420ae012009-04-24 05:16:32118// Cross-Site Navigations
119//
120// If a TabContents is told to navigate to a different web site (as determined
121// by SiteInstance), it will replace its current RenderViewHost with a new
122// RenderViewHost dedicated to the new SiteInstance. This works as follows:
123//
124// - Navigate determines whether the destination is cross-site, and if so,
125// it creates a pending_render_view_host_ and moves into the PENDING
126// RendererState.
127// - The pending RVH is "suspended," so that no navigation messages are sent to
128// its renderer until the onbeforeunload JavaScript handler has a chance to
129// run in the current RVH.
130// - The pending RVH tells CrossSiteRequestManager (a thread-safe singleton)
131// that it has a pending cross-site request. ResourceDispatcherHost will
132// check for this when the response arrives.
133// - The current RVH runs its onbeforeunload handler. If it returns false, we
134// cancel all the pending logic and go back to NORMAL. Otherwise we allow
135// the pending RVH to send the navigation request to its renderer.
136// - ResourceDispatcherHost receives a ResourceRequest on the IO thread. It
137// checks CrossSiteRequestManager to see that the RVH responsible has a
138// pending cross-site request, and then installs a CrossSiteEventHandler.
139// - When RDH receives a response, the BufferedEventHandler determines whether
140// it is a download. If so, it sends a message to the new renderer causing
141// it to cancel the request, and the download proceeds in the download
142// thread. For now, we stay in a PENDING state (with a pending RVH) until
[email protected]57c6a652009-05-04 07:58:34143// the next DidNavigate event for this TabContents. This isn't ideal, but it
[email protected]420ae012009-04-24 05:16:32144// doesn't affect any functionality.
145// - After RDH receives a response and determines that it is safe and not a
146// download, it pauses the response to first run the old page's onunload
147// handler. It does this by asynchronously calling the OnCrossSiteResponse
[email protected]57c6a652009-05-04 07:58:34148// method of TabContents on the UI thread, which sends a ClosePage message
[email protected]420ae012009-04-24 05:16:32149// to the current RVH.
150// - Once the onunload handler is finished, a ClosePage_ACK message is sent to
151// the ResourceDispatcherHost, who unpauses the response. Data is then sent
152// to the pending RVH.
153// - The pending renderer sends a FrameNavigate message that invokes the
154// DidNavigate method. This replaces the current RVH with the
155// pending RVH and goes back to the NORMAL RendererState.
156
157namespace {
158
159// Amount of time we wait between when a key event is received and the renderer
160// is queried for its state and pushed to the NavigationEntry.
161const int kQueryStateDelay = 5000;
162
[email protected]6ebdc9b2010-09-27 16:55:57163const int kSyncWaitDelay = 40;
164
[email protected]420ae012009-04-24 05:16:32165// If another javascript message box is displayed within
166// kJavascriptMessageExpectedDelay of a previous javascript message box being
167// dismissed, display an option to suppress future message boxes from this
168// contents.
169const int kJavascriptMessageExpectedDelay = 1000;
170
[email protected]420ae012009-04-24 05:16:32171// The list of prefs we want to observe.
[email protected]57ecc4b2010-08-11 03:02:51172const char* kPrefsToObserve[] = {
[email protected]420ae012009-04-24 05:16:32173 prefs::kAlternateErrorPagesEnabled,
[email protected]d0b8d092010-10-25 04:05:17174 prefs::kDefaultZoomLevel,
[email protected]420ae012009-04-24 05:16:32175 prefs::kWebKitJavaEnabled,
176 prefs::kWebKitJavascriptEnabled,
177 prefs::kWebKitLoadsImagesAutomatically,
178 prefs::kWebKitPluginsEnabled,
179 prefs::kWebKitUsesUniversalDetector,
180 prefs::kWebKitSerifFontFamily,
181 prefs::kWebKitSansSerifFontFamily,
182 prefs::kWebKitFixedFontFamily,
183 prefs::kWebKitDefaultFontSize,
184 prefs::kWebKitDefaultFixedFontSize,
[email protected]6bdf05b2010-10-13 20:34:13185 prefs::kWebKitMinimumFontSize,
186 prefs::kWebKitMinimumLogicalFontSize,
[email protected]1661a3c02010-05-20 16:32:36187 prefs::kWebkitTabsToLinks,
[email protected]420ae012009-04-24 05:16:32188 prefs::kDefaultCharset
189 // kWebKitStandardFontIsSerif needs to be added
190 // if we let users pick which font to use, serif or sans-serif when
191 // no font is specified or a CSS generic family (serif or sans-serif)
192 // is not specified.
193};
194
195const int kPrefsToObserveLength = arraysize(kPrefsToObserve);
196
[email protected]420ae012009-04-24 05:16:32197// Returns true if the entry's transition type is FORM_SUBMIT.
198bool IsFormSubmit(const NavigationEntry* entry) {
199 return (PageTransition::StripQualifier(entry->transition_type()) ==
200 PageTransition::FORM_SUBMIT);
201}
202
203#if defined(OS_WIN)
204
205BOOL CALLBACK InvalidateWindow(HWND hwnd, LPARAM lparam) {
206 // Note: erase is required to properly paint some widgets borders. This can
207 // be seen with textfields.
208 InvalidateRect(hwnd, NULL, TRUE);
209 return TRUE;
210}
211#endif
212
[email protected]5e369672009-11-03 23:48:30213ViewMsg_Navigate_Params::NavigationType GetNavigationType(
[email protected]1ccb3568d2010-02-19 10:51:16214 Profile* profile, const NavigationEntry& entry,
215 NavigationController::ReloadType reload_type) {
216 switch (reload_type) {
217 case NavigationController::RELOAD:
218 return ViewMsg_Navigate_Params::RELOAD;
219 case NavigationController::RELOAD_IGNORING_CACHE:
220 return ViewMsg_Navigate_Params::RELOAD_IGNORING_CACHE;
221 case NavigationController::NO_RELOAD:
222 break; // Fall through to rest of function.
223 }
[email protected]5e369672009-11-03 23:48:30224
225 if (entry.restore_type() == NavigationEntry::RESTORE_LAST_SESSION &&
226 profile->DidLastSessionExitCleanly())
227 return ViewMsg_Navigate_Params::RESTORE;
228
229 return ViewMsg_Navigate_Params::NORMAL;
230}
231
[email protected]876bc832010-09-07 16:29:54232void MakeNavigateParams(const NavigationEntry& entry,
233 const NavigationController& controller,
[email protected]1ccb3568d2010-02-19 10:51:16234 NavigationController::ReloadType reload_type,
235 ViewMsg_Navigate_Params* params) {
[email protected]056de2d2009-06-26 16:41:34236 params->page_id = entry.page_id();
[email protected]876bc832010-09-07 16:29:54237 params->pending_history_list_offset = controller.GetIndexOfEntry(&entry);
[email protected]3cc72b12010-03-18 23:03:00238 params->current_history_list_offset = controller.last_committed_entry_index();
239 params->current_history_list_length = controller.entry_count();
[email protected]056de2d2009-06-26 16:41:34240 params->url = entry.url();
241 params->referrer = entry.referrer();
242 params->transition = entry.transition_type();
243 params->state = entry.content_state();
[email protected]3cc72b12010-03-18 23:03:00244 params->navigation_type =
245 GetNavigationType(controller.profile(), entry, reload_type);
[email protected]056de2d2009-06-26 16:41:34246 params->request_time = base::Time::Now();
247}
248
[email protected]3c2826852010-12-16 19:09:14249class OutdatedPluginInfoBar : public ConfirmInfoBarDelegate {
[email protected]851b1eb2010-08-09 13:32:29250 public:
[email protected]3c2826852010-12-16 19:09:14251 OutdatedPluginInfoBar(TabContents* tab_contents,
[email protected]851b1eb2010-08-09 13:32:29252 const string16& name,
253 const GURL& update_url)
254 : ConfirmInfoBarDelegate(tab_contents),
255 tab_contents_(tab_contents),
256 name_(name),
257 update_url_(update_url) {
[email protected]97d5f9e2011-01-13 09:23:52258 UserMetrics::RecordAction(UserMetricsAction("OutdatedPluginInfobar.Shown"));
[email protected]851b1eb2010-08-09 13:32:29259 tab_contents->AddInfoBar(this);
260 }
261
262 virtual int GetButtons() const {
263 return BUTTON_OK | BUTTON_CANCEL | BUTTON_OK_DEFAULT;
264 }
265
[email protected]e23d3a32010-08-13 19:39:58266 virtual string16 GetButtonLabel(InfoBarButton button) const {
[email protected]851b1eb2010-08-09 13:32:29267 if (button == BUTTON_CANCEL)
[email protected]e23d3a32010-08-13 19:39:58268 return l10n_util::GetStringUTF16(IDS_PLUGIN_ENABLE_TEMPORARILY);
[email protected]851b1eb2010-08-09 13:32:29269 if (button == BUTTON_OK)
[email protected]e23d3a32010-08-13 19:39:58270 return l10n_util::GetStringUTF16(IDS_PLUGIN_UPDATE);
[email protected]851b1eb2010-08-09 13:32:29271 return ConfirmInfoBarDelegate::GetButtonLabel(button);
272 }
273
[email protected]e23d3a32010-08-13 19:39:58274 virtual string16 GetMessageText() const {
275 return l10n_util::GetStringFUTF16(IDS_PLUGIN_OUTDATED_PROMPT, name_);
[email protected]851b1eb2010-08-09 13:32:29276 }
277
[email protected]e23d3a32010-08-13 19:39:58278 virtual string16 GetLinkText() {
279 return l10n_util::GetStringUTF16(IDS_LEARN_MORE);
[email protected]851b1eb2010-08-09 13:32:29280 }
281
282 virtual SkBitmap* GetIcon() const {
283 return ResourceBundle::GetSharedInstance().GetBitmapNamed(
284 IDR_INFOBAR_PLUGIN_INSTALL);
285 }
286
287 virtual bool Accept() {
[email protected]97d5f9e2011-01-13 09:23:52288 UserMetrics::RecordAction(
289 UserMetricsAction("OutdatedPluginInfobar.Update"));
[email protected]851b1eb2010-08-09 13:32:29290 tab_contents_->OpenURL(update_url_, GURL(),
[email protected]57b66d02010-09-30 11:24:51291 NEW_FOREGROUND_TAB, PageTransition::LINK);
[email protected]851b1eb2010-08-09 13:32:29292 return false;
293 }
294
295 virtual bool Cancel() {
[email protected]97d5f9e2011-01-13 09:23:52296 UserMetrics::RecordAction(
297 UserMetricsAction("OutdatedPluginInfobar.AllowThisTime"));
[email protected]3c2826852010-12-16 19:09:14298 tab_contents_->render_view_host()->LoadBlockedPlugins();
299 return true;
[email protected]851b1eb2010-08-09 13:32:29300 }
301
302 virtual bool LinkClicked(WindowOpenDisposition disposition) {
[email protected]97d5f9e2011-01-13 09:23:52303 UserMetrics::RecordAction(
304 UserMetricsAction("OutdatedPluginInfobar.LearnMore"));
[email protected]851b1eb2010-08-09 13:32:29305 // TODO(bauerb): Navigate to a help page explaining why we disabled
306 // the plugin, once we have one.
307 return false;
308 }
309
[email protected]f5ec1ba2010-12-22 17:47:40310 virtual void InfoBarClosed() {
[email protected]97d5f9e2011-01-13 09:23:52311 UserMetrics::RecordAction(
312 UserMetricsAction("OutdatedPluginInfobar.Closed"));
[email protected]f5ec1ba2010-12-22 17:47:40313 delete this;
314 }
315
[email protected]851b1eb2010-08-09 13:32:29316 private:
317 TabContents* tab_contents_;
318 string16 name_;
319 GURL update_url_;
320};
321
[email protected]420ae012009-04-24 05:16:32322} // namespace
323
324// -----------------------------------------------------------------------------
325
[email protected]fdd61c62009-04-22 19:22:57326// static
327int TabContents::find_request_id_counter_ = -1;
328
[email protected]420ae012009-04-24 05:16:32329
[email protected]420ae012009-04-24 05:16:32330TabContents::TabContents(Profile* profile,
331 SiteInstance* site_instance,
332 int routing_id,
[email protected]6ee12c42010-09-14 09:36:07333 const TabContents* base_tab_contents,
334 SessionStorageNamespace* session_storage_namespace)
[email protected]b680ad22009-04-15 23:19:42335 : delegate_(NULL),
[email protected]6ee12c42010-09-14 09:36:07336 ALLOW_THIS_IN_INITIALIZER_LIST(controller_(
337 this, profile, session_storage_namespace)),
[email protected]66ba4932009-06-04 19:22:13338 ALLOW_THIS_IN_INITIALIZER_LIST(view_(
[email protected]d82ed61e2009-06-16 02:46:22339 TabContentsView::Create(this))),
340 ALLOW_THIS_IN_INITIALIZER_LIST(render_manager_(this, this)),
[email protected]fdd61c62009-04-22 19:22:57341 property_bag_(),
342 registrar_(),
[email protected]12802702010-07-09 19:43:09343 ALLOW_THIS_IN_INITIALIZER_LIST(printing_(
344 new printing::PrintViewManager(*this))),
[email protected]fdd61c62009-04-22 19:22:57345 save_package_(),
[email protected]6db0def92010-03-02 19:55:45346 autocomplete_history_manager_(),
[email protected]21674df2009-10-28 19:51:21347 autofill_manager_(),
[email protected]fdd61c62009-04-22 19:22:57348 plugin_installer_(),
[email protected]6f821dd2010-02-23 00:54:06349 bookmark_drag_(NULL),
[email protected]d82ed61e2009-06-16 02:46:22350 ALLOW_THIS_IN_INITIALIZER_LIST(fav_icon_helper_(this)),
[email protected]d5f942ba2008-09-26 19:30:34351 is_loading_(false),
[email protected]443b80e2010-12-14 00:42:23352 crashed_status_(base::TERMINATION_STATUS_STILL_RUNNING),
353 crashed_error_code_(0),
[email protected]d5f942ba2008-09-26 19:30:34354 waiting_for_response_(false),
[email protected]d5f942ba2008-09-26 19:30:34355 max_page_id_(-1),
[email protected]fdd61c62009-04-22 19:22:57356 current_load_start_(),
357 load_state_(net::LOAD_STATE_IDLE),
358 load_state_host_(),
[email protected]094e5b22009-09-25 04:23:56359 upload_size_(0),
360 upload_position_(0),
[email protected]fdd61c62009-04-22 19:22:57361 received_page_title_(false),
362 is_starred_(false),
363 contents_mime_type_(),
364 encoding_(),
[email protected]c65e2f152010-10-14 15:30:40365 blocked_contents_(NULL),
366 all_contents_blocked_(false),
[email protected]8cc8d492010-02-02 10:40:49367 dont_notify_render_view_(false),
[email protected]f17a0ee2010-05-17 17:38:47368 displayed_insecure_content_(false),
[email protected]fdd61c62009-04-22 19:22:57369 infobar_delegates_(),
[email protected]fdd61c62009-04-22 19:22:57370 find_ui_active_(false),
371 find_op_aborted_(false),
372 current_find_request_id_(find_request_id_counter_++),
[email protected]e491f1c2009-05-22 20:28:12373 last_search_case_sensitive_(false),
[email protected]e491f1c2009-05-22 20:28:12374 last_search_result_(),
[email protected]98aa0b52010-05-06 17:03:08375 extension_app_(NULL),
[email protected]fdd61c62009-04-22 19:22:57376 capturing_contents_(false),
377 is_being_destroyed_(false),
378 notify_disconnection_(false),
379 history_requests_(),
380#if defined(OS_WIN)
381 message_box_active_(CreateEvent(NULL, TRUE, FALSE, NULL)),
382#endif
383 last_javascript_message_dismissal_(),
[email protected]c2e74fe82009-09-03 17:57:44384 suppress_javascript_messages_(false),
[email protected]7ab1e7d62009-10-14 23:32:01385 is_showing_before_unload_dialog_(false),
[email protected]7ff431e2010-01-07 18:18:54386 renderer_preferences_(),
[email protected]1a242c32010-02-02 21:20:54387 opener_dom_ui_type_(DOMUIFactory::kNoDOMUI),
[email protected]777a7b62010-05-11 13:28:16388 language_state_(&controller_),
[email protected]ebf40a72010-07-22 01:46:38389 closed_by_user_gesture_(false),
[email protected]b75b8292010-10-01 07:28:25390 minimum_zoom_percent_(
391 static_cast<int>(WebKit::WebView::minTextSizeMultiplier * 100)),
392 maximum_zoom_percent_(
393 static_cast<int>(WebKit::WebView::maxTextSizeMultiplier * 100)),
[email protected]9e823662010-10-13 23:36:00394 temporary_zoom_settings_(false),
395 content_restrictions_(0) {
[email protected]7ff431e2010-01-07 18:18:54396 renderer_preferences_util::UpdateFromSystemSettings(
397 &renderer_preferences_, profile);
398
[email protected]9fb83e82010-07-02 18:24:55399 content_settings_delegate_.reset(
400 new TabSpecificContentSettings(this, profile));
401
[email protected]12636df2009-09-28 22:32:21402 render_manager_.Init(profile, site_instance, routing_id);
[email protected]420ae012009-04-24 05:16:32403
[email protected]34ac70502009-09-25 17:07:23404 // We have the initial size of the view be based on the size of the passed in
405 // tab contents (normally a tab from the same window).
406 view_->CreateView(base_tab_contents ?
407 base_tab_contents->view()->GetContainerSize() : gfx::Size());
[email protected]420ae012009-04-24 05:16:32408
409 // Register for notifications about all interested prefs change.
410 PrefService* prefs = profile->GetPrefs();
[email protected]2fb7dc982010-09-29 12:24:28411 pref_change_registrar_.Init(prefs);
[email protected]420ae012009-04-24 05:16:32412 if (prefs) {
413 for (int i = 0; i < kPrefsToObserveLength; ++i)
[email protected]2fb7dc982010-09-29 12:24:28414 pref_change_registrar_.Add(kPrefsToObserve[i], this);
[email protected]420ae012009-04-24 05:16:32415 }
416
417 // Register for notifications about URL starredness changing on any profile.
418 registrar_.Add(this, NotificationType::URLS_STARRED,
419 NotificationService::AllSources());
420 registrar_.Add(this, NotificationType::BOOKMARK_MODEL_LOADED,
421 NotificationService::AllSources());
422 registrar_.Add(this, NotificationType::RENDER_WIDGET_HOST_DESTROYED,
423 NotificationService::AllSources());
[email protected]d3ba77272009-09-03 00:06:09424#if defined(OS_LINUX)
425 registrar_.Add(this, NotificationType::BROWSER_THEME_CHANGED,
426 NotificationService::AllSources());
427#endif
[email protected]420ae012009-04-24 05:16:32428
[email protected]6c32ce72010-03-08 05:18:06429 registrar_.Add(this, NotificationType::USER_STYLE_SHEET_UPDATED,
430 NotificationService::AllSources());
431
[email protected]79580c62010-02-02 02:36:25432 // Register for notifications about content setting changes.
433 registrar_.Add(this, NotificationType::CONTENT_SETTINGS_CHANGED,
434 NotificationService::AllSources());
435
[email protected]a888b29e62010-04-01 13:38:57436 // Listen for extension changes so we can update extension_for_current_page_.
437 registrar_.Add(this, NotificationType::EXTENSION_LOADED,
438 NotificationService::AllSources());
439 registrar_.Add(this, NotificationType::EXTENSION_UNLOADED,
440 NotificationService::AllSources());
[email protected]a888b29e62010-04-01 13:38:57441
[email protected]88152052010-10-01 04:05:18442 // Listen for Google URL changes
443 registrar_.Add(this, NotificationType::GOOGLE_URL_UPDATED,
444 NotificationService::AllSources());
445
[email protected]c8865482009-07-23 20:40:10446 // Set-up the showing of the omnibox search infobar if applicable.
447 if (OmniboxSearchHint::IsEnabled(profile))
448 omnibox_search_hint_.reset(new OmniboxSearchHint(this));
[email protected]403415a2011-01-10 18:57:53449
450 autofill_manager_.reset(new AutoFillManager(this));
451 message_filters_.push_back(autofill_manager_.get());
452 autocomplete_history_manager_.reset(new AutocompleteHistoryManager(this));
453 message_filters_.push_back(autocomplete_history_manager_.get());
[email protected]332af7732009-03-11 13:21:35454}
initial.commit09911bf2008-07-26 23:55:29455
456TabContents::~TabContents() {
[email protected]420ae012009-04-24 05:16:32457 is_being_destroyed_ = true;
458
[email protected]bfe4c1582009-10-02 18:11:09459 // We don't want any notifications while we're running our destructor.
[email protected]420ae012009-04-24 05:16:32460 registrar_.RemoveAll();
[email protected]2fb7dc982010-09-29 12:24:28461 pref_change_registrar_.RemoveAll();
[email protected]420ae012009-04-24 05:16:32462
[email protected]420ae012009-04-24 05:16:32463 NotifyDisconnected();
[email protected]e7b8d832009-11-20 22:41:00464 hung_renderer_dialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:32465
[email protected]420ae012009-04-24 05:16:32466 // First cleanly close all child windows.
467 // TODO(mpcomplete): handle case if MaybeCloseChildWindows() already asked
468 // some of these to close. CloseWindows is async, so it might get called
469 // twice before it runs.
[email protected]8cc8d492010-02-02 10:40:49470 CloseConstrainedWindows();
[email protected]420ae012009-04-24 05:16:32471
[email protected]c65e2f152010-10-14 15:30:40472 // Close all blocked contents.
473 if (blocked_contents_)
474 blocked_contents_->Destroy();
[email protected]66ba4932009-06-04 19:22:13475
[email protected]420ae012009-04-24 05:16:32476 // Notify any observer that have a reference on this tab contents.
477 NotificationService::current()->Notify(
478 NotificationType::TAB_CONTENTS_DESTROYED,
479 Source<TabContents>(this),
480 NotificationService::NoDetails());
481
[email protected]1e5e74f2009-05-27 20:55:12482 // Notify any lasting InfobarDelegates that have not yet been removed that
483 // whatever infobar they were handling in this TabContents has closed,
484 // because the TabContents is going away entirely.
485 // This must happen after the TAB_CONTENTS_DESTROYED notification as the
486 // notification may trigger infobars calls that access their delegate. (and
487 // some implementations of InfoBarDelegate do delete themselves on
488 // InfoBarClosed()).
489 for (int i = 0; i < infobar_delegate_count(); ++i) {
490 InfoBarDelegate* delegate = GetInfoBarDelegateAt(i);
491 delegate->InfoBarClosed();
492 }
493 infobar_delegates_.clear();
494
[email protected]420ae012009-04-24 05:16:32495 // TODO(brettw) this should be moved to the view.
496#if defined(OS_WIN)
497 // If we still have a window handle, destroy it. GetNativeView can return
498 // NULL if this contents was part of a window that closed.
[email protected]50bd6452010-11-27 19:39:42499 if (GetNativeView()) {
500 RenderViewHost* host = render_view_host();
501 if (host && host->view()) {
502 host->view()->WillWmDestroy();
503 }
[email protected]420ae012009-04-24 05:16:32504 ::DestroyWindow(GetNativeView());
[email protected]50bd6452010-11-27 19:39:42505 }
[email protected]420ae012009-04-24 05:16:32506#endif
[email protected]7ab1e7d62009-10-14 23:32:01507
508 // OnCloseStarted isn't called in unit tests.
509 if (!tab_close_start_time_.is_null()) {
510 UMA_HISTOGRAM_TIMES("Tab.Close",
511 base::TimeTicks::Now() - tab_close_start_time_);
512 }
initial.commit09911bf2008-07-26 23:55:29513}
514
[email protected]d5f942ba2008-09-26 19:30:34515// static
516void TabContents::RegisterUserPrefs(PrefService* prefs) {
[email protected]420ae012009-04-24 05:16:32517 prefs->RegisterBooleanPref(prefs::kAlternateErrorPagesEnabled, true);
518
519 WebPreferences pref_defaults;
520 prefs->RegisterBooleanPref(prefs::kWebKitJavascriptEnabled,
521 pref_defaults.javascript_enabled);
522 prefs->RegisterBooleanPref(prefs::kWebKitWebSecurityEnabled,
523 pref_defaults.web_security_enabled);
524 prefs->RegisterBooleanPref(
525 prefs::kWebKitJavascriptCanOpenWindowsAutomatically, true);
526 prefs->RegisterBooleanPref(prefs::kWebKitLoadsImagesAutomatically,
527 pref_defaults.loads_images_automatically);
528 prefs->RegisterBooleanPref(prefs::kWebKitPluginsEnabled,
529 pref_defaults.plugins_enabled);
530 prefs->RegisterBooleanPref(prefs::kWebKitDomPasteEnabled,
531 pref_defaults.dom_paste_enabled);
532 prefs->RegisterBooleanPref(prefs::kWebKitShrinksStandaloneImagesToFit,
533 pref_defaults.shrinks_standalone_images_to_fit);
[email protected]c4e98902010-06-01 10:20:14534 prefs->RegisterDictionaryPref(prefs::kWebKitInspectorSettings);
[email protected]420ae012009-04-24 05:16:32535 prefs->RegisterBooleanPref(prefs::kWebKitTextAreasAreResizable,
536 pref_defaults.text_areas_are_resizable);
537 prefs->RegisterBooleanPref(prefs::kWebKitJavaEnabled,
538 pref_defaults.java_enabled);
[email protected]1661a3c02010-05-20 16:32:36539 prefs->RegisterBooleanPref(prefs::kWebkitTabsToLinks,
540 pref_defaults.tabs_to_links);
[email protected]420ae012009-04-24 05:16:32541
542 prefs->RegisterLocalizedStringPref(prefs::kAcceptLanguages,
543 IDS_ACCEPT_LANGUAGES);
544 prefs->RegisterLocalizedStringPref(prefs::kDefaultCharset,
545 IDS_DEFAULT_ENCODING);
546 prefs->RegisterLocalizedBooleanPref(prefs::kWebKitStandardFontIsSerif,
547 IDS_STANDARD_FONT_IS_SERIF);
548 prefs->RegisterLocalizedStringPref(prefs::kWebKitFixedFontFamily,
549 IDS_FIXED_FONT_FAMILY);
550 prefs->RegisterLocalizedStringPref(prefs::kWebKitSerifFontFamily,
551 IDS_SERIF_FONT_FAMILY);
552 prefs->RegisterLocalizedStringPref(prefs::kWebKitSansSerifFontFamily,
553 IDS_SANS_SERIF_FONT_FAMILY);
554 prefs->RegisterLocalizedStringPref(prefs::kWebKitCursiveFontFamily,
555 IDS_CURSIVE_FONT_FAMILY);
556 prefs->RegisterLocalizedStringPref(prefs::kWebKitFantasyFontFamily,
557 IDS_FANTASY_FONT_FAMILY);
558 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitDefaultFontSize,
559 IDS_DEFAULT_FONT_SIZE);
560 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitDefaultFixedFontSize,
561 IDS_DEFAULT_FIXED_FONT_SIZE);
562 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitMinimumFontSize,
563 IDS_MINIMUM_FONT_SIZE);
564 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitMinimumLogicalFontSize,
565 IDS_MINIMUM_LOGICAL_FONT_SIZE);
566 prefs->RegisterLocalizedBooleanPref(prefs::kWebKitUsesUniversalDetector,
567 IDS_USES_UNIVERSAL_DETECTOR);
568 prefs->RegisterLocalizedStringPref(prefs::kStaticEncodings,
569 IDS_STATIC_ENCODING_LIST);
initial.commit09911bf2008-07-26 23:55:29570}
571
[email protected]724159a2010-12-30 01:11:18572bool TabContents::OnMessageReceived(const IPC::Message& message) {
[email protected]403415a2011-01-10 18:57:53573 for (size_t i = 0; i < message_filters_.size(); ++i) {
574 if (message_filters_[i]->OnMessageReceived(message))
575 return true;
576 }
577
[email protected]724159a2010-12-30 01:11:18578 bool handled = true;
579 bool message_is_ok = true;
580 IPC_BEGIN_MESSAGE_MAP_EX(TabContents, message, message_is_ok)
581 IPC_MESSAGE_HANDLER(ViewHostMsg_DidStartProvisionalLoadForFrame,
582 OnDidStartProvisionalLoadForFrame)
583 IPC_MESSAGE_HANDLER(ViewHostMsg_DidRedirectProvisionalLoad,
584 OnDidRedirectProvisionalLoad)
585 IPC_MESSAGE_HANDLER(ViewHostMsg_DidFailProvisionalLoadWithError,
586 OnDidFailProvisionalLoadWithError)
587 IPC_MESSAGE_HANDLER(ViewHostMsg_DidLoadResourceFromMemoryCache,
588 OnDidLoadResourceFromMemoryCache)
589 IPC_MESSAGE_HANDLER(ViewHostMsg_DidDisplayInsecureContent,
590 OnDidDisplayInsecureContent)
591 IPC_MESSAGE_HANDLER(ViewHostMsg_DidRunInsecureContent,
592 OnDidRunInsecureContent)
593 IPC_MESSAGE_HANDLER(ViewHostMsg_DocumentLoadedInFrame,
594 OnDocumentLoadedInFrame)
595 IPC_MESSAGE_HANDLER(ViewHostMsg_DidFinishLoad, OnDidFinishLoad)
596 IPC_MESSAGE_UNHANDLED(handled = false)
597 IPC_END_MESSAGE_MAP_EX()
598
599 if (!message_is_ok) {
600 UserMetrics::RecordAction(UserMetricsAction("BadMessageTerminate_RVD"));
601 GetRenderProcessHost()->ReceivedBadMessage();
602 }
603
604 return handled;
605}
606
[email protected]13367f72009-07-08 16:39:02607// Returns true if contains content rendered by an extension.
608bool TabContents::HostsExtension() const {
609 return GetURL().SchemeIs(chrome::kExtensionScheme);
[email protected]d5f942ba2008-09-26 19:30:34610}
611
[email protected]6e95706d2009-04-23 22:55:01612PluginInstaller* TabContents::GetPluginInstaller() {
613 if (plugin_installer_.get() == NULL)
614 plugin_installer_.reset(new PluginInstaller(this));
615 return plugin_installer_.get();
616}
617
[email protected]1f18184a2010-07-21 19:34:49618TabContentsSSLHelper* TabContents::GetSSLHelper() {
619 if (ssl_helper_.get() == NULL)
620 ssl_helper_.reset(new TabContentsSSLHelper(this));
621 return ssl_helper_.get();
622}
623
[email protected]8cb5d5b2010-02-09 11:36:16624RenderProcessHost* TabContents::GetRenderProcessHost() const {
625 return render_manager_.current_host()->process();
626}
627
[email protected]9adb9692010-10-29 23:14:02628void TabContents::SetExtensionApp(const Extension* extension) {
[email protected]867a73e12010-03-19 20:45:46629 DCHECK(!extension || extension->GetFullLaunchURL().is_valid());
[email protected]98aa0b52010-05-06 17:03:08630 extension_app_ = extension;
[email protected]fca656c2010-02-10 20:30:10631
[email protected]98aa0b52010-05-06 17:03:08632 UpdateExtensionAppIcon(extension_app_);
[email protected]a888b29e62010-04-01 13:38:57633
[email protected]fca656c2010-02-10 20:30:10634 NotificationService::current()->Notify(
635 NotificationType::TAB_CONTENTS_APPLICATION_EXTENSION_CHANGED,
636 Source<TabContents>(this),
637 NotificationService::NoDetails());
[email protected]3b355642010-02-05 16:01:49638}
639
[email protected]98aa0b52010-05-06 17:03:08640void TabContents::SetExtensionAppById(const std::string& extension_app_id) {
641 if (extension_app_id.empty())
[email protected]911f8152010-03-18 16:46:40642 return;
643
[email protected]eaa7dd182010-12-14 11:09:00644 ExtensionService* extension_service = profile()->GetExtensionService();
[email protected]fa6a9102010-11-22 15:38:50645 if (!extension_service || !extension_service->is_ready())
646 return;
647
648 const Extension* extension =
649 extension_service->GetExtensionById(extension_app_id, false);
650 if (extension)
651 SetExtensionApp(extension);
[email protected]911f8152010-03-18 16:46:40652}
653
[email protected]98aa0b52010-05-06 17:03:08654SkBitmap* TabContents::GetExtensionAppIcon() {
[email protected]98aa0b52010-05-06 17:03:08655 if (extension_app_icon_.empty())
[email protected]8eb043b2010-04-30 07:19:51656 return NULL;
657
[email protected]98aa0b52010-05-06 17:03:08658 return &extension_app_icon_;
[email protected]8eb043b2010-04-30 07:19:51659}
660
[email protected]d5f942ba2008-09-26 19:30:34661const GURL& TabContents::GetURL() const {
662 // We may not have a navigation entry yet
[email protected]ce3fa3c2009-04-20 19:55:57663 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]ebe89e062009-08-13 23:16:54664 return entry ? entry->virtual_url() : GURL::EmptyGURL();
[email protected]d5f942ba2008-09-26 19:30:34665}
666
[email protected]96d185d2009-04-24 03:28:54667const string16& TabContents::GetTitle() const {
[email protected]4c6092c5b2009-06-06 00:23:55668 // Transient entries take precedence. They are used for interstitial pages
669 // that are shown on top of existing pages.
670 NavigationEntry* entry = controller_.GetTransientEntry();
[email protected]45d0ef7f2011-01-05 13:46:23671 if (entry) {
672 return entry->GetTitleForDisplay(profile()->GetPrefs()->
673 GetString(prefs::kAcceptLanguages));
674 }
[email protected]96d185d2009-04-24 03:28:54675 DOMUI* our_dom_ui = render_manager_.pending_dom_ui() ?
676 render_manager_.pending_dom_ui() : render_manager_.dom_ui();
677 if (our_dom_ui) {
678 // Don't override the title in view source mode.
[email protected]4c6092c5b2009-06-06 00:23:55679 entry = controller_.GetActiveEntry();
[email protected]96d185d2009-04-24 03:28:54680 if (!(entry && entry->IsViewSourceMode())) {
681 // Give the DOM UI the chance to override our title.
682 const string16& title = our_dom_ui->overridden_title();
683 if (!title.empty())
684 return title;
685 }
686 }
687
688 // We use the title for the last committed entry rather than a pending
689 // navigation entry. For example, when the user types in a URL, we want to
690 // keep the old page's title until the new load has committed and we get a new
691 // title.
[email protected]96d185d2009-04-24 03:28:54692 entry = controller_.GetLastCommittedEntry();
[email protected]45d0ef7f2011-01-05 13:46:23693 if (entry) {
694 return entry->GetTitleForDisplay(profile()->GetPrefs()->
695 GetString(prefs::kAcceptLanguages));
696 }
[email protected]96d185d2009-04-24 03:28:54697 return EmptyString16();
698}
699
[email protected]bbbc1ef2010-02-12 18:03:41700// static
701string16 TabContents::GetDefaultTitle() {
702 return l10n_util::GetStringUTF16(IDS_DEFAULT_TAB_TITLE);
703}
704
[email protected]d5f942ba2008-09-26 19:30:34705int32 TabContents::GetMaxPageID() {
706 if (GetSiteInstance())
707 return GetSiteInstance()->max_page_id();
708 else
709 return max_page_id_;
710}
711
712void TabContents::UpdateMaxPageID(int32 page_id) {
713 // Ensure both the SiteInstance and RenderProcessHost update their max page
[email protected]57c6a652009-05-04 07:58:34714 // IDs in sync. Only TabContents will also have site instances, except during
[email protected]d5f942ba2008-09-26 19:30:34715 // testing.
716 if (GetSiteInstance())
717 GetSiteInstance()->UpdateMaxPageID(page_id);
[email protected]8cb5d5b2010-02-09 11:36:16718 GetRenderProcessHost()->UpdateMaxPageID(page_id);
[email protected]d5f942ba2008-09-26 19:30:34719}
720
[email protected]96d185d2009-04-24 03:28:54721SiteInstance* TabContents::GetSiteInstance() const {
722 return render_manager_.current_host()->site_instance();
723}
724
[email protected]96d185d2009-04-24 03:28:54725bool TabContents::ShouldDisplayURL() {
[email protected]8ab24cc2009-11-13 20:56:15726 // Don't hide the url in view source mode and with interstitials.
[email protected]96d185d2009-04-24 03:28:54727 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]8ab24cc2009-11-13 20:56:15728 if (entry && (entry->IsViewSourceMode() ||
[email protected]cccd3762010-11-12 18:40:01729 entry->page_type() == INTERSTITIAL_PAGE)) {
[email protected]96d185d2009-04-24 03:28:54730 return true;
[email protected]8ab24cc2009-11-13 20:56:15731 }
[email protected]83e3895b2009-06-11 00:07:16732
[email protected]96d185d2009-04-24 03:28:54733 DOMUI* dom_ui = GetDOMUIForCurrentState();
734 if (dom_ui)
735 return !dom_ui->should_hide_url();
736 return true;
737}
738
[email protected]d5f942ba2008-09-26 19:30:34739SkBitmap TabContents::GetFavIcon() const {
740 // Like GetTitle(), we also want to use the favicon for the last committed
741 // entry rather than a pending navigation entry.
[email protected]ce3fa3c2009-04-20 19:55:57742 NavigationEntry* entry = controller_.GetTransientEntry();
[email protected]cbab76d2008-10-13 22:42:47743 if (entry)
744 return entry->favicon().bitmap();
745
[email protected]ce3fa3c2009-04-20 19:55:57746 entry = controller_.GetLastCommittedEntry();
[email protected]d5f942ba2008-09-26 19:30:34747 if (entry)
748 return entry->favicon().bitmap();
[email protected]d5f942ba2008-09-26 19:30:34749 return SkBitmap();
750}
751
[email protected]0b303cc2009-09-28 22:35:15752bool TabContents::FavIconIsValid() const {
753 NavigationEntry* entry = controller_.GetTransientEntry();
754 if (entry)
755 return entry->favicon().is_valid();
756
757 entry = controller_.GetLastCommittedEntry();
758 if (entry)
759 return entry->favicon().is_valid();
760
761 return false;
762}
763
[email protected]96d185d2009-04-24 03:28:54764bool TabContents::ShouldDisplayFavIcon() {
765 // Always display a throbber during pending loads.
766 if (controller_.GetLastCommittedEntry() && controller_.pending_entry())
767 return true;
768
769 DOMUI* dom_ui = GetDOMUIForCurrentState();
770 if (dom_ui)
771 return !dom_ui->hide_favicon();
772 return true;
773}
774
[email protected]42197a22010-12-28 23:29:42775string16 TabContents::GetStatusText() const {
[email protected]96d185d2009-04-24 03:28:54776 if (!is_loading() || load_state_ == net::LOAD_STATE_IDLE)
[email protected]42197a22010-12-28 23:29:42777 return string16();
[email protected]96d185d2009-04-24 03:28:54778
779 switch (load_state_) {
780 case net::LOAD_STATE_WAITING_FOR_CACHE:
[email protected]42197a22010-12-28 23:29:42781 return l10n_util::GetStringUTF16(IDS_LOAD_STATE_WAITING_FOR_CACHE);
[email protected]8a1f3312010-05-25 19:25:04782 case net::LOAD_STATE_ESTABLISHING_PROXY_TUNNEL:
[email protected]42197a22010-12-28 23:29:42783 return
784 l10n_util::GetStringUTF16(IDS_LOAD_STATE_ESTABLISHING_PROXY_TUNNEL);
[email protected]96d185d2009-04-24 03:28:54785 case net::LOAD_STATE_RESOLVING_PROXY_FOR_URL:
[email protected]42197a22010-12-28 23:29:42786 return l10n_util::GetStringUTF16(IDS_LOAD_STATE_RESOLVING_PROXY_FOR_URL);
[email protected]96d185d2009-04-24 03:28:54787 case net::LOAD_STATE_RESOLVING_HOST:
[email protected]42197a22010-12-28 23:29:42788 return l10n_util::GetStringUTF16(IDS_LOAD_STATE_RESOLVING_HOST);
[email protected]96d185d2009-04-24 03:28:54789 case net::LOAD_STATE_CONNECTING:
[email protected]42197a22010-12-28 23:29:42790 return l10n_util::GetStringUTF16(IDS_LOAD_STATE_CONNECTING);
[email protected]375e3112010-05-27 20:32:06791 case net::LOAD_STATE_SSL_HANDSHAKE:
[email protected]42197a22010-12-28 23:29:42792 return l10n_util::GetStringUTF16(IDS_LOAD_STATE_SSL_HANDSHAKE);
[email protected]96d185d2009-04-24 03:28:54793 case net::LOAD_STATE_SENDING_REQUEST:
[email protected]094e5b22009-09-25 04:23:56794 if (upload_size_)
[email protected]42197a22010-12-28 23:29:42795 return l10n_util::GetStringFUTF16Int(
[email protected]094e5b22009-09-25 04:23:56796 IDS_LOAD_STATE_SENDING_REQUEST_WITH_PROGRESS,
797 static_cast<int>((100 * upload_position_) / upload_size_));
798 else
[email protected]42197a22010-12-28 23:29:42799 return l10n_util::GetStringUTF16(IDS_LOAD_STATE_SENDING_REQUEST);
[email protected]96d185d2009-04-24 03:28:54800 case net::LOAD_STATE_WAITING_FOR_RESPONSE:
[email protected]42197a22010-12-28 23:29:42801 return l10n_util::GetStringFUTF16(IDS_LOAD_STATE_WAITING_FOR_RESPONSE,
802 load_state_host_);
[email protected]96d185d2009-04-24 03:28:54803 // Ignore net::LOAD_STATE_READING_RESPONSE and net::LOAD_STATE_IDLE
804 case net::LOAD_STATE_IDLE:
805 case net::LOAD_STATE_READING_RESPONSE:
806 break;
807 }
808
[email protected]42197a22010-12-28 23:29:42809 return string16();
[email protected]96d185d2009-04-24 03:28:54810}
811
[email protected]3c9e1872010-11-18 16:17:49812void TabContents::AddNavigationObserver(WebNavigationObserver* observer) {
813 web_navigation_observers_.AddObserver(observer);
814}
815
816void TabContents::RemoveNavigationObserver(WebNavigationObserver* observer) {
817 web_navigation_observers_.RemoveObserver(observer);
818}
819
[email protected]443b80e2010-12-14 00:42:23820void TabContents::SetIsCrashed(base::TerminationStatus status, int error_code) {
821 if (status == crashed_status_)
[email protected]d5f942ba2008-09-26 19:30:34822 return;
823
[email protected]443b80e2010-12-14 00:42:23824 crashed_status_ = status;
825 crashed_error_code_ = error_code;
[email protected]c9cd2222009-05-06 05:16:50826 NotifyNavigationStateChanged(INVALIDATE_TAB);
[email protected]d5f942ba2008-09-26 19:30:34827}
828
[email protected]744ef172009-10-16 21:53:46829void TabContents::PageActionStateChanged() {
830 NotifyNavigationStateChanged(TabContents::INVALIDATE_PAGE_ACTIONS);
[email protected]d7eaf572009-07-01 21:57:00831}
[email protected]ce5c4502009-05-06 16:46:11832
[email protected]d5f942ba2008-09-26 19:30:34833void TabContents::NotifyNavigationStateChanged(unsigned changed_flags) {
834 if (delegate_)
835 delegate_->NavigationStateChanged(this, changed_flags);
836}
837
[email protected]96d185d2009-04-24 03:28:54838void TabContents::DidBecomeSelected() {
839 controller_.SetActive(true);
[email protected]8cb5d5b2010-02-09 11:36:16840 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
841 if (rwhv) {
842 rwhv->DidBecomeSelected();
[email protected]789e9152009-08-04 21:59:43843#if defined(OS_MACOSX)
[email protected]8cb5d5b2010-02-09 11:36:16844 rwhv->SetActive(true);
[email protected]789e9152009-08-04 21:59:43845#endif
846 }
[email protected]96d185d2009-04-24 03:28:54847
[email protected]8cb5d5b2010-02-09 11:36:16848 WebCacheManager::GetInstance()->ObserveActivity(GetRenderProcessHost()->id());
[email protected]5ac20162010-11-24 23:33:11849 last_selected_time_ = base::TimeTicks::Now();
[email protected]61d68ef12011-01-13 14:02:56850#if defined(OS_CHROMEOS)
851 chromeos::LocaleChangeGuard::Check(this);
852#endif
[email protected]96d185d2009-04-24 03:28:54853}
854
[email protected]a94454b2010-12-16 04:16:03855void TabContents::FadeForInstant(bool animate) {
[email protected]1b072562010-12-13 19:37:45856 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
857 SkColor whitish = SkColorSetARGB(192, 255, 255, 255);
858 if (rwhv)
[email protected]a94454b2010-12-16 04:16:03859 rwhv->SetVisuallyDeemphasized(&whitish, animate);
[email protected]1b072562010-12-13 19:37:45860}
861
862void TabContents::CancelInstantFade() {
863 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
864 if (rwhv)
865 rwhv->SetVisuallyDeemphasized(NULL, false);
866}
867
[email protected]96d185d2009-04-24 03:28:54868void TabContents::WasHidden() {
869 if (!capturing_contents()) {
870 // |render_view_host()| can be NULL if the user middle clicks a link to open
871 // a tab in then background, then closes the tab before selecting it. This
872 // is because closing the tab calls TabContents::Destroy(), which removes
873 // the |render_view_host()|; then when we actually destroy the window,
874 // OnWindowPosChanged() notices and calls HideContents() (which calls us).
[email protected]8cb5d5b2010-02-09 11:36:16875 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
876 if (rwhv)
877 rwhv->WasHidden();
[email protected]96d185d2009-04-24 03:28:54878 }
879
880 NotificationService::current()->Notify(
881 NotificationType::TAB_CONTENTS_HIDDEN,
882 Source<TabContents>(this),
883 NotificationService::NoDetails());
884}
885
[email protected]d5f942ba2008-09-26 19:30:34886void TabContents::Activate() {
887 if (delegate_)
888 delegate_->ActivateContents(this);
889}
890
[email protected]ea42e7782010-08-23 23:58:12891void TabContents::Deactivate() {
892 if (delegate_)
893 delegate_->DeactivateContents(this);
894}
895
[email protected]96d185d2009-04-24 03:28:54896void TabContents::ShowContents() {
[email protected]8cb5d5b2010-02-09 11:36:16897 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
898 if (rwhv)
899 rwhv->DidBecomeSelected();
[email protected]96d185d2009-04-24 03:28:54900}
901
902void TabContents::HideContents() {
903 // TODO(pkasting): http://b/1239839 Right now we purposefully don't call
904 // our superclass HideContents(), because some callers want to be very picky
905 // about the order in which these get called. In addition to making the code
906 // here practically impossible to understand, this also means we end up
907 // calling TabContents::WasHidden() twice if callers call both versions of
[email protected]57c6a652009-05-04 07:58:34908 // HideContents() on a TabContents.
[email protected]96d185d2009-04-24 03:28:54909 WasHidden();
910}
911
[email protected]5aab5e22010-12-08 22:13:29912bool TabContents::NeedToFireBeforeUnload() {
913 // TODO(creis): Should we fire even for interstitial pages?
914 return notify_disconnection() &&
915 !showing_interstitial_page() &&
916 !render_view_host()->SuddenTerminationAllowed();
917}
918
[email protected]c0588052008-10-27 23:01:50919void TabContents::OpenURL(const GURL& url, const GURL& referrer,
[email protected]d5f942ba2008-09-26 19:30:34920 WindowOpenDisposition disposition,
921 PageTransition::Type transition) {
922 if (delegate_)
[email protected]c0588052008-10-27 23:01:50923 delegate_->OpenURLFromTab(this, url, referrer, disposition, transition);
[email protected]d5f942ba2008-09-26 19:30:34924}
925
[email protected]1ccb3568d2010-02-19 10:51:16926bool TabContents::NavigateToPendingEntry(
927 NavigationController::ReloadType reload_type) {
[email protected]876bc832010-09-07 16:29:54928 return NavigateToEntry(*controller_.pending_entry(), reload_type);
929}
[email protected]96d185d2009-04-24 03:28:54930
[email protected]876bc832010-09-07 16:29:54931bool TabContents::NavigateToEntry(
932 const NavigationEntry& entry,
933 NavigationController::ReloadType reload_type) {
[email protected]96d185d2009-04-24 03:28:54934 RenderViewHost* dest_render_view_host = render_manager_.Navigate(entry);
935 if (!dest_render_view_host)
936 return false; // Unable to create the desired render view host.
937
[email protected]770dd8b2010-05-24 18:11:39938 if (delegate_ && delegate_->ShouldEnablePreferredSizeNotifications()) {
939 dest_render_view_host->EnablePreferredSizeChangedMode(
940 kPreferredSizeWidth | kPreferredSizeHeightThisIsSlow);
941 }
[email protected]9fb325e2010-05-06 18:23:24942
[email protected]05fdd492010-11-15 17:52:07943 // For security, we should never send non-DOM-UI URLs to a DOM UI renderer.
944 // Double check that here.
[email protected]54ec6472010-04-09 19:39:58945 int enabled_bindings = dest_render_view_host->enabled_bindings();
[email protected]0352e5472010-04-29 00:11:40946 bool is_allowed_in_dom_ui_renderer =
[email protected]05fdd492010-11-15 17:52:07947 DOMUIFactory::IsURLAcceptableForDOMUI(profile(), entry.url());
[email protected]54ec6472010-04-09 19:39:58948 CHECK(!BindingsPolicy::is_dom_ui_enabled(enabled_bindings) ||
[email protected]0352e5472010-04-29 00:11:40949 is_allowed_in_dom_ui_renderer);
[email protected]54ec6472010-04-09 19:39:58950
[email protected]96d185d2009-04-24 03:28:54951 // Tell DevTools agent that it is attached prior to the navigation.
[email protected]d9f9b792009-06-24 13:17:12952 DevToolsManager* devtools_manager = DevToolsManager::GetInstance();
[email protected]7e8e6b62009-05-08 11:28:32953 if (devtools_manager) { // NULL in unit tests.
[email protected]3cc72b12010-03-18 23:03:00954 devtools_manager->OnNavigatingToPendingEntry(render_view_host(),
955 dest_render_view_host,
956 entry.url());
[email protected]7e8e6b62009-05-08 11:28:32957 }
[email protected]96d185d2009-04-24 03:28:54958
959 // Used for page load time metrics.
960 current_load_start_ = base::TimeTicks::Now();
961
962 // Navigate in the desired RenderViewHost.
[email protected]056de2d2009-06-26 16:41:34963 ViewMsg_Navigate_Params navigate_params;
[email protected]876bc832010-09-07 16:29:54964 MakeNavigateParams(entry, controller_, reload_type, &navigate_params);
[email protected]52c68652010-12-07 17:47:04965 if (delegate_) {
966 navigate_params.extra_headers =
967 delegate_->GetNavigationHeaders(navigate_params.url);
968 }
[email protected]056de2d2009-06-26 16:41:34969 dest_render_view_host->Navigate(navigate_params);
[email protected]96d185d2009-04-24 03:28:54970
971 if (entry.page_id() == -1) {
972 // HACK!! This code suppresses javascript: URLs from being added to
973 // session history, which is what we want to do for javascript: URLs that
974 // do not generate content. What we really need is a message from the
975 // renderer telling us that a new page was not created. The same message
976 // could be used for mailto: URLs and the like.
977 if (entry.url().SchemeIs(chrome::kJavaScriptScheme))
978 return false;
979 }
980
[email protected]3c9e1872010-11-18 16:17:49981 // Notify observers about navigation.
982 FOR_EACH_OBSERVER(WebNavigationObserver, web_navigation_observers_,
983 NavigateToPendingEntry());
[email protected]96d185d2009-04-24 03:28:54984
[email protected]1ccb3568d2010-02-19 10:51:16985 if (reload_type != NavigationController::NO_RELOAD &&
986 !profile()->IsOffTheRecord()) {
[email protected]0189bc722009-08-28 21:56:48987 FaviconService* favicon_service =
988 profile()->GetFaviconService(Profile::IMPLICIT_ACCESS);
989 if (favicon_service)
990 favicon_service->SetFaviconOutOfDateForPage(entry.url());
[email protected]96d185d2009-04-24 03:28:54991 }
992
[email protected]d5f942ba2008-09-26 19:30:34993 return true;
994}
995
[email protected]96d185d2009-04-24 03:28:54996void TabContents::Stop() {
997 render_manager_.Stop();
[email protected]12802702010-07-09 19:43:09998 printing_->Stop();
[email protected]96d185d2009-04-24 03:28:54999}
1000
[email protected]96d185d2009-04-24 03:28:541001void TabContents::DisassociateFromPopupCount() {
1002 render_view_host()->DisassociateFromPopupCount();
1003}
1004
1005TabContents* TabContents::Clone() {
1006 // We create a new SiteInstance so that the new tab won't share processes
1007 // with the old one. This can be changed in the future if we need it to share
1008 // processes for some reason.
[email protected]420ae012009-04-24 05:16:321009 TabContents* tc = new TabContents(profile(),
[email protected]96d185d2009-04-24 03:28:541010 SiteInstance::CreateSiteInstance(profile()),
[email protected]6ee12c42010-09-14 09:36:071011 MSG_ROUTING_NONE, this, NULL);
[email protected]96d185d2009-04-24 03:28:541012 tc->controller().CopyStateFrom(controller_);
[email protected]98aa0b52010-05-06 17:03:081013 tc->extension_app_ = extension_app_;
[email protected]98aa0b52010-05-06 17:03:081014 tc->extension_app_icon_ = extension_app_icon_;
[email protected]96d185d2009-04-24 03:28:541015 return tc;
1016}
1017
[email protected]4d677202009-07-19 07:37:121018void TabContents::ShowPageInfo(const GURL& url,
1019 const NavigationEntry::SSLStatus& ssl,
1020 bool show_history) {
1021 if (!delegate_)
1022 return;
1023
[email protected]bb678332009-07-21 00:15:501024 delegate_->ShowPageInfo(profile(), url, ssl, show_history);
[email protected]4d677202009-07-19 07:37:121025}
1026
[email protected]ef0d04c2010-12-15 23:34:211027void TabContents::SaveFavicon() {
1028 NavigationEntry* entry = controller_.GetActiveEntry();
1029 if (!entry || entry->url().is_empty())
1030 return;
1031
1032 // Make sure the page is in history, otherwise adding the favicon does
1033 // nothing.
1034 HistoryService* history = profile()->GetOriginalProfile()->GetHistoryService(
1035 Profile::IMPLICIT_ACCESS);
1036 if (!history)
1037 return;
1038 history->AddPageNoVisitForBookmark(entry->url());
1039
1040 FaviconService* service = profile()->GetOriginalProfile()->GetFaviconService(
1041 Profile::IMPLICIT_ACCESS);
1042 if (!service)
1043 return;
1044 const NavigationEntry::FaviconStatus& favicon(entry->favicon());
1045 if (!favicon.is_valid() || favicon.url().is_empty() ||
1046 favicon.bitmap().empty()) {
1047 return;
1048 }
1049 std::vector<unsigned char> image_data;
1050 gfx::PNGCodec::EncodeBGRASkBitmap(favicon.bitmap(), false, &image_data);
1051 service->SetFavicon(entry->url(), favicon.url(), image_data);
1052}
1053
initial.commit09911bf2008-07-26 23:55:291054ConstrainedWindow* TabContents::CreateConstrainedDialog(
[email protected]e8382172009-06-19 22:16:281055 ConstrainedWindowDelegate* delegate) {
initial.commit09911bf2008-07-26 23:55:291056 ConstrainedWindow* window =
[email protected]e8382172009-06-19 22:16:281057 ConstrainedWindow::CreateConstrainedDialog(this, delegate);
initial.commit09911bf2008-07-26 23:55:291058 child_windows_.push_back(window);
[email protected]fa1cf0b82010-01-15 21:49:441059
1060 if (child_windows_.size() == 1) {
1061 window->ShowConstrainedWindow();
1062 BlockTabContent(true);
1063 }
1064
initial.commit09911bf2008-07-26 23:55:291065 return window;
1066}
1067
[email protected]fa1cf0b82010-01-15 21:49:441068void TabContents::BlockTabContent(bool blocked) {
[email protected]8dccd7cb2010-02-25 05:19:551069 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
[email protected]1b072562010-12-13 19:37:451070 // 70% opaque grey.
1071 SkColor greyish = SkColorSetARGB(178, 0, 0, 0);
[email protected]8dccd7cb2010-02-25 05:19:551072 if (rwhv)
[email protected]1b072562010-12-13 19:37:451073 rwhv->SetVisuallyDeemphasized(blocked ? &greyish : NULL, false);
[email protected]fa1cf0b82010-01-15 21:49:441074 render_view_host()->set_ignore_input_events(blocked);
1075 if (delegate_)
1076 delegate_->SetTabContentBlocked(this, blocked);
1077}
1078
initial.commit09911bf2008-07-26 23:55:291079void TabContents::AddNewContents(TabContents* new_contents,
1080 WindowOpenDisposition disposition,
1081 const gfx::Rect& initial_pos,
[email protected]7e7414ae2010-01-26 20:19:291082 bool user_gesture) {
[email protected]c65e2f152010-10-14 15:30:401083 if (all_contents_blocked_) {
1084 if (!blocked_contents_)
1085 blocked_contents_ = new BlockedContentContainer(this);
1086 blocked_contents_->AddTabContents(
1087 new_contents, disposition, initial_pos, user_gesture);
1088 return;
1089 }
1090
initial.commit09911bf2008-07-26 23:55:291091 if (!delegate_)
1092 return;
1093
[email protected]35f7d212009-04-29 21:19:271094 if ((disposition == NEW_POPUP) && !user_gesture &&
1095 !CommandLine::ForCurrentProcess()->HasSwitch(
1096 switches::kDisablePopupBlocking)) {
[email protected]2c4410d2009-05-06 23:46:221097 // Unrequested popups from normal pages are constrained unless they're in
[email protected]b6c874582009-05-08 19:38:311098 // the whitelist. The popup owner will handle checking this.
[email protected]7e7414ae2010-01-26 20:19:291099 delegate_->GetConstrainingContents(this)->AddPopup(
1100 new_contents, initial_pos);
[email protected]b6c874582009-05-08 19:38:311101 } else {
[email protected]0aa55312008-10-17 21:53:081102 new_contents->DisassociateFromPopupCount();
initial.commit09911bf2008-07-26 23:55:291103 delegate_->AddNewContents(this, new_contents, disposition, initial_pos,
1104 user_gesture);
[email protected]7f37dd92009-09-15 16:35:491105 NotificationService::current()->Notify(
1106 NotificationType::TAB_ADDED,
1107 Source<TabContentsDelegate>(delegate_),
1108 Details<TabContents>(this));
initial.commit09911bf2008-07-26 23:55:291109 }
initial.commit09911bf2008-07-26 23:55:291110
[email protected]8cc8d492010-02-02 10:40:491111 // TODO(pkasting): Why is this necessary?
[email protected]c65e2f152010-10-14 15:30:401112 PopupNotificationVisibilityChanged(blocked_contents_ != NULL);
initial.commit09911bf2008-07-26 23:55:291113}
1114
[email protected]bcd9dc42010-01-23 16:38:181115bool TabContents::ExecuteCode(int request_id, const std::string& extension_id,
[email protected]20ad2692009-11-20 18:27:201116 bool is_js_code, const std::string& code_string,
1117 bool all_frames) {
[email protected]ebd0eae72010-02-16 23:50:511118 RenderViewHost* host = render_view_host();
1119 if (!host)
1120 return false;
1121
1122 return host->Send(new ViewMsg_ExecuteCode(host->routing_id(),
[email protected]be7e5cb2010-10-04 12:53:171123 ViewMsg_ExecuteCode_Params(request_id, extension_id,
[email protected]ebd0eae72010-02-16 23:50:511124 is_js_code, code_string, all_frames)));
[email protected]912256b32009-09-18 09:47:351125}
1126
[email protected]96d185d2009-04-24 03:28:541127void TabContents::PopupNotificationVisibilityChanged(bool visible) {
[email protected]8cc8d492010-02-02 10:40:491128 if (is_being_destroyed_)
1129 return;
[email protected]9fb83e82010-07-02 18:24:551130 content_settings_delegate_->SetPopupsBlocked(visible);
[email protected]8cc8d492010-02-02 10:40:491131 if (!dont_notify_render_view_)
[email protected]e8345242010-05-06 03:00:401132 render_view_host()->AllowScriptToClose(!visible);
[email protected]96d185d2009-04-24 03:28:541133}
1134
[email protected]5c9e97a2009-09-09 23:48:301135gfx::NativeView TabContents::GetContentNativeView() const {
[email protected]96d185d2009-04-24 03:28:541136 return view_->GetContentNativeView();
1137}
1138
1139gfx::NativeView TabContents::GetNativeView() const {
1140 return view_->GetNativeView();
1141}
1142
1143void TabContents::GetContainerBounds(gfx::Rect *out) const {
1144 view_->GetContainerBounds(out);
1145}
1146
1147void TabContents::Focus() {
1148 view_->Focus();
1149}
1150
[email protected]90daadb42009-06-08 21:27:281151void TabContents::FocusThroughTabTraversal(bool reverse) {
[email protected]7e383692009-06-12 19:14:541152 if (showing_interstitial_page()) {
1153 render_manager_.interstitial_page()->FocusThroughTabTraversal(reverse);
1154 return;
1155 }
[email protected]96d185d2009-04-24 03:28:541156 render_view_host()->SetInitialFocus(reverse);
1157}
1158
1159bool TabContents::FocusLocationBarByDefault() {
1160 DOMUI* dom_ui = GetDOMUIForCurrentState();
1161 if (dom_ui)
1162 return dom_ui->focus_location_bar_by_default();
[email protected]3e3f0eb2009-06-22 18:33:431163 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]e0d481582009-09-15 21:06:251164 if (entry && entry->url() == GURL(chrome::kAboutBlankURL))
[email protected]3e3f0eb2009-06-22 18:33:431165 return true;
[email protected]96d185d2009-04-24 03:28:541166 return false;
1167}
1168
[email protected]a26dc362010-04-23 01:48:581169void TabContents::SetFocusToLocationBar(bool select_all) {
[email protected]a11aaf22010-03-30 00:03:381170 if (delegate())
[email protected]a26dc362010-04-23 01:48:581171 delegate()->SetFocusToLocationBar(select_all);
[email protected]a11aaf22010-03-30 00:03:381172}
1173
[email protected]616ed5a2008-11-21 22:27:241174void TabContents::AddInfoBar(InfoBarDelegate* delegate) {
[email protected]f7019302010-03-26 19:58:321175 if (delegate_ && !delegate_->infobars_enabled()) {
1176 delegate->InfoBarClosed();
1177 return;
1178 }
1179
[email protected]616ed5a2008-11-21 22:27:241180 // Look through the existing InfoBarDelegates we have for a match. If we've
1181 // already got one that matches, then we don't add the new one.
[email protected]f86a07022008-11-25 01:06:051182 for (int i = 0; i < infobar_delegate_count(); ++i) {
[email protected]6d27a7b2009-12-18 23:25:451183 if (GetInfoBarDelegateAt(i)->EqualsDelegate(delegate)) {
1184 // Tell the new infobar to close so that it can clean itself up.
1185 delegate->InfoBarClosed();
[email protected]616ed5a2008-11-21 22:27:241186 return;
[email protected]6d27a7b2009-12-18 23:25:451187 }
[email protected]616ed5a2008-11-21 22:27:241188 }
1189
1190 infobar_delegates_.push_back(delegate);
[email protected]bfd04a62009-02-01 18:16:561191 NotificationService::current()->Notify(
1192 NotificationType::TAB_CONTENTS_INFOBAR_ADDED,
1193 Source<TabContents>(this),
1194 Details<InfoBarDelegate>(delegate));
[email protected]616ed5a2008-11-21 22:27:241195
1196 // Add ourselves as an observer for navigations the first time a delegate is
1197 // added. We use this notification to expire InfoBars that need to expire on
1198 // page transitions.
1199 if (infobar_delegates_.size() == 1) {
[email protected]bfd04a62009-02-01 18:16:561200 registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED,
[email protected]ce3fa3c2009-04-20 19:55:571201 Source<NavigationController>(&controller_));
[email protected]616ed5a2008-11-21 22:27:241202 }
1203}
1204
1205void TabContents::RemoveInfoBar(InfoBarDelegate* delegate) {
[email protected]f7019302010-03-26 19:58:321206 if (delegate_ && !delegate_->infobars_enabled()) {
1207 return;
1208 }
1209
[email protected]616ed5a2008-11-21 22:27:241210 std::vector<InfoBarDelegate*>::iterator it =
1211 find(infobar_delegates_.begin(), infobar_delegates_.end(), delegate);
1212 if (it != infobar_delegates_.end()) {
1213 InfoBarDelegate* delegate = *it;
[email protected]bfd04a62009-02-01 18:16:561214 NotificationService::current()->Notify(
1215 NotificationType::TAB_CONTENTS_INFOBAR_REMOVED,
1216 Source<TabContents>(this),
1217 Details<InfoBarDelegate>(delegate));
[email protected]616ed5a2008-11-21 22:27:241218
[email protected]cdadf6f2010-08-20 16:36:081219 // Just to be safe, make sure the delegate was not removed by an observer.
1220 it = find(infobar_delegates_.begin(), infobar_delegates_.end(), delegate);
1221 if (it != infobar_delegates_.end()) {
1222 infobar_delegates_.erase(it);
1223 // Remove ourselves as an observer if we are tracking no more InfoBars.
1224 if (infobar_delegates_.empty()) {
1225 registrar_.Remove(this, NotificationType::NAV_ENTRY_COMMITTED,
1226 Source<NavigationController>(&controller_));
1227 }
1228 } else {
1229 // If you hit this NOTREACHED, please comment in bug
1230 // http://crbug.com/50428 how you got there.
1231 NOTREACHED();
[email protected]6a02963e2009-01-06 16:58:031232 }
[email protected]616ed5a2008-11-21 22:27:241233 }
1234}
[email protected]3edd9522009-03-04 22:19:411235
[email protected]f81821c2009-08-07 01:29:471236void TabContents::ReplaceInfoBar(InfoBarDelegate* old_delegate,
1237 InfoBarDelegate* new_delegate) {
[email protected]f7019302010-03-26 19:58:321238 if (delegate_ && !delegate_->infobars_enabled()) {
1239 new_delegate->InfoBarClosed();
1240 return;
1241 }
1242
[email protected]f81821c2009-08-07 01:29:471243 std::vector<InfoBarDelegate*>::iterator it =
1244 find(infobar_delegates_.begin(), infobar_delegates_.end(), old_delegate);
1245 DCHECK(it != infobar_delegates_.end());
1246
1247 // Notify the container about the change of plans.
1248 scoped_ptr<std::pair<InfoBarDelegate*, InfoBarDelegate*> > details(
1249 new std::pair<InfoBarDelegate*, InfoBarDelegate*>(
1250 old_delegate, new_delegate));
1251 NotificationService::current()->Notify(
1252 NotificationType::TAB_CONTENTS_INFOBAR_REPLACED,
1253 Source<TabContents>(this),
1254 Details<std::pair<InfoBarDelegate*, InfoBarDelegate*> >(details.get()));
1255
[email protected]cdadf6f2010-08-20 16:36:081256 // Just to be safe, make sure the delegate was not removed by an observer.
1257 it = find(infobar_delegates_.begin(), infobar_delegates_.end(), old_delegate);
1258 if (it != infobar_delegates_.end()) {
1259 // Remove the old one.
1260 infobar_delegates_.erase(it);
1261 } else {
1262 // If you hit this NOTREACHED, please comment in bug
1263 // http://crbug.com/50428 how you got there.
1264 NOTREACHED();
1265 }
[email protected]f81821c2009-08-07 01:29:471266
1267 // Add the new one.
[email protected]cdadf6f2010-08-20 16:36:081268 DCHECK(find(infobar_delegates_.begin(),
1269 infobar_delegates_.end(), new_delegate) ==
1270 infobar_delegates_.end());
[email protected]f81821c2009-08-07 01:29:471271 infobar_delegates_.push_back(new_delegate);
1272}
1273
[email protected]20ede072009-10-06 00:28:351274bool TabContents::ShouldShowBookmarkBar() {
[email protected]aa9196f2009-10-22 16:20:271275 if (showing_interstitial_page())
1276 return false;
1277
[email protected]c8f74942010-09-03 10:04:141278 // Do not show bookmarks bar if bookmarks aren't enabled.
1279 if (!browser_defaults::bookmarks_enabled)
1280 return false;
1281
[email protected]96d185d2009-04-24 03:28:541282 // See GetDOMUIForCurrentState() comment for more info. This case is very
1283 // similar, but for non-first loads, we want to use the committed entry. This
1284 // is so the bookmarks bar disappears at the same time the page does.
1285 if (controller_.GetLastCommittedEntry()) {
1286 // Not the first load, always use the committed DOM UI.
[email protected]d6219912010-11-17 19:47:371287 return (render_manager_.dom_ui() == NULL) ?
1288 false : render_manager_.dom_ui()->force_bookmark_bar_visible();
[email protected]96d185d2009-04-24 03:28:541289 }
1290
1291 // When it's the first load, we know either the pending one or the committed
1292 // one will have the DOM UI in it (see GetDOMUIForCurrentState), and only one
1293 // of them will be valid, so we can just check both.
1294 if (render_manager_.pending_dom_ui())
1295 return render_manager_.pending_dom_ui()->force_bookmark_bar_visible();
[email protected]d6219912010-11-17 19:47:371296 return (render_manager_.dom_ui() == NULL) ?
1297 false : render_manager_.dom_ui()->force_bookmark_bar_visible();
[email protected]96d185d2009-04-24 03:28:541298}
1299
[email protected]3edd9522009-03-04 22:19:411300void TabContents::ToolbarSizeChanged(bool is_animating) {
1301 TabContentsDelegate* d = delegate();
1302 if (d)
1303 d->ToolbarSizeChanged(this, is_animating);
1304}
[email protected]616ed5a2008-11-21 22:27:241305
[email protected]fc6fb7fb2009-11-07 02:35:041306bool TabContents::CanDownload(int request_id) {
1307 TabContentsDelegate* d = delegate();
1308 if (d)
1309 return d->CanDownload(request_id);
1310 return true;
1311}
1312
[email protected]d5f942ba2008-09-26 19:30:341313void TabContents::OnStartDownload(DownloadItem* download) {
1314 DCHECK(download);
[email protected]d5f942ba2008-09-26 19:30:341315
1316 // Download in a constrained popup is shown in the tab that opened it.
[email protected]b6c874582009-05-08 19:38:311317 TabContents* tab_contents = delegate()->GetConstrainingContents(this);
[email protected]d5f942ba2008-09-26 19:30:341318
[email protected]5b928b62010-08-03 11:46:511319 if (tab_contents && tab_contents->delegate())
1320 tab_contents->delegate()->OnStartDownload(download, this);
[email protected]3edd9522009-03-04 22:19:411321}
[email protected]3edd9522009-03-04 22:19:411322
[email protected]d5f942ba2008-09-26 19:30:341323void TabContents::WillClose(ConstrainedWindow* window) {
[email protected]d6219912010-11-17 19:47:371324 ConstrainedWindowList::iterator i(
1325 std::find(child_windows_.begin(), child_windows_.end(), window));
1326 bool removed_topmost_window = i == child_windows_.begin();
1327 if (i != child_windows_.end())
1328 child_windows_.erase(i);
1329 if (child_windows_.empty()) {
[email protected]fa1cf0b82010-01-15 21:49:441330 BlockTabContent(false);
[email protected]d6219912010-11-17 19:47:371331 } else {
1332 if (removed_topmost_window)
1333 child_windows_[0]->ShowConstrainedWindow();
1334 BlockTabContent(true);
[email protected]fa1cf0b82010-01-15 21:49:441335 }
[email protected]66ba4932009-06-04 19:22:131336}
[email protected]d5f942ba2008-09-26 19:30:341337
[email protected]c65e2f152010-10-14 15:30:401338void TabContents::WillCloseBlockedContentContainer(
1339 BlockedContentContainer* container) {
1340 DCHECK(blocked_contents_ == container);
1341 blocked_contents_ = NULL;
[email protected]8cc8d492010-02-02 10:40:491342 PopupNotificationVisibilityChanged(false);
[email protected]d5f942ba2008-09-26 19:30:341343}
1344
[email protected]d5f942ba2008-09-26 19:30:341345void TabContents::DidMoveOrResize(ConstrainedWindow* window) {
[email protected]332af7732009-03-11 13:21:351346#if defined(OS_WIN)
[email protected]92edc472009-02-10 20:32:061347 UpdateWindow(GetNativeView());
[email protected]332af7732009-03-11 13:21:351348#endif
[email protected]d5f942ba2008-09-26 19:30:341349}
1350
[email protected]c1dc9f132009-11-20 23:13:211351void TabContents::StartFinding(string16 search_string,
[email protected]e491f1c2009-05-22 20:28:121352 bool forward_direction,
1353 bool case_sensitive) {
[email protected]c1dc9f132009-11-20 23:13:211354 // If search_string is empty, it means FindNext was pressed with a keyboard
[email protected]6e95706d2009-04-23 22:55:011355 // shortcut so unless we have something to search for we return early.
[email protected]c1dc9f132009-11-20 23:13:211356 if (search_string.empty() && find_text_.empty()) {
[email protected]e5d2f7132010-04-09 18:13:531357 string16 last_search_prepopulate_text =
1358 FindBarState::GetLastPrepopulateText(profile());
1359
[email protected]87fae512009-12-11 02:20:211360 // Try the last thing we searched for on this tab, then the last thing
1361 // searched for on any tab.
1362 if (!previous_find_text_.empty())
1363 search_string = previous_find_text_;
[email protected]e5d2f7132010-04-09 18:13:531364 else if (!last_search_prepopulate_text.empty())
1365 search_string = last_search_prepopulate_text;
[email protected]87fae512009-12-11 02:20:211366 else
[email protected]e491f1c2009-05-22 20:28:121367 return;
[email protected]e491f1c2009-05-22 20:28:121368 }
[email protected]6e95706d2009-04-23 22:55:011369
[email protected]101c90e2009-10-26 21:14:441370 // Keep track of the previous search.
1371 previous_find_text_ = find_text_;
1372
[email protected]6e95706d2009-04-23 22:55:011373 // This is a FindNext operation if we are searching for the same text again,
1374 // or if the passed in search text is empty (FindNext keyboard shortcut). The
1375 // exception to this is if the Find was aborted (then we don't want FindNext
1376 // because the highlighting has been cleared and we need it to reappear). We
1377 // therefore treat FindNext after an aborted Find operation as a full fledged
1378 // Find.
[email protected]c1dc9f132009-11-20 23:13:211379 bool find_next = (find_text_ == search_string || search_string.empty()) &&
[email protected]e491f1c2009-05-22 20:28:121380 (last_search_case_sensitive_ == case_sensitive) &&
[email protected]6e95706d2009-04-23 22:55:011381 !find_op_aborted_;
1382 if (!find_next)
1383 current_find_request_id_ = find_request_id_counter_++;
1384
[email protected]c1dc9f132009-11-20 23:13:211385 if (!search_string.empty())
1386 find_text_ = search_string;
[email protected]e491f1c2009-05-22 20:28:121387 last_search_case_sensitive_ = case_sensitive;
[email protected]6e95706d2009-04-23 22:55:011388
1389 find_op_aborted_ = false;
1390
1391 // Keep track of what the last search was across the tabs.
[email protected]e5d2f7132010-04-09 18:13:531392 FindBarState* find_bar_state = profile()->GetFindBarState();
1393 find_bar_state->set_last_prepopulate_text(find_text_);
[email protected]6e95706d2009-04-23 22:55:011394 render_view_host()->StartFinding(current_find_request_id_,
1395 find_text_,
1396 forward_direction,
[email protected]e491f1c2009-05-22 20:28:121397 case_sensitive,
[email protected]6e95706d2009-04-23 22:55:011398 find_next);
1399}
1400
[email protected]5bc8fe92010-03-11 18:19:001401void TabContents::StopFinding(
1402 FindBarController::SelectionAction selection_action) {
[email protected]d6e8d332010-06-04 22:26:011403 if (selection_action == FindBarController::kClearSelection) {
1404 // kClearSelection means the find string has been cleared by the user, but
1405 // the UI has not been dismissed. In that case we want to clear the
1406 // previously remembered search (http://crbug.com/42639).
1407 previous_find_text_ = string16();
1408 } else {
[email protected]2d2dffc2009-06-09 21:41:001409 find_ui_active_ = false;
[email protected]d6e8d332010-06-04 22:26:011410 if (!find_text_.empty())
1411 previous_find_text_ = find_text_;
1412 }
[email protected]101c90e2009-10-26 21:14:441413 find_text_.clear();
[email protected]6e95706d2009-04-23 22:55:011414 find_op_aborted_ = true;
[email protected]e491f1c2009-05-22 20:28:121415 last_search_result_ = FindNotificationDetails();
[email protected]5bc8fe92010-03-11 18:19:001416 render_view_host()->StopFinding(selection_action);
[email protected]6e95706d2009-04-23 22:55:011417}
1418
[email protected]420ae012009-04-24 05:16:321419void TabContents::OnSavePage() {
1420 // If we can not save the page, try to download it.
1421 if (!SavePackage::IsSavableContents(contents_mime_type())) {
1422 DownloadManager* dlm = profile()->GetDownloadManager();
1423 const GURL& current_page_url = GetURL();
1424 if (dlm && current_page_url.is_valid())
[email protected]d82ed61e2009-06-16 02:46:221425 dlm->DownloadUrl(current_page_url, GURL(), "", this);
[email protected]420ae012009-04-24 05:16:321426 return;
1427 }
1428
1429 Stop();
1430
1431 // Create the save package and possibly prompt the user for the name to save
1432 // the page as. The user prompt is an asynchronous operation that runs on
1433 // another thread.
[email protected]d82ed61e2009-06-16 02:46:221434 save_package_ = new SavePackage(this);
[email protected]420ae012009-04-24 05:16:321435 save_package_->GetSaveInfo();
1436}
1437
1438// Used in automated testing to bypass prompting the user for file names.
1439// Instead, the names and paths are hard coded rather than running them through
1440// file name sanitation and extension / mime checking.
[email protected]828cabe2009-09-26 22:47:111441bool TabContents::SavePage(const FilePath& main_file, const FilePath& dir_path,
[email protected]420ae012009-04-24 05:16:321442 SavePackage::SavePackageType save_type) {
1443 // Stop the page from navigating.
1444 Stop();
1445
[email protected]828cabe2009-09-26 22:47:111446 save_package_ = new SavePackage(this, save_type, main_file, dir_path);
[email protected]cdf6cc82009-08-04 21:48:591447 return save_package_->Init();
[email protected]420ae012009-04-24 05:16:321448}
1449
[email protected]89d6e6e2009-12-03 23:06:511450void TabContents::EmailPageLocation() {
1451 std::string title = EscapeQueryParamValue(UTF16ToUTF8(GetTitle()), false);
1452 std::string page_url = EscapeQueryParamValue(GetURL().spec(), false);
1453 std::string mailto = std::string("mailto:?subject=Fwd:%20") +
1454 title + "&body=%0A%0A" + page_url;
1455 platform_util::OpenExternal(GURL(mailto));
1456}
1457
[email protected]420ae012009-04-24 05:16:321458void TabContents::PrintPreview() {
[email protected]dbeebd52010-11-16 20:34:161459 if (CommandLine::ForCurrentProcess()->HasSwitch(
1460 switches::kEnablePrintPreview)) {
1461 if (showing_interstitial_page())
1462 return;
1463 render_view_host()->PrintPreview();
1464 } else {
1465 PrintNow();
1466 }
[email protected]420ae012009-04-24 05:16:321467}
1468
1469bool TabContents::PrintNow() {
1470 // We can't print interstitial page for now.
1471 if (showing_interstitial_page())
1472 return false;
1473
1474 return render_view_host()->PrintPages();
1475}
1476
[email protected]82270452009-06-19 15:58:011477void TabContents::PrintingDone(int document_cookie, bool success) {
1478 render_view_host()->PrintingDone(document_cookie, success);
1479}
1480
[email protected]420ae012009-04-24 05:16:321481bool TabContents::IsActiveEntry(int32 page_id) {
1482 NavigationEntry* active_entry = controller_.GetActiveEntry();
1483 return (active_entry != NULL &&
1484 active_entry->site_instance() == GetSiteInstance() &&
1485 active_entry->page_id() == page_id);
1486}
1487
[email protected]b2fe07d12010-02-09 14:38:081488void TabContents::SetOverrideEncoding(const std::string& encoding) {
[email protected]8cb5d5b2010-02-09 11:36:161489 set_encoding(encoding);
1490 render_view_host()->SetPageEncoding(encoding);
1491}
1492
[email protected]b2fe07d12010-02-09 14:38:081493void TabContents::ResetOverrideEncoding() {
[email protected]8cb5d5b2010-02-09 11:36:161494 reset_encoding();
1495 render_view_host()->ResetPageEncodingToDefault();
1496}
1497
1498void TabContents::WindowMoveOrResizeStarted() {
1499 render_view_host()->WindowMoveOrResizeStarted();
1500}
1501
[email protected]c65e2f152010-10-14 15:30:401502void TabContents::SetAllContentsBlocked(bool value) {
1503 if (all_contents_blocked_ == value)
1504 return;
1505
1506 all_contents_blocked_ = value;
1507 if (!all_contents_blocked_ && blocked_contents_) {
1508 std::vector<TabContents*> blocked;
1509 blocked_contents_->GetBlockedContents(&blocked);
1510 for (size_t i = 0; i < blocked.size(); ++i)
1511 blocked_contents_->LaunchForContents(blocked[i]);
1512 }
1513}
1514
[email protected]3f8eb7f2009-10-02 23:58:051515void TabContents::LogNewTabTime(const std::string& event_name) {
1516 // Not all new tab pages get timed. In those cases, we don't have a
1517 // new_tab_start_time_.
1518 if (new_tab_start_time_.is_null())
1519 return;
1520
1521 base::TimeDelta duration = base::TimeTicks::Now() - new_tab_start_time_;
[email protected]84abba62009-10-07 17:01:441522 MetricEventDurationDetails details(event_name,
1523 static_cast<int>(duration.InMilliseconds()));
1524
[email protected]7ab1e7d62009-10-14 23:32:011525 if (event_name == "Tab.NewTabScriptStart") {
1526 UMA_HISTOGRAM_TIMES("Tab.NewTabScriptStart", duration);
1527 } else if (event_name == "Tab.NewTabDOMContentLoaded") {
1528 UMA_HISTOGRAM_TIMES("Tab.NewTabDOMContentLoaded", duration);
1529 } else if (event_name == "Tab.NewTabOnload") {
1530 UMA_HISTOGRAM_TIMES("Tab.NewTabOnload", duration);
[email protected]3f8eb7f2009-10-02 23:58:051531 // The new tab page has finished loading; reset it.
1532 new_tab_start_time_ = base::TimeTicks();
[email protected]84abba62009-10-07 17:01:441533 } else {
1534 NOTREACHED();
[email protected]3f8eb7f2009-10-02 23:58:051535 }
[email protected]84abba62009-10-07 17:01:441536 NotificationService::current()->Notify(
1537 NotificationType::METRIC_EVENT_DURATION,
1538 Source<TabContents>(this),
1539 Details<MetricEventDurationDetails>(&details));
[email protected]3f8eb7f2009-10-02 23:58:051540}
1541
[email protected]7ab1e7d62009-10-14 23:32:011542void TabContents::OnCloseStarted() {
1543 if (tab_close_start_time_.is_null())
1544 tab_close_start_time_ = base::TimeTicks::Now();
1545}
1546
[email protected]46624bf2010-06-09 16:04:191547bool TabContents::ShouldAcceptDragAndDrop() const {
1548#if defined(OS_CHROMEOS)
1549 // ChromeOS panels (pop-ups) do not take drag-n-drop.
1550 // See http://crosbug.com/2413
[email protected]b83af492010-10-09 03:21:201551 if (delegate() && delegate()->IsPopup(this))
1552 return false;
1553 return true;
[email protected]46624bf2010-06-09 16:04:191554#else
1555 return true;
1556#endif
1557}
1558
[email protected]ec0b6c42010-08-26 03:16:581559void TabContents::UpdateHistoryForNavigation(
1560 scoped_refptr<history::HistoryAddPageArgs> add_page_args) {
1561 if (profile()->IsOffTheRecord())
1562 return;
1563
1564 // Add to history service.
1565 HistoryService* hs = profile()->GetHistoryService(Profile::IMPLICIT_ACCESS);
1566 if (hs)
1567 hs->AddPage(*add_page_args);
1568}
1569
1570void TabContents::UpdateHistoryPageTitle(const NavigationEntry& entry) {
1571 if (profile()->IsOffTheRecord())
1572 return;
1573
1574 HistoryService* hs = profile()->GetHistoryService(Profile::IMPLICIT_ACCESS);
1575 if (hs)
1576 hs->SetPageTitle(entry.virtual_url(), entry.title());
1577}
1578
[email protected]d0b8d092010-10-25 04:05:171579double TabContents::GetZoomLevel() const {
[email protected]b75b8292010-10-01 07:28:251580 HostZoomMap* zoom_map = profile()->GetHostZoomMap();
1581 if (!zoom_map)
[email protected]d0b8d092010-10-25 04:05:171582 return 0;
[email protected]b75b8292010-10-01 07:28:251583
1584 double zoom_level;
1585 if (temporary_zoom_settings_) {
1586 zoom_level = zoom_map->GetTemporaryZoomLevel(
1587 GetRenderProcessHost()->id(), render_view_host()->routing_id());
1588 } else {
1589 zoom_level = zoom_map->GetZoomLevel(GetURL());
1590 }
[email protected]d0b8d092010-10-25 04:05:171591 return zoom_level;
1592}
[email protected]b75b8292010-10-01 07:28:251593
[email protected]d0b8d092010-10-25 04:05:171594int TabContents::GetZoomPercent(bool* enable_increment,
1595 bool* enable_decrement) {
1596 *enable_decrement = *enable_increment = false;
[email protected]b75b8292010-10-01 07:28:251597 int percent = static_cast<int>(
[email protected]d0b8d092010-10-25 04:05:171598 WebKit::WebView::zoomLevelToZoomFactor(GetZoomLevel()) * 100);
[email protected]b75b8292010-10-01 07:28:251599 *enable_decrement = percent > minimum_zoom_percent_;
1600 *enable_increment = percent < maximum_zoom_percent_;
1601 return percent;
1602}
1603
[email protected]3c733bde2010-12-21 19:56:311604void TabContents::ViewSource() {
[email protected]1788e772010-12-15 16:40:501605 if (!delegate_)
1606 return;
1607
1608 NavigationEntry* active_entry = controller().GetActiveEntry();
1609 if (!active_entry)
1610 return;
1611
1612 delegate_->ViewSourceForTab(this, active_entry->url());
[email protected]77d8d622010-12-15 10:30:121613}
1614
[email protected]724159a2010-12-30 01:11:181615void TabContents::OnDidStartProvisionalLoadForFrame(int64 frame_id,
1616 bool is_main_frame,
1617 const GURL& url) {
1618 bool is_error_page = (url.spec() == chrome::kUnreachableWebDataURL);
1619 GURL validated_url(url);
1620 render_view_host()->FilterURL(ChildProcessSecurityPolicy::GetInstance(),
1621 GetRenderProcessHost()->id(), &validated_url);
1622
1623 ProvisionalLoadDetails details(
1624 is_main_frame,
1625 controller_.IsURLInPageNavigation(validated_url),
1626 validated_url, std::string(), false, is_error_page, frame_id);
1627 NotificationService::current()->Notify(
1628 NotificationType::FRAME_PROVISIONAL_LOAD_START,
1629 Source<NavigationController>(&controller_),
1630 Details<ProvisionalLoadDetails>(&details));
1631 if (is_main_frame) {
1632 // If we're displaying a network error page do not reset the content
1633 // settings delegate's cookies so the user has a chance to modify cookie
1634 // settings.
1635 if (!is_error_page)
1636 content_settings_delegate_->ClearCookieSpecificContentSettings();
1637 content_settings_delegate_->ClearGeolocationContentSettings();
1638 }
1639}
1640
1641void TabContents::OnDidRedirectProvisionalLoad(int32 page_id,
1642 const GURL& source_url,
1643 const GURL& target_url) {
1644 NavigationEntry* entry;
1645 if (page_id == -1)
1646 entry = controller_.pending_entry();
1647 else
1648 entry = controller_.GetEntryWithPageID(GetSiteInstance(), page_id);
1649 if (!entry || entry->url() != source_url)
1650 return;
1651 entry->set_url(target_url);
1652}
1653
1654void TabContents::OnDidFailProvisionalLoadWithError(
1655 int64 frame_id,
1656 bool is_main_frame,
1657 int error_code,
1658 const GURL& url,
1659 bool showing_repost_interstitial) {
1660 VLOG(1) << "Failed Provisional Load: " << url.possibly_invalid_spec()
1661 << ", error_code: " << error_code
1662 << " is_main_frame: " << is_main_frame
1663 << " showing_repost_interstitial: " << showing_repost_interstitial
1664 << " frame_id: " << frame_id;
1665 GURL validated_url(url);
1666 render_view_host()->FilterURL(ChildProcessSecurityPolicy::GetInstance(),
1667 GetRenderProcessHost()->id(), &validated_url);
1668
1669 if (net::ERR_ABORTED == error_code) {
1670 // EVIL HACK ALERT! Ignore failed loads when we're showing interstitials.
1671 // This means that the interstitial won't be torn down properly, which is
1672 // bad. But if we have an interstitial, go back to another tab type, and
1673 // then load the same interstitial again, we could end up getting the first
1674 // interstitial's "failed" message (as a result of the cancel) when we're on
1675 // the second one.
1676 //
1677 // We can't tell this apart, so we think we're tearing down the current page
1678 // which will cause a crash later one. There is also some code in
1679 // RenderViewHostManager::RendererAbortedProvisionalLoad that is commented
1680 // out because of this problem.
1681 //
1682 // http://code.google.com/p/chromium/issues/detail?id=2855
1683 // Because this will not tear down the interstitial properly, if "back" is
1684 // back to another tab type, the interstitial will still be somewhat alive
1685 // in the previous tab type. If you navigate somewhere that activates the
1686 // tab with the interstitial again, you'll see a flash before the new load
1687 // commits of the interstitial page.
1688 if (showing_interstitial_page()) {
1689 LOG(WARNING) << "Discarding message during interstitial.";
1690 return;
1691 }
1692
1693 // This will discard our pending entry if we cancelled the load (e.g., if we
1694 // decided to download the file instead of load it). Only discard the
1695 // pending entry if the URLs match, otherwise the user initiated a navigate
1696 // before the page loaded so that the discard would discard the wrong entry.
1697 NavigationEntry* pending_entry = controller_.pending_entry();
1698 if (pending_entry && pending_entry->url() == validated_url) {
1699 controller_.DiscardNonCommittedEntries();
1700 // Update the URL display.
1701 NotifyNavigationStateChanged(TabContents::INVALIDATE_URL);
1702 }
1703
1704 render_manager_.RendererAbortedProvisionalLoad(render_view_host());
1705 }
1706
1707 // Send out a notification that we failed a provisional load with an error.
1708 ProvisionalLoadDetails details(
1709 is_main_frame, controller_.IsURLInPageNavigation(validated_url),
1710 validated_url, std::string(), false, false, frame_id);
1711 details.set_error_code(error_code);
1712
1713 NotificationService::current()->Notify(
1714 NotificationType::FAIL_PROVISIONAL_LOAD_WITH_ERROR,
1715 Source<NavigationController>(&controller_),
1716 Details<ProvisionalLoadDetails>(&details));
1717}
1718
1719void TabContents::OnDidLoadResourceFromMemoryCache(
1720 const GURL& url,
1721 const std::string& frame_origin,
1722 const std::string& main_frame_origin,
1723 const std::string& security_info) {
1724 static base::StatsCounter cache("WebKit.CacheHit");
1725 cache.Increment();
1726
1727 // Send out a notification that we loaded a resource from our memory cache.
1728 int cert_id = 0, cert_status = 0, security_bits = -1, connection_status = 0;
1729 SSLManager::DeserializeSecurityInfo(security_info,
1730 &cert_id, &cert_status,
1731 &security_bits,
1732 &connection_status);
1733 LoadFromMemoryCacheDetails details(url, frame_origin, main_frame_origin,
1734 GetRenderProcessHost()->id(), cert_id,
1735 cert_status);
1736
1737 NotificationService::current()->Notify(
1738 NotificationType::LOAD_FROM_MEMORY_CACHE,
1739 Source<NavigationController>(&controller_),
1740 Details<LoadFromMemoryCacheDetails>(&details));
1741}
1742
1743void TabContents::OnDidDisplayInsecureContent() {
1744 displayed_insecure_content_ = true;
1745 SSLManager::NotifySSLInternalStateChanged();
1746}
1747
1748void TabContents::OnDidRunInsecureContent(
1749 const std::string& security_origin) {
1750 controller_.ssl_manager()->DidRunInsecureContent(security_origin);
1751}
1752
1753void TabContents::OnDocumentLoadedInFrame(int64 frame_id) {
1754 controller_.DocumentLoadedInFrame();
1755 NotificationService::current()->Notify(
1756 NotificationType::FRAME_DOM_CONTENT_LOADED,
1757 Source<NavigationController>(&controller_),
1758 Details<int64>(&frame_id));
1759}
1760
1761void TabContents::OnDidFinishLoad(int64 frame_id) {
1762 NotificationService::current()->Notify(
1763 NotificationType::FRAME_DID_FINISH_LOAD,
1764 Source<NavigationController>(&controller_),
1765 Details<int64>(&frame_id));
1766}
1767
[email protected]96d185d2009-04-24 03:28:541768// Notifies the RenderWidgetHost instance about the fact that the page is
1769// loading, or done loading and calls the base implementation.
1770void TabContents::SetIsLoading(bool is_loading,
1771 LoadNotificationDetails* details) {
1772 if (is_loading == is_loading_)
1773 return;
1774
1775 if (!is_loading) {
1776 load_state_ = net::LOAD_STATE_IDLE;
1777 load_state_host_.clear();
[email protected]094e5b22009-09-25 04:23:561778 upload_size_ = 0;
1779 upload_position_ = 0;
[email protected]96d185d2009-04-24 03:28:541780 }
1781
1782 render_manager_.SetIsLoading(is_loading);
1783
1784 is_loading_ = is_loading;
1785 waiting_for_response_ = is_loading;
1786
[email protected]6ebdc9b2010-09-27 16:55:571787 if (delegate_)
1788 delegate_->LoadingStateChanged(this);
[email protected]329581b2009-04-28 06:52:351789 NotifyNavigationStateChanged(INVALIDATE_LOAD);
[email protected]96d185d2009-04-24 03:28:541790
1791 NotificationType type = is_loading ? NotificationType::LOAD_START :
1792 NotificationType::LOAD_STOP;
[email protected]ce5c4502009-05-06 16:46:111793 NotificationDetails det = NotificationService::NoDetails();
[email protected]96d185d2009-04-24 03:28:541794 if (details)
1795 det = Details<LoadNotificationDetails>(details);
1796 NotificationService::current()->Notify(type,
1797 Source<NavigationController>(&controller_),
1798 det);
1799}
1800
[email protected]b6c874582009-05-08 19:38:311801void TabContents::AddPopup(TabContents* new_contents,
[email protected]7e7414ae2010-01-26 20:19:291802 const gfx::Rect& initial_pos) {
[email protected]57eda82a2010-05-05 21:28:061803 // A page can't spawn popups (or do anything else, either) until its load
1804 // commits, so when we reach here, the popup was spawned by the
1805 // NavigationController's last committed entry, not the active entry. For
1806 // example, if a page opens a popup in an onunload() handler, then the active
1807 // entry is the page to be loaded as we navigate away from the unloading
1808 // page. For this reason, we can't use GetURL() to get the opener URL,
1809 // because it returns the active entry.
1810 NavigationEntry* entry = controller_.GetLastCommittedEntry();
1811 GURL creator = entry ? entry->virtual_url() : GURL::EmptyGURL();
1812
1813 if (creator.is_valid() &&
[email protected]8cc8d492010-02-02 10:40:491814 profile()->GetHostContentSettingsMap()->GetContentSetting(
[email protected]ca352452010-08-06 11:14:091815 creator, CONTENT_SETTINGS_TYPE_POPUPS, "") == CONTENT_SETTING_ALLOW) {
[email protected]8cc8d492010-02-02 10:40:491816 AddNewContents(new_contents, NEW_POPUP, initial_pos, true);
1817 } else {
[email protected]c65e2f152010-10-14 15:30:401818 if (!blocked_contents_)
1819 blocked_contents_ = new BlockedContentContainer(this);
1820 blocked_contents_->AddTabContents(new_contents, NEW_POPUP, initial_pos,
1821 true);
[email protected]55126132010-08-19 14:53:281822 content_settings_delegate_->OnContentBlocked(CONTENT_SETTINGS_TYPE_POPUPS,
1823 std::string());
[email protected]8cc8d492010-02-02 10:40:491824 }
[email protected]b9681312008-11-07 00:08:261825}
[email protected]616ed5a2008-11-21 22:27:241826
[email protected]616ed5a2008-11-21 22:27:241827void TabContents::ExpireInfoBars(
1828 const NavigationController::LoadCommittedDetails& details) {
1829 // Only hide InfoBars when the user has done something that makes the main
1830 // frame load. We don't want various automatic or subframe navigations making
1831 // it disappear.
1832 if (!details.is_user_initiated_main_frame_load())
1833 return;
1834
[email protected]f86a07022008-11-25 01:06:051835 for (int i = infobar_delegate_count() - 1; i >= 0; --i) {
[email protected]616ed5a2008-11-21 22:27:241836 InfoBarDelegate* delegate = GetInfoBarDelegateAt(i);
[email protected]cdadf6f2010-08-20 16:36:081837 if (!delegate) {
1838 // If you hit this NOTREACHED, please comment in bug
1839 // http://crbug.com/50428 how you got there.
1840 NOTREACHED();
1841 continue;
1842 }
1843
[email protected]f86a07022008-11-25 01:06:051844 if (delegate->ShouldExpire(details))
[email protected]616ed5a2008-11-21 22:27:241845 RemoveInfoBar(delegate);
[email protected]616ed5a2008-11-21 22:27:241846 }
1847}
[email protected]fdd61c62009-04-22 19:22:571848
[email protected]96d185d2009-04-24 03:28:541849DOMUI* TabContents::GetDOMUIForCurrentState() {
1850 // When there is a pending navigation entry, we want to use the pending DOMUI
1851 // that goes along with it to control the basic flags. For example, we want to
1852 // show the pending URL in the URL bar, so we want the display_url flag to
1853 // be from the pending entry.
1854 //
1855 // The confusion comes because there are multiple possibilities for the
1856 // initial load in a tab as a side effect of the way the RenderViewHostManager
1857 // works.
1858 //
1859 // - For the very first tab the load looks "normal". The new tab DOM UI is
1860 // the pending one, and we want it to apply here.
1861 //
1862 // - For subsequent new tabs, they'll get a new SiteInstance which will then
1863 // get switched to the one previously associated with the new tab pages.
1864 // This switching will cause the manager to commit the RVH/DOMUI. So we'll
1865 // have a committed DOM UI in this case.
1866 //
1867 // This condition handles all of these cases:
1868 //
1869 // - First load in first tab: no committed nav entry + pending nav entry +
1870 // pending dom ui:
1871 // -> Use pending DOM UI if any.
1872 //
1873 // - First load in second tab: no committed nav entry + pending nav entry +
1874 // no pending DOM UI:
1875 // -> Use the committed DOM UI if any.
1876 //
1877 // - Second navigation in any tab: committed nav entry + pending nav entry:
1878 // -> Use pending DOM UI if any.
1879 //
1880 // - Normal state with no load: committed nav entry + no pending nav entry:
1881 // -> Use committed DOM UI.
1882 if (controller_.pending_entry() &&
1883 (controller_.GetLastCommittedEntry() ||
1884 render_manager_.pending_dom_ui()))
1885 return render_manager_.pending_dom_ui();
1886 return render_manager_.dom_ui();
1887}
[email protected]420ae012009-04-24 05:16:321888
1889void TabContents::DidNavigateMainFramePostCommit(
1890 const NavigationController::LoadCommittedDetails& details,
1891 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]c2e74fe82009-09-03 17:57:441892 if (opener_dom_ui_type_ != DOMUIFactory::kNoDOMUI) {
1893 // If this is a window.open navigation, use the same DOMUI as the renderer
1894 // that opened the window, as long as both renderers have the same
1895 // privileges.
[email protected]583d45c12010-08-31 02:48:121896 if (opener_dom_ui_type_ ==
1897 DOMUIFactory::GetDOMUIType(profile(), GetURL())) {
[email protected]c2e74fe82009-09-03 17:57:441898 DOMUI* dom_ui = DOMUIFactory::CreateDOMUIForURL(this, GetURL());
1899 // dom_ui might be NULL if the URL refers to a non-existent extension.
1900 if (dom_ui) {
1901 render_manager_.SetDOMUIPostCommit(dom_ui);
1902 dom_ui->RenderViewCreated(render_view_host());
1903 }
1904 }
1905 opener_dom_ui_type_ = DOMUIFactory::kNoDOMUI;
1906 }
1907
[email protected]420ae012009-04-24 05:16:321908 if (details.is_user_initiated_main_frame_load()) {
1909 // Clear the status bubble. This is a workaround for a bug where WebKit
1910 // doesn't let us know that the cursor left an element during a
1911 // transition (this is also why the mouse cursor remains as a hand after
1912 // clicking on a link); see bugs 1184641 and 980803. We don't want to
1913 // clear the bubble when a user navigates to a named anchor in the same
1914 // page.
1915 UpdateTargetURL(details.entry->page_id(), GURL());
[email protected]420ae012009-04-24 05:16:321916 }
1917
1918 // The keyword generator uses the navigation entries, so must be called after
1919 // the commit.
1920 GenerateKeywordIfNecessary(params);
1921
1922 // Allow the new page to set the title again.
1923 received_page_title_ = false;
1924
1925 // Get the favicon, either from history or request it from the net.
1926 fav_icon_helper_.FetchFavIcon(details.entry->url());
1927
[email protected]0de80162010-02-03 04:52:351928 // Clear all page actions, blocked content notifications and browser actions
1929 // for this tab, unless this is an in-page navigation.
[email protected]a6e82fc2010-02-24 22:28:141930 if (!details.is_in_page) {
[email protected]eaa7dd182010-12-14 11:09:001931 ExtensionService* service = profile()->GetExtensionService();
[email protected]92c6f9b92009-10-24 04:35:081932 if (service) {
1933 for (size_t i = 0; i < service->extensions()->size(); ++i) {
[email protected]5d246db22009-10-27 06:17:571934 ExtensionAction* browser_action =
[email protected]92c6f9b92009-10-24 04:35:081935 service->extensions()->at(i)->browser_action();
[email protected]56ce6e52009-10-27 00:10:521936 if (browser_action) {
1937 browser_action->ClearAllValuesForTab(controller().session_id().id());
1938 NotificationService::current()->Notify(
1939 NotificationType::EXTENSION_BROWSER_ACTION_UPDATED,
[email protected]5d246db22009-10-27 06:17:571940 Source<ExtensionAction>(browser_action),
[email protected]56ce6e52009-10-27 00:10:521941 NotificationService::NoDetails());
1942 }
[email protected]92c6f9b92009-10-24 04:35:081943
[email protected]5d246db22009-10-27 06:17:571944 ExtensionAction* page_action =
[email protected]56ce6e52009-10-27 00:10:521945 service->extensions()->at(i)->page_action();
1946 if (page_action) {
1947 page_action->ClearAllValuesForTab(controller().session_id().id());
1948 PageActionStateChanged();
1949 }
[email protected]92c6f9b92009-10-24 04:35:081950 }
[email protected]aece2c7f2009-08-27 20:43:171951 }
[email protected]0de80162010-02-03 04:52:351952
1953 // Close blocked popups.
[email protected]c65e2f152010-10-14 15:30:401954 if (blocked_contents_) {
[email protected]0fbd70332010-06-01 19:28:341955 AutoReset<bool> auto_reset(&dont_notify_render_view_, true);
[email protected]c65e2f152010-10-14 15:30:401956 blocked_contents_->Destroy();
1957 blocked_contents_ = NULL;
[email protected]0de80162010-02-03 04:52:351958 }
1959
1960 // Clear "blocked" flags.
[email protected]da4f8a12010-08-19 08:33:411961 content_settings_delegate_->ClearBlockedContentSettingsExceptForCookies();
[email protected]9fb83e82010-07-02 18:24:551962 content_settings_delegate_->GeolocationDidNavigate(details);
[email protected]f17a0ee2010-05-17 17:38:471963
1964 // Once the main frame is navigated, we're no longer considered to have
1965 // displayed insecure content.
1966 displayed_insecure_content_ = false;
[email protected]aece2c7f2009-08-27 20:43:171967 }
[email protected]ce5c4502009-05-06 16:46:111968
[email protected]8cc8d492010-02-02 10:40:491969 // Close constrained windows if necessary.
1970 if (!net::RegistryControlledDomainService::SameDomainOrHost(
1971 details.previous_url, details.entry->url()))
1972 CloseConstrainedWindows();
1973
[email protected]420ae012009-04-24 05:16:321974 // Update the starred state.
1975 UpdateStarredStateForCurrentURL();
[email protected]c07c9752010-03-12 05:57:531976
[email protected]3c9e1872010-11-18 16:17:491977 // Notify observers about navigation.
1978 FOR_EACH_OBSERVER(WebNavigationObserver, web_navigation_observers_,
1979 DidNavigateMainFramePostCommit(details, params));
1980
[email protected]c07c9752010-03-12 05:57:531981 // Clear the cache of forms in AutoFill.
[email protected]403415a2011-01-10 18:57:531982 autofill_manager_->Reset();
[email protected]420ae012009-04-24 05:16:321983}
1984
1985void TabContents::DidNavigateAnyFramePostCommit(
1986 RenderViewHost* render_view_host,
1987 const NavigationController::LoadCommittedDetails& details,
1988 const ViewHostMsg_FrameNavigate_Params& params) {
1989 // If we navigate, start showing messages again. This does nothing to prevent
1990 // a malicious script from spamming messages, since the script could just
1991 // reload the page to stop blocking.
1992 suppress_javascript_messages_ = false;
1993
[email protected]3c9e1872010-11-18 16:17:491994 // Notify observers about navigation.
1995 FOR_EACH_OBSERVER(WebNavigationObserver, web_navigation_observers_,
1996 DidNavigateAnyFramePostCommit(details, params));
[email protected]3b1c1812010-02-12 21:53:551997
1998 // Let the LanguageState clear its state.
[email protected]80fc6112010-08-13 20:10:581999 language_state_.DidNavigate(details);
[email protected]420ae012009-04-24 05:16:322000}
2001
[email protected]8cc8d492010-02-02 10:40:492002void TabContents::CloseConstrainedWindows() {
2003 // Clear out any constrained windows since we are leaving this page entirely.
[email protected]420ae012009-04-24 05:16:322004 // We use indices instead of iterators in case CloseWindow does something
2005 // that may invalidate an iterator.
[email protected]61d68ef12011-01-13 14:02:562006 for (size_t i = 0; i < child_windows_.size(); ++i) {
2007 ConstrainedWindow* window = child_windows_[child_windows_.size() - 1 - i];
[email protected]fa1cf0b82010-01-15 21:49:442008 if (window) {
[email protected]420ae012009-04-24 05:16:322009 window->CloseConstrainedWindow();
[email protected]fa1cf0b82010-01-15 21:49:442010 BlockTabContent(false);
2011 }
[email protected]420ae012009-04-24 05:16:322012 }
2013}
2014
2015void TabContents::UpdateStarredStateForCurrentURL() {
2016 BookmarkModel* model = profile()->GetBookmarkModel();
2017 const bool old_state = is_starred_;
2018 is_starred_ = (model && model->IsBookmarked(GetURL()));
2019
2020 if (is_starred_ != old_state && delegate())
2021 delegate()->URLStarredChanged(this, is_starred_);
2022}
2023
2024void TabContents::UpdateAlternateErrorPageURL() {
2025 GURL url = GetAlternateErrorPageURL();
2026 render_view_host()->SetAlternateErrorPageURL(url);
2027}
2028
2029void TabContents::UpdateWebPreferences() {
2030 render_view_host()->UpdateWebPreferences(GetWebkitPrefs());
2031}
2032
[email protected]d0b8d092010-10-25 04:05:172033void TabContents::UpdateZoomLevel() {
2034 render_view_host()->SetZoomLevel(GetZoomLevel());
2035}
2036
[email protected]420ae012009-04-24 05:16:322037void TabContents::UpdateMaxPageIDIfNecessary(SiteInstance* site_instance,
2038 RenderViewHost* rvh) {
2039 // If we are creating a RVH for a restored controller, then we might
2040 // have more page IDs than the SiteInstance's current max page ID. We must
2041 // make sure that the max page ID is larger than any restored page ID.
2042 // Note that it is ok for conflicting page IDs to exist in another tab
2043 // (i.e., NavigationController), but if any page ID is larger than the max,
2044 // the back/forward list will get confused.
2045 int max_restored_page_id = controller_.max_restored_page_id();
2046 if (max_restored_page_id > 0) {
2047 int curr_max_page_id = site_instance->max_page_id();
2048 if (max_restored_page_id > curr_max_page_id) {
2049 // Need to update the site instance immediately.
2050 site_instance->UpdateMaxPageID(max_restored_page_id);
2051
2052 // Also tell the renderer to update its internal representation. We
2053 // need to reserve enough IDs to make all restored page IDs less than
2054 // the max.
2055 if (curr_max_page_id < 0)
2056 curr_max_page_id = 0;
2057 rvh->ReservePageIDRange(max_restored_page_id - curr_max_page_id);
2058 }
2059 }
2060}
2061
[email protected]ec0b6c42010-08-26 03:16:582062scoped_refptr<history::HistoryAddPageArgs>
2063TabContents::CreateHistoryAddPageArgs(
[email protected]ebe89e062009-08-13 23:16:542064 const GURL& virtual_url,
[email protected]befd8d822009-07-01 04:51:472065 const NavigationController::LoadCommittedDetails& details,
[email protected]420ae012009-04-24 05:16:322066 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]ec0b6c42010-08-26 03:16:582067 scoped_refptr<history::HistoryAddPageArgs> add_page_args(
2068 new history::HistoryAddPageArgs(
2069 params.url, base::Time::Now(), this, params.page_id, params.referrer,
2070 params.redirects, params.transition, history::SOURCE_BROWSED,
2071 details.did_replace_entry));
2072 if (PageTransition::IsMainFrame(params.transition) &&
2073 virtual_url != params.url) {
2074 // Hack on the "virtual" URL so that it will appear in history. For some
2075 // types of URLs, we will display a magic URL that is different from where
2076 // the page is actually navigated. We want the user to see in history what
2077 // they saw in the URL bar, so we add the virtual URL as a redirect. This
2078 // only applies to the main frame, as the virtual URL doesn't apply to
2079 // sub-frames.
2080 add_page_args->url = virtual_url;
2081 if (!add_page_args->redirects.empty())
2082 add_page_args->redirects.back() = virtual_url;
[email protected]420ae012009-04-24 05:16:322083 }
[email protected]ec0b6c42010-08-26 03:16:582084 return add_page_args;
[email protected]420ae012009-04-24 05:16:322085}
2086
2087bool TabContents::UpdateTitleForEntry(NavigationEntry* entry,
2088 const std::wstring& title) {
2089 // For file URLs without a title, use the pathname instead. In the case of a
2090 // synthesized title, we don't want the update to count toward the "one set
2091 // per page of the title to history."
[email protected]e53668962010-06-23 15:35:252092 string16 final_title;
[email protected]420ae012009-04-24 05:16:322093 bool explicit_set;
2094 if (entry->url().SchemeIsFile() && title.empty()) {
[email protected]e53668962010-06-23 15:35:252095 final_title = UTF8ToUTF16(entry->url().ExtractFileName());
[email protected]420ae012009-04-24 05:16:322096 explicit_set = false; // Don't count synthetic titles toward the set limit.
2097 } else {
[email protected]e53668962010-06-23 15:35:252098 TrimWhitespace(WideToUTF16Hack(title), TRIM_ALL, &final_title);
[email protected]420ae012009-04-24 05:16:322099 explicit_set = true;
2100 }
2101
[email protected]e53668962010-06-23 15:35:252102 if (final_title == entry->title())
[email protected]420ae012009-04-24 05:16:322103 return false; // Nothing changed, don't bother.
2104
[email protected]e53668962010-06-23 15:35:252105 entry->set_title(final_title);
[email protected]420ae012009-04-24 05:16:322106
[email protected]ec0b6c42010-08-26 03:16:582107 if (!received_page_title_) {
2108 UpdateHistoryPageTitle(*entry);
[email protected]420ae012009-04-24 05:16:322109 received_page_title_ = explicit_set;
2110 }
2111
2112 // Lastly, set the title for the view.
[email protected]e53668962010-06-23 15:35:252113 view_->SetPageTitle(UTF16ToWideHack(final_title));
[email protected]420ae012009-04-24 05:16:322114
[email protected]cbc0e1b2010-04-12 18:33:042115 NotificationService::current()->Notify(
2116 NotificationType::TAB_CONTENTS_TITLE_UPDATED,
2117 Source<TabContents>(this),
2118 NotificationService::NoDetails());
2119
[email protected]420ae012009-04-24 05:16:322120 return true;
2121}
2122
2123void TabContents::NotifySwapped() {
2124 // After sending out a swap notification, we need to send a disconnect
2125 // notification so that clients that pick up a pointer to |this| can NULL the
2126 // pointer. See Bug 1230284.
2127 notify_disconnection_ = true;
2128 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:342129 NotificationType::TAB_CONTENTS_SWAPPED,
[email protected]d82ed61e2009-06-16 02:46:222130 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:322131 NotificationService::NoDetails());
2132}
2133
2134void TabContents::NotifyConnected() {
2135 notify_disconnection_ = true;
2136 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:342137 NotificationType::TAB_CONTENTS_CONNECTED,
[email protected]d82ed61e2009-06-16 02:46:222138 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:322139 NotificationService::NoDetails());
2140}
2141
2142void TabContents::NotifyDisconnected() {
2143 if (!notify_disconnection_)
2144 return;
2145
2146 notify_disconnection_ = false;
2147 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:342148 NotificationType::TAB_CONTENTS_DISCONNECTED,
[email protected]d82ed61e2009-06-16 02:46:222149 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:322150 NotificationService::NoDetails());
2151}
2152
2153void TabContents::GenerateKeywordIfNecessary(
2154 const ViewHostMsg_FrameNavigate_Params& params) {
2155 if (!params.searchable_form_url.is_valid())
2156 return;
2157
2158 if (profile()->IsOffTheRecord())
2159 return;
2160
2161 int last_index = controller_.last_committed_entry_index();
2162 // When there was no previous page, the last index will be 0. This is
2163 // normally due to a form submit that opened in a new tab.
2164 // TODO(brettw) bug 916126: we should support keywords when form submits
2165 // happen in new tabs.
2166 if (last_index <= 0)
2167 return;
2168 const NavigationEntry* previous_entry =
2169 controller_.GetEntryAtIndex(last_index - 1);
2170 if (IsFormSubmit(previous_entry)) {
2171 // Only generate a keyword if the previous page wasn't itself a form
2172 // submit.
2173 return;
2174 }
2175
2176 GURL keyword_url = previous_entry->user_typed_url().is_valid() ?
2177 previous_entry->user_typed_url() : previous_entry->url();
[email protected]62355412011-01-14 22:18:252178 std::wstring keyword =
[email protected]420ae012009-04-24 05:16:322179 TemplateURLModel::GenerateKeyword(keyword_url, true); // autodetected
2180 if (keyword.empty())
2181 return;
2182
2183 TemplateURLModel* url_model = profile()->GetTemplateURLModel();
2184 if (!url_model)
2185 return;
2186
2187 if (!url_model->loaded()) {
2188 url_model->Load();
2189 return;
2190 }
2191
2192 const TemplateURL* current_url;
[email protected]ddd231e2010-06-29 20:35:192193 GURL url = params.searchable_form_url;
[email protected]420ae012009-04-24 05:16:322194 if (!url_model->CanReplaceKeyword(keyword, url, &current_url))
2195 return;
2196
2197 if (current_url) {
2198 if (current_url->originating_url().is_valid()) {
2199 // The existing keyword was generated from an OpenSearch description
2200 // document, don't regenerate.
2201 return;
2202 }
2203 url_model->Remove(current_url);
2204 }
2205 TemplateURL* new_url = new TemplateURL();
2206 new_url->set_keyword(keyword);
2207 new_url->set_short_name(keyword);
[email protected]ddd231e2010-06-29 20:35:192208 new_url->SetURL(url.spec(), 0, 0);
[email protected]420ae012009-04-24 05:16:322209 new_url->add_input_encoding(params.searchable_form_encoding);
2210 DCHECK(controller_.GetLastCommittedEntry());
2211 const GURL& favicon_url =
2212 controller_.GetLastCommittedEntry()->favicon().url();
2213 if (favicon_url.is_valid()) {
2214 new_url->SetFavIconURL(favicon_url);
2215 } else {
2216 // The favicon url isn't valid. This means there really isn't a favicon,
2217 // or the favicon url wasn't obtained before the load started. This assumes
2218 // the later.
2219 // TODO(sky): Need a way to set the favicon that doesn't involve generating
2220 // its url.
2221 new_url->SetFavIconURL(TemplateURL::GenerateFaviconURL(params.referrer));
2222 }
2223 new_url->set_safe_for_autoreplace(true);
2224 url_model->Add(new_url);
2225}
2226
[email protected]bf5c2ff392009-07-08 16:24:332227void TabContents::OnUserGesture() {
2228 // See comment in RenderViewHostDelegate::OnUserGesture as to why we do this.
[email protected]41291322010-07-15 17:09:012229 DownloadRequestLimiter* limiter =
2230 g_browser_process->download_request_limiter();
2231 if (limiter)
2232 limiter->OnUserGesture(this);
[email protected]d070ec62010-07-27 21:28:262233 ExternalProtocolHandler::PermitLaunchUrl();
[email protected]bf5c2ff392009-07-08 16:24:332234}
2235
2236void TabContents::OnFindReply(int request_id,
2237 int number_of_matches,
2238 const gfx::Rect& selection_rect,
2239 int active_match_ordinal,
2240 bool final_update) {
[email protected]3ce7ced2009-07-24 18:52:422241 // Ignore responses for requests that have been aborted.
2242 if (find_op_aborted_)
2243 return;
2244
[email protected]bf5c2ff392009-07-08 16:24:332245 // Ignore responses for requests other than the one we have most recently
2246 // issued. That way we won't act on stale results when the user has
2247 // already typed in another query.
2248 if (request_id != current_find_request_id_)
2249 return;
2250
2251 if (number_of_matches == -1)
2252 number_of_matches = last_search_result_.number_of_matches();
2253 if (active_match_ordinal == -1)
2254 active_match_ordinal = last_search_result_.active_match_ordinal();
2255
2256 gfx::Rect selection = selection_rect;
2257 if (selection.IsEmpty())
2258 selection = last_search_result_.selection_rect();
2259
2260 // Notify the UI, automation and any other observers that a find result was
2261 // found.
2262 last_search_result_ = FindNotificationDetails(request_id, number_of_matches,
2263 selection, active_match_ordinal,
2264 final_update);
2265 NotificationService::current()->Notify(
2266 NotificationType::FIND_RESULT_AVAILABLE,
2267 Source<TabContents>(this),
2268 Details<FindNotificationDetails>(&last_search_result_));
2269}
2270
2271void TabContents::GoToEntryAtOffset(int offset) {
[email protected]876bc832010-09-07 16:29:542272 if (!delegate_ || delegate_->OnGoToEntryOffset(offset)) {
2273 NavigationEntry* entry = controller_.GetEntryAtOffset(offset);
2274 if (!entry)
2275 return;
2276 // Note that we don't call NavigationController::GotToOffset() as we don't
2277 // want to create a pending navigation entry (it might end up lingering
2278 // http://crbug.com/51680).
[email protected]784688a62010-09-13 07:06:522279 entry->set_transition_type(entry->transition_type() |
2280 PageTransition::FORWARD_BACK);
[email protected]876bc832010-09-07 16:29:542281 NavigateToEntry(*entry, NavigationController::NO_RELOAD);
2282 }
[email protected]bf5c2ff392009-07-08 16:24:332283}
2284
[email protected]bf5c2ff392009-07-08 16:24:332285void TabContents::OnMissingPluginStatus(int status) {
2286#if defined(OS_WIN)
2287// TODO(PORT): pull in when plug-ins work
2288 GetPluginInstaller()->OnMissingPluginStatus(status);
2289#endif
2290}
2291
2292void TabContents::OnCrashedPlugin(const FilePath& plugin_path) {
[email protected]bf5c2ff392009-07-08 16:24:332293 DCHECK(!plugin_path.value().empty());
2294
2295 std::wstring plugin_name = plugin_path.ToWStringHack();
[email protected]191eb3f72010-12-21 06:27:502296 webkit::npapi::WebPluginInfo plugin_info;
2297 if (webkit::npapi::PluginList::Singleton()->GetPluginInfoByPath(
[email protected]45446a52010-11-04 17:41:002298 plugin_path, &plugin_info) &&
2299 !plugin_info.name.empty()) {
2300 plugin_name = UTF16ToWide(plugin_info.name);
[email protected]996fd702009-09-04 19:12:372301#if defined(OS_MACOSX)
[email protected]45446a52010-11-04 17:41:002302 // Many plugins on the Mac have .plugin in the actual name, which looks
2303 // terrible, so look for that and strip it off if present.
2304 const std::wstring plugin_extension(L".plugin");
2305 if (EndsWith(plugin_name, plugin_extension, true))
2306 plugin_name.erase(plugin_name.length() - plugin_extension.length());
[email protected]996fd702009-09-04 19:12:372307#endif // OS_MACOSX
[email protected]bf5c2ff392009-07-08 16:24:332308 }
[email protected]2ae8a922010-06-28 20:27:532309 SkBitmap* crash_icon = ResourceBundle::GetSharedInstance().GetBitmapNamed(
2310 IDR_INFOBAR_PLUGIN_CRASHED);
[email protected]bf5c2ff392009-07-08 16:24:332311 AddInfoBar(new SimpleAlertInfoBarDelegate(
[email protected]e23d3a32010-08-13 19:39:582312 this, l10n_util::GetStringFUTF16(IDS_PLUGIN_CRASHED_PROMPT,
2313 WideToUTF16Hack(plugin_name)),
[email protected]2ae8a922010-06-28 20:27:532314 crash_icon, true));
[email protected]bf5c2ff392009-07-08 16:24:332315}
2316
2317void TabContents::OnCrashedWorker() {
2318 AddInfoBar(new SimpleAlertInfoBarDelegate(
[email protected]e23d3a32010-08-13 19:39:582319 this, l10n_util::GetStringUTF16(IDS_WEBWORKER_CRASHED_PROMPT),
[email protected]938e1f92010-04-01 18:09:422320 NULL, true));
[email protected]bf5c2ff392009-07-08 16:24:332321}
2322
[email protected]38789d82010-11-17 06:03:442323void TabContents::OnDidGetApplicationInfo(int32 page_id,
2324 const WebApplicationInfo& info) {
[email protected]ed5431872009-11-17 08:39:512325 web_app_info_ = info;
[email protected]bf5c2ff392009-07-08 16:24:332326
[email protected]ed5431872009-11-17 08:39:512327 if (delegate())
2328 delegate()->OnDidGetApplicationInfo(this, page_id);
[email protected]bf5c2ff392009-07-08 16:24:332329}
2330
[email protected]bb461532010-11-26 21:50:232331void TabContents::OnInstallApplication(const WebApplicationInfo& info) {
2332 if (delegate())
2333 delegate()->OnInstallApplication(this, info);
2334}
2335
[email protected]3c2826852010-12-16 19:09:142336void TabContents::OnBlockedOutdatedPlugin(const string16& name,
2337 const GURL& update_url) {
2338 new OutdatedPluginInfoBar(this, name, update_url);
[email protected]851b1eb2010-08-09 13:32:292339}
2340
[email protected]a2cf65eb2010-01-18 08:20:382341void TabContents::OnPageContents(const GURL& url,
2342 int renderer_process_id,
2343 int32 page_id,
[email protected]e5106202010-06-11 21:12:362344 const string16& contents,
[email protected]1c57b7b2010-07-12 17:38:102345 const std::string& language,
2346 bool page_translatable) {
[email protected]a2cf65eb2010-01-18 08:20:382347 // Don't index any https pages. People generally don't want their bank
2348 // accounts, etc. indexed on their computer, especially since some of these
2349 // things are not marked cachable.
2350 // TODO(brettw) we may want to consider more elaborate heuristics such as
2351 // the cachability of the page. We may also want to consider subframes (this
2352 // test will still index subframes if the subframe is SSL).
[email protected]ccd40a52010-05-11 16:15:272353 // TODO(zelidrag) bug chromium-os:2808 - figure out if we want to reenable
2354 // content indexing for chromeos in some future releases.
2355#if !defined(OS_CHROMEOS)
[email protected]a2cf65eb2010-01-18 08:20:382356 if (!url.SchemeIsSecure()) {
2357 Profile* p = profile();
2358 if (p && !p->IsOffTheRecord()) {
2359 HistoryService* hs = p->GetHistoryService(Profile::IMPLICIT_ACCESS);
2360 if (hs)
2361 hs->SetPageContents(url, contents);
2362 }
2363 }
[email protected]ccd40a52010-05-11 16:15:272364#endif
[email protected]a8a81292010-01-21 00:32:452365
[email protected]1c57b7b2010-07-12 17:38:102366 language_state_.LanguageDetermined(language, page_translatable);
[email protected]a8a81292010-01-21 00:32:452367
2368 std::string lang = language;
2369 NotificationService::current()->Notify(
2370 NotificationType::TAB_LANGUAGE_DETERMINED,
[email protected]7e0831b2010-02-01 08:45:152371 Source<TabContents>(this),
[email protected]a8a81292010-01-21 00:32:452372 Details<std::string>(&lang));
[email protected]a2cf65eb2010-01-18 08:20:382373}
2374
[email protected]7e0831b2010-02-01 08:45:152375void TabContents::OnPageTranslated(int32 page_id,
2376 const std::string& original_lang,
[email protected]152b8ba32010-03-29 19:40:162377 const std::string& translated_lang,
2378 TranslateErrors::Type error_type) {
[email protected]3b1c1812010-02-12 21:53:552379 language_state_.set_current_language(translated_lang);
2380 language_state_.set_translation_pending(false);
[email protected]152b8ba32010-03-29 19:40:162381 PageTranslatedDetails details(original_lang, translated_lang, error_type);
[email protected]7e0831b2010-02-01 08:45:152382 NotificationService::current()->Notify(
2383 NotificationType::PAGE_TRANSLATED,
2384 Source<TabContents>(this),
[email protected]152b8ba32010-03-29 19:40:162385 Details<PageTranslatedDetails>(&details));
[email protected]7e0831b2010-02-01 08:45:152386}
2387
[email protected]ce833282010-11-04 15:48:392388void TabContents::OnSetSuggestions(
2389 int32 page_id,
2390 const std::vector<std::string>& suggestions) {
[email protected]15d9c0c2010-09-10 22:36:262391 if (delegate())
[email protected]ce833282010-11-04 15:48:392392 delegate()->OnSetSuggestions(page_id, suggestions);
2393}
2394
2395void TabContents::OnInstantSupportDetermined(int32 page_id, bool result) {
2396 if (delegate())
2397 delegate()->OnInstantSupportDetermined(page_id, result);
[email protected]15d9c0c2010-09-10 22:36:262398}
2399
[email protected]dcd5b332010-08-11 08:55:182400void TabContents::OnContentSettingsAccessed(bool content_was_blocked) {
[email protected]614c0e12010-03-25 10:12:562401 if (delegate_)
[email protected]20ad6a92010-04-21 14:07:242402 delegate_->OnContentSettingsChange(this);
[email protected]614c0e12010-03-25 10:12:562403}
2404
[email protected]8d3347f2009-07-09 22:00:212405RenderViewHostDelegate::View* TabContents::GetViewDelegate() {
[email protected]420ae012009-04-24 05:16:322406 return view_.get();
2407}
2408
[email protected]8d3347f2009-07-09 22:00:212409RenderViewHostDelegate::RendererManagement*
2410TabContents::GetRendererManagementDelegate() {
2411 return &render_manager_;
2412}
2413
[email protected]bf5c2ff392009-07-08 16:24:332414RenderViewHostDelegate::BrowserIntegration*
[email protected]8d3347f2009-07-09 22:00:212415 TabContents::GetBrowserIntegrationDelegate() {
2416 return this;
[email protected]bf5c2ff392009-07-08 16:24:332417}
2418
[email protected]46ba9082010-06-22 10:00:582419RenderViewHostDelegate::ContentSettings*
2420TabContents::GetContentSettingsDelegate() {
[email protected]9fb83e82010-07-02 18:24:552421 return content_settings_delegate_.get();
[email protected]46ba9082010-06-22 10:00:582422}
2423
[email protected]8d3347f2009-07-09 22:00:212424RenderViewHostDelegate::Save* TabContents::GetSaveDelegate() {
[email protected]420ae012009-04-24 05:16:322425 return save_package_.get(); // May be NULL, but we can return NULL.
2426}
2427
[email protected]8d3347f2009-07-09 22:00:212428RenderViewHostDelegate::Printing* TabContents::GetPrintingDelegate() {
[email protected]12802702010-07-09 19:43:092429 return printing_.get();
[email protected]8d3347f2009-07-09 22:00:212430}
2431
2432RenderViewHostDelegate::FavIcon* TabContents::GetFavIconDelegate() {
2433 return &fav_icon_helper_;
2434}
2435
[email protected]1f18184a2010-07-21 19:34:492436RenderViewHostDelegate::SSL* TabContents::GetSSLDelegate() {
2437 return GetSSLHelper();
2438}
2439
[email protected]b5977a0c2010-08-24 19:46:262440RenderViewHostDelegate::FileSelect* TabContents::GetFileSelectDelegate() {
2441 if (file_select_helper_.get() == NULL)
[email protected]ba70d082010-09-10 16:54:492442 file_select_helper_.reset(new FileSelectHelper(profile()));
[email protected]b5977a0c2010-08-24 19:46:262443 return file_select_helper_.get();
2444}
2445
[email protected]fd571bb2010-03-19 01:23:542446AutomationResourceRoutingDelegate*
2447TabContents::GetAutomationResourceRoutingDelegate() {
2448 return delegate();
2449}
2450
[email protected]ced90ae12010-02-20 02:06:162451RenderViewHostDelegate::BookmarkDrag* TabContents::GetBookmarkDragDelegate() {
2452 return bookmark_drag_;
2453}
2454
2455void TabContents::SetBookmarkDragDelegate(
2456 RenderViewHostDelegate::BookmarkDrag* bookmark_drag) {
2457 bookmark_drag_ = bookmark_drag;
2458}
2459
[email protected]9fb83e82010-07-02 18:24:552460TabSpecificContentSettings* TabContents::GetTabSpecificContentSettings() const {
2461 return content_settings_delegate_.get();
2462}
2463
[email protected]93623c5d2009-12-10 21:40:322464RendererPreferences TabContents::GetRendererPrefs(Profile* profile) const {
[email protected]840b1512009-07-21 16:53:462465 return renderer_preferences_;
[email protected]80d96fa2009-06-10 22:34:512466}
2467
[email protected]57c6a652009-05-04 07:58:342468TabContents* TabContents::GetAsTabContents() {
2469 return this;
2470}
2471
[email protected]7b291f92009-08-14 05:43:532472ViewType::Type TabContents::GetRenderViewType() const {
2473 return ViewType::TAB_CONTENTS;
2474}
2475
2476int TabContents::GetBrowserWindowID() const {
2477 return controller().window_id().id();
2478}
2479
[email protected]420ae012009-04-24 05:16:322480void TabContents::RenderViewCreated(RenderViewHost* render_view_host) {
[email protected]2e4633c2009-07-09 16:58:062481 NotificationService::current()->Notify(
2482 NotificationType::RENDER_VIEW_HOST_CREATED_FOR_TAB,
2483 Source<TabContents>(this),
2484 Details<RenderViewHost>(render_view_host));
[email protected]420ae012009-04-24 05:16:322485 NavigationEntry* entry = controller_.GetActiveEntry();
2486 if (!entry)
2487 return;
2488
2489 // When we're creating views, we're still doing initial setup, so we always
2490 // use the pending DOM UI rather than any possibly existing committed one.
2491 if (render_manager_.pending_dom_ui()) {
2492 render_manager_.pending_dom_ui()->RenderViewCreated(render_view_host);
2493 }
2494
2495 if (entry->IsViewSourceMode()) {
2496 // Put the renderer in view source mode.
2497 render_view_host->Send(
2498 new ViewMsg_EnableViewSourceMode(render_view_host->routing_id()));
2499 }
[email protected]0666aef2009-05-13 19:48:082500
2501 view()->RenderViewCreated(render_view_host);
[email protected]420ae012009-04-24 05:16:322502}
2503
2504void TabContents::RenderViewReady(RenderViewHost* rvh) {
2505 if (rvh != render_view_host()) {
2506 // Don't notify the world, since this came from a renderer in the
2507 // background.
2508 return;
2509 }
2510
2511 NotifyConnected();
[email protected]9d8a4642009-07-29 17:25:302512 bool was_crashed = is_crashed();
[email protected]443b80e2010-12-14 00:42:232513 SetIsCrashed(base::TERMINATION_STATUS_STILL_RUNNING, 0);
[email protected]9d8a4642009-07-29 17:25:302514
2515 // Restore the focus to the tab (otherwise the focus will be on the top
2516 // window).
[email protected]484ae5912010-09-29 19:16:142517 if (was_crashed && !FocusLocationBarByDefault() &&
2518 (!delegate_ || delegate_->ShouldFocusPageAfterCrash())) {
[email protected]9d8a4642009-07-29 17:25:302519 Focus();
[email protected]484ae5912010-09-29 19:16:142520 }
[email protected]420ae012009-04-24 05:16:322521}
2522
[email protected]443b80e2010-12-14 00:42:232523void TabContents::RenderViewGone(RenderViewHost* rvh,
2524 base::TerminationStatus status,
2525 int error_code) {
[email protected]420ae012009-04-24 05:16:322526 // Ask the print preview if this renderer was valuable.
[email protected]12802702010-07-09 19:43:092527 if (!printing_->OnRenderViewGone(rvh))
[email protected]420ae012009-04-24 05:16:322528 return;
2529 if (rvh != render_view_host()) {
2530 // The pending page's RenderViewHost is gone.
2531 return;
2532 }
2533
2534 SetIsLoading(false, NULL);
2535 NotifyDisconnected();
[email protected]443b80e2010-12-14 00:42:232536 SetIsCrashed(status, error_code);
[email protected]420ae012009-04-24 05:16:322537
[email protected]ff9efc62010-02-18 22:10:542538 // Remove all infobars.
[email protected]e5430c92010-02-18 22:39:212539 for (int i = infobar_delegate_count() - 1; i >=0 ; --i)
[email protected]ff9efc62010-02-18 22:10:542540 RemoveInfoBar(GetInfoBarDelegateAt(i));
2541
[email protected]8e537302009-06-10 21:19:582542 // Tell the view that we've crashed so it can prepare the sad tab page.
[email protected]cf68b092010-01-15 20:05:552543 // Only do this if we're not in browser shutdown, so that TabContents
2544 // objects that are not in a browser (e.g., HTML dialogs) and thus are
2545 // visible do not flash a sad tab page.
2546 if (browser_shutdown::GetShutdownType() == browser_shutdown::NOT_VALID)
[email protected]a8f024392011-01-13 21:50:162547 view_->OnTabCrashed(status, error_code);
[email protected]420ae012009-04-24 05:16:322548
2549 // Hide any visible hung renderer warning for this web contents' process.
[email protected]e7b8d832009-11-20 22:41:002550 hung_renderer_dialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:322551}
2552
[email protected]2e4633c2009-07-09 16:58:062553void TabContents::RenderViewDeleted(RenderViewHost* rvh) {
2554 NotificationService::current()->Notify(
2555 NotificationType::RENDER_VIEW_HOST_DELETED,
2556 Source<TabContents>(this),
2557 Details<RenderViewHost>(rvh));
[email protected]fb7b79f72009-11-06 18:00:482558 render_manager_.RenderViewDeleted(rvh);
[email protected]2e4633c2009-07-09 16:58:062559}
2560
[email protected]420ae012009-04-24 05:16:322561void TabContents::DidNavigate(RenderViewHost* rvh,
2562 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]8030f012009-09-25 18:09:372563 int extra_invalidate_flags = 0;
2564
2565 if (PageTransition::IsMainFrame(params.transition)) {
[email protected]33f74972010-12-08 16:40:362566 PrerenderManager* pm = profile()->GetPrerenderManager();
2567 if (pm != NULL) {
2568 if (pm->MaybeUsePreloadedPage(this, params.url)) {
2569 // TODO(tburkard): If the preloaded page has not finished preloading
2570 // yet, we should not do this.
2571 DidStopLoading();
2572 return;
2573 }
2574 }
2575
[email protected]20ede072009-10-06 00:28:352576 bool was_bookmark_bar_visible = ShouldShowBookmarkBar();
[email protected]8030f012009-09-25 18:09:372577
[email protected]420ae012009-04-24 05:16:322578 render_manager_.DidNavigateMainFrame(rvh);
2579
[email protected]20ede072009-10-06 00:28:352580 if (was_bookmark_bar_visible != ShouldShowBookmarkBar())
[email protected]8030f012009-09-25 18:09:372581 extra_invalidate_flags |= INVALIDATE_BOOKMARK_BAR;
[email protected]8030f012009-09-25 18:09:372582 }
2583
[email protected]420ae012009-04-24 05:16:322584 // Update the site of the SiteInstance if it doesn't have one yet.
2585 if (!GetSiteInstance()->has_site())
2586 GetSiteInstance()->SetSite(params.url);
2587
2588 // Need to update MIME type here because it's referred to in
2589 // UpdateNavigationCommands() called by RendererDidNavigate() to
2590 // determine whether or not to enable the encoding menu.
2591 // It's updated only for the main frame. For a subframe,
2592 // RenderView::UpdateURL does not set params.contents_mime_type.
2593 // (see http://code.google.com/p/chromium/issues/detail?id=2929 )
2594 // TODO(jungshik): Add a test for the encoding menu to avoid
2595 // regressing it again.
2596 if (PageTransition::IsMainFrame(params.transition))
2597 contents_mime_type_ = params.contents_mime_type;
2598
2599 NavigationController::LoadCommittedDetails details;
[email protected]8030f012009-09-25 18:09:372600 bool did_navigate = controller_.RendererDidNavigate(
2601 params, extra_invalidate_flags, &details);
[email protected]3e90d4a2009-07-03 17:38:392602
[email protected]a9c0bfe2010-09-17 08:35:222603 // Send notification about committed provisional loads. This notification is
2604 // different from the NAV_ENTRY_COMMITTED notification which doesn't include
2605 // the actual URL navigated to and isn't sent for AUTO_SUBFRAME navigations.
2606 if (details.type != NavigationType::NAV_IGNORE) {
[email protected]dabb0d12010-10-05 12:50:072607 // For AUTO_SUBFRAME navigations, an event for the main frame is generated
2608 // that is not recorded in the navigation history. For the purpose of
2609 // tracking navigation events, we treat this event as a sub frame navigation
2610 // event.
2611 bool is_main_frame = did_navigate ? details.is_main_frame : false;
2612 ProvisionalLoadDetails load_details(
2613 is_main_frame, details.is_in_page, params.url, std::string(), false,
[email protected]dd0aafd2010-12-02 13:13:412614 false, params.frame_id);
[email protected]a9c0bfe2010-09-17 08:35:222615 load_details.set_transition_type(params.transition);
2616 // Whether or not a page transition was triggered by going backward or
2617 // forward in the history is only stored in the navigation controller's
2618 // entry list.
2619 if (did_navigate &&
2620 (controller_.GetActiveEntry()->transition_type() &
2621 PageTransition::FORWARD_BACK)) {
2622 load_details.set_transition_type(
2623 params.transition | PageTransition::FORWARD_BACK);
2624 }
2625 NotificationService::current()->Notify(
2626 NotificationType::FRAME_PROVISIONAL_LOAD_COMMITTED,
2627 Source<NavigationController>(&controller_),
2628 Details<ProvisionalLoadDetails>(&load_details));
[email protected]a9c0bfe2010-09-17 08:35:222629 }
2630
[email protected]3e90d4a2009-07-03 17:38:392631 // Update history. Note that this needs to happen after the entry is complete,
2632 // which WillNavigate[Main,Sub]Frame will do before this function is called.
[email protected]ec0b6c42010-08-26 03:16:582633 if (params.should_update_history) {
[email protected]3e90d4a2009-07-03 17:38:392634 // Most of the time, the displayURL matches the loaded URL, but for about:
[email protected]ec0b6c42010-08-26 03:16:582635 // URLs, we use a data: URL as the real value. We actually want to save the
2636 // about: URL to the history db and keep the data: URL hidden. This is what
2637 // the TabContents' URL getter does.
2638 scoped_refptr<history::HistoryAddPageArgs> add_page_args(
2639 CreateHistoryAddPageArgs(GetURL(), details, params));
2640 if (!delegate() ||
2641 delegate()->ShouldAddNavigationToHistory(*add_page_args,
2642 details.type)) {
2643 UpdateHistoryForNavigation(add_page_args);
2644 }
[email protected]3e90d4a2009-07-03 17:38:392645 }
2646
[email protected]76543b92009-08-31 17:27:452647 if (!did_navigate)
2648 return; // No navigation happened.
[email protected]420ae012009-04-24 05:16:322649
2650 // DO NOT ADD MORE STUFF TO THIS FUNCTION! Your component should either listen
2651 // for the appropriate notification (best) or you can add it to
2652 // DidNavigateMainFramePostCommit / DidNavigateAnyFramePostCommit (only if
2653 // necessary, please).
2654
2655 // Run post-commit tasks.
2656 if (details.is_main_frame)
2657 DidNavigateMainFramePostCommit(details, params);
2658 DidNavigateAnyFramePostCommit(rvh, details, params);
2659}
2660
2661void TabContents::UpdateState(RenderViewHost* rvh,
2662 int32 page_id,
2663 const std::string& state) {
2664 DCHECK(rvh == render_view_host());
2665
2666 // We must be prepared to handle state updates for any page, these occur
2667 // when the user is scrolling and entering form data, as well as when we're
2668 // leaving a page, in which case our state may have already been moved to
2669 // the next page. The navigation controller will look up the appropriate
2670 // NavigationEntry and update it when it is notified via the delegate.
2671
2672 int entry_index = controller_.GetEntryIndexWithPageID(
2673 GetSiteInstance(), page_id);
2674 if (entry_index < 0)
2675 return;
2676 NavigationEntry* entry = controller_.GetEntryAtIndex(entry_index);
2677
2678 if (state == entry->content_state())
2679 return; // Nothing to update.
2680 entry->set_content_state(state);
2681 controller_.NotifyEntryChanged(entry, entry_index);
2682}
2683
2684void TabContents::UpdateTitle(RenderViewHost* rvh,
2685 int32 page_id, const std::wstring& title) {
2686 // If we have a title, that's a pretty good indication that we've started
2687 // getting useful data.
2688 SetNotWaitingForResponse();
2689
2690 DCHECK(rvh == render_view_host());
[email protected]371cc8b2010-09-01 16:03:482691 NavigationEntry* entry = controller_.GetEntryWithPageID(rvh->site_instance(),
[email protected]ec0b6c42010-08-26 03:16:582692 page_id);
[email protected]420ae012009-04-24 05:16:322693 if (!entry || !UpdateTitleForEntry(entry, title))
2694 return;
2695
2696 // Broadcast notifications when the UI should be updated.
2697 if (entry == controller_.GetEntryAtOffset(0))
[email protected]f1cd5e82009-10-23 17:19:032698 NotifyNavigationStateChanged(INVALIDATE_TITLE);
[email protected]420ae012009-04-24 05:16:322699}
2700
[email protected]420ae012009-04-24 05:16:322701void TabContents::UpdateEncoding(RenderViewHost* render_view_host,
[email protected]41fc0322009-09-04 22:23:402702 const std::string& encoding) {
[email protected]420ae012009-04-24 05:16:322703 set_encoding(encoding);
2704}
2705
2706void TabContents::UpdateTargetURL(int32 page_id, const GURL& url) {
2707 if (delegate())
2708 delegate()->UpdateTargetURL(this, url);
2709}
2710
2711void TabContents::UpdateThumbnail(const GURL& url,
2712 const SkBitmap& bitmap,
2713 const ThumbnailScore& score) {
[email protected]e6baed562010-11-02 22:33:442714 if (profile()->IsOffTheRecord())
2715 return;
2716
[email protected]420ae012009-04-24 05:16:322717 // Tell History about this thumbnail
[email protected]2ec53b52010-09-21 03:24:492718 if (history::TopSites::IsEnabled()) {
[email protected]e6baed562010-11-02 22:33:442719 history::TopSites* ts = profile()->GetTopSites();
2720 if (ts)
2721 ts->SetPageThumbnail(url, bitmap, score);
[email protected]449478302009-06-09 20:04:282722 } else {
[email protected]e6baed562010-11-02 22:33:442723 HistoryService* hs =
2724 profile()->GetHistoryService(Profile::IMPLICIT_ACCESS);
2725 if (hs)
[email protected]449478302009-06-09 20:04:282726 hs->SetPageThumbnail(url, bitmap, score);
[email protected]420ae012009-04-24 05:16:322727 }
2728}
2729
[email protected]c4e98902010-06-01 10:20:142730void TabContents::UpdateInspectorSetting(const std::string& key,
2731 const std::string& value) {
[email protected]24d27d72010-10-12 16:30:052732 RenderViewHostDelegateHelper::UpdateInspectorSetting(profile(), key, value);
[email protected]0df30122009-06-03 12:13:082733}
2734
[email protected]45aafc92010-06-07 14:18:532735void TabContents::ClearInspectorSettings() {
[email protected]24d27d72010-10-12 16:30:052736 RenderViewHostDelegateHelper::ClearInspectorSettings(profile());
[email protected]45aafc92010-06-07 14:18:532737}
2738
[email protected]420ae012009-04-24 05:16:322739void TabContents::Close(RenderViewHost* rvh) {
[email protected]07707302009-11-06 00:50:292740 // The UI may be in an event-tracking loop, such as between the
2741 // mouse-down and mouse-up in text selection or a button click.
2742 // Defer the close until after tracking is complete, so that we
2743 // don't free objects out from under the UI.
2744 // TODO(shess): This could probably be integrated with the
2745 // IsDoingDrag() test below. Punting for now because I need more
2746 // research to understand how this impacts platforms other than Mac.
2747 // TODO(shess): This could get more fine-grained. For instance,
2748 // closing a tab in another window while selecting text in the
2749 // current window's Omnibox should be just fine.
2750 if (view()->IsEventTracking()) {
2751 view()->CloseTabAfterEventTracking();
2752 return;
2753 }
2754
[email protected]24a4d1062009-07-10 23:10:422755 // If we close the tab while we're in the middle of a drag, we'll crash.
2756 // Instead, cancel the drag and close it as soon as the drag ends.
2757 if (view()->IsDoingDrag()) {
2758 view()->CancelDragAndCloseTab();
2759 return;
2760 }
2761
[email protected]420ae012009-04-24 05:16:322762 // Ignore this if it comes from a RenderViewHost that we aren't showing.
2763 if (delegate() && rvh == render_view_host())
2764 delegate()->CloseContents(this);
2765}
2766
2767void TabContents::RequestMove(const gfx::Rect& new_bounds) {
2768 if (delegate() && delegate()->IsPopup(this))
2769 delegate()->MoveContents(this, new_bounds);
2770}
2771
[email protected]7ab1e7d62009-10-14 23:32:012772void TabContents::DidStartLoading() {
[email protected]420ae012009-04-24 05:16:322773 SetIsLoading(true, NULL);
[email protected]3c9e1872010-11-18 16:17:492774
[email protected]de4761b2010-12-07 21:05:212775 if (delegate()) {
2776 bool is_print_preview_tab =
2777 printing::PrintPreviewTabController::IsPrintPreviewTab(this);
2778 if (content_restrictions_ || is_print_preview_tab) {
2779 content_restrictions_= is_print_preview_tab ?
2780 CONTENT_RESTRICTION_PRINT : 0;
2781 delegate()->ContentRestrictionsChanged(this);
2782 }
[email protected]9e823662010-10-13 23:36:002783 }
[email protected]3c9e1872010-11-18 16:17:492784
2785 // Notify observers about navigation.
2786 FOR_EACH_OBSERVER(WebNavigationObserver, web_navigation_observers_,
2787 DidStartLoading());
[email protected]420ae012009-04-24 05:16:322788}
2789
[email protected]7ab1e7d62009-10-14 23:32:012790void TabContents::DidStopLoading() {
[email protected]420ae012009-04-24 05:16:322791 scoped_ptr<LoadNotificationDetails> details;
2792
2793 NavigationEntry* entry = controller_.GetActiveEntry();
2794 // An entry may not exist for a stop when loading an initial blank page or
2795 // if an iframe injected by script into a blank page finishes loading.
2796 if (entry) {
[email protected]420ae012009-04-24 05:16:322797 base::TimeDelta elapsed = base::TimeTicks::Now() - current_load_start_;
2798
2799 details.reset(new LoadNotificationDetails(
[email protected]ebe89e062009-08-13 23:16:542800 entry->virtual_url(),
[email protected]420ae012009-04-24 05:16:322801 entry->transition_type(),
2802 elapsed,
2803 &controller_,
2804 controller_.GetCurrentEntryIndex()));
2805 }
2806
[email protected]420ae012009-04-24 05:16:322807 SetIsLoading(false, details.get());
[email protected]3c9e1872010-11-18 16:17:492808
2809 // Notify observers about navigation.
2810 FOR_EACH_OBSERVER(WebNavigationObserver, web_navigation_observers_,
2811 DidStopLoading());
[email protected]420ae012009-04-24 05:16:322812}
2813
[email protected]1a3c3cb2010-12-16 21:03:402814void TabContents::DidChangeLoadProgress(double progress) {
2815 if (delegate())
2816 delegate()->LoadProgressChanged(progress);
2817}
2818
[email protected]25497492010-09-11 15:15:082819void TabContents::DocumentOnLoadCompletedInMainFrame(
2820 RenderViewHost* render_view_host,
2821 int32 page_id) {
2822 NotificationService::current()->Notify(
2823 NotificationType::LOAD_COMPLETED_MAIN_FRAME,
2824 Source<TabContents>(this),
2825 Details<int>(&page_id));
2826}
2827
[email protected]420ae012009-04-24 05:16:322828void TabContents::RequestOpenURL(const GURL& url, const GURL& referrer,
2829 WindowOpenDisposition disposition) {
2830 if (render_manager_.dom_ui()) {
2831 // When we're a DOM UI, it will provide a page transition type for us (this
2832 // is so the new tab page can specify AUTO_BOOKMARK for automatically
2833 // generated suggestions).
2834 //
2835 // Note also that we hide the referrer for DOM UI pages. We don't really
[email protected]60e448982009-05-06 04:21:162836 // want web sites to see a referrer of "chrome://blah" (and some
2837 // chrome: URLs might have search terms or other stuff we don't want to
[email protected]420ae012009-04-24 05:16:322838 // send to the site), so we send no referrer.
2839 OpenURL(url, GURL(), disposition,
2840 render_manager_.dom_ui()->link_transition_type());
2841 } else {
2842 OpenURL(url, referrer, disposition, PageTransition::LINK);
2843 }
2844}
2845
[email protected]1ee614862009-07-06 20:29:182846void TabContents::DomOperationResponse(const std::string& json_string,
2847 int automation_id) {
2848}
2849
[email protected]8b8e7c92010-08-19 18:05:562850void TabContents::ProcessDOMUIMessage(
2851 const ViewHostMsg_DomMessage_Params& params) {
[email protected]420ae012009-04-24 05:16:322852 if (!render_manager_.dom_ui()) {
[email protected]c2e74fe82009-09-03 17:57:442853 // This can happen if someone uses window.open() to open an extension URL
2854 // from a non-extension context.
[email protected]8b8e7c92010-08-19 18:05:562855 render_view_host()->BlockExtensionRequest(params.request_id);
[email protected]420ae012009-04-24 05:16:322856 return;
2857 }
[email protected]8b8e7c92010-08-19 18:05:562858 render_manager_.dom_ui()->ProcessDOMUIMessage(params);
[email protected]420ae012009-04-24 05:16:322859}
2860
2861void TabContents::ProcessExternalHostMessage(const std::string& message,
2862 const std::string& origin,
2863 const std::string& target) {
2864 if (delegate())
2865 delegate()->ForwardMessageToExternalHost(message, origin, target);
2866}
2867
[email protected]420ae012009-04-24 05:16:322868void TabContents::RunJavaScriptMessage(
2869 const std::wstring& message,
2870 const std::wstring& default_prompt,
2871 const GURL& frame_url,
2872 const int flags,
2873 IPC::Message* reply_msg,
2874 bool* did_suppress_message) {
2875 // Suppress javascript messages when requested and when inside a constrained
2876 // popup window (because that activates them and breaks them out of the
2877 // constrained window jail).
[email protected]91ed49a2009-07-07 21:29:552878 // Also suppress messages when showing an interstitial. The interstitial is
2879 // shown over the previous page, we don't want the hidden page dialogs to
2880 // interfere with the interstitial.
[email protected]8f55e802010-12-06 18:11:502881 bool suppress_this_message =
2882 suppress_javascript_messages_ ||
2883 showing_interstitial_page() ||
2884 (delegate() && delegate()->ShouldSuppressDialogs());
[email protected]420ae012009-04-24 05:16:322885 if (delegate())
2886 suppress_this_message |=
[email protected]b6c874582009-05-08 19:38:312887 (delegate()->GetConstrainingContents(this) != this);
[email protected]420ae012009-04-24 05:16:322888
2889 *did_suppress_message = suppress_this_message;
2890
2891 if (!suppress_this_message) {
2892 base::TimeDelta time_since_last_message(
2893 base::TimeTicks::Now() - last_javascript_message_dismissal_);
2894 bool show_suppress_checkbox = false;
2895 // Show a checkbox offering to suppress further messages if this message is
2896 // being displayed within kJavascriptMessageExpectedDelay of the last one.
2897 if (time_since_last_message <
2898 base::TimeDelta::FromMilliseconds(kJavascriptMessageExpectedDelay))
2899 show_suppress_checkbox = true;
2900
[email protected]d6ad7442010-09-05 19:06:522901 RunJavascriptMessageBox(profile(), this, frame_url, flags, message,
2902 default_prompt, show_suppress_checkbox, reply_msg);
[email protected]420ae012009-04-24 05:16:322903 } else {
2904 // If we are suppressing messages, just reply as is if the user immediately
2905 // pressed "Cancel".
[email protected]beb440c2009-11-06 04:08:542906 OnMessageBoxClosed(reply_msg, false, std::wstring());
[email protected]420ae012009-04-24 05:16:322907 }
2908}
2909
2910void TabContents::RunBeforeUnloadConfirm(const std::wstring& message,
2911 IPC::Message* reply_msg) {
[email protected]5aab5e22010-12-08 22:13:292912 if (delegate())
2913 delegate()->WillRunBeforeUnloadConfirm();
[email protected]8f55e802010-12-06 18:11:502914 if (delegate() && delegate()->ShouldSuppressDialogs()) {
2915 render_view_host()->JavaScriptMessageBoxClosed(reply_msg, true,
2916 std::wstring());
2917 return;
2918 }
[email protected]7ab1e7d62009-10-14 23:32:012919 is_showing_before_unload_dialog_ = true;
[email protected]d82ed61e2009-06-16 02:46:222920 RunBeforeUnloadDialog(this, message, reply_msg);
[email protected]420ae012009-04-24 05:16:322921}
2922
2923void TabContents::ShowModalHTMLDialog(const GURL& url, int width, int height,
2924 const std::string& json_arguments,
2925 IPC::Message* reply_msg) {
2926 if (delegate()) {
2927 HtmlDialogUIDelegate* dialog_delegate =
2928 new ModalHtmlDialogDelegate(url, width, height, json_arguments,
[email protected]d82ed61e2009-06-16 02:46:222929 reply_msg, this);
[email protected]420ae012009-04-24 05:16:322930 delegate()->ShowHtmlDialog(dialog_delegate, NULL);
2931 }
2932}
2933
[email protected]c4aa86962010-11-24 23:08:042934void TabContents::PasswordFormsFound(
2935 const std::vector<webkit_glue::PasswordForm>& forms) {
2936 FOR_EACH_OBSERVER(WebNavigationObserver, web_navigation_observers_,
2937 PasswordFormsFound(forms));
2938}
2939
2940void TabContents::PasswordFormsVisible(
2941 const std::vector<webkit_glue::PasswordForm>& visible_forms) {
2942 FOR_EACH_OBSERVER(WebNavigationObserver, web_navigation_observers_,
2943 PasswordFormsVisible(visible_forms));
2944}
2945
[email protected]420ae012009-04-24 05:16:322946// Checks to see if we should generate a keyword based on the OSDD, and if
2947// necessary uses TemplateURLFetcher to download the OSDD and create a keyword.
[email protected]2de7e002010-10-11 22:58:202948void TabContents::PageHasOSDD(
2949 RenderViewHost* render_view_host,
2950 int32 page_id,
2951 const GURL& url,
2952 const ViewHostMsg_PageHasOSDD_Type& msg_provider_type) {
[email protected]0b5b85c2010-10-11 23:00:122953 // Make sure page_id is the current page and other basic checks.
[email protected]420ae012009-04-24 05:16:322954 DCHECK(url.is_valid());
2955 if (!IsActiveEntry(page_id))
2956 return;
[email protected]420ae012009-04-24 05:16:322957 if (!profile()->GetTemplateURLFetcher())
2958 return;
[email protected]420ae012009-04-24 05:16:322959 if (profile()->IsOffTheRecord())
2960 return;
2961
[email protected]2de7e002010-10-11 22:58:202962 TemplateURLFetcher::ProviderType provider_type;
2963 switch (msg_provider_type.type) {
2964 case ViewHostMsg_PageHasOSDD_Type::AUTODETECTED_PROVIDER:
2965 provider_type = TemplateURLFetcher::AUTODETECTED_PROVIDER;
2966 break;
2967
2968 case ViewHostMsg_PageHasOSDD_Type::EXPLICIT_DEFAULT_PROVIDER:
2969 provider_type = TemplateURLFetcher::EXPLICIT_DEFAULT_PROVIDER;
2970 break;
2971
2972 case ViewHostMsg_PageHasOSDD_Type::EXPLICIT_PROVIDER:
2973 provider_type = TemplateURLFetcher::EXPLICIT_PROVIDER;
2974 break;
2975
2976 default:
2977 NOTREACHED();
2978 return;
2979 }
2980
[email protected]420ae012009-04-24 05:16:322981 const NavigationEntry* entry = controller_.GetLastCommittedEntry();
2982 DCHECK(entry);
2983
2984 const NavigationEntry* base_entry = entry;
2985 if (IsFormSubmit(base_entry)) {
2986 // If the current page is a form submit, find the last page that was not
2987 // a form submit and use its url to generate the keyword from.
2988 int index = controller_.last_committed_entry_index() - 1;
2989 while (index >= 0 && IsFormSubmit(controller_.GetEntryAtIndex(index)))
2990 index--;
2991 if (index >= 0)
2992 base_entry = controller_.GetEntryAtIndex(index);
2993 else
2994 base_entry = NULL;
2995 }
2996
2997 // We want to use the user typed URL if available since that represents what
2998 // the user typed to get here, and fall back on the regular URL if not.
2999 if (!base_entry)
3000 return;
3001 GURL keyword_url = base_entry->user_typed_url().is_valid() ?
3002 base_entry->user_typed_url() : base_entry->url();
3003 if (!keyword_url.is_valid())
3004 return;
[email protected]2de7e002010-10-11 22:58:203005
[email protected]62355412011-01-14 22:18:253006 std::wstring keyword = TemplateURLModel::GenerateKeyword(
[email protected]0b5b85c2010-10-11 23:00:123007 keyword_url,
3008 provider_type == TemplateURLFetcher::AUTODETECTED_PROVIDER);
[email protected]a88dbdd52010-01-15 19:25:533009
[email protected]0b5b85c2010-10-11 23:00:123010 // Download the OpenSearch description document. If this is successful, a
[email protected]420ae012009-04-24 05:16:323011 // new keyword will be created when done.
[email protected]420ae012009-04-24 05:16:323012 profile()->GetTemplateURLFetcher()->ScheduleDownload(
3013 keyword,
3014 url,
3015 base_entry->favicon().url(),
[email protected]0b5b85c2010-10-11 23:00:123016 new TemplateURLFetcherUICallbacks(this),
[email protected]2de7e002010-10-11 22:58:203017 provider_type);
[email protected]420ae012009-04-24 05:16:323018}
3019
[email protected]420ae012009-04-24 05:16:323020GURL TabContents::GetAlternateErrorPageURL() const {
3021 GURL url;
3022 // Disable alternate error pages when in OffTheRecord/Incognito mode.
3023 if (profile()->IsOffTheRecord())
3024 return url;
3025
3026 PrefService* prefs = profile()->GetPrefs();
3027 DCHECK(prefs);
3028 if (prefs->GetBoolean(prefs::kAlternateErrorPagesEnabled)) {
[email protected]a33457b82009-09-21 23:07:153029 url = google_util::AppendGoogleLocaleParam(
3030 GURL(google_util::kLinkDoctorBaseURL));
[email protected]420ae012009-04-24 05:16:323031 url = google_util::AppendGoogleTLDParam(url);
3032 }
3033 return url;
3034}
3035
3036WebPreferences TabContents::GetWebkitPrefs() {
[email protected]6c32ce72010-03-08 05:18:063037 Profile* profile = render_view_host()->process()->profile();
[email protected]7ccddb8c2009-08-04 17:36:553038 bool is_dom_ui = false;
[email protected]447021c2010-09-08 21:29:083039 WebPreferences web_prefs =
3040 RenderViewHostDelegateHelper::GetWebkitPrefs(profile, is_dom_ui);
3041
[email protected]9beff752010-09-22 19:35:433042 // Force accelerated compositing and 2d canvas off for chrome: and
3043 // chrome-extension: pages.
[email protected]27c790d42010-10-22 09:28:433044 if (GetURL().SchemeIs(chrome::kChromeDevToolsScheme) ||
3045 GetURL().SchemeIs(chrome::kChromeUIScheme)) {
[email protected]447021c2010-09-08 21:29:083046 web_prefs.accelerated_compositing_enabled = false;
[email protected]9beff752010-09-22 19:35:433047 web_prefs.accelerated_2d_canvas_enabled = false;
[email protected]447021c2010-09-08 21:29:083048 }
3049
[email protected]39553442010-09-09 00:01:553050#if defined(OS_MACOSX)
3051 // Disable accelerated compositing if IOSurface's are not supported,
3052 // as is the case in 10.5.
3053 if (!IOSurfaceSupport::Initialize())
3054 web_prefs.accelerated_compositing_enabled = false;
3055#endif
3056
[email protected]447021c2010-09-08 21:29:083057 return web_prefs;
[email protected]420ae012009-04-24 05:16:323058}
3059
[email protected]fa1cf0b82010-01-15 21:49:443060void TabContents::OnIgnoredUIEvent() {
3061 if (constrained_window_count()) {
3062 ConstrainedWindow* window = *constrained_window_begin();
3063 window->FocusConstrainedWindow();
3064 }
3065}
3066
[email protected]420ae012009-04-24 05:16:323067void TabContents::OnJSOutOfMemory() {
3068 AddInfoBar(new SimpleAlertInfoBarDelegate(
[email protected]e23d3a32010-08-13 19:39:583069 this, l10n_util::GetStringUTF16(IDS_JS_OUT_OF_MEMORY_PROMPT),
3070 NULL, true));
[email protected]420ae012009-04-24 05:16:323071}
3072
[email protected]420ae012009-04-24 05:16:323073void TabContents::OnCrossSiteResponse(int new_render_process_host_id,
3074 int new_request_id) {
[email protected]57c6a652009-05-04 07:58:343075 // Allows the TabContents to react when a cross-site response is ready to be
[email protected]420ae012009-04-24 05:16:323076 // delivered to a pending RenderViewHost. We must first run the onunload
3077 // handler of the old RenderViewHost before we can allow it to proceed.
3078 render_manager_.OnCrossSiteResponse(new_render_process_host_id,
3079 new_request_id);
3080}
3081
[email protected]420ae012009-04-24 05:16:323082void TabContents::RendererUnresponsive(RenderViewHost* rvh,
3083 bool is_during_unload) {
3084 if (is_during_unload) {
3085 // Hang occurred while firing the beforeunload/unload handler.
3086 // Pretend the handler fired so tab closing continues as if it had.
[email protected]829e7612009-04-25 01:15:113087 rvh->set_sudden_termination_allowed(true);
[email protected]420ae012009-04-24 05:16:323088
3089 if (!render_manager_.ShouldCloseTabOnUnresponsiveRenderer())
3090 return;
3091
3092 // If the tab hangs in the beforeunload/unload handler there's really
3093 // nothing we can do to recover. Pretend the unload listeners have
3094 // all fired and close the tab. If the hang is in the beforeunload handler
3095 // then the user will not have the option of cancelling the close.
3096 Close(rvh);
3097 return;
3098 }
3099
3100 if (render_view_host() && render_view_host()->IsRenderViewLive())
[email protected]e7b8d832009-11-20 22:41:003101 hung_renderer_dialog::ShowForTabContents(this);
[email protected]420ae012009-04-24 05:16:323102}
3103
3104void TabContents::RendererResponsive(RenderViewHost* render_view_host) {
[email protected]e7b8d832009-11-20 22:41:003105 hung_renderer_dialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:323106}
3107
3108void TabContents::LoadStateChanged(const GURL& url,
[email protected]094e5b22009-09-25 04:23:563109 net::LoadState load_state,
3110 uint64 upload_position,
3111 uint64 upload_size) {
[email protected]420ae012009-04-24 05:16:323112 load_state_ = load_state;
[email protected]094e5b22009-09-25 04:23:563113 upload_position_ = upload_position;
3114 upload_size_ = upload_size;
[email protected]d686e812009-06-03 19:10:293115 std::wstring languages =
[email protected]ddd231e2010-06-29 20:35:193116 UTF8ToWide(profile()->GetPrefs()->GetString(prefs::kAcceptLanguages));
[email protected]d686e812009-06-03 19:10:293117 std::string host = url.host();
[email protected]42197a22010-12-28 23:29:423118 load_state_host_ = WideToUTF16Hack(
3119 net::IDNToUnicode(host.c_str(), host.size(), languages, NULL));
[email protected]420ae012009-04-24 05:16:323120 if (load_state_ == net::LOAD_STATE_READING_RESPONSE)
3121 SetNotWaitingForResponse();
3122 if (is_loading())
[email protected]c9cd2222009-05-06 05:16:503123 NotifyNavigationStateChanged(INVALIDATE_LOAD | INVALIDATE_TAB);
[email protected]420ae012009-04-24 05:16:323124}
3125
[email protected]1ee614862009-07-06 20:29:183126bool TabContents::IsExternalTabContainer() const {
3127 if (!delegate())
3128 return false;
3129
3130 return delegate()->IsExternalTabContainer();
3131}
3132
[email protected]1ee614862009-07-06 20:29:183133void TabContents::DidInsertCSS() {
3134 // This RVHDelegate function is used for extensions and not us.
[email protected]420ae012009-04-24 05:16:323135}
3136
[email protected]546ae4e02010-12-08 14:57:193137void TabContents::FocusedNodeChanged(bool is_editable_node) {
[email protected]130efb02009-09-18 18:54:353138 NotificationService::current()->Notify(
3139 NotificationType::FOCUS_CHANGED_IN_PAGE,
3140 Source<RenderViewHost>(render_view_host()),
[email protected]546ae4e02010-12-08 14:57:193141 Details<const bool>(&is_editable_node));
[email protected]130efb02009-09-18 18:54:353142}
3143
[email protected]b75b8292010-10-01 07:28:253144void TabContents::UpdateZoomLimits(int minimum_percent,
3145 int maximum_percent,
3146 bool remember) {
3147 minimum_zoom_percent_ = minimum_percent;
3148 maximum_zoom_percent_ = maximum_percent;
3149 temporary_zoom_settings_ = !remember;
[email protected]ebf40a72010-07-22 01:46:383150}
3151
[email protected]9e823662010-10-13 23:36:003152void TabContents::UpdateContentRestrictions(int restrictions) {
3153 content_restrictions_ = restrictions;
3154 delegate()->ContentRestrictionsChanged(this);
[email protected]166e3042010-10-12 22:42:323155}
3156
[email protected]420ae012009-04-24 05:16:323157void TabContents::BeforeUnloadFiredFromRenderManager(
3158 bool proceed,
3159 bool* proceed_to_fire_unload) {
3160 if (delegate())
3161 delegate()->BeforeUnloadFired(this, proceed, proceed_to_fire_unload);
3162}
3163
[email protected]3a3d47472010-07-15 21:03:543164void TabContents::DidStartLoadingFromRenderManager(
3165 RenderViewHost* render_view_host) {
3166 DidStartLoading();
3167}
3168
3169void TabContents::RenderViewGoneFromRenderManager(
3170 RenderViewHost* render_view_host) {
[email protected]443b80e2010-12-14 00:42:233171 DCHECK(crashed_status_ != base::TERMINATION_STATUS_STILL_RUNNING);
3172 RenderViewGone(render_view_host, crashed_status_, crashed_error_code_);
[email protected]3a3d47472010-07-15 21:03:543173}
3174
[email protected]420ae012009-04-24 05:16:323175void TabContents::UpdateRenderViewSizeForRenderManager() {
3176 // TODO(brettw) this is a hack. See TabContentsView::SizeContents.
[email protected]0323ee42010-02-17 22:03:263177 gfx::Size size = view_->GetContainerSize();
3178 // 0x0 isn't a valid window size (minimal window size is 1x1) but it may be
3179 // here during container initialization and normal window size will be set
3180 // later. In case of tab duplication this resizing to 0x0 prevents setting
3181 // normal size later so just ignore it.
3182 if (!size.IsEmpty())
3183 view_->SizeContents(size);
[email protected]420ae012009-04-24 05:16:323184}
3185
[email protected]3a3d47472010-07-15 21:03:543186void TabContents::NotifySwappedFromRenderManager() {
3187 NotifySwapped();
3188}
3189
3190NavigationController& TabContents::GetControllerForRenderManager() {
3191 return controller();
3192}
3193
[email protected]420ae012009-04-24 05:16:323194DOMUI* TabContents::CreateDOMUIForRenderManager(const GURL& url) {
[email protected]d82ed61e2009-06-16 02:46:223195 return DOMUIFactory::CreateDOMUIForURL(this, url);
[email protected]420ae012009-04-24 05:16:323196}
3197
3198NavigationEntry*
3199TabContents::GetLastCommittedNavigationEntryForRenderManager() {
3200 return controller_.GetLastCommittedEntry();
3201}
3202
3203bool TabContents::CreateRenderViewForRenderManager(
3204 RenderViewHost* render_view_host) {
[email protected]420ae012009-04-24 05:16:323205 RenderWidgetHostView* rwh_view = view_->CreateViewForWidget(render_view_host);
[email protected]1a98a932009-11-17 00:12:523206
[email protected]674b3822010-08-04 04:02:513207 if (!render_view_host->CreateRenderView(string16()))
[email protected]420ae012009-04-24 05:16:323208 return false;
3209
3210 // Now that the RenderView has been created, we need to tell it its size.
3211 rwh_view->SetSize(view_->GetContainerSize());
3212
3213 UpdateMaxPageIDIfNecessary(render_view_host->site_instance(),
3214 render_view_host);
3215 return true;
3216}
3217
3218void TabContents::Observe(NotificationType type,
3219 const NotificationSource& source,
3220 const NotificationDetails& details) {
3221 switch (type.value) {
3222 case NotificationType::BOOKMARK_MODEL_LOADED:
3223 // BookmarkModel finished loading, fall through to update starred state.
3224 case NotificationType::URLS_STARRED: {
3225 // Somewhere, a URL has been starred.
3226 // Ignore notifications for profiles other than our current one.
3227 Profile* source_profile = Source<Profile>(source).ptr();
3228 if (!source_profile || !source_profile->IsSameProfile(profile()))
3229 return;
3230
3231 UpdateStarredStateForCurrentURL();
3232 break;
3233 }
3234 case NotificationType::PREF_CHANGED: {
[email protected]57ecc4b2010-08-11 03:02:513235 std::string* pref_name_in = Details<std::string>(details).ptr();
[email protected]420ae012009-04-24 05:16:323236 DCHECK(Source<PrefService>(source).ptr() == profile()->GetPrefs());
3237 if (*pref_name_in == prefs::kAlternateErrorPagesEnabled) {
3238 UpdateAlternateErrorPageURL();
[email protected]88e14382010-10-14 21:45:533239 } else if ((*pref_name_in == prefs::kDefaultCharset) ||
3240 StartsWithASCII(*pref_name_in, "webkit.webprefs.", true)) {
[email protected]420ae012009-04-24 05:16:323241 UpdateWebPreferences();
[email protected]d0b8d092010-10-25 04:05:173242 } else if (*pref_name_in == prefs::kDefaultZoomLevel) {
3243 UpdateZoomLevel();
[email protected]420ae012009-04-24 05:16:323244 } else {
3245 NOTREACHED() << "unexpected pref change notification" << *pref_name_in;
3246 }
3247 break;
3248 }
3249 case NotificationType::RENDER_WIDGET_HOST_DESTROYED:
3250 view_->RenderWidgetHostDestroyed(Source<RenderWidgetHost>(source).ptr());
3251 break;
3252
3253 case NotificationType::NAV_ENTRY_COMMITTED: {
3254 DCHECK(&controller_ == Source<NavigationController>(source).ptr());
3255
3256 NavigationController::LoadCommittedDetails& committed_details =
3257 *(Details<NavigationController::LoadCommittedDetails>(details).ptr());
3258 ExpireInfoBars(committed_details);
3259 break;
3260 }
3261
[email protected]d3ba77272009-09-03 00:06:093262#if defined(OS_LINUX)
3263 case NotificationType::BROWSER_THEME_CHANGED: {
[email protected]7ff431e2010-01-07 18:18:543264 renderer_preferences_util::UpdateFromSystemSettings(
3265 &renderer_preferences_, profile());
[email protected]d3ba77272009-09-03 00:06:093266 render_view_host()->SyncRendererPrefs();
3267 break;
3268 }
3269#endif
3270
[email protected]6c32ce72010-03-08 05:18:063271 case NotificationType::USER_STYLE_SHEET_UPDATED:
3272 UpdateWebPreferences();
3273 break;
3274
[email protected]79580c62010-02-02 02:36:253275 case NotificationType::CONTENT_SETTINGS_CHANGED: {
[email protected]5f24f212010-12-02 14:47:453276 Details<const ContentSettingsDetails> settings_details(details);
[email protected]79580c62010-02-02 02:36:253277 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]da0c8e92010-02-09 23:25:493278 GURL entry_url;
[email protected]0314ae02010-04-08 09:18:293279 if (entry)
[email protected]da0c8e92010-02-09 23:25:493280 entry_url = entry->url();
[email protected]0314ae02010-04-08 09:18:293281 if (settings_details.ptr()->update_all() ||
3282 settings_details.ptr()->pattern().Matches(entry_url)) {
3283 render_view_host()->SendContentSettings(entry_url,
[email protected]b9a4ea7f2010-06-25 07:11:233284 profile()->GetHostContentSettingsMap()->
3285 GetContentSettings(entry_url));
[email protected]79580c62010-02-02 02:36:253286 }
3287 break;
3288 }
3289
[email protected]e5adff72010-06-14 18:13:063290 case NotificationType::EXTENSION_LOADED:
[email protected]a888b29e62010-04-01 13:38:573291 break;
[email protected]a888b29e62010-04-01 13:38:573292
3293 case NotificationType::EXTENSION_UNLOADED:
[email protected]a888b29e62010-04-01 13:38:573294 break;
[email protected]a888b29e62010-04-01 13:38:573295
[email protected]88152052010-10-01 04:05:183296 case NotificationType::GOOGLE_URL_UPDATED:
3297 UpdateAlternateErrorPageURL();
3298 break;
3299
[email protected]420ae012009-04-24 05:16:323300 default:
3301 NOTREACHED();
3302 }
3303}
[email protected]aacd7cc2009-09-02 14:51:103304
[email protected]9adb9692010-10-29 23:14:023305void TabContents::UpdateExtensionAppIcon(const Extension* extension) {
[email protected]98aa0b52010-05-06 17:03:083306 extension_app_icon_.reset();
[email protected]f075fe0a22010-04-26 04:06:433307
[email protected]a888b29e62010-04-01 13:38:573308 if (extension) {
[email protected]98aa0b52010-05-06 17:03:083309 extension_app_image_loader_.reset(new ImageLoadingTracker(this));
3310 extension_app_image_loader_->LoadImage(
[email protected]a888b29e62010-04-01 13:38:573311 extension,
[email protected]807871f2010-09-16 01:04:483312 extension->GetIconResource(Extension::EXTENSION_ICON_SMALLISH,
3313 ExtensionIconSet::MATCH_EXACTLY),
[email protected]a888b29e62010-04-01 13:38:573314 gfx::Size(Extension::EXTENSION_ICON_SMALLISH,
3315 Extension::EXTENSION_ICON_SMALLISH),
3316 ImageLoadingTracker::CACHE);
3317 } else {
[email protected]98aa0b52010-05-06 17:03:083318 extension_app_image_loader_.reset(NULL);
[email protected]a888b29e62010-04-01 13:38:573319 }
3320}
3321
[email protected]9adb9692010-10-29 23:14:023322const Extension* TabContents::GetExtensionContaining(const GURL& url) {
[email protected]eaa7dd182010-12-14 11:09:003323 ExtensionService* extensions_service = profile()->GetExtensionService();
[email protected]a888b29e62010-04-01 13:38:573324 if (!extensions_service)
3325 return NULL;
3326
[email protected]9adb9692010-10-29 23:14:023327 const Extension* extension = extensions_service->GetExtensionByURL(url);
[email protected]a888b29e62010-04-01 13:38:573328 return extension ?
3329 extension : extensions_service->GetExtensionByWebExtent(url);
3330}
3331
[email protected]a888b29e62010-04-01 13:38:573332void TabContents::OnImageLoaded(SkBitmap* image, ExtensionResource resource,
3333 int index) {
3334 if (image) {
[email protected]98aa0b52010-05-06 17:03:083335 extension_app_icon_ = *image;
[email protected]a888b29e62010-04-01 13:38:573336 NotifyNavigationStateChanged(INVALIDATE_TAB);
3337 }
3338}
3339
[email protected]beb440c2009-11-06 04:08:543340gfx::NativeWindow TabContents::GetMessageBoxRootWindow() {
3341 return view_->GetTopLevelNativeWindow();
3342}
3343
3344void TabContents::OnMessageBoxClosed(IPC::Message* reply_msg,
3345 bool success,
3346 const std::wstring& prompt) {
3347 last_javascript_message_dismissal_ = base::TimeTicks::Now();
3348 if (is_showing_before_unload_dialog_ && !success) {
3349 // If a beforeunload dialog is canceled, we need to stop the throbber from
3350 // spinning, since we forced it to start spinning in Navigate.
3351 DidStopLoading();
3352
3353 tab_close_start_time_ = base::TimeTicks();
3354 }
3355 is_showing_before_unload_dialog_ = false;
3356 render_view_host()->JavaScriptMessageBoxClosed(reply_msg, success, prompt);
3357}
3358
3359void TabContents::SetSuppressMessageBoxes(bool suppress_message_boxes) {
3360 set_suppress_javascript_messages(suppress_message_boxes);
3361}
3362
[email protected]3a3d47472010-07-15 21:03:543363TabContents* TabContents::AsTabContents() {
3364 return this;
3365}
3366
3367ExtensionHost* TabContents::AsExtensionHost() {
3368 return NULL;
3369}
3370
[email protected]41fc0322009-09-04 22:23:403371void TabContents::set_encoding(const std::string& encoding) {
[email protected]aacd7cc2009-09-02 14:51:103372 encoding_ = CharacterEncoding::GetCanonicalEncodingNameByAliasName(encoding);
3373}
[email protected]f45d2a72010-03-08 23:28:353374
3375void TabContents::SetAppIcon(const SkBitmap& app_icon) {
3376 app_icon_ = app_icon;
3377 NotifyNavigationStateChanged(INVALIDATE_TITLE);
3378}
[email protected]33f74972010-12-08 16:40:363379
3380void TabContents::SwapInRenderViewHost(RenderViewHost* rvh) {
3381 render_manager_.SwapInRenderViewHost(rvh);
3382}
3383
3384void TabContents::CreateViewAndSetSizeForRVH(RenderViewHost* rvh) {
3385 RenderWidgetHostView* rwh_view = view()->CreateViewForWidget(rvh);
3386 rwh_view->SetSize(view()->GetContainerSize());
3387}