blob: 46498e31d9d611206744c7e2c3970debc5017ee6 [file] [log] [blame]
[email protected]7e7414ae2010-01-26 20:19:291// Copyright (c) 2010 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]39553442010-09-09 00:01:5511#if defined(OS_MACOSX)
12#include "app/surface/io_surface_support_mac.h"
13#endif
[email protected]614519812010-03-19 08:20:5614#include "app/text_elider.h"
[email protected]8cc8d492010-02-02 10:40:4915#include "base/auto_reset.h"
[email protected]420ae012009-04-24 05:16:3216#include "base/file_version_info.h"
[email protected]7cf1b6ce2010-03-20 06:37:0117#include "base/i18n/rtl.h"
[email protected]835d7c82010-10-14 04:38:3818#include "base/metrics/histogram.h"
[email protected]420ae012009-04-24 05:16:3219#include "base/process_util.h"
[email protected]96d185d2009-04-24 03:28:5420#include "base/string16.h"
[email protected]996fd702009-09-04 19:12:3721#include "base/string_util.h"
[email protected]440b37b22010-08-30 05:31:4022#include "base/utf_string_conversions.h"
[email protected]96d185d2009-04-24 03:28:5423#include "base/time.h"
[email protected]6db0def92010-03-02 19:55:4524#include "chrome/browser/autocomplete_history_manager.h"
[email protected]21674df2009-10-28 19:51:2125#include "chrome/browser/autofill/autofill_manager.h"
[email protected]c65e2f152010-10-14 15:30:4026#include "chrome/browser/blocked_content_container.h"
[email protected]420ae012009-04-24 05:16:3227#include "chrome/browser/bookmarks/bookmark_model.h"
[email protected]96d185d2009-04-24 03:28:5428#include "chrome/browser/browser_process.h"
[email protected]cf68b092010-01-15 20:05:5529#include "chrome/browser/browser_shutdown.h"
[email protected]aacd7cc2009-09-02 14:51:1030#include "chrome/browser/character_encoding.h"
[email protected]5f24f212010-12-02 14:47:4531#include "chrome/browser/content_settings/content_settings_details.h"
[email protected]9d01a6a2010-11-30 12:03:3332#include "chrome/browser/content_settings/host_content_settings_map.h"
[email protected]96d185d2009-04-24 03:28:5433#include "chrome/browser/debugger/devtools_manager.h"
[email protected]c8f74942010-09-03 10:04:1434#include "chrome/browser/defaults.h"
[email protected]420ae012009-04-24 05:16:3235#include "chrome/browser/dom_operation_notification_details.h"
[email protected]96d185d2009-04-24 03:28:5436#include "chrome/browser/dom_ui/dom_ui.h"
37#include "chrome/browser/dom_ui/dom_ui_factory.h"
[email protected]bcef0dc02009-02-28 00:35:0238#include "chrome/browser/download/download_item_model.h"
[email protected]420ae012009-04-24 05:16:3239#include "chrome/browser/download/download_manager.h"
[email protected]41291322010-07-15 17:09:0140#include "chrome/browser/download/download_request_limiter.h"
[email protected]e7eaedde2009-09-25 20:09:4941#include "chrome/browser/external_protocol_handler.h"
[email protected]eaa7dd182010-12-14 11:09:0042#include "chrome/browser/extensions/extension_service.h"
[email protected]ef0d04c2010-12-15 23:34:2143#include "chrome/browser/history/history.h"
[email protected]ec0b6c42010-08-26 03:16:5844#include "chrome/browser/history/history_types.h"
[email protected]523623c2010-05-19 16:03:4245#include "chrome/browser/history/top_sites.h"
[email protected]b75b8292010-10-01 07:28:2546#include "chrome/browser/host_zoom_map.h"
[email protected]0189bc722009-08-28 21:56:4847#include "chrome/browser/favicon_service.h"
[email protected]ba70d082010-09-10 16:54:4948#include "chrome/browser/file_select_helper.h"
[email protected]f7578f52010-08-30 22:22:4949#include "chrome/browser/google/google_util.h"
[email protected]8897c382009-05-06 17:53:2650#include "chrome/browser/hung_renderer_dialog.h"
[email protected]6ee12c42010-09-14 09:36:0751#include "chrome/browser/in_process_webkit/session_storage_namespace.h"
[email protected]420ae012009-04-24 05:16:3252#include "chrome/browser/load_from_memory_cache_details.h"
53#include "chrome/browser/load_notification_details.h"
[email protected]84abba62009-10-07 17:01:4454#include "chrome/browser/metrics/metric_event_duration_details.h"
[email protected]ea0e80892009-08-22 00:36:0555#include "chrome/browser/modal_html_dialog_delegate.h"
[email protected]c8865482009-07-23 20:40:1056#include "chrome/browser/omnibox_search_hint.h"
[email protected]14a000d2010-04-29 21:44:2457#include "chrome/browser/platform_util.h"
[email protected]fdd61c62009-04-22 19:22:5758#include "chrome/browser/plugin_installer.h"
[email protected]37858e52010-08-26 00:22:0259#include "chrome/browser/prefs/pref_service.h"
[email protected]33f74972010-12-08 16:40:3660#include "chrome/browser/prerender/prerender_manager.h"
[email protected]dbeebd52010-11-16 20:34:1661#include "chrome/browser/printing/print_preview_tab_controller.h"
[email protected]12802702010-07-09 19:43:0962#include "chrome/browser/printing/print_view_manager.h"
[email protected]8ecad5e2010-12-02 21:18:3363#include "chrome/browser/profiles/profile.h"
[email protected]1db6ff152009-10-12 15:32:0764#include "chrome/browser/renderer_host/render_process_host.h"
[email protected]8cb5d5b2010-02-09 11:36:1665#include "chrome/browser/renderer_host/render_view_host.h"
[email protected]96d185d2009-04-24 03:28:5466#include "chrome/browser/renderer_host/render_widget_host_view.h"
[email protected]fd694982009-07-03 17:32:3767#include "chrome/browser/renderer_host/resource_request_details.h"
[email protected]1db6ff152009-10-12 15:32:0768#include "chrome/browser/renderer_host/site_instance.h"
[email protected]96d185d2009-04-24 03:28:5469#include "chrome/browser/renderer_host/web_cache_manager.h"
[email protected]93623c5d2009-12-10 21:40:3270#include "chrome/browser/renderer_preferences_util.h"
[email protected]8b62334b2010-08-31 22:37:1171#include "chrome/browser/search_engines/template_url.h"
[email protected]152b8ba32010-03-29 19:40:1672#include "chrome/browser/search_engines/template_url_fetcher.h"
[email protected]0b5b85c2010-10-11 23:00:1273#include "chrome/browser/search_engines/template_url_fetcher_ui_callbacks.h"
[email protected]152b8ba32010-03-29 19:40:1674#include "chrome/browser/search_engines/template_url_model.h"
[email protected]c37e3b62010-01-22 00:15:1775#include "chrome/browser/sessions/session_types.h"
[email protected]bd1ad682009-05-15 22:19:1776#include "chrome/browser/tab_contents/infobar_delegate.h"
[email protected]7e383692009-06-12 19:14:5477#include "chrome/browser/tab_contents/interstitial_page.h"
[email protected]f3ec7742009-01-15 00:59:1678#include "chrome/browser/tab_contents/navigation_entry.h"
[email protected]de34a962009-06-26 23:22:4379#include "chrome/browser/tab_contents/provisional_load_details.h"
[email protected]f3ec7742009-01-15 00:59:1680#include "chrome/browser/tab_contents/tab_contents_delegate.h"
[email protected]1f18184a2010-07-21 19:34:4981#include "chrome/browser/tab_contents/tab_contents_ssl_helper.h"
[email protected]fdd61c62009-04-22 19:22:5782#include "chrome/browser/tab_contents/tab_contents_view.h"
[email protected]3c9e1872010-11-18 16:17:4983#include "chrome/browser/tab_contents/web_navigation_observer.h"
[email protected]152b8ba32010-03-29 19:40:1684#include "chrome/browser/translate/page_translated_details.h"
[email protected]1caa1522010-12-01 20:23:3585#include "chrome/browser/ui/app_modal_dialogs/message_box_handler.h"
[email protected]45300ad42010-12-02 15:51:1486#include "chrome/browser/ui/find_bar/find_bar_state.h"
[email protected]54ec6472010-04-09 19:39:5887#include "chrome/common/bindings_policy.h"
[email protected]35f7d212009-04-29 21:19:2788#include "chrome/common/chrome_switches.h"
[email protected]de4761b2010-12-07 21:05:2189#include "chrome/common/content_restriction.h"
[email protected]3b355642010-02-05 16:01:4990#include "chrome/common/extensions/extension.h"
[email protected]5d246db22009-10-27 06:17:5791#include "chrome/common/extensions/extension_action.h"
[email protected]807871f2010-09-16 01:04:4892#include "chrome/common/extensions/extension_icon_set.h"
[email protected]942690b132010-05-11 06:42:1493#include "chrome/common/extensions/extension_resource.h"
[email protected]12802702010-07-09 19:43:0994#include "chrome/common/extensions/url_pattern.h"
95#include "chrome/common/navigation_types.h"
96#include "chrome/common/net/url_request_context_getter.h"
[email protected]bfd04a62009-02-01 18:16:5697#include "chrome/common/notification_service.h"
initial.commit09911bf2008-07-26 23:55:2998#include "chrome/common/pref_names.h"
[email protected]420ae012009-04-24 05:16:3299#include "chrome/common/render_messages.h"
[email protected]939856a2010-08-24 20:29:02100#include "chrome/common/render_messages_params.h"
[email protected]1ee614862009-07-06 20:29:18101#include "chrome/common/renderer_preferences.h"
[email protected]96d185d2009-04-24 03:28:54102#include "chrome/common/url_constants.h"
[email protected]ef0d04c2010-12-15 23:34:21103#include "gfx/codec/png_codec.h"
[email protected]ea68c06a2010-04-23 17:25:37104#include "grit/chromium_strings.h"
[email protected]8897c382009-05-06 17:53:26105#include "grit/generated_resources.h"
[email protected]420ae012009-04-24 05:16:32106#include "grit/locale_settings.h"
[email protected]fd626782010-01-22 05:26:06107#include "grit/platform_locale_settings.h"
[email protected]ea68c06a2010-04-23 17:25:37108#include "grit/theme_resources.h"
[email protected]420ae012009-04-24 05:16:32109#include "net/base/mime_util.h"
110#include "net/base/net_errors.h"
[email protected]d686e812009-06-03 19:10:29111#include "net/base/net_util.h"
[email protected]420ae012009-04-24 05:16:32112#include "net/base/registry_controlled_domain.h"
[email protected]b75b8292010-10-01 07:28:25113#include "third_party/WebKit/WebKit/chromium/public/WebView.h"
[email protected]1ee614862009-07-06 20:29:18114#include "webkit/glue/webpreferences.h"
[email protected]d5e311b2010-07-20 17:15:03115#include "webkit/glue/password_form.h"
[email protected]45446a52010-11-04 17:41:00116#include "webkit/glue/plugins/plugin_list.h"
[email protected]3e45ba92009-02-20 21:09:00117
[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) {
258 tab_contents->AddInfoBar(this);
259 }
260
261 virtual int GetButtons() const {
262 return BUTTON_OK | BUTTON_CANCEL | BUTTON_OK_DEFAULT;
263 }
264
[email protected]e23d3a32010-08-13 19:39:58265 virtual string16 GetButtonLabel(InfoBarButton button) const {
[email protected]851b1eb2010-08-09 13:32:29266 if (button == BUTTON_CANCEL)
[email protected]e23d3a32010-08-13 19:39:58267 return l10n_util::GetStringUTF16(IDS_PLUGIN_ENABLE_TEMPORARILY);
[email protected]851b1eb2010-08-09 13:32:29268 if (button == BUTTON_OK)
[email protected]e23d3a32010-08-13 19:39:58269 return l10n_util::GetStringUTF16(IDS_PLUGIN_UPDATE);
[email protected]851b1eb2010-08-09 13:32:29270 return ConfirmInfoBarDelegate::GetButtonLabel(button);
271 }
272
[email protected]e23d3a32010-08-13 19:39:58273 virtual string16 GetMessageText() const {
274 return l10n_util::GetStringFUTF16(IDS_PLUGIN_OUTDATED_PROMPT, name_);
[email protected]851b1eb2010-08-09 13:32:29275 }
276
[email protected]e23d3a32010-08-13 19:39:58277 virtual string16 GetLinkText() {
278 return l10n_util::GetStringUTF16(IDS_LEARN_MORE);
[email protected]851b1eb2010-08-09 13:32:29279 }
280
281 virtual SkBitmap* GetIcon() const {
282 return ResourceBundle::GetSharedInstance().GetBitmapNamed(
283 IDR_INFOBAR_PLUGIN_INSTALL);
284 }
285
286 virtual bool Accept() {
287 tab_contents_->OpenURL(update_url_, GURL(),
[email protected]57b66d02010-09-30 11:24:51288 NEW_FOREGROUND_TAB, PageTransition::LINK);
[email protected]851b1eb2010-08-09 13:32:29289 return false;
290 }
291
292 virtual bool Cancel() {
[email protected]3c2826852010-12-16 19:09:14293 tab_contents_->render_view_host()->LoadBlockedPlugins();
294 return true;
[email protected]851b1eb2010-08-09 13:32:29295 }
296
297 virtual bool LinkClicked(WindowOpenDisposition disposition) {
298 // TODO(bauerb): Navigate to a help page explaining why we disabled
299 // the plugin, once we have one.
300 return false;
301 }
302
303 private:
304 TabContents* tab_contents_;
305 string16 name_;
306 GURL update_url_;
307};
308
[email protected]420ae012009-04-24 05:16:32309} // namespace
310
311// -----------------------------------------------------------------------------
312
[email protected]fdd61c62009-04-22 19:22:57313// static
314int TabContents::find_request_id_counter_ = -1;
315
[email protected]420ae012009-04-24 05:16:32316
[email protected]420ae012009-04-24 05:16:32317TabContents::TabContents(Profile* profile,
318 SiteInstance* site_instance,
319 int routing_id,
[email protected]6ee12c42010-09-14 09:36:07320 const TabContents* base_tab_contents,
321 SessionStorageNamespace* session_storage_namespace)
[email protected]b680ad22009-04-15 23:19:42322 : delegate_(NULL),
[email protected]6ee12c42010-09-14 09:36:07323 ALLOW_THIS_IN_INITIALIZER_LIST(controller_(
324 this, profile, session_storage_namespace)),
[email protected]66ba4932009-06-04 19:22:13325 ALLOW_THIS_IN_INITIALIZER_LIST(view_(
[email protected]d82ed61e2009-06-16 02:46:22326 TabContentsView::Create(this))),
327 ALLOW_THIS_IN_INITIALIZER_LIST(render_manager_(this, this)),
[email protected]fdd61c62009-04-22 19:22:57328 property_bag_(),
329 registrar_(),
[email protected]12802702010-07-09 19:43:09330 ALLOW_THIS_IN_INITIALIZER_LIST(printing_(
331 new printing::PrintViewManager(*this))),
[email protected]fdd61c62009-04-22 19:22:57332 save_package_(),
[email protected]6db0def92010-03-02 19:55:45333 autocomplete_history_manager_(),
[email protected]21674df2009-10-28 19:51:21334 autofill_manager_(),
[email protected]fdd61c62009-04-22 19:22:57335 plugin_installer_(),
[email protected]6f821dd2010-02-23 00:54:06336 bookmark_drag_(NULL),
[email protected]d82ed61e2009-06-16 02:46:22337 ALLOW_THIS_IN_INITIALIZER_LIST(fav_icon_helper_(this)),
[email protected]d5f942ba2008-09-26 19:30:34338 is_loading_(false),
[email protected]443b80e2010-12-14 00:42:23339 crashed_status_(base::TERMINATION_STATUS_STILL_RUNNING),
340 crashed_error_code_(0),
[email protected]d5f942ba2008-09-26 19:30:34341 waiting_for_response_(false),
[email protected]d5f942ba2008-09-26 19:30:34342 max_page_id_(-1),
[email protected]fdd61c62009-04-22 19:22:57343 current_load_start_(),
344 load_state_(net::LOAD_STATE_IDLE),
345 load_state_host_(),
[email protected]094e5b22009-09-25 04:23:56346 upload_size_(0),
347 upload_position_(0),
[email protected]fdd61c62009-04-22 19:22:57348 received_page_title_(false),
349 is_starred_(false),
350 contents_mime_type_(),
351 encoding_(),
[email protected]c65e2f152010-10-14 15:30:40352 blocked_contents_(NULL),
353 all_contents_blocked_(false),
[email protected]8cc8d492010-02-02 10:40:49354 dont_notify_render_view_(false),
[email protected]f17a0ee2010-05-17 17:38:47355 displayed_insecure_content_(false),
[email protected]fdd61c62009-04-22 19:22:57356 infobar_delegates_(),
[email protected]fdd61c62009-04-22 19:22:57357 find_ui_active_(false),
358 find_op_aborted_(false),
359 current_find_request_id_(find_request_id_counter_++),
[email protected]e491f1c2009-05-22 20:28:12360 last_search_case_sensitive_(false),
[email protected]e491f1c2009-05-22 20:28:12361 last_search_result_(),
[email protected]98aa0b52010-05-06 17:03:08362 extension_app_(NULL),
[email protected]fdd61c62009-04-22 19:22:57363 capturing_contents_(false),
364 is_being_destroyed_(false),
365 notify_disconnection_(false),
366 history_requests_(),
367#if defined(OS_WIN)
368 message_box_active_(CreateEvent(NULL, TRUE, FALSE, NULL)),
369#endif
370 last_javascript_message_dismissal_(),
[email protected]c2e74fe82009-09-03 17:57:44371 suppress_javascript_messages_(false),
[email protected]7ab1e7d62009-10-14 23:32:01372 is_showing_before_unload_dialog_(false),
[email protected]7ff431e2010-01-07 18:18:54373 renderer_preferences_(),
[email protected]1a242c32010-02-02 21:20:54374 opener_dom_ui_type_(DOMUIFactory::kNoDOMUI),
[email protected]777a7b62010-05-11 13:28:16375 language_state_(&controller_),
[email protected]ebf40a72010-07-22 01:46:38376 closed_by_user_gesture_(false),
[email protected]b75b8292010-10-01 07:28:25377 minimum_zoom_percent_(
378 static_cast<int>(WebKit::WebView::minTextSizeMultiplier * 100)),
379 maximum_zoom_percent_(
380 static_cast<int>(WebKit::WebView::maxTextSizeMultiplier * 100)),
[email protected]9e823662010-10-13 23:36:00381 temporary_zoom_settings_(false),
382 content_restrictions_(0) {
[email protected]7ff431e2010-01-07 18:18:54383 renderer_preferences_util::UpdateFromSystemSettings(
384 &renderer_preferences_, profile);
385
[email protected]9fb83e82010-07-02 18:24:55386 content_settings_delegate_.reset(
387 new TabSpecificContentSettings(this, profile));
388
[email protected]12636df2009-09-28 22:32:21389 render_manager_.Init(profile, site_instance, routing_id);
[email protected]420ae012009-04-24 05:16:32390
[email protected]34ac70502009-09-25 17:07:23391 // We have the initial size of the view be based on the size of the passed in
392 // tab contents (normally a tab from the same window).
393 view_->CreateView(base_tab_contents ?
394 base_tab_contents->view()->GetContainerSize() : gfx::Size());
[email protected]420ae012009-04-24 05:16:32395
396 // Register for notifications about all interested prefs change.
397 PrefService* prefs = profile->GetPrefs();
[email protected]2fb7dc982010-09-29 12:24:28398 pref_change_registrar_.Init(prefs);
[email protected]420ae012009-04-24 05:16:32399 if (prefs) {
400 for (int i = 0; i < kPrefsToObserveLength; ++i)
[email protected]2fb7dc982010-09-29 12:24:28401 pref_change_registrar_.Add(kPrefsToObserve[i], this);
[email protected]420ae012009-04-24 05:16:32402 }
403
404 // Register for notifications about URL starredness changing on any profile.
405 registrar_.Add(this, NotificationType::URLS_STARRED,
406 NotificationService::AllSources());
407 registrar_.Add(this, NotificationType::BOOKMARK_MODEL_LOADED,
408 NotificationService::AllSources());
409 registrar_.Add(this, NotificationType::RENDER_WIDGET_HOST_DESTROYED,
410 NotificationService::AllSources());
[email protected]d3ba77272009-09-03 00:06:09411#if defined(OS_LINUX)
412 registrar_.Add(this, NotificationType::BROWSER_THEME_CHANGED,
413 NotificationService::AllSources());
414#endif
[email protected]420ae012009-04-24 05:16:32415
[email protected]6c32ce72010-03-08 05:18:06416 registrar_.Add(this, NotificationType::USER_STYLE_SHEET_UPDATED,
417 NotificationService::AllSources());
418
[email protected]79580c62010-02-02 02:36:25419 // Register for notifications about content setting changes.
420 registrar_.Add(this, NotificationType::CONTENT_SETTINGS_CHANGED,
421 NotificationService::AllSources());
422
[email protected]a888b29e62010-04-01 13:38:57423 // Listen for extension changes so we can update extension_for_current_page_.
424 registrar_.Add(this, NotificationType::EXTENSION_LOADED,
425 NotificationService::AllSources());
426 registrar_.Add(this, NotificationType::EXTENSION_UNLOADED,
427 NotificationService::AllSources());
428 registrar_.Add(this, NotificationType::EXTENSION_UNLOADED_DISABLED,
429 NotificationService::AllSources());
430
[email protected]88152052010-10-01 04:05:18431 // Listen for Google URL changes
432 registrar_.Add(this, NotificationType::GOOGLE_URL_UPDATED,
433 NotificationService::AllSources());
434
[email protected]c8865482009-07-23 20:40:10435 // Set-up the showing of the omnibox search infobar if applicable.
436 if (OmniboxSearchHint::IsEnabled(profile))
437 omnibox_search_hint_.reset(new OmniboxSearchHint(this));
[email protected]332af7732009-03-11 13:21:35438}
initial.commit09911bf2008-07-26 23:55:29439
440TabContents::~TabContents() {
[email protected]420ae012009-04-24 05:16:32441 is_being_destroyed_ = true;
442
[email protected]bfe4c1582009-10-02 18:11:09443 // We don't want any notifications while we're running our destructor.
[email protected]420ae012009-04-24 05:16:32444 registrar_.RemoveAll();
[email protected]2fb7dc982010-09-29 12:24:28445 pref_change_registrar_.RemoveAll();
[email protected]420ae012009-04-24 05:16:32446
[email protected]420ae012009-04-24 05:16:32447 NotifyDisconnected();
[email protected]e7b8d832009-11-20 22:41:00448 hung_renderer_dialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:32449
[email protected]420ae012009-04-24 05:16:32450 // First cleanly close all child windows.
451 // TODO(mpcomplete): handle case if MaybeCloseChildWindows() already asked
452 // some of these to close. CloseWindows is async, so it might get called
453 // twice before it runs.
[email protected]8cc8d492010-02-02 10:40:49454 CloseConstrainedWindows();
[email protected]420ae012009-04-24 05:16:32455
[email protected]c65e2f152010-10-14 15:30:40456 // Close all blocked contents.
457 if (blocked_contents_)
458 blocked_contents_->Destroy();
[email protected]66ba4932009-06-04 19:22:13459
[email protected]420ae012009-04-24 05:16:32460 // Notify any observer that have a reference on this tab contents.
461 NotificationService::current()->Notify(
462 NotificationType::TAB_CONTENTS_DESTROYED,
463 Source<TabContents>(this),
464 NotificationService::NoDetails());
465
[email protected]1e5e74f2009-05-27 20:55:12466 // Notify any lasting InfobarDelegates that have not yet been removed that
467 // whatever infobar they were handling in this TabContents has closed,
468 // because the TabContents is going away entirely.
469 // This must happen after the TAB_CONTENTS_DESTROYED notification as the
470 // notification may trigger infobars calls that access their delegate. (and
471 // some implementations of InfoBarDelegate do delete themselves on
472 // InfoBarClosed()).
473 for (int i = 0; i < infobar_delegate_count(); ++i) {
474 InfoBarDelegate* delegate = GetInfoBarDelegateAt(i);
475 delegate->InfoBarClosed();
476 }
477 infobar_delegates_.clear();
478
[email protected]420ae012009-04-24 05:16:32479 // TODO(brettw) this should be moved to the view.
480#if defined(OS_WIN)
481 // If we still have a window handle, destroy it. GetNativeView can return
482 // NULL if this contents was part of a window that closed.
[email protected]50bd6452010-11-27 19:39:42483 if (GetNativeView()) {
484 RenderViewHost* host = render_view_host();
485 if (host && host->view()) {
486 host->view()->WillWmDestroy();
487 }
[email protected]420ae012009-04-24 05:16:32488 ::DestroyWindow(GetNativeView());
[email protected]50bd6452010-11-27 19:39:42489 }
[email protected]420ae012009-04-24 05:16:32490#endif
[email protected]7ab1e7d62009-10-14 23:32:01491
492 // OnCloseStarted isn't called in unit tests.
493 if (!tab_close_start_time_.is_null()) {
494 UMA_HISTOGRAM_TIMES("Tab.Close",
495 base::TimeTicks::Now() - tab_close_start_time_);
496 }
initial.commit09911bf2008-07-26 23:55:29497}
498
[email protected]d5f942ba2008-09-26 19:30:34499// static
500void TabContents::RegisterUserPrefs(PrefService* prefs) {
[email protected]420ae012009-04-24 05:16:32501 prefs->RegisterBooleanPref(prefs::kAlternateErrorPagesEnabled, true);
502
503 WebPreferences pref_defaults;
504 prefs->RegisterBooleanPref(prefs::kWebKitJavascriptEnabled,
505 pref_defaults.javascript_enabled);
506 prefs->RegisterBooleanPref(prefs::kWebKitWebSecurityEnabled,
507 pref_defaults.web_security_enabled);
508 prefs->RegisterBooleanPref(
509 prefs::kWebKitJavascriptCanOpenWindowsAutomatically, true);
510 prefs->RegisterBooleanPref(prefs::kWebKitLoadsImagesAutomatically,
511 pref_defaults.loads_images_automatically);
512 prefs->RegisterBooleanPref(prefs::kWebKitPluginsEnabled,
513 pref_defaults.plugins_enabled);
514 prefs->RegisterBooleanPref(prefs::kWebKitDomPasteEnabled,
515 pref_defaults.dom_paste_enabled);
516 prefs->RegisterBooleanPref(prefs::kWebKitShrinksStandaloneImagesToFit,
517 pref_defaults.shrinks_standalone_images_to_fit);
[email protected]c4e98902010-06-01 10:20:14518 prefs->RegisterDictionaryPref(prefs::kWebKitInspectorSettings);
[email protected]420ae012009-04-24 05:16:32519 prefs->RegisterBooleanPref(prefs::kWebKitTextAreasAreResizable,
520 pref_defaults.text_areas_are_resizable);
521 prefs->RegisterBooleanPref(prefs::kWebKitJavaEnabled,
522 pref_defaults.java_enabled);
[email protected]1661a3c02010-05-20 16:32:36523 prefs->RegisterBooleanPref(prefs::kWebkitTabsToLinks,
524 pref_defaults.tabs_to_links);
[email protected]420ae012009-04-24 05:16:32525
526 prefs->RegisterLocalizedStringPref(prefs::kAcceptLanguages,
527 IDS_ACCEPT_LANGUAGES);
528 prefs->RegisterLocalizedStringPref(prefs::kDefaultCharset,
529 IDS_DEFAULT_ENCODING);
530 prefs->RegisterLocalizedBooleanPref(prefs::kWebKitStandardFontIsSerif,
531 IDS_STANDARD_FONT_IS_SERIF);
532 prefs->RegisterLocalizedStringPref(prefs::kWebKitFixedFontFamily,
533 IDS_FIXED_FONT_FAMILY);
534 prefs->RegisterLocalizedStringPref(prefs::kWebKitSerifFontFamily,
535 IDS_SERIF_FONT_FAMILY);
536 prefs->RegisterLocalizedStringPref(prefs::kWebKitSansSerifFontFamily,
537 IDS_SANS_SERIF_FONT_FAMILY);
538 prefs->RegisterLocalizedStringPref(prefs::kWebKitCursiveFontFamily,
539 IDS_CURSIVE_FONT_FAMILY);
540 prefs->RegisterLocalizedStringPref(prefs::kWebKitFantasyFontFamily,
541 IDS_FANTASY_FONT_FAMILY);
542 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitDefaultFontSize,
543 IDS_DEFAULT_FONT_SIZE);
544 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitDefaultFixedFontSize,
545 IDS_DEFAULT_FIXED_FONT_SIZE);
546 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitMinimumFontSize,
547 IDS_MINIMUM_FONT_SIZE);
548 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitMinimumLogicalFontSize,
549 IDS_MINIMUM_LOGICAL_FONT_SIZE);
550 prefs->RegisterLocalizedBooleanPref(prefs::kWebKitUsesUniversalDetector,
551 IDS_USES_UNIVERSAL_DETECTOR);
552 prefs->RegisterLocalizedStringPref(prefs::kStaticEncodings,
553 IDS_STATIC_ENCODING_LIST);
initial.commit09911bf2008-07-26 23:55:29554}
555
[email protected]13367f72009-07-08 16:39:02556// Returns true if contains content rendered by an extension.
557bool TabContents::HostsExtension() const {
558 return GetURL().SchemeIs(chrome::kExtensionScheme);
[email protected]d5f942ba2008-09-26 19:30:34559}
560
[email protected]cf28ec52010-04-09 22:56:34561AutoFillManager* TabContents::GetAutoFillManager() {
562 if (autofill_manager_.get() == NULL)
563 autofill_manager_.reset(new AutoFillManager(this));
564 return autofill_manager_.get();
565}
566
[email protected]6e95706d2009-04-23 22:55:01567PluginInstaller* TabContents::GetPluginInstaller() {
568 if (plugin_installer_.get() == NULL)
569 plugin_installer_.reset(new PluginInstaller(this));
570 return plugin_installer_.get();
571}
572
[email protected]1f18184a2010-07-21 19:34:49573TabContentsSSLHelper* TabContents::GetSSLHelper() {
574 if (ssl_helper_.get() == NULL)
575 ssl_helper_.reset(new TabContentsSSLHelper(this));
576 return ssl_helper_.get();
577}
578
[email protected]8cb5d5b2010-02-09 11:36:16579RenderProcessHost* TabContents::GetRenderProcessHost() const {
580 return render_manager_.current_host()->process();
581}
582
[email protected]9adb9692010-10-29 23:14:02583void TabContents::SetExtensionApp(const Extension* extension) {
[email protected]867a73e12010-03-19 20:45:46584 DCHECK(!extension || extension->GetFullLaunchURL().is_valid());
[email protected]98aa0b52010-05-06 17:03:08585 extension_app_ = extension;
[email protected]fca656c2010-02-10 20:30:10586
[email protected]98aa0b52010-05-06 17:03:08587 UpdateExtensionAppIcon(extension_app_);
[email protected]a888b29e62010-04-01 13:38:57588
[email protected]fca656c2010-02-10 20:30:10589 NotificationService::current()->Notify(
590 NotificationType::TAB_CONTENTS_APPLICATION_EXTENSION_CHANGED,
591 Source<TabContents>(this),
592 NotificationService::NoDetails());
[email protected]3b355642010-02-05 16:01:49593}
594
[email protected]98aa0b52010-05-06 17:03:08595void TabContents::SetExtensionAppById(const std::string& extension_app_id) {
596 if (extension_app_id.empty())
[email protected]911f8152010-03-18 16:46:40597 return;
598
[email protected]eaa7dd182010-12-14 11:09:00599 ExtensionService* extension_service = profile()->GetExtensionService();
[email protected]fa6a9102010-11-22 15:38:50600 if (!extension_service || !extension_service->is_ready())
601 return;
602
603 const Extension* extension =
604 extension_service->GetExtensionById(extension_app_id, false);
605 if (extension)
606 SetExtensionApp(extension);
[email protected]911f8152010-03-18 16:46:40607}
608
[email protected]98aa0b52010-05-06 17:03:08609SkBitmap* TabContents::GetExtensionAppIcon() {
[email protected]98aa0b52010-05-06 17:03:08610 if (extension_app_icon_.empty())
[email protected]8eb043b2010-04-30 07:19:51611 return NULL;
612
[email protected]98aa0b52010-05-06 17:03:08613 return &extension_app_icon_;
[email protected]8eb043b2010-04-30 07:19:51614}
615
[email protected]d5f942ba2008-09-26 19:30:34616const GURL& TabContents::GetURL() const {
617 // We may not have a navigation entry yet
[email protected]ce3fa3c2009-04-20 19:55:57618 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]ebe89e062009-08-13 23:16:54619 return entry ? entry->virtual_url() : GURL::EmptyGURL();
[email protected]d5f942ba2008-09-26 19:30:34620}
621
[email protected]96d185d2009-04-24 03:28:54622const string16& TabContents::GetTitle() const {
[email protected]4c6092c5b2009-06-06 00:23:55623 // Transient entries take precedence. They are used for interstitial pages
624 // that are shown on top of existing pages.
625 NavigationEntry* entry = controller_.GetTransientEntry();
626 if (entry)
627 return entry->GetTitleForDisplay(&controller_);
628
[email protected]96d185d2009-04-24 03:28:54629 DOMUI* our_dom_ui = render_manager_.pending_dom_ui() ?
630 render_manager_.pending_dom_ui() : render_manager_.dom_ui();
631 if (our_dom_ui) {
632 // Don't override the title in view source mode.
[email protected]4c6092c5b2009-06-06 00:23:55633 entry = controller_.GetActiveEntry();
[email protected]96d185d2009-04-24 03:28:54634 if (!(entry && entry->IsViewSourceMode())) {
635 // Give the DOM UI the chance to override our title.
636 const string16& title = our_dom_ui->overridden_title();
637 if (!title.empty())
638 return title;
639 }
640 }
641
642 // We use the title for the last committed entry rather than a pending
643 // navigation entry. For example, when the user types in a URL, we want to
644 // keep the old page's title until the new load has committed and we get a new
645 // title.
[email protected]96d185d2009-04-24 03:28:54646 entry = controller_.GetLastCommittedEntry();
647 if (entry)
648 return entry->GetTitleForDisplay(&controller_);
[email protected]96d185d2009-04-24 03:28:54649 return EmptyString16();
650}
651
[email protected]bbbc1ef2010-02-12 18:03:41652// static
653string16 TabContents::GetDefaultTitle() {
654 return l10n_util::GetStringUTF16(IDS_DEFAULT_TAB_TITLE);
655}
656
[email protected]d5f942ba2008-09-26 19:30:34657int32 TabContents::GetMaxPageID() {
658 if (GetSiteInstance())
659 return GetSiteInstance()->max_page_id();
660 else
661 return max_page_id_;
662}
663
664void TabContents::UpdateMaxPageID(int32 page_id) {
665 // Ensure both the SiteInstance and RenderProcessHost update their max page
[email protected]57c6a652009-05-04 07:58:34666 // IDs in sync. Only TabContents will also have site instances, except during
[email protected]d5f942ba2008-09-26 19:30:34667 // testing.
668 if (GetSiteInstance())
669 GetSiteInstance()->UpdateMaxPageID(page_id);
[email protected]8cb5d5b2010-02-09 11:36:16670 GetRenderProcessHost()->UpdateMaxPageID(page_id);
[email protected]d5f942ba2008-09-26 19:30:34671}
672
[email protected]96d185d2009-04-24 03:28:54673SiteInstance* TabContents::GetSiteInstance() const {
674 return render_manager_.current_host()->site_instance();
675}
676
[email protected]96d185d2009-04-24 03:28:54677bool TabContents::ShouldDisplayURL() {
[email protected]8ab24cc2009-11-13 20:56:15678 // Don't hide the url in view source mode and with interstitials.
[email protected]96d185d2009-04-24 03:28:54679 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]8ab24cc2009-11-13 20:56:15680 if (entry && (entry->IsViewSourceMode() ||
[email protected]cccd3762010-11-12 18:40:01681 entry->page_type() == INTERSTITIAL_PAGE)) {
[email protected]96d185d2009-04-24 03:28:54682 return true;
[email protected]8ab24cc2009-11-13 20:56:15683 }
[email protected]83e3895b2009-06-11 00:07:16684
[email protected]96d185d2009-04-24 03:28:54685 DOMUI* dom_ui = GetDOMUIForCurrentState();
686 if (dom_ui)
687 return !dom_ui->should_hide_url();
688 return true;
689}
690
[email protected]d5f942ba2008-09-26 19:30:34691SkBitmap TabContents::GetFavIcon() const {
692 // Like GetTitle(), we also want to use the favicon for the last committed
693 // entry rather than a pending navigation entry.
[email protected]ce3fa3c2009-04-20 19:55:57694 NavigationEntry* entry = controller_.GetTransientEntry();
[email protected]cbab76d2008-10-13 22:42:47695 if (entry)
696 return entry->favicon().bitmap();
697
[email protected]ce3fa3c2009-04-20 19:55:57698 entry = controller_.GetLastCommittedEntry();
[email protected]d5f942ba2008-09-26 19:30:34699 if (entry)
700 return entry->favicon().bitmap();
[email protected]d5f942ba2008-09-26 19:30:34701 return SkBitmap();
702}
703
[email protected]0b303cc2009-09-28 22:35:15704bool TabContents::FavIconIsValid() const {
705 NavigationEntry* entry = controller_.GetTransientEntry();
706 if (entry)
707 return entry->favicon().is_valid();
708
709 entry = controller_.GetLastCommittedEntry();
710 if (entry)
711 return entry->favicon().is_valid();
712
713 return false;
714}
715
[email protected]96d185d2009-04-24 03:28:54716bool TabContents::ShouldDisplayFavIcon() {
717 // Always display a throbber during pending loads.
718 if (controller_.GetLastCommittedEntry() && controller_.pending_entry())
719 return true;
720
721 DOMUI* dom_ui = GetDOMUIForCurrentState();
722 if (dom_ui)
723 return !dom_ui->hide_favicon();
724 return true;
725}
726
[email protected]96d185d2009-04-24 03:28:54727std::wstring TabContents::GetStatusText() const {
728 if (!is_loading() || load_state_ == net::LOAD_STATE_IDLE)
729 return std::wstring();
730
731 switch (load_state_) {
732 case net::LOAD_STATE_WAITING_FOR_CACHE:
733 return l10n_util::GetString(IDS_LOAD_STATE_WAITING_FOR_CACHE);
[email protected]8a1f3312010-05-25 19:25:04734 case net::LOAD_STATE_ESTABLISHING_PROXY_TUNNEL:
735 return l10n_util::GetString(IDS_LOAD_STATE_ESTABLISHING_PROXY_TUNNEL);
[email protected]96d185d2009-04-24 03:28:54736 case net::LOAD_STATE_RESOLVING_PROXY_FOR_URL:
737 return l10n_util::GetString(IDS_LOAD_STATE_RESOLVING_PROXY_FOR_URL);
738 case net::LOAD_STATE_RESOLVING_HOST:
739 return l10n_util::GetString(IDS_LOAD_STATE_RESOLVING_HOST);
740 case net::LOAD_STATE_CONNECTING:
741 return l10n_util::GetString(IDS_LOAD_STATE_CONNECTING);
[email protected]375e3112010-05-27 20:32:06742 case net::LOAD_STATE_SSL_HANDSHAKE:
743 return l10n_util::GetString(IDS_LOAD_STATE_SSL_HANDSHAKE);
[email protected]96d185d2009-04-24 03:28:54744 case net::LOAD_STATE_SENDING_REQUEST:
[email protected]094e5b22009-09-25 04:23:56745 if (upload_size_)
746 return l10n_util::GetStringF(
747 IDS_LOAD_STATE_SENDING_REQUEST_WITH_PROGRESS,
748 static_cast<int>((100 * upload_position_) / upload_size_));
749 else
750 return l10n_util::GetString(IDS_LOAD_STATE_SENDING_REQUEST);
[email protected]96d185d2009-04-24 03:28:54751 case net::LOAD_STATE_WAITING_FOR_RESPONSE:
752 return l10n_util::GetStringF(IDS_LOAD_STATE_WAITING_FOR_RESPONSE,
753 load_state_host_);
754 // Ignore net::LOAD_STATE_READING_RESPONSE and net::LOAD_STATE_IDLE
755 case net::LOAD_STATE_IDLE:
756 case net::LOAD_STATE_READING_RESPONSE:
757 break;
758 }
759
760 return std::wstring();
761}
762
[email protected]3c9e1872010-11-18 16:17:49763void TabContents::AddNavigationObserver(WebNavigationObserver* observer) {
764 web_navigation_observers_.AddObserver(observer);
765}
766
767void TabContents::RemoveNavigationObserver(WebNavigationObserver* observer) {
768 web_navigation_observers_.RemoveObserver(observer);
769}
770
[email protected]443b80e2010-12-14 00:42:23771void TabContents::SetIsCrashed(base::TerminationStatus status, int error_code) {
772 if (status == crashed_status_)
[email protected]d5f942ba2008-09-26 19:30:34773 return;
774
[email protected]443b80e2010-12-14 00:42:23775 crashed_status_ = status;
776 crashed_error_code_ = error_code;
[email protected]c9cd2222009-05-06 05:16:50777 NotifyNavigationStateChanged(INVALIDATE_TAB);
[email protected]d5f942ba2008-09-26 19:30:34778}
779
[email protected]744ef172009-10-16 21:53:46780void TabContents::PageActionStateChanged() {
781 NotifyNavigationStateChanged(TabContents::INVALIDATE_PAGE_ACTIONS);
[email protected]d7eaf572009-07-01 21:57:00782}
[email protected]ce5c4502009-05-06 16:46:11783
[email protected]d5f942ba2008-09-26 19:30:34784void TabContents::NotifyNavigationStateChanged(unsigned changed_flags) {
785 if (delegate_)
786 delegate_->NavigationStateChanged(this, changed_flags);
787}
788
[email protected]96d185d2009-04-24 03:28:54789void TabContents::DidBecomeSelected() {
790 controller_.SetActive(true);
[email protected]8cb5d5b2010-02-09 11:36:16791 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
792 if (rwhv) {
793 rwhv->DidBecomeSelected();
[email protected]789e9152009-08-04 21:59:43794#if defined(OS_MACOSX)
[email protected]8cb5d5b2010-02-09 11:36:16795 rwhv->SetActive(true);
[email protected]789e9152009-08-04 21:59:43796#endif
797 }
[email protected]96d185d2009-04-24 03:28:54798
[email protected]8cb5d5b2010-02-09 11:36:16799 WebCacheManager::GetInstance()->ObserveActivity(GetRenderProcessHost()->id());
[email protected]5ac20162010-11-24 23:33:11800 last_selected_time_ = base::TimeTicks::Now();
[email protected]96d185d2009-04-24 03:28:54801}
802
[email protected]a94454b2010-12-16 04:16:03803void TabContents::FadeForInstant(bool animate) {
[email protected]1b072562010-12-13 19:37:45804 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
805 SkColor whitish = SkColorSetARGB(192, 255, 255, 255);
806 if (rwhv)
[email protected]a94454b2010-12-16 04:16:03807 rwhv->SetVisuallyDeemphasized(&whitish, animate);
[email protected]1b072562010-12-13 19:37:45808}
809
810void TabContents::CancelInstantFade() {
811 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
812 if (rwhv)
813 rwhv->SetVisuallyDeemphasized(NULL, false);
814}
815
[email protected]96d185d2009-04-24 03:28:54816void TabContents::WasHidden() {
817 if (!capturing_contents()) {
818 // |render_view_host()| can be NULL if the user middle clicks a link to open
819 // a tab in then background, then closes the tab before selecting it. This
820 // is because closing the tab calls TabContents::Destroy(), which removes
821 // the |render_view_host()|; then when we actually destroy the window,
822 // OnWindowPosChanged() notices and calls HideContents() (which calls us).
[email protected]8cb5d5b2010-02-09 11:36:16823 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
824 if (rwhv)
825 rwhv->WasHidden();
[email protected]96d185d2009-04-24 03:28:54826 }
827
828 NotificationService::current()->Notify(
829 NotificationType::TAB_CONTENTS_HIDDEN,
830 Source<TabContents>(this),
831 NotificationService::NoDetails());
832}
833
[email protected]d5f942ba2008-09-26 19:30:34834void TabContents::Activate() {
835 if (delegate_)
836 delegate_->ActivateContents(this);
837}
838
[email protected]ea42e7782010-08-23 23:58:12839void TabContents::Deactivate() {
840 if (delegate_)
841 delegate_->DeactivateContents(this);
842}
843
[email protected]96d185d2009-04-24 03:28:54844void TabContents::ShowContents() {
[email protected]8cb5d5b2010-02-09 11:36:16845 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
846 if (rwhv)
847 rwhv->DidBecomeSelected();
[email protected]96d185d2009-04-24 03:28:54848}
849
850void TabContents::HideContents() {
851 // TODO(pkasting): http://b/1239839 Right now we purposefully don't call
852 // our superclass HideContents(), because some callers want to be very picky
853 // about the order in which these get called. In addition to making the code
854 // here practically impossible to understand, this also means we end up
855 // calling TabContents::WasHidden() twice if callers call both versions of
[email protected]57c6a652009-05-04 07:58:34856 // HideContents() on a TabContents.
[email protected]96d185d2009-04-24 03:28:54857 WasHidden();
858}
859
[email protected]5aab5e22010-12-08 22:13:29860bool TabContents::NeedToFireBeforeUnload() {
861 // TODO(creis): Should we fire even for interstitial pages?
862 return notify_disconnection() &&
863 !showing_interstitial_page() &&
864 !render_view_host()->SuddenTerminationAllowed();
865}
866
[email protected]c0588052008-10-27 23:01:50867void TabContents::OpenURL(const GURL& url, const GURL& referrer,
[email protected]d5f942ba2008-09-26 19:30:34868 WindowOpenDisposition disposition,
869 PageTransition::Type transition) {
870 if (delegate_)
[email protected]c0588052008-10-27 23:01:50871 delegate_->OpenURLFromTab(this, url, referrer, disposition, transition);
[email protected]d5f942ba2008-09-26 19:30:34872}
873
[email protected]1ccb3568d2010-02-19 10:51:16874bool TabContents::NavigateToPendingEntry(
875 NavigationController::ReloadType reload_type) {
[email protected]876bc832010-09-07 16:29:54876 return NavigateToEntry(*controller_.pending_entry(), reload_type);
877}
[email protected]96d185d2009-04-24 03:28:54878
[email protected]876bc832010-09-07 16:29:54879bool TabContents::NavigateToEntry(
880 const NavigationEntry& entry,
881 NavigationController::ReloadType reload_type) {
[email protected]96d185d2009-04-24 03:28:54882 RenderViewHost* dest_render_view_host = render_manager_.Navigate(entry);
883 if (!dest_render_view_host)
884 return false; // Unable to create the desired render view host.
885
[email protected]770dd8b2010-05-24 18:11:39886 if (delegate_ && delegate_->ShouldEnablePreferredSizeNotifications()) {
887 dest_render_view_host->EnablePreferredSizeChangedMode(
888 kPreferredSizeWidth | kPreferredSizeHeightThisIsSlow);
889 }
[email protected]9fb325e2010-05-06 18:23:24890
[email protected]05fdd492010-11-15 17:52:07891 // For security, we should never send non-DOM-UI URLs to a DOM UI renderer.
892 // Double check that here.
[email protected]54ec6472010-04-09 19:39:58893 int enabled_bindings = dest_render_view_host->enabled_bindings();
[email protected]0352e5472010-04-29 00:11:40894 bool is_allowed_in_dom_ui_renderer =
[email protected]05fdd492010-11-15 17:52:07895 DOMUIFactory::IsURLAcceptableForDOMUI(profile(), entry.url());
[email protected]54ec6472010-04-09 19:39:58896 CHECK(!BindingsPolicy::is_dom_ui_enabled(enabled_bindings) ||
[email protected]0352e5472010-04-29 00:11:40897 is_allowed_in_dom_ui_renderer);
[email protected]54ec6472010-04-09 19:39:58898
[email protected]96d185d2009-04-24 03:28:54899 // Tell DevTools agent that it is attached prior to the navigation.
[email protected]d9f9b792009-06-24 13:17:12900 DevToolsManager* devtools_manager = DevToolsManager::GetInstance();
[email protected]7e8e6b62009-05-08 11:28:32901 if (devtools_manager) { // NULL in unit tests.
[email protected]3cc72b12010-03-18 23:03:00902 devtools_manager->OnNavigatingToPendingEntry(render_view_host(),
903 dest_render_view_host,
904 entry.url());
[email protected]7e8e6b62009-05-08 11:28:32905 }
[email protected]96d185d2009-04-24 03:28:54906
907 // Used for page load time metrics.
908 current_load_start_ = base::TimeTicks::Now();
909
910 // Navigate in the desired RenderViewHost.
[email protected]056de2d2009-06-26 16:41:34911 ViewMsg_Navigate_Params navigate_params;
[email protected]876bc832010-09-07 16:29:54912 MakeNavigateParams(entry, controller_, reload_type, &navigate_params);
[email protected]52c68652010-12-07 17:47:04913 if (delegate_) {
914 navigate_params.extra_headers =
915 delegate_->GetNavigationHeaders(navigate_params.url);
916 }
[email protected]056de2d2009-06-26 16:41:34917 dest_render_view_host->Navigate(navigate_params);
[email protected]96d185d2009-04-24 03:28:54918
919 if (entry.page_id() == -1) {
920 // HACK!! This code suppresses javascript: URLs from being added to
921 // session history, which is what we want to do for javascript: URLs that
922 // do not generate content. What we really need is a message from the
923 // renderer telling us that a new page was not created. The same message
924 // could be used for mailto: URLs and the like.
925 if (entry.url().SchemeIs(chrome::kJavaScriptScheme))
926 return false;
927 }
928
[email protected]3c9e1872010-11-18 16:17:49929 // Notify observers about navigation.
930 FOR_EACH_OBSERVER(WebNavigationObserver, web_navigation_observers_,
931 NavigateToPendingEntry());
[email protected]96d185d2009-04-24 03:28:54932
[email protected]1ccb3568d2010-02-19 10:51:16933 if (reload_type != NavigationController::NO_RELOAD &&
934 !profile()->IsOffTheRecord()) {
[email protected]0189bc722009-08-28 21:56:48935 FaviconService* favicon_service =
936 profile()->GetFaviconService(Profile::IMPLICIT_ACCESS);
937 if (favicon_service)
938 favicon_service->SetFaviconOutOfDateForPage(entry.url());
[email protected]96d185d2009-04-24 03:28:54939 }
940
[email protected]d5f942ba2008-09-26 19:30:34941 return true;
942}
943
[email protected]96d185d2009-04-24 03:28:54944void TabContents::Stop() {
945 render_manager_.Stop();
[email protected]12802702010-07-09 19:43:09946 printing_->Stop();
[email protected]96d185d2009-04-24 03:28:54947}
948
[email protected]96d185d2009-04-24 03:28:54949void TabContents::DisassociateFromPopupCount() {
950 render_view_host()->DisassociateFromPopupCount();
951}
952
953TabContents* TabContents::Clone() {
954 // We create a new SiteInstance so that the new tab won't share processes
955 // with the old one. This can be changed in the future if we need it to share
956 // processes for some reason.
[email protected]420ae012009-04-24 05:16:32957 TabContents* tc = new TabContents(profile(),
[email protected]96d185d2009-04-24 03:28:54958 SiteInstance::CreateSiteInstance(profile()),
[email protected]6ee12c42010-09-14 09:36:07959 MSG_ROUTING_NONE, this, NULL);
[email protected]96d185d2009-04-24 03:28:54960 tc->controller().CopyStateFrom(controller_);
[email protected]98aa0b52010-05-06 17:03:08961 tc->extension_app_ = extension_app_;
[email protected]98aa0b52010-05-06 17:03:08962 tc->extension_app_icon_ = extension_app_icon_;
[email protected]96d185d2009-04-24 03:28:54963 return tc;
964}
965
[email protected]4d677202009-07-19 07:37:12966void TabContents::ShowPageInfo(const GURL& url,
967 const NavigationEntry::SSLStatus& ssl,
968 bool show_history) {
969 if (!delegate_)
970 return;
971
[email protected]bb678332009-07-21 00:15:50972 delegate_->ShowPageInfo(profile(), url, ssl, show_history);
[email protected]4d677202009-07-19 07:37:12973}
974
[email protected]ef0d04c2010-12-15 23:34:21975void TabContents::SaveFavicon() {
976 NavigationEntry* entry = controller_.GetActiveEntry();
977 if (!entry || entry->url().is_empty())
978 return;
979
980 // Make sure the page is in history, otherwise adding the favicon does
981 // nothing.
982 HistoryService* history = profile()->GetOriginalProfile()->GetHistoryService(
983 Profile::IMPLICIT_ACCESS);
984 if (!history)
985 return;
986 history->AddPageNoVisitForBookmark(entry->url());
987
988 FaviconService* service = profile()->GetOriginalProfile()->GetFaviconService(
989 Profile::IMPLICIT_ACCESS);
990 if (!service)
991 return;
992 const NavigationEntry::FaviconStatus& favicon(entry->favicon());
993 if (!favicon.is_valid() || favicon.url().is_empty() ||
994 favicon.bitmap().empty()) {
995 return;
996 }
997 std::vector<unsigned char> image_data;
998 gfx::PNGCodec::EncodeBGRASkBitmap(favicon.bitmap(), false, &image_data);
999 service->SetFavicon(entry->url(), favicon.url(), image_data);
1000}
1001
initial.commit09911bf2008-07-26 23:55:291002ConstrainedWindow* TabContents::CreateConstrainedDialog(
[email protected]e8382172009-06-19 22:16:281003 ConstrainedWindowDelegate* delegate) {
initial.commit09911bf2008-07-26 23:55:291004 ConstrainedWindow* window =
[email protected]e8382172009-06-19 22:16:281005 ConstrainedWindow::CreateConstrainedDialog(this, delegate);
initial.commit09911bf2008-07-26 23:55:291006 child_windows_.push_back(window);
[email protected]fa1cf0b82010-01-15 21:49:441007
1008 if (child_windows_.size() == 1) {
1009 window->ShowConstrainedWindow();
1010 BlockTabContent(true);
1011 }
1012
initial.commit09911bf2008-07-26 23:55:291013 return window;
1014}
1015
[email protected]fa1cf0b82010-01-15 21:49:441016void TabContents::BlockTabContent(bool blocked) {
[email protected]8dccd7cb2010-02-25 05:19:551017 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
[email protected]1b072562010-12-13 19:37:451018 // 70% opaque grey.
1019 SkColor greyish = SkColorSetARGB(178, 0, 0, 0);
[email protected]8dccd7cb2010-02-25 05:19:551020 if (rwhv)
[email protected]1b072562010-12-13 19:37:451021 rwhv->SetVisuallyDeemphasized(blocked ? &greyish : NULL, false);
[email protected]fa1cf0b82010-01-15 21:49:441022 render_view_host()->set_ignore_input_events(blocked);
1023 if (delegate_)
1024 delegate_->SetTabContentBlocked(this, blocked);
1025}
1026
initial.commit09911bf2008-07-26 23:55:291027void TabContents::AddNewContents(TabContents* new_contents,
1028 WindowOpenDisposition disposition,
1029 const gfx::Rect& initial_pos,
[email protected]7e7414ae2010-01-26 20:19:291030 bool user_gesture) {
[email protected]c65e2f152010-10-14 15:30:401031 if (all_contents_blocked_) {
1032 if (!blocked_contents_)
1033 blocked_contents_ = new BlockedContentContainer(this);
1034 blocked_contents_->AddTabContents(
1035 new_contents, disposition, initial_pos, user_gesture);
1036 return;
1037 }
1038
initial.commit09911bf2008-07-26 23:55:291039 if (!delegate_)
1040 return;
1041
[email protected]35f7d212009-04-29 21:19:271042 if ((disposition == NEW_POPUP) && !user_gesture &&
1043 !CommandLine::ForCurrentProcess()->HasSwitch(
1044 switches::kDisablePopupBlocking)) {
[email protected]2c4410d2009-05-06 23:46:221045 // Unrequested popups from normal pages are constrained unless they're in
[email protected]b6c874582009-05-08 19:38:311046 // the whitelist. The popup owner will handle checking this.
[email protected]7e7414ae2010-01-26 20:19:291047 delegate_->GetConstrainingContents(this)->AddPopup(
1048 new_contents, initial_pos);
[email protected]b6c874582009-05-08 19:38:311049 } else {
[email protected]0aa55312008-10-17 21:53:081050 new_contents->DisassociateFromPopupCount();
initial.commit09911bf2008-07-26 23:55:291051 delegate_->AddNewContents(this, new_contents, disposition, initial_pos,
1052 user_gesture);
[email protected]7f37dd92009-09-15 16:35:491053 NotificationService::current()->Notify(
1054 NotificationType::TAB_ADDED,
1055 Source<TabContentsDelegate>(delegate_),
1056 Details<TabContents>(this));
initial.commit09911bf2008-07-26 23:55:291057 }
initial.commit09911bf2008-07-26 23:55:291058
[email protected]8cc8d492010-02-02 10:40:491059 // TODO(pkasting): Why is this necessary?
[email protected]c65e2f152010-10-14 15:30:401060 PopupNotificationVisibilityChanged(blocked_contents_ != NULL);
initial.commit09911bf2008-07-26 23:55:291061}
1062
[email protected]bcd9dc42010-01-23 16:38:181063bool TabContents::ExecuteCode(int request_id, const std::string& extension_id,
[email protected]20ad2692009-11-20 18:27:201064 bool is_js_code, const std::string& code_string,
1065 bool all_frames) {
[email protected]ebd0eae72010-02-16 23:50:511066 RenderViewHost* host = render_view_host();
1067 if (!host)
1068 return false;
1069
1070 return host->Send(new ViewMsg_ExecuteCode(host->routing_id(),
[email protected]be7e5cb2010-10-04 12:53:171071 ViewMsg_ExecuteCode_Params(request_id, extension_id,
[email protected]ebd0eae72010-02-16 23:50:511072 is_js_code, code_string, all_frames)));
[email protected]912256b32009-09-18 09:47:351073}
1074
[email protected]96d185d2009-04-24 03:28:541075void TabContents::PopupNotificationVisibilityChanged(bool visible) {
[email protected]8cc8d492010-02-02 10:40:491076 if (is_being_destroyed_)
1077 return;
[email protected]9fb83e82010-07-02 18:24:551078 content_settings_delegate_->SetPopupsBlocked(visible);
[email protected]8cc8d492010-02-02 10:40:491079 if (!dont_notify_render_view_)
[email protected]e8345242010-05-06 03:00:401080 render_view_host()->AllowScriptToClose(!visible);
[email protected]96d185d2009-04-24 03:28:541081}
1082
[email protected]5c9e97a2009-09-09 23:48:301083gfx::NativeView TabContents::GetContentNativeView() const {
[email protected]96d185d2009-04-24 03:28:541084 return view_->GetContentNativeView();
1085}
1086
1087gfx::NativeView TabContents::GetNativeView() const {
1088 return view_->GetNativeView();
1089}
1090
1091void TabContents::GetContainerBounds(gfx::Rect *out) const {
1092 view_->GetContainerBounds(out);
1093}
1094
1095void TabContents::Focus() {
1096 view_->Focus();
1097}
1098
[email protected]90daadb42009-06-08 21:27:281099void TabContents::FocusThroughTabTraversal(bool reverse) {
[email protected]7e383692009-06-12 19:14:541100 if (showing_interstitial_page()) {
1101 render_manager_.interstitial_page()->FocusThroughTabTraversal(reverse);
1102 return;
1103 }
[email protected]96d185d2009-04-24 03:28:541104 render_view_host()->SetInitialFocus(reverse);
1105}
1106
1107bool TabContents::FocusLocationBarByDefault() {
1108 DOMUI* dom_ui = GetDOMUIForCurrentState();
1109 if (dom_ui)
1110 return dom_ui->focus_location_bar_by_default();
[email protected]3e3f0eb2009-06-22 18:33:431111 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]e0d481582009-09-15 21:06:251112 if (entry && entry->url() == GURL(chrome::kAboutBlankURL))
[email protected]3e3f0eb2009-06-22 18:33:431113 return true;
[email protected]96d185d2009-04-24 03:28:541114 return false;
1115}
1116
[email protected]a26dc362010-04-23 01:48:581117void TabContents::SetFocusToLocationBar(bool select_all) {
[email protected]a11aaf22010-03-30 00:03:381118 if (delegate())
[email protected]a26dc362010-04-23 01:48:581119 delegate()->SetFocusToLocationBar(select_all);
[email protected]a11aaf22010-03-30 00:03:381120}
1121
[email protected]616ed5a2008-11-21 22:27:241122void TabContents::AddInfoBar(InfoBarDelegate* delegate) {
[email protected]f7019302010-03-26 19:58:321123 if (delegate_ && !delegate_->infobars_enabled()) {
1124 delegate->InfoBarClosed();
1125 return;
1126 }
1127
[email protected]616ed5a2008-11-21 22:27:241128 // Look through the existing InfoBarDelegates we have for a match. If we've
1129 // already got one that matches, then we don't add the new one.
[email protected]f86a07022008-11-25 01:06:051130 for (int i = 0; i < infobar_delegate_count(); ++i) {
[email protected]6d27a7b2009-12-18 23:25:451131 if (GetInfoBarDelegateAt(i)->EqualsDelegate(delegate)) {
1132 // Tell the new infobar to close so that it can clean itself up.
1133 delegate->InfoBarClosed();
[email protected]616ed5a2008-11-21 22:27:241134 return;
[email protected]6d27a7b2009-12-18 23:25:451135 }
[email protected]616ed5a2008-11-21 22:27:241136 }
1137
1138 infobar_delegates_.push_back(delegate);
[email protected]bfd04a62009-02-01 18:16:561139 NotificationService::current()->Notify(
1140 NotificationType::TAB_CONTENTS_INFOBAR_ADDED,
1141 Source<TabContents>(this),
1142 Details<InfoBarDelegate>(delegate));
[email protected]616ed5a2008-11-21 22:27:241143
1144 // Add ourselves as an observer for navigations the first time a delegate is
1145 // added. We use this notification to expire InfoBars that need to expire on
1146 // page transitions.
1147 if (infobar_delegates_.size() == 1) {
[email protected]bfd04a62009-02-01 18:16:561148 registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED,
[email protected]ce3fa3c2009-04-20 19:55:571149 Source<NavigationController>(&controller_));
[email protected]616ed5a2008-11-21 22:27:241150 }
1151}
1152
1153void TabContents::RemoveInfoBar(InfoBarDelegate* delegate) {
[email protected]f7019302010-03-26 19:58:321154 if (delegate_ && !delegate_->infobars_enabled()) {
1155 return;
1156 }
1157
[email protected]616ed5a2008-11-21 22:27:241158 std::vector<InfoBarDelegate*>::iterator it =
1159 find(infobar_delegates_.begin(), infobar_delegates_.end(), delegate);
1160 if (it != infobar_delegates_.end()) {
1161 InfoBarDelegate* delegate = *it;
[email protected]bfd04a62009-02-01 18:16:561162 NotificationService::current()->Notify(
1163 NotificationType::TAB_CONTENTS_INFOBAR_REMOVED,
1164 Source<TabContents>(this),
1165 Details<InfoBarDelegate>(delegate));
[email protected]616ed5a2008-11-21 22:27:241166
[email protected]cdadf6f2010-08-20 16:36:081167 // Just to be safe, make sure the delegate was not removed by an observer.
1168 it = find(infobar_delegates_.begin(), infobar_delegates_.end(), delegate);
1169 if (it != infobar_delegates_.end()) {
1170 infobar_delegates_.erase(it);
1171 // Remove ourselves as an observer if we are tracking no more InfoBars.
1172 if (infobar_delegates_.empty()) {
1173 registrar_.Remove(this, NotificationType::NAV_ENTRY_COMMITTED,
1174 Source<NavigationController>(&controller_));
1175 }
1176 } else {
1177 // If you hit this NOTREACHED, please comment in bug
1178 // http://crbug.com/50428 how you got there.
1179 NOTREACHED();
[email protected]6a02963e2009-01-06 16:58:031180 }
[email protected]616ed5a2008-11-21 22:27:241181 }
1182}
[email protected]3edd9522009-03-04 22:19:411183
[email protected]f81821c2009-08-07 01:29:471184void TabContents::ReplaceInfoBar(InfoBarDelegate* old_delegate,
1185 InfoBarDelegate* new_delegate) {
[email protected]f7019302010-03-26 19:58:321186 if (delegate_ && !delegate_->infobars_enabled()) {
1187 new_delegate->InfoBarClosed();
1188 return;
1189 }
1190
[email protected]f81821c2009-08-07 01:29:471191 std::vector<InfoBarDelegate*>::iterator it =
1192 find(infobar_delegates_.begin(), infobar_delegates_.end(), old_delegate);
1193 DCHECK(it != infobar_delegates_.end());
1194
1195 // Notify the container about the change of plans.
1196 scoped_ptr<std::pair<InfoBarDelegate*, InfoBarDelegate*> > details(
1197 new std::pair<InfoBarDelegate*, InfoBarDelegate*>(
1198 old_delegate, new_delegate));
1199 NotificationService::current()->Notify(
1200 NotificationType::TAB_CONTENTS_INFOBAR_REPLACED,
1201 Source<TabContents>(this),
1202 Details<std::pair<InfoBarDelegate*, InfoBarDelegate*> >(details.get()));
1203
[email protected]cdadf6f2010-08-20 16:36:081204 // Just to be safe, make sure the delegate was not removed by an observer.
1205 it = find(infobar_delegates_.begin(), infobar_delegates_.end(), old_delegate);
1206 if (it != infobar_delegates_.end()) {
1207 // Remove the old one.
1208 infobar_delegates_.erase(it);
1209 } else {
1210 // If you hit this NOTREACHED, please comment in bug
1211 // http://crbug.com/50428 how you got there.
1212 NOTREACHED();
1213 }
[email protected]f81821c2009-08-07 01:29:471214
1215 // Add the new one.
[email protected]cdadf6f2010-08-20 16:36:081216 DCHECK(find(infobar_delegates_.begin(),
1217 infobar_delegates_.end(), new_delegate) ==
1218 infobar_delegates_.end());
[email protected]f81821c2009-08-07 01:29:471219 infobar_delegates_.push_back(new_delegate);
1220}
1221
[email protected]20ede072009-10-06 00:28:351222bool TabContents::ShouldShowBookmarkBar() {
[email protected]aa9196f2009-10-22 16:20:271223 if (showing_interstitial_page())
1224 return false;
1225
[email protected]c8f74942010-09-03 10:04:141226 // Do not show bookmarks bar if bookmarks aren't enabled.
1227 if (!browser_defaults::bookmarks_enabled)
1228 return false;
1229
[email protected]96d185d2009-04-24 03:28:541230 // See GetDOMUIForCurrentState() comment for more info. This case is very
1231 // similar, but for non-first loads, we want to use the committed entry. This
1232 // is so the bookmarks bar disappears at the same time the page does.
1233 if (controller_.GetLastCommittedEntry()) {
1234 // Not the first load, always use the committed DOM UI.
[email protected]d6219912010-11-17 19:47:371235 return (render_manager_.dom_ui() == NULL) ?
1236 false : render_manager_.dom_ui()->force_bookmark_bar_visible();
[email protected]96d185d2009-04-24 03:28:541237 }
1238
1239 // When it's the first load, we know either the pending one or the committed
1240 // one will have the DOM UI in it (see GetDOMUIForCurrentState), and only one
1241 // of them will be valid, so we can just check both.
1242 if (render_manager_.pending_dom_ui())
1243 return render_manager_.pending_dom_ui()->force_bookmark_bar_visible();
[email protected]d6219912010-11-17 19:47:371244 return (render_manager_.dom_ui() == NULL) ?
1245 false : render_manager_.dom_ui()->force_bookmark_bar_visible();
[email protected]96d185d2009-04-24 03:28:541246}
1247
[email protected]3edd9522009-03-04 22:19:411248void TabContents::ToolbarSizeChanged(bool is_animating) {
1249 TabContentsDelegate* d = delegate();
1250 if (d)
1251 d->ToolbarSizeChanged(this, is_animating);
1252}
[email protected]616ed5a2008-11-21 22:27:241253
[email protected]fc6fb7fb2009-11-07 02:35:041254bool TabContents::CanDownload(int request_id) {
1255 TabContentsDelegate* d = delegate();
1256 if (d)
1257 return d->CanDownload(request_id);
1258 return true;
1259}
1260
[email protected]d5f942ba2008-09-26 19:30:341261void TabContents::OnStartDownload(DownloadItem* download) {
1262 DCHECK(download);
[email protected]d5f942ba2008-09-26 19:30:341263
1264 // Download in a constrained popup is shown in the tab that opened it.
[email protected]b6c874582009-05-08 19:38:311265 TabContents* tab_contents = delegate()->GetConstrainingContents(this);
[email protected]d5f942ba2008-09-26 19:30:341266
[email protected]5b928b62010-08-03 11:46:511267 if (tab_contents && tab_contents->delegate())
1268 tab_contents->delegate()->OnStartDownload(download, this);
[email protected]3edd9522009-03-04 22:19:411269}
[email protected]3edd9522009-03-04 22:19:411270
[email protected]d5f942ba2008-09-26 19:30:341271void TabContents::WillClose(ConstrainedWindow* window) {
[email protected]d6219912010-11-17 19:47:371272 ConstrainedWindowList::iterator i(
1273 std::find(child_windows_.begin(), child_windows_.end(), window));
1274 bool removed_topmost_window = i == child_windows_.begin();
1275 if (i != child_windows_.end())
1276 child_windows_.erase(i);
1277 if (child_windows_.empty()) {
[email protected]fa1cf0b82010-01-15 21:49:441278 BlockTabContent(false);
[email protected]d6219912010-11-17 19:47:371279 } else {
1280 if (removed_topmost_window)
1281 child_windows_[0]->ShowConstrainedWindow();
1282 BlockTabContent(true);
[email protected]fa1cf0b82010-01-15 21:49:441283 }
[email protected]66ba4932009-06-04 19:22:131284}
[email protected]d5f942ba2008-09-26 19:30:341285
[email protected]c65e2f152010-10-14 15:30:401286void TabContents::WillCloseBlockedContentContainer(
1287 BlockedContentContainer* container) {
1288 DCHECK(blocked_contents_ == container);
1289 blocked_contents_ = NULL;
[email protected]8cc8d492010-02-02 10:40:491290 PopupNotificationVisibilityChanged(false);
[email protected]d5f942ba2008-09-26 19:30:341291}
1292
[email protected]d5f942ba2008-09-26 19:30:341293void TabContents::DidMoveOrResize(ConstrainedWindow* window) {
[email protected]332af7732009-03-11 13:21:351294#if defined(OS_WIN)
[email protected]92edc472009-02-10 20:32:061295 UpdateWindow(GetNativeView());
[email protected]332af7732009-03-11 13:21:351296#endif
[email protected]d5f942ba2008-09-26 19:30:341297}
1298
[email protected]c1dc9f132009-11-20 23:13:211299void TabContents::StartFinding(string16 search_string,
[email protected]e491f1c2009-05-22 20:28:121300 bool forward_direction,
1301 bool case_sensitive) {
[email protected]c1dc9f132009-11-20 23:13:211302 // If search_string is empty, it means FindNext was pressed with a keyboard
[email protected]6e95706d2009-04-23 22:55:011303 // shortcut so unless we have something to search for we return early.
[email protected]c1dc9f132009-11-20 23:13:211304 if (search_string.empty() && find_text_.empty()) {
[email protected]e5d2f7132010-04-09 18:13:531305 string16 last_search_prepopulate_text =
1306 FindBarState::GetLastPrepopulateText(profile());
1307
[email protected]87fae512009-12-11 02:20:211308 // Try the last thing we searched for on this tab, then the last thing
1309 // searched for on any tab.
1310 if (!previous_find_text_.empty())
1311 search_string = previous_find_text_;
[email protected]e5d2f7132010-04-09 18:13:531312 else if (!last_search_prepopulate_text.empty())
1313 search_string = last_search_prepopulate_text;
[email protected]87fae512009-12-11 02:20:211314 else
[email protected]e491f1c2009-05-22 20:28:121315 return;
[email protected]e491f1c2009-05-22 20:28:121316 }
[email protected]6e95706d2009-04-23 22:55:011317
[email protected]101c90e2009-10-26 21:14:441318 // Keep track of the previous search.
1319 previous_find_text_ = find_text_;
1320
[email protected]6e95706d2009-04-23 22:55:011321 // This is a FindNext operation if we are searching for the same text again,
1322 // or if the passed in search text is empty (FindNext keyboard shortcut). The
1323 // exception to this is if the Find was aborted (then we don't want FindNext
1324 // because the highlighting has been cleared and we need it to reappear). We
1325 // therefore treat FindNext after an aborted Find operation as a full fledged
1326 // Find.
[email protected]c1dc9f132009-11-20 23:13:211327 bool find_next = (find_text_ == search_string || search_string.empty()) &&
[email protected]e491f1c2009-05-22 20:28:121328 (last_search_case_sensitive_ == case_sensitive) &&
[email protected]6e95706d2009-04-23 22:55:011329 !find_op_aborted_;
1330 if (!find_next)
1331 current_find_request_id_ = find_request_id_counter_++;
1332
[email protected]c1dc9f132009-11-20 23:13:211333 if (!search_string.empty())
1334 find_text_ = search_string;
[email protected]e491f1c2009-05-22 20:28:121335 last_search_case_sensitive_ = case_sensitive;
[email protected]6e95706d2009-04-23 22:55:011336
1337 find_op_aborted_ = false;
1338
1339 // Keep track of what the last search was across the tabs.
[email protected]e5d2f7132010-04-09 18:13:531340 FindBarState* find_bar_state = profile()->GetFindBarState();
1341 find_bar_state->set_last_prepopulate_text(find_text_);
[email protected]6e95706d2009-04-23 22:55:011342 render_view_host()->StartFinding(current_find_request_id_,
1343 find_text_,
1344 forward_direction,
[email protected]e491f1c2009-05-22 20:28:121345 case_sensitive,
[email protected]6e95706d2009-04-23 22:55:011346 find_next);
1347}
1348
[email protected]5bc8fe92010-03-11 18:19:001349void TabContents::StopFinding(
1350 FindBarController::SelectionAction selection_action) {
[email protected]d6e8d332010-06-04 22:26:011351 if (selection_action == FindBarController::kClearSelection) {
1352 // kClearSelection means the find string has been cleared by the user, but
1353 // the UI has not been dismissed. In that case we want to clear the
1354 // previously remembered search (http://crbug.com/42639).
1355 previous_find_text_ = string16();
1356 } else {
[email protected]2d2dffc2009-06-09 21:41:001357 find_ui_active_ = false;
[email protected]d6e8d332010-06-04 22:26:011358 if (!find_text_.empty())
1359 previous_find_text_ = find_text_;
1360 }
[email protected]101c90e2009-10-26 21:14:441361 find_text_.clear();
[email protected]6e95706d2009-04-23 22:55:011362 find_op_aborted_ = true;
[email protected]e491f1c2009-05-22 20:28:121363 last_search_result_ = FindNotificationDetails();
[email protected]5bc8fe92010-03-11 18:19:001364 render_view_host()->StopFinding(selection_action);
[email protected]6e95706d2009-04-23 22:55:011365}
1366
[email protected]420ae012009-04-24 05:16:321367void TabContents::OnSavePage() {
1368 // If we can not save the page, try to download it.
1369 if (!SavePackage::IsSavableContents(contents_mime_type())) {
1370 DownloadManager* dlm = profile()->GetDownloadManager();
1371 const GURL& current_page_url = GetURL();
1372 if (dlm && current_page_url.is_valid())
[email protected]d82ed61e2009-06-16 02:46:221373 dlm->DownloadUrl(current_page_url, GURL(), "", this);
[email protected]420ae012009-04-24 05:16:321374 return;
1375 }
1376
1377 Stop();
1378
1379 // Create the save package and possibly prompt the user for the name to save
1380 // the page as. The user prompt is an asynchronous operation that runs on
1381 // another thread.
[email protected]d82ed61e2009-06-16 02:46:221382 save_package_ = new SavePackage(this);
[email protected]420ae012009-04-24 05:16:321383 save_package_->GetSaveInfo();
1384}
1385
1386// Used in automated testing to bypass prompting the user for file names.
1387// Instead, the names and paths are hard coded rather than running them through
1388// file name sanitation and extension / mime checking.
[email protected]828cabe2009-09-26 22:47:111389bool TabContents::SavePage(const FilePath& main_file, const FilePath& dir_path,
[email protected]420ae012009-04-24 05:16:321390 SavePackage::SavePackageType save_type) {
1391 // Stop the page from navigating.
1392 Stop();
1393
[email protected]828cabe2009-09-26 22:47:111394 save_package_ = new SavePackage(this, save_type, main_file, dir_path);
[email protected]cdf6cc82009-08-04 21:48:591395 return save_package_->Init();
[email protected]420ae012009-04-24 05:16:321396}
1397
[email protected]89d6e6e2009-12-03 23:06:511398void TabContents::EmailPageLocation() {
1399 std::string title = EscapeQueryParamValue(UTF16ToUTF8(GetTitle()), false);
1400 std::string page_url = EscapeQueryParamValue(GetURL().spec(), false);
1401 std::string mailto = std::string("mailto:?subject=Fwd:%20") +
1402 title + "&body=%0A%0A" + page_url;
1403 platform_util::OpenExternal(GURL(mailto));
1404}
1405
[email protected]420ae012009-04-24 05:16:321406void TabContents::PrintPreview() {
[email protected]dbeebd52010-11-16 20:34:161407 if (CommandLine::ForCurrentProcess()->HasSwitch(
1408 switches::kEnablePrintPreview)) {
1409 if (showing_interstitial_page())
1410 return;
1411 render_view_host()->PrintPreview();
1412 } else {
1413 PrintNow();
1414 }
[email protected]420ae012009-04-24 05:16:321415}
1416
1417bool TabContents::PrintNow() {
1418 // We can't print interstitial page for now.
1419 if (showing_interstitial_page())
1420 return false;
1421
1422 return render_view_host()->PrintPages();
1423}
1424
[email protected]82270452009-06-19 15:58:011425void TabContents::PrintingDone(int document_cookie, bool success) {
1426 render_view_host()->PrintingDone(document_cookie, success);
1427}
1428
[email protected]420ae012009-04-24 05:16:321429bool TabContents::IsActiveEntry(int32 page_id) {
1430 NavigationEntry* active_entry = controller_.GetActiveEntry();
1431 return (active_entry != NULL &&
1432 active_entry->site_instance() == GetSiteInstance() &&
1433 active_entry->page_id() == page_id);
1434}
1435
[email protected]b2fe07d12010-02-09 14:38:081436void TabContents::SetOverrideEncoding(const std::string& encoding) {
[email protected]8cb5d5b2010-02-09 11:36:161437 set_encoding(encoding);
1438 render_view_host()->SetPageEncoding(encoding);
1439}
1440
[email protected]b2fe07d12010-02-09 14:38:081441void TabContents::ResetOverrideEncoding() {
[email protected]8cb5d5b2010-02-09 11:36:161442 reset_encoding();
1443 render_view_host()->ResetPageEncodingToDefault();
1444}
1445
1446void TabContents::WindowMoveOrResizeStarted() {
1447 render_view_host()->WindowMoveOrResizeStarted();
1448}
1449
[email protected]c65e2f152010-10-14 15:30:401450void TabContents::SetAllContentsBlocked(bool value) {
1451 if (all_contents_blocked_ == value)
1452 return;
1453
1454 all_contents_blocked_ = value;
1455 if (!all_contents_blocked_ && blocked_contents_) {
1456 std::vector<TabContents*> blocked;
1457 blocked_contents_->GetBlockedContents(&blocked);
1458 for (size_t i = 0; i < blocked.size(); ++i)
1459 blocked_contents_->LaunchForContents(blocked[i]);
1460 }
1461}
1462
[email protected]3f8eb7f2009-10-02 23:58:051463void TabContents::LogNewTabTime(const std::string& event_name) {
1464 // Not all new tab pages get timed. In those cases, we don't have a
1465 // new_tab_start_time_.
1466 if (new_tab_start_time_.is_null())
1467 return;
1468
1469 base::TimeDelta duration = base::TimeTicks::Now() - new_tab_start_time_;
[email protected]84abba62009-10-07 17:01:441470 MetricEventDurationDetails details(event_name,
1471 static_cast<int>(duration.InMilliseconds()));
1472
[email protected]7ab1e7d62009-10-14 23:32:011473 if (event_name == "Tab.NewTabScriptStart") {
1474 UMA_HISTOGRAM_TIMES("Tab.NewTabScriptStart", duration);
1475 } else if (event_name == "Tab.NewTabDOMContentLoaded") {
1476 UMA_HISTOGRAM_TIMES("Tab.NewTabDOMContentLoaded", duration);
1477 } else if (event_name == "Tab.NewTabOnload") {
1478 UMA_HISTOGRAM_TIMES("Tab.NewTabOnload", duration);
[email protected]3f8eb7f2009-10-02 23:58:051479 // The new tab page has finished loading; reset it.
1480 new_tab_start_time_ = base::TimeTicks();
[email protected]84abba62009-10-07 17:01:441481 } else {
1482 NOTREACHED();
[email protected]3f8eb7f2009-10-02 23:58:051483 }
[email protected]84abba62009-10-07 17:01:441484 NotificationService::current()->Notify(
1485 NotificationType::METRIC_EVENT_DURATION,
1486 Source<TabContents>(this),
1487 Details<MetricEventDurationDetails>(&details));
[email protected]3f8eb7f2009-10-02 23:58:051488}
1489
[email protected]7ab1e7d62009-10-14 23:32:011490void TabContents::OnCloseStarted() {
1491 if (tab_close_start_time_.is_null())
1492 tab_close_start_time_ = base::TimeTicks::Now();
1493}
1494
[email protected]46624bf2010-06-09 16:04:191495bool TabContents::ShouldAcceptDragAndDrop() const {
1496#if defined(OS_CHROMEOS)
1497 // ChromeOS panels (pop-ups) do not take drag-n-drop.
1498 // See http://crosbug.com/2413
[email protected]b83af492010-10-09 03:21:201499 if (delegate() && delegate()->IsPopup(this))
1500 return false;
1501 return true;
[email protected]46624bf2010-06-09 16:04:191502#else
1503 return true;
1504#endif
1505}
1506
[email protected]ec0b6c42010-08-26 03:16:581507void TabContents::UpdateHistoryForNavigation(
1508 scoped_refptr<history::HistoryAddPageArgs> add_page_args) {
1509 if (profile()->IsOffTheRecord())
1510 return;
1511
1512 // Add to history service.
1513 HistoryService* hs = profile()->GetHistoryService(Profile::IMPLICIT_ACCESS);
1514 if (hs)
1515 hs->AddPage(*add_page_args);
1516}
1517
1518void TabContents::UpdateHistoryPageTitle(const NavigationEntry& entry) {
1519 if (profile()->IsOffTheRecord())
1520 return;
1521
1522 HistoryService* hs = profile()->GetHistoryService(Profile::IMPLICIT_ACCESS);
1523 if (hs)
1524 hs->SetPageTitle(entry.virtual_url(), entry.title());
1525}
1526
[email protected]d0b8d092010-10-25 04:05:171527double TabContents::GetZoomLevel() const {
[email protected]b75b8292010-10-01 07:28:251528 HostZoomMap* zoom_map = profile()->GetHostZoomMap();
1529 if (!zoom_map)
[email protected]d0b8d092010-10-25 04:05:171530 return 0;
[email protected]b75b8292010-10-01 07:28:251531
1532 double zoom_level;
1533 if (temporary_zoom_settings_) {
1534 zoom_level = zoom_map->GetTemporaryZoomLevel(
1535 GetRenderProcessHost()->id(), render_view_host()->routing_id());
1536 } else {
1537 zoom_level = zoom_map->GetZoomLevel(GetURL());
1538 }
[email protected]d0b8d092010-10-25 04:05:171539 return zoom_level;
1540}
[email protected]b75b8292010-10-01 07:28:251541
[email protected]d0b8d092010-10-25 04:05:171542int TabContents::GetZoomPercent(bool* enable_increment,
1543 bool* enable_decrement) {
1544 *enable_decrement = *enable_increment = false;
[email protected]b75b8292010-10-01 07:28:251545 int percent = static_cast<int>(
[email protected]d0b8d092010-10-25 04:05:171546 WebKit::WebView::zoomLevelToZoomFactor(GetZoomLevel()) * 100);
[email protected]b75b8292010-10-01 07:28:251547 *enable_decrement = percent > minimum_zoom_percent_;
1548 *enable_increment = percent < maximum_zoom_percent_;
1549 return percent;
1550}
1551
[email protected]77d8d622010-12-15 10:30:121552void TabContents::ViewSource()
1553{
[email protected]1788e772010-12-15 16:40:501554 if (!delegate_)
1555 return;
1556
1557 NavigationEntry* active_entry = controller().GetActiveEntry();
1558 if (!active_entry)
1559 return;
1560
1561 delegate_->ViewSourceForTab(this, active_entry->url());
[email protected]77d8d622010-12-15 10:30:121562}
1563
[email protected]96d185d2009-04-24 03:28:541564// Notifies the RenderWidgetHost instance about the fact that the page is
1565// loading, or done loading and calls the base implementation.
1566void TabContents::SetIsLoading(bool is_loading,
1567 LoadNotificationDetails* details) {
1568 if (is_loading == is_loading_)
1569 return;
1570
1571 if (!is_loading) {
1572 load_state_ = net::LOAD_STATE_IDLE;
1573 load_state_host_.clear();
[email protected]094e5b22009-09-25 04:23:561574 upload_size_ = 0;
1575 upload_position_ = 0;
[email protected]96d185d2009-04-24 03:28:541576 }
1577
1578 render_manager_.SetIsLoading(is_loading);
1579
1580 is_loading_ = is_loading;
1581 waiting_for_response_ = is_loading;
1582
[email protected]6ebdc9b2010-09-27 16:55:571583 if (delegate_)
1584 delegate_->LoadingStateChanged(this);
[email protected]329581b2009-04-28 06:52:351585 NotifyNavigationStateChanged(INVALIDATE_LOAD);
[email protected]96d185d2009-04-24 03:28:541586
1587 NotificationType type = is_loading ? NotificationType::LOAD_START :
1588 NotificationType::LOAD_STOP;
[email protected]ce5c4502009-05-06 16:46:111589 NotificationDetails det = NotificationService::NoDetails();
[email protected]96d185d2009-04-24 03:28:541590 if (details)
1591 det = Details<LoadNotificationDetails>(details);
1592 NotificationService::current()->Notify(type,
1593 Source<NavigationController>(&controller_),
1594 det);
1595}
1596
[email protected]b6c874582009-05-08 19:38:311597void TabContents::AddPopup(TabContents* new_contents,
[email protected]7e7414ae2010-01-26 20:19:291598 const gfx::Rect& initial_pos) {
[email protected]57eda82a2010-05-05 21:28:061599 // A page can't spawn popups (or do anything else, either) until its load
1600 // commits, so when we reach here, the popup was spawned by the
1601 // NavigationController's last committed entry, not the active entry. For
1602 // example, if a page opens a popup in an onunload() handler, then the active
1603 // entry is the page to be loaded as we navigate away from the unloading
1604 // page. For this reason, we can't use GetURL() to get the opener URL,
1605 // because it returns the active entry.
1606 NavigationEntry* entry = controller_.GetLastCommittedEntry();
1607 GURL creator = entry ? entry->virtual_url() : GURL::EmptyGURL();
1608
1609 if (creator.is_valid() &&
[email protected]8cc8d492010-02-02 10:40:491610 profile()->GetHostContentSettingsMap()->GetContentSetting(
[email protected]ca352452010-08-06 11:14:091611 creator, CONTENT_SETTINGS_TYPE_POPUPS, "") == CONTENT_SETTING_ALLOW) {
[email protected]8cc8d492010-02-02 10:40:491612 AddNewContents(new_contents, NEW_POPUP, initial_pos, true);
1613 } else {
[email protected]c65e2f152010-10-14 15:30:401614 if (!blocked_contents_)
1615 blocked_contents_ = new BlockedContentContainer(this);
1616 blocked_contents_->AddTabContents(new_contents, NEW_POPUP, initial_pos,
1617 true);
[email protected]55126132010-08-19 14:53:281618 content_settings_delegate_->OnContentBlocked(CONTENT_SETTINGS_TYPE_POPUPS,
1619 std::string());
[email protected]8cc8d492010-02-02 10:40:491620 }
[email protected]b9681312008-11-07 00:08:261621}
[email protected]616ed5a2008-11-21 22:27:241622
[email protected]616ed5a2008-11-21 22:27:241623void TabContents::ExpireInfoBars(
1624 const NavigationController::LoadCommittedDetails& details) {
1625 // Only hide InfoBars when the user has done something that makes the main
1626 // frame load. We don't want various automatic or subframe navigations making
1627 // it disappear.
1628 if (!details.is_user_initiated_main_frame_load())
1629 return;
1630
[email protected]f86a07022008-11-25 01:06:051631 for (int i = infobar_delegate_count() - 1; i >= 0; --i) {
[email protected]616ed5a2008-11-21 22:27:241632 InfoBarDelegate* delegate = GetInfoBarDelegateAt(i);
[email protected]cdadf6f2010-08-20 16:36:081633 if (!delegate) {
1634 // If you hit this NOTREACHED, please comment in bug
1635 // http://crbug.com/50428 how you got there.
1636 NOTREACHED();
1637 continue;
1638 }
1639
[email protected]f86a07022008-11-25 01:06:051640 if (delegate->ShouldExpire(details))
[email protected]616ed5a2008-11-21 22:27:241641 RemoveInfoBar(delegate);
[email protected]616ed5a2008-11-21 22:27:241642 }
1643}
[email protected]fdd61c62009-04-22 19:22:571644
[email protected]96d185d2009-04-24 03:28:541645DOMUI* TabContents::GetDOMUIForCurrentState() {
1646 // When there is a pending navigation entry, we want to use the pending DOMUI
1647 // that goes along with it to control the basic flags. For example, we want to
1648 // show the pending URL in the URL bar, so we want the display_url flag to
1649 // be from the pending entry.
1650 //
1651 // The confusion comes because there are multiple possibilities for the
1652 // initial load in a tab as a side effect of the way the RenderViewHostManager
1653 // works.
1654 //
1655 // - For the very first tab the load looks "normal". The new tab DOM UI is
1656 // the pending one, and we want it to apply here.
1657 //
1658 // - For subsequent new tabs, they'll get a new SiteInstance which will then
1659 // get switched to the one previously associated with the new tab pages.
1660 // This switching will cause the manager to commit the RVH/DOMUI. So we'll
1661 // have a committed DOM UI in this case.
1662 //
1663 // This condition handles all of these cases:
1664 //
1665 // - First load in first tab: no committed nav entry + pending nav entry +
1666 // pending dom ui:
1667 // -> Use pending DOM UI if any.
1668 //
1669 // - First load in second tab: no committed nav entry + pending nav entry +
1670 // no pending DOM UI:
1671 // -> Use the committed DOM UI if any.
1672 //
1673 // - Second navigation in any tab: committed nav entry + pending nav entry:
1674 // -> Use pending DOM UI if any.
1675 //
1676 // - Normal state with no load: committed nav entry + no pending nav entry:
1677 // -> Use committed DOM UI.
1678 if (controller_.pending_entry() &&
1679 (controller_.GetLastCommittedEntry() ||
1680 render_manager_.pending_dom_ui()))
1681 return render_manager_.pending_dom_ui();
1682 return render_manager_.dom_ui();
1683}
[email protected]420ae012009-04-24 05:16:321684
1685void TabContents::DidNavigateMainFramePostCommit(
1686 const NavigationController::LoadCommittedDetails& details,
1687 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]c2e74fe82009-09-03 17:57:441688 if (opener_dom_ui_type_ != DOMUIFactory::kNoDOMUI) {
1689 // If this is a window.open navigation, use the same DOMUI as the renderer
1690 // that opened the window, as long as both renderers have the same
1691 // privileges.
[email protected]583d45c12010-08-31 02:48:121692 if (opener_dom_ui_type_ ==
1693 DOMUIFactory::GetDOMUIType(profile(), GetURL())) {
[email protected]c2e74fe82009-09-03 17:57:441694 DOMUI* dom_ui = DOMUIFactory::CreateDOMUIForURL(this, GetURL());
1695 // dom_ui might be NULL if the URL refers to a non-existent extension.
1696 if (dom_ui) {
1697 render_manager_.SetDOMUIPostCommit(dom_ui);
1698 dom_ui->RenderViewCreated(render_view_host());
1699 }
1700 }
1701 opener_dom_ui_type_ = DOMUIFactory::kNoDOMUI;
1702 }
1703
[email protected]420ae012009-04-24 05:16:321704 if (details.is_user_initiated_main_frame_load()) {
1705 // Clear the status bubble. This is a workaround for a bug where WebKit
1706 // doesn't let us know that the cursor left an element during a
1707 // transition (this is also why the mouse cursor remains as a hand after
1708 // clicking on a link); see bugs 1184641 and 980803. We don't want to
1709 // clear the bubble when a user navigates to a named anchor in the same
1710 // page.
1711 UpdateTargetURL(details.entry->page_id(), GURL());
[email protected]420ae012009-04-24 05:16:321712 }
1713
1714 // The keyword generator uses the navigation entries, so must be called after
1715 // the commit.
1716 GenerateKeywordIfNecessary(params);
1717
1718 // Allow the new page to set the title again.
1719 received_page_title_ = false;
1720
1721 // Get the favicon, either from history or request it from the net.
1722 fav_icon_helper_.FetchFavIcon(details.entry->url());
1723
[email protected]0de80162010-02-03 04:52:351724 // Clear all page actions, blocked content notifications and browser actions
1725 // for this tab, unless this is an in-page navigation.
[email protected]a6e82fc2010-02-24 22:28:141726 if (!details.is_in_page) {
[email protected]eaa7dd182010-12-14 11:09:001727 ExtensionService* service = profile()->GetExtensionService();
[email protected]92c6f9b92009-10-24 04:35:081728 if (service) {
1729 for (size_t i = 0; i < service->extensions()->size(); ++i) {
[email protected]5d246db22009-10-27 06:17:571730 ExtensionAction* browser_action =
[email protected]92c6f9b92009-10-24 04:35:081731 service->extensions()->at(i)->browser_action();
[email protected]56ce6e52009-10-27 00:10:521732 if (browser_action) {
1733 browser_action->ClearAllValuesForTab(controller().session_id().id());
1734 NotificationService::current()->Notify(
1735 NotificationType::EXTENSION_BROWSER_ACTION_UPDATED,
[email protected]5d246db22009-10-27 06:17:571736 Source<ExtensionAction>(browser_action),
[email protected]56ce6e52009-10-27 00:10:521737 NotificationService::NoDetails());
1738 }
[email protected]92c6f9b92009-10-24 04:35:081739
[email protected]5d246db22009-10-27 06:17:571740 ExtensionAction* page_action =
[email protected]56ce6e52009-10-27 00:10:521741 service->extensions()->at(i)->page_action();
1742 if (page_action) {
1743 page_action->ClearAllValuesForTab(controller().session_id().id());
1744 PageActionStateChanged();
1745 }
[email protected]92c6f9b92009-10-24 04:35:081746 }
[email protected]aece2c7f2009-08-27 20:43:171747 }
[email protected]0de80162010-02-03 04:52:351748
1749 // Close blocked popups.
[email protected]c65e2f152010-10-14 15:30:401750 if (blocked_contents_) {
[email protected]0fbd70332010-06-01 19:28:341751 AutoReset<bool> auto_reset(&dont_notify_render_view_, true);
[email protected]c65e2f152010-10-14 15:30:401752 blocked_contents_->Destroy();
1753 blocked_contents_ = NULL;
[email protected]0de80162010-02-03 04:52:351754 }
1755
1756 // Clear "blocked" flags.
[email protected]da4f8a12010-08-19 08:33:411757 content_settings_delegate_->ClearBlockedContentSettingsExceptForCookies();
[email protected]9fb83e82010-07-02 18:24:551758 content_settings_delegate_->GeolocationDidNavigate(details);
[email protected]f17a0ee2010-05-17 17:38:471759
1760 // Once the main frame is navigated, we're no longer considered to have
1761 // displayed insecure content.
1762 displayed_insecure_content_ = false;
[email protected]aece2c7f2009-08-27 20:43:171763 }
[email protected]ce5c4502009-05-06 16:46:111764
[email protected]8cc8d492010-02-02 10:40:491765 // Close constrained windows if necessary.
1766 if (!net::RegistryControlledDomainService::SameDomainOrHost(
1767 details.previous_url, details.entry->url()))
1768 CloseConstrainedWindows();
1769
[email protected]420ae012009-04-24 05:16:321770 // Update the starred state.
1771 UpdateStarredStateForCurrentURL();
[email protected]c07c9752010-03-12 05:57:531772
[email protected]3c9e1872010-11-18 16:17:491773 // Notify observers about navigation.
1774 FOR_EACH_OBSERVER(WebNavigationObserver, web_navigation_observers_,
1775 DidNavigateMainFramePostCommit(details, params));
1776
[email protected]c07c9752010-03-12 05:57:531777 // Clear the cache of forms in AutoFill.
1778 if (autofill_manager_.get())
1779 autofill_manager_->Reset();
[email protected]420ae012009-04-24 05:16:321780}
1781
1782void TabContents::DidNavigateAnyFramePostCommit(
1783 RenderViewHost* render_view_host,
1784 const NavigationController::LoadCommittedDetails& details,
1785 const ViewHostMsg_FrameNavigate_Params& params) {
1786 // If we navigate, start showing messages again. This does nothing to prevent
1787 // a malicious script from spamming messages, since the script could just
1788 // reload the page to stop blocking.
1789 suppress_javascript_messages_ = false;
1790
[email protected]3c9e1872010-11-18 16:17:491791 // Notify observers about navigation.
1792 FOR_EACH_OBSERVER(WebNavigationObserver, web_navigation_observers_,
1793 DidNavigateAnyFramePostCommit(details, params));
[email protected]3b1c1812010-02-12 21:53:551794
1795 // Let the LanguageState clear its state.
[email protected]80fc6112010-08-13 20:10:581796 language_state_.DidNavigate(details);
[email protected]420ae012009-04-24 05:16:321797}
1798
[email protected]8cc8d492010-02-02 10:40:491799void TabContents::CloseConstrainedWindows() {
1800 // Clear out any constrained windows since we are leaving this page entirely.
[email protected]420ae012009-04-24 05:16:321801 // We use indices instead of iterators in case CloseWindow does something
1802 // that may invalidate an iterator.
1803 int size = static_cast<int>(child_windows_.size());
1804 for (int i = size - 1; i >= 0; --i) {
1805 ConstrainedWindow* window = child_windows_[i];
[email protected]fa1cf0b82010-01-15 21:49:441806 if (window) {
[email protected]420ae012009-04-24 05:16:321807 window->CloseConstrainedWindow();
[email protected]fa1cf0b82010-01-15 21:49:441808 BlockTabContent(false);
1809 }
[email protected]420ae012009-04-24 05:16:321810 }
1811}
1812
1813void TabContents::UpdateStarredStateForCurrentURL() {
1814 BookmarkModel* model = profile()->GetBookmarkModel();
1815 const bool old_state = is_starred_;
1816 is_starred_ = (model && model->IsBookmarked(GetURL()));
1817
1818 if (is_starred_ != old_state && delegate())
1819 delegate()->URLStarredChanged(this, is_starred_);
1820}
1821
1822void TabContents::UpdateAlternateErrorPageURL() {
1823 GURL url = GetAlternateErrorPageURL();
1824 render_view_host()->SetAlternateErrorPageURL(url);
1825}
1826
1827void TabContents::UpdateWebPreferences() {
1828 render_view_host()->UpdateWebPreferences(GetWebkitPrefs());
1829}
1830
[email protected]d0b8d092010-10-25 04:05:171831void TabContents::UpdateZoomLevel() {
1832 render_view_host()->SetZoomLevel(GetZoomLevel());
1833}
1834
[email protected]420ae012009-04-24 05:16:321835void TabContents::UpdateMaxPageIDIfNecessary(SiteInstance* site_instance,
1836 RenderViewHost* rvh) {
1837 // If we are creating a RVH for a restored controller, then we might
1838 // have more page IDs than the SiteInstance's current max page ID. We must
1839 // make sure that the max page ID is larger than any restored page ID.
1840 // Note that it is ok for conflicting page IDs to exist in another tab
1841 // (i.e., NavigationController), but if any page ID is larger than the max,
1842 // the back/forward list will get confused.
1843 int max_restored_page_id = controller_.max_restored_page_id();
1844 if (max_restored_page_id > 0) {
1845 int curr_max_page_id = site_instance->max_page_id();
1846 if (max_restored_page_id > curr_max_page_id) {
1847 // Need to update the site instance immediately.
1848 site_instance->UpdateMaxPageID(max_restored_page_id);
1849
1850 // Also tell the renderer to update its internal representation. We
1851 // need to reserve enough IDs to make all restored page IDs less than
1852 // the max.
1853 if (curr_max_page_id < 0)
1854 curr_max_page_id = 0;
1855 rvh->ReservePageIDRange(max_restored_page_id - curr_max_page_id);
1856 }
1857 }
1858}
1859
[email protected]ec0b6c42010-08-26 03:16:581860scoped_refptr<history::HistoryAddPageArgs>
1861TabContents::CreateHistoryAddPageArgs(
[email protected]ebe89e062009-08-13 23:16:541862 const GURL& virtual_url,
[email protected]befd8d822009-07-01 04:51:471863 const NavigationController::LoadCommittedDetails& details,
[email protected]420ae012009-04-24 05:16:321864 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]ec0b6c42010-08-26 03:16:581865 scoped_refptr<history::HistoryAddPageArgs> add_page_args(
1866 new history::HistoryAddPageArgs(
1867 params.url, base::Time::Now(), this, params.page_id, params.referrer,
1868 params.redirects, params.transition, history::SOURCE_BROWSED,
1869 details.did_replace_entry));
1870 if (PageTransition::IsMainFrame(params.transition) &&
1871 virtual_url != params.url) {
1872 // Hack on the "virtual" URL so that it will appear in history. For some
1873 // types of URLs, we will display a magic URL that is different from where
1874 // the page is actually navigated. We want the user to see in history what
1875 // they saw in the URL bar, so we add the virtual URL as a redirect. This
1876 // only applies to the main frame, as the virtual URL doesn't apply to
1877 // sub-frames.
1878 add_page_args->url = virtual_url;
1879 if (!add_page_args->redirects.empty())
1880 add_page_args->redirects.back() = virtual_url;
[email protected]420ae012009-04-24 05:16:321881 }
[email protected]ec0b6c42010-08-26 03:16:581882 return add_page_args;
[email protected]420ae012009-04-24 05:16:321883}
1884
1885bool TabContents::UpdateTitleForEntry(NavigationEntry* entry,
1886 const std::wstring& title) {
1887 // For file URLs without a title, use the pathname instead. In the case of a
1888 // synthesized title, we don't want the update to count toward the "one set
1889 // per page of the title to history."
[email protected]e53668962010-06-23 15:35:251890 string16 final_title;
[email protected]420ae012009-04-24 05:16:321891 bool explicit_set;
1892 if (entry->url().SchemeIsFile() && title.empty()) {
[email protected]e53668962010-06-23 15:35:251893 final_title = UTF8ToUTF16(entry->url().ExtractFileName());
[email protected]420ae012009-04-24 05:16:321894 explicit_set = false; // Don't count synthetic titles toward the set limit.
1895 } else {
[email protected]e53668962010-06-23 15:35:251896 TrimWhitespace(WideToUTF16Hack(title), TRIM_ALL, &final_title);
[email protected]420ae012009-04-24 05:16:321897 explicit_set = true;
1898 }
1899
[email protected]e53668962010-06-23 15:35:251900 if (final_title == entry->title())
[email protected]420ae012009-04-24 05:16:321901 return false; // Nothing changed, don't bother.
1902
[email protected]e53668962010-06-23 15:35:251903 entry->set_title(final_title);
[email protected]420ae012009-04-24 05:16:321904
[email protected]ec0b6c42010-08-26 03:16:581905 if (!received_page_title_) {
1906 UpdateHistoryPageTitle(*entry);
[email protected]420ae012009-04-24 05:16:321907 received_page_title_ = explicit_set;
1908 }
1909
1910 // Lastly, set the title for the view.
[email protected]e53668962010-06-23 15:35:251911 view_->SetPageTitle(UTF16ToWideHack(final_title));
[email protected]420ae012009-04-24 05:16:321912
[email protected]cbc0e1b2010-04-12 18:33:041913 NotificationService::current()->Notify(
1914 NotificationType::TAB_CONTENTS_TITLE_UPDATED,
1915 Source<TabContents>(this),
1916 NotificationService::NoDetails());
1917
[email protected]420ae012009-04-24 05:16:321918 return true;
1919}
1920
1921void TabContents::NotifySwapped() {
1922 // After sending out a swap notification, we need to send a disconnect
1923 // notification so that clients that pick up a pointer to |this| can NULL the
1924 // pointer. See Bug 1230284.
1925 notify_disconnection_ = true;
1926 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341927 NotificationType::TAB_CONTENTS_SWAPPED,
[email protected]d82ed61e2009-06-16 02:46:221928 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321929 NotificationService::NoDetails());
1930}
1931
1932void TabContents::NotifyConnected() {
1933 notify_disconnection_ = true;
1934 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341935 NotificationType::TAB_CONTENTS_CONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221936 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321937 NotificationService::NoDetails());
1938}
1939
1940void TabContents::NotifyDisconnected() {
1941 if (!notify_disconnection_)
1942 return;
1943
1944 notify_disconnection_ = false;
1945 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341946 NotificationType::TAB_CONTENTS_DISCONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221947 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321948 NotificationService::NoDetails());
1949}
1950
1951void TabContents::GenerateKeywordIfNecessary(
1952 const ViewHostMsg_FrameNavigate_Params& params) {
1953 if (!params.searchable_form_url.is_valid())
1954 return;
1955
1956 if (profile()->IsOffTheRecord())
1957 return;
1958
1959 int last_index = controller_.last_committed_entry_index();
1960 // When there was no previous page, the last index will be 0. This is
1961 // normally due to a form submit that opened in a new tab.
1962 // TODO(brettw) bug 916126: we should support keywords when form submits
1963 // happen in new tabs.
1964 if (last_index <= 0)
1965 return;
1966 const NavigationEntry* previous_entry =
1967 controller_.GetEntryAtIndex(last_index - 1);
1968 if (IsFormSubmit(previous_entry)) {
1969 // Only generate a keyword if the previous page wasn't itself a form
1970 // submit.
1971 return;
1972 }
1973
1974 GURL keyword_url = previous_entry->user_typed_url().is_valid() ?
1975 previous_entry->user_typed_url() : previous_entry->url();
1976 std::wstring keyword =
1977 TemplateURLModel::GenerateKeyword(keyword_url, true); // autodetected
1978 if (keyword.empty())
1979 return;
1980
1981 TemplateURLModel* url_model = profile()->GetTemplateURLModel();
1982 if (!url_model)
1983 return;
1984
1985 if (!url_model->loaded()) {
1986 url_model->Load();
1987 return;
1988 }
1989
1990 const TemplateURL* current_url;
[email protected]ddd231e2010-06-29 20:35:191991 GURL url = params.searchable_form_url;
[email protected]420ae012009-04-24 05:16:321992 if (!url_model->CanReplaceKeyword(keyword, url, &current_url))
1993 return;
1994
1995 if (current_url) {
1996 if (current_url->originating_url().is_valid()) {
1997 // The existing keyword was generated from an OpenSearch description
1998 // document, don't regenerate.
1999 return;
2000 }
2001 url_model->Remove(current_url);
2002 }
2003 TemplateURL* new_url = new TemplateURL();
2004 new_url->set_keyword(keyword);
2005 new_url->set_short_name(keyword);
[email protected]ddd231e2010-06-29 20:35:192006 new_url->SetURL(url.spec(), 0, 0);
[email protected]420ae012009-04-24 05:16:322007 new_url->add_input_encoding(params.searchable_form_encoding);
2008 DCHECK(controller_.GetLastCommittedEntry());
2009 const GURL& favicon_url =
2010 controller_.GetLastCommittedEntry()->favicon().url();
2011 if (favicon_url.is_valid()) {
2012 new_url->SetFavIconURL(favicon_url);
2013 } else {
2014 // The favicon url isn't valid. This means there really isn't a favicon,
2015 // or the favicon url wasn't obtained before the load started. This assumes
2016 // the later.
2017 // TODO(sky): Need a way to set the favicon that doesn't involve generating
2018 // its url.
2019 new_url->SetFavIconURL(TemplateURL::GenerateFaviconURL(params.referrer));
2020 }
2021 new_url->set_safe_for_autoreplace(true);
2022 url_model->Add(new_url);
2023}
2024
[email protected]bf5c2ff392009-07-08 16:24:332025void TabContents::OnUserGesture() {
2026 // See comment in RenderViewHostDelegate::OnUserGesture as to why we do this.
[email protected]41291322010-07-15 17:09:012027 DownloadRequestLimiter* limiter =
2028 g_browser_process->download_request_limiter();
2029 if (limiter)
2030 limiter->OnUserGesture(this);
[email protected]d070ec62010-07-27 21:28:262031 ExternalProtocolHandler::PermitLaunchUrl();
[email protected]bf5c2ff392009-07-08 16:24:332032}
2033
2034void TabContents::OnFindReply(int request_id,
2035 int number_of_matches,
2036 const gfx::Rect& selection_rect,
2037 int active_match_ordinal,
2038 bool final_update) {
[email protected]3ce7ced2009-07-24 18:52:422039 // Ignore responses for requests that have been aborted.
2040 if (find_op_aborted_)
2041 return;
2042
[email protected]bf5c2ff392009-07-08 16:24:332043 // Ignore responses for requests other than the one we have most recently
2044 // issued. That way we won't act on stale results when the user has
2045 // already typed in another query.
2046 if (request_id != current_find_request_id_)
2047 return;
2048
2049 if (number_of_matches == -1)
2050 number_of_matches = last_search_result_.number_of_matches();
2051 if (active_match_ordinal == -1)
2052 active_match_ordinal = last_search_result_.active_match_ordinal();
2053
2054 gfx::Rect selection = selection_rect;
2055 if (selection.IsEmpty())
2056 selection = last_search_result_.selection_rect();
2057
2058 // Notify the UI, automation and any other observers that a find result was
2059 // found.
2060 last_search_result_ = FindNotificationDetails(request_id, number_of_matches,
2061 selection, active_match_ordinal,
2062 final_update);
2063 NotificationService::current()->Notify(
2064 NotificationType::FIND_RESULT_AVAILABLE,
2065 Source<TabContents>(this),
2066 Details<FindNotificationDetails>(&last_search_result_));
2067}
2068
2069void TabContents::GoToEntryAtOffset(int offset) {
[email protected]876bc832010-09-07 16:29:542070 if (!delegate_ || delegate_->OnGoToEntryOffset(offset)) {
2071 NavigationEntry* entry = controller_.GetEntryAtOffset(offset);
2072 if (!entry)
2073 return;
2074 // Note that we don't call NavigationController::GotToOffset() as we don't
2075 // want to create a pending navigation entry (it might end up lingering
2076 // http://crbug.com/51680).
[email protected]784688a62010-09-13 07:06:522077 entry->set_transition_type(entry->transition_type() |
2078 PageTransition::FORWARD_BACK);
[email protected]876bc832010-09-07 16:29:542079 NavigateToEntry(*entry, NavigationController::NO_RELOAD);
2080 }
[email protected]bf5c2ff392009-07-08 16:24:332081}
2082
[email protected]bf5c2ff392009-07-08 16:24:332083void TabContents::OnMissingPluginStatus(int status) {
2084#if defined(OS_WIN)
2085// TODO(PORT): pull in when plug-ins work
2086 GetPluginInstaller()->OnMissingPluginStatus(status);
2087#endif
2088}
2089
2090void TabContents::OnCrashedPlugin(const FilePath& plugin_path) {
[email protected]bf5c2ff392009-07-08 16:24:332091 DCHECK(!plugin_path.value().empty());
2092
2093 std::wstring plugin_name = plugin_path.ToWStringHack();
[email protected]45446a52010-11-04 17:41:002094 WebPluginInfo plugin_info;
2095 if (NPAPI::PluginList::Singleton()->GetPluginInfoByPath(
2096 plugin_path, &plugin_info) &&
2097 !plugin_info.name.empty()) {
2098 plugin_name = UTF16ToWide(plugin_info.name);
[email protected]996fd702009-09-04 19:12:372099#if defined(OS_MACOSX)
[email protected]45446a52010-11-04 17:41:002100 // Many plugins on the Mac have .plugin in the actual name, which looks
2101 // terrible, so look for that and strip it off if present.
2102 const std::wstring plugin_extension(L".plugin");
2103 if (EndsWith(plugin_name, plugin_extension, true))
2104 plugin_name.erase(plugin_name.length() - plugin_extension.length());
[email protected]996fd702009-09-04 19:12:372105#endif // OS_MACOSX
[email protected]bf5c2ff392009-07-08 16:24:332106 }
[email protected]2ae8a922010-06-28 20:27:532107 SkBitmap* crash_icon = ResourceBundle::GetSharedInstance().GetBitmapNamed(
2108 IDR_INFOBAR_PLUGIN_CRASHED);
[email protected]bf5c2ff392009-07-08 16:24:332109 AddInfoBar(new SimpleAlertInfoBarDelegate(
[email protected]e23d3a32010-08-13 19:39:582110 this, l10n_util::GetStringFUTF16(IDS_PLUGIN_CRASHED_PROMPT,
2111 WideToUTF16Hack(plugin_name)),
[email protected]2ae8a922010-06-28 20:27:532112 crash_icon, true));
[email protected]bf5c2ff392009-07-08 16:24:332113}
2114
2115void TabContents::OnCrashedWorker() {
2116 AddInfoBar(new SimpleAlertInfoBarDelegate(
[email protected]e23d3a32010-08-13 19:39:582117 this, l10n_util::GetStringUTF16(IDS_WEBWORKER_CRASHED_PROMPT),
[email protected]938e1f92010-04-01 18:09:422118 NULL, true));
[email protected]bf5c2ff392009-07-08 16:24:332119}
2120
[email protected]38789d82010-11-17 06:03:442121void TabContents::OnDidGetApplicationInfo(int32 page_id,
2122 const WebApplicationInfo& info) {
[email protected]ed5431872009-11-17 08:39:512123 web_app_info_ = info;
[email protected]bf5c2ff392009-07-08 16:24:332124
[email protected]ed5431872009-11-17 08:39:512125 if (delegate())
2126 delegate()->OnDidGetApplicationInfo(this, page_id);
[email protected]bf5c2ff392009-07-08 16:24:332127}
2128
[email protected]bb461532010-11-26 21:50:232129void TabContents::OnInstallApplication(const WebApplicationInfo& info) {
2130 if (delegate())
2131 delegate()->OnInstallApplication(this, info);
2132}
2133
[email protected]3c2826852010-12-16 19:09:142134void TabContents::OnBlockedOutdatedPlugin(const string16& name,
2135 const GURL& update_url) {
2136 new OutdatedPluginInfoBar(this, name, update_url);
[email protected]851b1eb2010-08-09 13:32:292137}
2138
[email protected]a2cf65eb2010-01-18 08:20:382139void TabContents::OnPageContents(const GURL& url,
2140 int renderer_process_id,
2141 int32 page_id,
[email protected]e5106202010-06-11 21:12:362142 const string16& contents,
[email protected]1c57b7b2010-07-12 17:38:102143 const std::string& language,
2144 bool page_translatable) {
[email protected]a2cf65eb2010-01-18 08:20:382145 // Don't index any https pages. People generally don't want their bank
2146 // accounts, etc. indexed on their computer, especially since some of these
2147 // things are not marked cachable.
2148 // TODO(brettw) we may want to consider more elaborate heuristics such as
2149 // the cachability of the page. We may also want to consider subframes (this
2150 // test will still index subframes if the subframe is SSL).
[email protected]ccd40a52010-05-11 16:15:272151 // TODO(zelidrag) bug chromium-os:2808 - figure out if we want to reenable
2152 // content indexing for chromeos in some future releases.
2153#if !defined(OS_CHROMEOS)
[email protected]a2cf65eb2010-01-18 08:20:382154 if (!url.SchemeIsSecure()) {
2155 Profile* p = profile();
2156 if (p && !p->IsOffTheRecord()) {
2157 HistoryService* hs = p->GetHistoryService(Profile::IMPLICIT_ACCESS);
2158 if (hs)
2159 hs->SetPageContents(url, contents);
2160 }
2161 }
[email protected]ccd40a52010-05-11 16:15:272162#endif
[email protected]a8a81292010-01-21 00:32:452163
[email protected]1c57b7b2010-07-12 17:38:102164 language_state_.LanguageDetermined(language, page_translatable);
[email protected]a8a81292010-01-21 00:32:452165
2166 std::string lang = language;
2167 NotificationService::current()->Notify(
2168 NotificationType::TAB_LANGUAGE_DETERMINED,
[email protected]7e0831b2010-02-01 08:45:152169 Source<TabContents>(this),
[email protected]a8a81292010-01-21 00:32:452170 Details<std::string>(&lang));
[email protected]a2cf65eb2010-01-18 08:20:382171}
2172
[email protected]7e0831b2010-02-01 08:45:152173void TabContents::OnPageTranslated(int32 page_id,
2174 const std::string& original_lang,
[email protected]152b8ba32010-03-29 19:40:162175 const std::string& translated_lang,
2176 TranslateErrors::Type error_type) {
[email protected]3b1c1812010-02-12 21:53:552177 language_state_.set_current_language(translated_lang);
2178 language_state_.set_translation_pending(false);
[email protected]152b8ba32010-03-29 19:40:162179 PageTranslatedDetails details(original_lang, translated_lang, error_type);
[email protected]7e0831b2010-02-01 08:45:152180 NotificationService::current()->Notify(
2181 NotificationType::PAGE_TRANSLATED,
2182 Source<TabContents>(this),
[email protected]152b8ba32010-03-29 19:40:162183 Details<PageTranslatedDetails>(&details));
[email protected]7e0831b2010-02-01 08:45:152184}
2185
[email protected]ce833282010-11-04 15:48:392186void TabContents::OnSetSuggestions(
2187 int32 page_id,
2188 const std::vector<std::string>& suggestions) {
[email protected]15d9c0c2010-09-10 22:36:262189 if (delegate())
[email protected]ce833282010-11-04 15:48:392190 delegate()->OnSetSuggestions(page_id, suggestions);
2191}
2192
2193void TabContents::OnInstantSupportDetermined(int32 page_id, bool result) {
2194 if (delegate())
2195 delegate()->OnInstantSupportDetermined(page_id, result);
[email protected]15d9c0c2010-09-10 22:36:262196}
2197
[email protected]bf5c2ff392009-07-08 16:24:332198void TabContents::DidStartProvisionalLoadForFrame(
2199 RenderViewHost* render_view_host,
[email protected]4b8e2b02010-11-30 09:15:182200 int64 frame_id,
[email protected]bf5c2ff392009-07-08 16:24:332201 bool is_main_frame,
[email protected]ea51f1f2010-12-01 10:07:272202 bool is_error_page,
[email protected]bf5c2ff392009-07-08 16:24:332203 const GURL& url) {
2204 ProvisionalLoadDetails details(is_main_frame,
2205 controller_.IsURLInPageNavigation(url),
[email protected]dd0aafd2010-12-02 13:13:412206 url, std::string(), false,
2207 is_error_page, frame_id);
[email protected]bf5c2ff392009-07-08 16:24:332208 NotificationService::current()->Notify(
2209 NotificationType::FRAME_PROVISIONAL_LOAD_START,
2210 Source<NavigationController>(&controller_),
2211 Details<ProvisionalLoadDetails>(&details));
[email protected]4f8452d12010-09-02 16:13:142212 if (is_main_frame) {
[email protected]ea51f1f2010-12-01 10:07:272213 // If we're displaying a network error page do not reset the content
2214 // settings delegate's cookies so the user has a chance to modify cookie
2215 // settings.
2216 if (!is_error_page)
2217 content_settings_delegate_->ClearCookieSpecificContentSettings();
[email protected]4f8452d12010-09-02 16:13:142218 content_settings_delegate_->ClearGeolocationContentSettings();
2219 }
[email protected]bf5c2ff392009-07-08 16:24:332220}
2221
2222void TabContents::DidStartReceivingResourceResponse(
[email protected]3dbb80b2010-02-09 22:41:202223 const ResourceRequestDetails& details) {
[email protected]bf5c2ff392009-07-08 16:24:332224 NotificationService::current()->Notify(
2225 NotificationType::RESOURCE_RESPONSE_STARTED,
2226 Source<NavigationController>(&controller()),
[email protected]3dbb80b2010-02-09 22:41:202227 Details<const ResourceRequestDetails>(&details));
[email protected]bf5c2ff392009-07-08 16:24:332228}
2229
[email protected]3dbb80b2010-02-09 22:41:202230void TabContents::DidRedirectResource(
2231 const ResourceRedirectDetails& details) {
[email protected]bf5c2ff392009-07-08 16:24:332232 NotificationService::current()->Notify(
2233 NotificationType::RESOURCE_RECEIVED_REDIRECT,
2234 Source<NavigationController>(&controller()),
[email protected]3dbb80b2010-02-09 22:41:202235 Details<const ResourceRequestDetails>(&details));
[email protected]bf5c2ff392009-07-08 16:24:332236}
2237
2238void TabContents::DidLoadResourceFromMemoryCache(
2239 const GURL& url,
2240 const std::string& frame_origin,
2241 const std::string& main_frame_origin,
2242 const std::string& security_info) {
2243 // Send out a notification that we loaded a resource from our memory cache.
[email protected]c88d24f12010-09-02 02:07:442244 int cert_id = 0, cert_status = 0, security_bits = -1, connection_status = 0;
[email protected]bf5c2ff392009-07-08 16:24:332245 SSLManager::DeserializeSecurityInfo(security_info,
2246 &cert_id, &cert_status,
[email protected]fc7de492010-07-12 14:49:042247 &security_bits,
2248 &connection_status);
[email protected]bf5c2ff392009-07-08 16:24:332249 LoadFromMemoryCacheDetails details(url, frame_origin, main_frame_origin,
[email protected]8cb5d5b2010-02-09 11:36:162250 GetRenderProcessHost()->id(), cert_id,
2251 cert_status);
[email protected]bf5c2ff392009-07-08 16:24:332252
2253 NotificationService::current()->Notify(
2254 NotificationType::LOAD_FROM_MEMORY_CACHE,
2255 Source<NavigationController>(&controller_),
2256 Details<LoadFromMemoryCacheDetails>(&details));
2257}
2258
[email protected]e3d60e5d2009-09-25 21:08:292259void TabContents::DidDisplayInsecureContent() {
[email protected]f17a0ee2010-05-17 17:38:472260 displayed_insecure_content_ = true;
2261 SSLManager::NotifySSLInternalStateChanged();
[email protected]e3d60e5d2009-09-25 21:08:292262}
2263
2264void TabContents::DidRunInsecureContent(const std::string& security_origin) {
[email protected]09843b5f2009-09-26 08:30:592265 controller_.ssl_manager()->DidRunInsecureContent(security_origin);
[email protected]e3d60e5d2009-09-25 21:08:292266}
2267
[email protected]bf5c2ff392009-07-08 16:24:332268void TabContents::DidFailProvisionalLoadWithError(
2269 RenderViewHost* render_view_host,
[email protected]4b8e2b02010-11-30 09:15:182270 int64 frame_id,
[email protected]bf5c2ff392009-07-08 16:24:332271 bool is_main_frame,
2272 int error_code,
2273 const GURL& url,
2274 bool showing_repost_interstitial) {
2275 if (net::ERR_ABORTED == error_code) {
2276 // EVIL HACK ALERT! Ignore failed loads when we're showing interstitials.
2277 // This means that the interstitial won't be torn down properly, which is
2278 // bad. But if we have an interstitial, go back to another tab type, and
2279 // then load the same interstitial again, we could end up getting the first
2280 // interstitial's "failed" message (as a result of the cancel) when we're on
2281 // the second one.
2282 //
2283 // We can't tell this apart, so we think we're tearing down the current page
2284 // which will cause a crash later one. There is also some code in
2285 // RenderViewHostManager::RendererAbortedProvisionalLoad that is commented
2286 // out because of this problem.
2287 //
2288 // http://code.google.com/p/chromium/issues/detail?id=2855
2289 // Because this will not tear down the interstitial properly, if "back" is
2290 // back to another tab type, the interstitial will still be somewhat alive
2291 // in the previous tab type. If you navigate somewhere that activates the
2292 // tab with the interstitial again, you'll see a flash before the new load
2293 // commits of the interstitial page.
2294 if (showing_interstitial_page()) {
2295 LOG(WARNING) << "Discarding message during interstitial.";
2296 return;
2297 }
2298
2299 // This will discard our pending entry if we cancelled the load (e.g., if we
2300 // decided to download the file instead of load it). Only discard the
2301 // pending entry if the URLs match, otherwise the user initiated a navigate
2302 // before the page loaded so that the discard would discard the wrong entry.
2303 NavigationEntry* pending_entry = controller_.pending_entry();
2304 if (pending_entry && pending_entry->url() == url) {
2305 controller_.DiscardNonCommittedEntries();
2306 // Update the URL display.
2307 NotifyNavigationStateChanged(TabContents::INVALIDATE_URL);
2308 }
2309
2310 render_manager_.RendererAbortedProvisionalLoad(render_view_host);
2311 }
2312
2313 // Send out a notification that we failed a provisional load with an error.
2314 ProvisionalLoadDetails details(is_main_frame,
2315 controller_.IsURLInPageNavigation(url),
[email protected]dd0aafd2010-12-02 13:13:412316 url, std::string(), false, false, frame_id);
[email protected]bf5c2ff392009-07-08 16:24:332317 details.set_error_code(error_code);
2318
2319 NotificationService::current()->Notify(
2320 NotificationType::FAIL_PROVISIONAL_LOAD_WITH_ERROR,
2321 Source<NavigationController>(&controller_),
2322 Details<ProvisionalLoadDetails>(&details));
2323}
2324
[email protected]4b8e2b02010-11-30 09:15:182325void TabContents::DocumentLoadedInFrame(int64 frame_id) {
[email protected]bf5c2ff392009-07-08 16:24:332326 controller_.DocumentLoadedInFrame();
[email protected]622474d2010-11-04 09:21:082327 NotificationService::current()->Notify(
2328 NotificationType::FRAME_DOM_CONTENT_LOADED,
2329 Source<NavigationController>(&controller_),
[email protected]4b8e2b02010-11-30 09:15:182330 Details<int64>(&frame_id));
[email protected]bf5c2ff392009-07-08 16:24:332331}
2332
[email protected]4b8e2b02010-11-30 09:15:182333void TabContents::DidFinishLoad(int64 frame_id) {
[email protected]fdd94a02010-11-05 08:07:172334 NotificationService::current()->Notify(
2335 NotificationType::FRAME_DID_FINISH_LOAD,
2336 Source<NavigationController>(&controller_),
[email protected]4b8e2b02010-11-30 09:15:182337 Details<int64>(&frame_id));
[email protected]fdd94a02010-11-05 08:07:172338}
2339
[email protected]dcd5b332010-08-11 08:55:182340void TabContents::OnContentSettingsAccessed(bool content_was_blocked) {
[email protected]614c0e12010-03-25 10:12:562341 if (delegate_)
[email protected]20ad6a92010-04-21 14:07:242342 delegate_->OnContentSettingsChange(this);
[email protected]614c0e12010-03-25 10:12:562343}
2344
[email protected]8d3347f2009-07-09 22:00:212345RenderViewHostDelegate::View* TabContents::GetViewDelegate() {
[email protected]420ae012009-04-24 05:16:322346 return view_.get();
2347}
2348
[email protected]8d3347f2009-07-09 22:00:212349RenderViewHostDelegate::RendererManagement*
2350TabContents::GetRendererManagementDelegate() {
2351 return &render_manager_;
2352}
2353
[email protected]bf5c2ff392009-07-08 16:24:332354RenderViewHostDelegate::BrowserIntegration*
[email protected]8d3347f2009-07-09 22:00:212355 TabContents::GetBrowserIntegrationDelegate() {
2356 return this;
[email protected]bf5c2ff392009-07-08 16:24:332357}
2358
[email protected]8d3347f2009-07-09 22:00:212359RenderViewHostDelegate::Resource* TabContents::GetResourceDelegate() {
2360 return this;
[email protected]bf5c2ff392009-07-08 16:24:332361}
2362
[email protected]46ba9082010-06-22 10:00:582363RenderViewHostDelegate::ContentSettings*
2364TabContents::GetContentSettingsDelegate() {
[email protected]9fb83e82010-07-02 18:24:552365 return content_settings_delegate_.get();
[email protected]46ba9082010-06-22 10:00:582366}
2367
[email protected]8d3347f2009-07-09 22:00:212368RenderViewHostDelegate::Save* TabContents::GetSaveDelegate() {
[email protected]420ae012009-04-24 05:16:322369 return save_package_.get(); // May be NULL, but we can return NULL.
2370}
2371
[email protected]8d3347f2009-07-09 22:00:212372RenderViewHostDelegate::Printing* TabContents::GetPrintingDelegate() {
[email protected]12802702010-07-09 19:43:092373 return printing_.get();
[email protected]8d3347f2009-07-09 22:00:212374}
2375
2376RenderViewHostDelegate::FavIcon* TabContents::GetFavIconDelegate() {
2377 return &fav_icon_helper_;
2378}
2379
[email protected]e8dbd674b2010-03-11 18:34:402380RenderViewHostDelegate::Autocomplete* TabContents::GetAutocompleteDelegate() {
[email protected]6db0def92010-03-02 19:55:452381 if (autocomplete_history_manager_.get() == NULL)
2382 autocomplete_history_manager_.reset(new AutocompleteHistoryManager(this));
2383 return autocomplete_history_manager_.get();
[email protected]bf5c2ff392009-07-08 16:24:332384}
2385
[email protected]21674df2009-10-28 19:51:212386RenderViewHostDelegate::AutoFill* TabContents::GetAutoFillDelegate() {
[email protected]cf28ec52010-04-09 22:56:342387 return GetAutoFillManager();
[email protected]21674df2009-10-28 19:51:212388}
2389
[email protected]1f18184a2010-07-21 19:34:492390RenderViewHostDelegate::SSL* TabContents::GetSSLDelegate() {
2391 return GetSSLHelper();
2392}
2393
[email protected]b5977a0c2010-08-24 19:46:262394RenderViewHostDelegate::FileSelect* TabContents::GetFileSelectDelegate() {
2395 if (file_select_helper_.get() == NULL)
[email protected]ba70d082010-09-10 16:54:492396 file_select_helper_.reset(new FileSelectHelper(profile()));
[email protected]b5977a0c2010-08-24 19:46:262397 return file_select_helper_.get();
2398}
2399
[email protected]fd571bb2010-03-19 01:23:542400AutomationResourceRoutingDelegate*
2401TabContents::GetAutomationResourceRoutingDelegate() {
2402 return delegate();
2403}
2404
[email protected]ced90ae12010-02-20 02:06:162405RenderViewHostDelegate::BookmarkDrag* TabContents::GetBookmarkDragDelegate() {
2406 return bookmark_drag_;
2407}
2408
2409void TabContents::SetBookmarkDragDelegate(
2410 RenderViewHostDelegate::BookmarkDrag* bookmark_drag) {
2411 bookmark_drag_ = bookmark_drag;
2412}
2413
[email protected]9fb83e82010-07-02 18:24:552414TabSpecificContentSettings* TabContents::GetTabSpecificContentSettings() const {
2415 return content_settings_delegate_.get();
2416}
2417
[email protected]93623c5d2009-12-10 21:40:322418RendererPreferences TabContents::GetRendererPrefs(Profile* profile) const {
[email protected]840b1512009-07-21 16:53:462419 return renderer_preferences_;
[email protected]80d96fa2009-06-10 22:34:512420}
2421
[email protected]57c6a652009-05-04 07:58:342422TabContents* TabContents::GetAsTabContents() {
2423 return this;
2424}
2425
[email protected]7b291f92009-08-14 05:43:532426ViewType::Type TabContents::GetRenderViewType() const {
2427 return ViewType::TAB_CONTENTS;
2428}
2429
2430int TabContents::GetBrowserWindowID() const {
2431 return controller().window_id().id();
2432}
2433
[email protected]420ae012009-04-24 05:16:322434void TabContents::RenderViewCreated(RenderViewHost* render_view_host) {
[email protected]2e4633c2009-07-09 16:58:062435 NotificationService::current()->Notify(
2436 NotificationType::RENDER_VIEW_HOST_CREATED_FOR_TAB,
2437 Source<TabContents>(this),
2438 Details<RenderViewHost>(render_view_host));
[email protected]420ae012009-04-24 05:16:322439 NavigationEntry* entry = controller_.GetActiveEntry();
2440 if (!entry)
2441 return;
2442
2443 // When we're creating views, we're still doing initial setup, so we always
2444 // use the pending DOM UI rather than any possibly existing committed one.
2445 if (render_manager_.pending_dom_ui()) {
2446 render_manager_.pending_dom_ui()->RenderViewCreated(render_view_host);
2447 }
2448
2449 if (entry->IsViewSourceMode()) {
2450 // Put the renderer in view source mode.
2451 render_view_host->Send(
2452 new ViewMsg_EnableViewSourceMode(render_view_host->routing_id()));
2453 }
[email protected]0666aef2009-05-13 19:48:082454
2455 view()->RenderViewCreated(render_view_host);
[email protected]420ae012009-04-24 05:16:322456}
2457
2458void TabContents::RenderViewReady(RenderViewHost* rvh) {
2459 if (rvh != render_view_host()) {
2460 // Don't notify the world, since this came from a renderer in the
2461 // background.
2462 return;
2463 }
2464
2465 NotifyConnected();
[email protected]9d8a4642009-07-29 17:25:302466 bool was_crashed = is_crashed();
[email protected]443b80e2010-12-14 00:42:232467 SetIsCrashed(base::TERMINATION_STATUS_STILL_RUNNING, 0);
[email protected]9d8a4642009-07-29 17:25:302468
2469 // Restore the focus to the tab (otherwise the focus will be on the top
2470 // window).
[email protected]484ae5912010-09-29 19:16:142471 if (was_crashed && !FocusLocationBarByDefault() &&
2472 (!delegate_ || delegate_->ShouldFocusPageAfterCrash())) {
[email protected]9d8a4642009-07-29 17:25:302473 Focus();
[email protected]484ae5912010-09-29 19:16:142474 }
[email protected]420ae012009-04-24 05:16:322475}
2476
[email protected]443b80e2010-12-14 00:42:232477void TabContents::RenderViewGone(RenderViewHost* rvh,
2478 base::TerminationStatus status,
2479 int error_code) {
[email protected]420ae012009-04-24 05:16:322480 // Ask the print preview if this renderer was valuable.
[email protected]12802702010-07-09 19:43:092481 if (!printing_->OnRenderViewGone(rvh))
[email protected]420ae012009-04-24 05:16:322482 return;
2483 if (rvh != render_view_host()) {
2484 // The pending page's RenderViewHost is gone.
2485 return;
2486 }
2487
2488 SetIsLoading(false, NULL);
2489 NotifyDisconnected();
[email protected]443b80e2010-12-14 00:42:232490 SetIsCrashed(status, error_code);
[email protected]420ae012009-04-24 05:16:322491
[email protected]ff9efc62010-02-18 22:10:542492 // Remove all infobars.
[email protected]e5430c92010-02-18 22:39:212493 for (int i = infobar_delegate_count() - 1; i >=0 ; --i)
[email protected]ff9efc62010-02-18 22:10:542494 RemoveInfoBar(GetInfoBarDelegateAt(i));
2495
[email protected]8e537302009-06-10 21:19:582496 // Tell the view that we've crashed so it can prepare the sad tab page.
[email protected]cf68b092010-01-15 20:05:552497 // Only do this if we're not in browser shutdown, so that TabContents
2498 // objects that are not in a browser (e.g., HTML dialogs) and thus are
2499 // visible do not flash a sad tab page.
2500 if (browser_shutdown::GetShutdownType() == browser_shutdown::NOT_VALID)
2501 view_->OnTabCrashed();
[email protected]420ae012009-04-24 05:16:322502
2503 // Hide any visible hung renderer warning for this web contents' process.
[email protected]e7b8d832009-11-20 22:41:002504 hung_renderer_dialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:322505}
2506
[email protected]2e4633c2009-07-09 16:58:062507void TabContents::RenderViewDeleted(RenderViewHost* rvh) {
2508 NotificationService::current()->Notify(
2509 NotificationType::RENDER_VIEW_HOST_DELETED,
2510 Source<TabContents>(this),
2511 Details<RenderViewHost>(rvh));
[email protected]fb7b79f72009-11-06 18:00:482512 render_manager_.RenderViewDeleted(rvh);
[email protected]2e4633c2009-07-09 16:58:062513}
2514
[email protected]420ae012009-04-24 05:16:322515void TabContents::DidNavigate(RenderViewHost* rvh,
2516 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]8030f012009-09-25 18:09:372517 int extra_invalidate_flags = 0;
2518
2519 if (PageTransition::IsMainFrame(params.transition)) {
[email protected]33f74972010-12-08 16:40:362520 PrerenderManager* pm = profile()->GetPrerenderManager();
2521 if (pm != NULL) {
2522 if (pm->MaybeUsePreloadedPage(this, params.url)) {
2523 // TODO(tburkard): If the preloaded page has not finished preloading
2524 // yet, we should not do this.
2525 DidStopLoading();
2526 return;
2527 }
2528 }
2529
[email protected]20ede072009-10-06 00:28:352530 bool was_bookmark_bar_visible = ShouldShowBookmarkBar();
[email protected]8030f012009-09-25 18:09:372531
[email protected]420ae012009-04-24 05:16:322532 render_manager_.DidNavigateMainFrame(rvh);
2533
[email protected]20ede072009-10-06 00:28:352534 if (was_bookmark_bar_visible != ShouldShowBookmarkBar())
[email protected]8030f012009-09-25 18:09:372535 extra_invalidate_flags |= INVALIDATE_BOOKMARK_BAR;
[email protected]8030f012009-09-25 18:09:372536 }
2537
[email protected]420ae012009-04-24 05:16:322538 // Update the site of the SiteInstance if it doesn't have one yet.
2539 if (!GetSiteInstance()->has_site())
2540 GetSiteInstance()->SetSite(params.url);
2541
2542 // Need to update MIME type here because it's referred to in
2543 // UpdateNavigationCommands() called by RendererDidNavigate() to
2544 // determine whether or not to enable the encoding menu.
2545 // It's updated only for the main frame. For a subframe,
2546 // RenderView::UpdateURL does not set params.contents_mime_type.
2547 // (see http://code.google.com/p/chromium/issues/detail?id=2929 )
2548 // TODO(jungshik): Add a test for the encoding menu to avoid
2549 // regressing it again.
2550 if (PageTransition::IsMainFrame(params.transition))
2551 contents_mime_type_ = params.contents_mime_type;
2552
2553 NavigationController::LoadCommittedDetails details;
[email protected]8030f012009-09-25 18:09:372554 bool did_navigate = controller_.RendererDidNavigate(
2555 params, extra_invalidate_flags, &details);
[email protected]3e90d4a2009-07-03 17:38:392556
[email protected]a9c0bfe2010-09-17 08:35:222557 // Send notification about committed provisional loads. This notification is
2558 // different from the NAV_ENTRY_COMMITTED notification which doesn't include
2559 // the actual URL navigated to and isn't sent for AUTO_SUBFRAME navigations.
2560 if (details.type != NavigationType::NAV_IGNORE) {
[email protected]dabb0d12010-10-05 12:50:072561 // For AUTO_SUBFRAME navigations, an event for the main frame is generated
2562 // that is not recorded in the navigation history. For the purpose of
2563 // tracking navigation events, we treat this event as a sub frame navigation
2564 // event.
2565 bool is_main_frame = did_navigate ? details.is_main_frame : false;
2566 ProvisionalLoadDetails load_details(
2567 is_main_frame, details.is_in_page, params.url, std::string(), false,
[email protected]dd0aafd2010-12-02 13:13:412568 false, params.frame_id);
[email protected]a9c0bfe2010-09-17 08:35:222569 load_details.set_transition_type(params.transition);
2570 // Whether or not a page transition was triggered by going backward or
2571 // forward in the history is only stored in the navigation controller's
2572 // entry list.
2573 if (did_navigate &&
2574 (controller_.GetActiveEntry()->transition_type() &
2575 PageTransition::FORWARD_BACK)) {
2576 load_details.set_transition_type(
2577 params.transition | PageTransition::FORWARD_BACK);
2578 }
2579 NotificationService::current()->Notify(
2580 NotificationType::FRAME_PROVISIONAL_LOAD_COMMITTED,
2581 Source<NavigationController>(&controller_),
2582 Details<ProvisionalLoadDetails>(&load_details));
[email protected]a9c0bfe2010-09-17 08:35:222583 }
2584
[email protected]3e90d4a2009-07-03 17:38:392585 // Update history. Note that this needs to happen after the entry is complete,
2586 // which WillNavigate[Main,Sub]Frame will do before this function is called.
[email protected]ec0b6c42010-08-26 03:16:582587 if (params.should_update_history) {
[email protected]3e90d4a2009-07-03 17:38:392588 // Most of the time, the displayURL matches the loaded URL, but for about:
[email protected]ec0b6c42010-08-26 03:16:582589 // URLs, we use a data: URL as the real value. We actually want to save the
2590 // about: URL to the history db and keep the data: URL hidden. This is what
2591 // the TabContents' URL getter does.
2592 scoped_refptr<history::HistoryAddPageArgs> add_page_args(
2593 CreateHistoryAddPageArgs(GetURL(), details, params));
2594 if (!delegate() ||
2595 delegate()->ShouldAddNavigationToHistory(*add_page_args,
2596 details.type)) {
2597 UpdateHistoryForNavigation(add_page_args);
2598 }
[email protected]3e90d4a2009-07-03 17:38:392599 }
2600
[email protected]76543b92009-08-31 17:27:452601 if (!did_navigate)
2602 return; // No navigation happened.
[email protected]420ae012009-04-24 05:16:322603
2604 // DO NOT ADD MORE STUFF TO THIS FUNCTION! Your component should either listen
2605 // for the appropriate notification (best) or you can add it to
2606 // DidNavigateMainFramePostCommit / DidNavigateAnyFramePostCommit (only if
2607 // necessary, please).
2608
2609 // Run post-commit tasks.
2610 if (details.is_main_frame)
2611 DidNavigateMainFramePostCommit(details, params);
2612 DidNavigateAnyFramePostCommit(rvh, details, params);
2613}
2614
2615void TabContents::UpdateState(RenderViewHost* rvh,
2616 int32 page_id,
2617 const std::string& state) {
2618 DCHECK(rvh == render_view_host());
2619
2620 // We must be prepared to handle state updates for any page, these occur
2621 // when the user is scrolling and entering form data, as well as when we're
2622 // leaving a page, in which case our state may have already been moved to
2623 // the next page. The navigation controller will look up the appropriate
2624 // NavigationEntry and update it when it is notified via the delegate.
2625
2626 int entry_index = controller_.GetEntryIndexWithPageID(
2627 GetSiteInstance(), page_id);
2628 if (entry_index < 0)
2629 return;
2630 NavigationEntry* entry = controller_.GetEntryAtIndex(entry_index);
2631
2632 if (state == entry->content_state())
2633 return; // Nothing to update.
2634 entry->set_content_state(state);
2635 controller_.NotifyEntryChanged(entry, entry_index);
2636}
2637
2638void TabContents::UpdateTitle(RenderViewHost* rvh,
2639 int32 page_id, const std::wstring& title) {
2640 // If we have a title, that's a pretty good indication that we've started
2641 // getting useful data.
2642 SetNotWaitingForResponse();
2643
2644 DCHECK(rvh == render_view_host());
[email protected]371cc8b2010-09-01 16:03:482645 NavigationEntry* entry = controller_.GetEntryWithPageID(rvh->site_instance(),
[email protected]ec0b6c42010-08-26 03:16:582646 page_id);
[email protected]420ae012009-04-24 05:16:322647 if (!entry || !UpdateTitleForEntry(entry, title))
2648 return;
2649
2650 // Broadcast notifications when the UI should be updated.
2651 if (entry == controller_.GetEntryAtOffset(0))
[email protected]f1cd5e82009-10-23 17:19:032652 NotifyNavigationStateChanged(INVALIDATE_TITLE);
[email protected]420ae012009-04-24 05:16:322653}
2654
[email protected]420ae012009-04-24 05:16:322655void TabContents::UpdateEncoding(RenderViewHost* render_view_host,
[email protected]41fc0322009-09-04 22:23:402656 const std::string& encoding) {
[email protected]420ae012009-04-24 05:16:322657 set_encoding(encoding);
2658}
2659
2660void TabContents::UpdateTargetURL(int32 page_id, const GURL& url) {
2661 if (delegate())
2662 delegate()->UpdateTargetURL(this, url);
2663}
2664
2665void TabContents::UpdateThumbnail(const GURL& url,
2666 const SkBitmap& bitmap,
2667 const ThumbnailScore& score) {
[email protected]e6baed562010-11-02 22:33:442668 if (profile()->IsOffTheRecord())
2669 return;
2670
[email protected]420ae012009-04-24 05:16:322671 // Tell History about this thumbnail
[email protected]2ec53b52010-09-21 03:24:492672 if (history::TopSites::IsEnabled()) {
[email protected]e6baed562010-11-02 22:33:442673 history::TopSites* ts = profile()->GetTopSites();
2674 if (ts)
2675 ts->SetPageThumbnail(url, bitmap, score);
[email protected]449478302009-06-09 20:04:282676 } else {
[email protected]e6baed562010-11-02 22:33:442677 HistoryService* hs =
2678 profile()->GetHistoryService(Profile::IMPLICIT_ACCESS);
2679 if (hs)
[email protected]449478302009-06-09 20:04:282680 hs->SetPageThumbnail(url, bitmap, score);
[email protected]420ae012009-04-24 05:16:322681 }
2682}
2683
[email protected]c4e98902010-06-01 10:20:142684void TabContents::UpdateInspectorSetting(const std::string& key,
2685 const std::string& value) {
[email protected]24d27d72010-10-12 16:30:052686 RenderViewHostDelegateHelper::UpdateInspectorSetting(profile(), key, value);
[email protected]0df30122009-06-03 12:13:082687}
2688
[email protected]45aafc92010-06-07 14:18:532689void TabContents::ClearInspectorSettings() {
[email protected]24d27d72010-10-12 16:30:052690 RenderViewHostDelegateHelper::ClearInspectorSettings(profile());
[email protected]45aafc92010-06-07 14:18:532691}
2692
[email protected]420ae012009-04-24 05:16:322693void TabContents::Close(RenderViewHost* rvh) {
[email protected]07707302009-11-06 00:50:292694 // The UI may be in an event-tracking loop, such as between the
2695 // mouse-down and mouse-up in text selection or a button click.
2696 // Defer the close until after tracking is complete, so that we
2697 // don't free objects out from under the UI.
2698 // TODO(shess): This could probably be integrated with the
2699 // IsDoingDrag() test below. Punting for now because I need more
2700 // research to understand how this impacts platforms other than Mac.
2701 // TODO(shess): This could get more fine-grained. For instance,
2702 // closing a tab in another window while selecting text in the
2703 // current window's Omnibox should be just fine.
2704 if (view()->IsEventTracking()) {
2705 view()->CloseTabAfterEventTracking();
2706 return;
2707 }
2708
[email protected]24a4d1062009-07-10 23:10:422709 // If we close the tab while we're in the middle of a drag, we'll crash.
2710 // Instead, cancel the drag and close it as soon as the drag ends.
2711 if (view()->IsDoingDrag()) {
2712 view()->CancelDragAndCloseTab();
2713 return;
2714 }
2715
[email protected]420ae012009-04-24 05:16:322716 // Ignore this if it comes from a RenderViewHost that we aren't showing.
2717 if (delegate() && rvh == render_view_host())
2718 delegate()->CloseContents(this);
2719}
2720
2721void TabContents::RequestMove(const gfx::Rect& new_bounds) {
2722 if (delegate() && delegate()->IsPopup(this))
2723 delegate()->MoveContents(this, new_bounds);
2724}
2725
[email protected]7ab1e7d62009-10-14 23:32:012726void TabContents::DidStartLoading() {
[email protected]420ae012009-04-24 05:16:322727 SetIsLoading(true, NULL);
[email protected]3c9e1872010-11-18 16:17:492728
[email protected]de4761b2010-12-07 21:05:212729 if (delegate()) {
2730 bool is_print_preview_tab =
2731 printing::PrintPreviewTabController::IsPrintPreviewTab(this);
2732 if (content_restrictions_ || is_print_preview_tab) {
2733 content_restrictions_= is_print_preview_tab ?
2734 CONTENT_RESTRICTION_PRINT : 0;
2735 delegate()->ContentRestrictionsChanged(this);
2736 }
[email protected]9e823662010-10-13 23:36:002737 }
[email protected]3c9e1872010-11-18 16:17:492738
2739 // Notify observers about navigation.
2740 FOR_EACH_OBSERVER(WebNavigationObserver, web_navigation_observers_,
2741 DidStartLoading());
[email protected]420ae012009-04-24 05:16:322742}
2743
[email protected]7ab1e7d62009-10-14 23:32:012744void TabContents::DidStopLoading() {
[email protected]420ae012009-04-24 05:16:322745 scoped_ptr<LoadNotificationDetails> details;
2746
2747 NavigationEntry* entry = controller_.GetActiveEntry();
2748 // An entry may not exist for a stop when loading an initial blank page or
2749 // if an iframe injected by script into a blank page finishes loading.
2750 if (entry) {
[email protected]420ae012009-04-24 05:16:322751 base::TimeDelta elapsed = base::TimeTicks::Now() - current_load_start_;
2752
2753 details.reset(new LoadNotificationDetails(
[email protected]ebe89e062009-08-13 23:16:542754 entry->virtual_url(),
[email protected]420ae012009-04-24 05:16:322755 entry->transition_type(),
2756 elapsed,
2757 &controller_,
2758 controller_.GetCurrentEntryIndex()));
2759 }
2760
[email protected]420ae012009-04-24 05:16:322761 SetIsLoading(false, details.get());
[email protected]3c9e1872010-11-18 16:17:492762
2763 // Notify observers about navigation.
2764 FOR_EACH_OBSERVER(WebNavigationObserver, web_navigation_observers_,
2765 DidStopLoading());
[email protected]420ae012009-04-24 05:16:322766}
2767
[email protected]1a3c3cb2010-12-16 21:03:402768void TabContents::DidChangeLoadProgress(double progress) {
2769 if (delegate())
2770 delegate()->LoadProgressChanged(progress);
2771}
2772
[email protected]25497492010-09-11 15:15:082773void TabContents::DocumentOnLoadCompletedInMainFrame(
2774 RenderViewHost* render_view_host,
2775 int32 page_id) {
2776 NotificationService::current()->Notify(
2777 NotificationType::LOAD_COMPLETED_MAIN_FRAME,
2778 Source<TabContents>(this),
2779 Details<int>(&page_id));
2780}
2781
[email protected]420ae012009-04-24 05:16:322782void TabContents::DidRedirectProvisionalLoad(int32 page_id,
2783 const GURL& source_url,
2784 const GURL& target_url) {
2785 NavigationEntry* entry;
2786 if (page_id == -1)
2787 entry = controller_.pending_entry();
2788 else
2789 entry = controller_.GetEntryWithPageID(GetSiteInstance(), page_id);
2790 if (!entry || entry->url() != source_url)
2791 return;
2792 entry->set_url(target_url);
2793}
2794
[email protected]420ae012009-04-24 05:16:322795void TabContents::RequestOpenURL(const GURL& url, const GURL& referrer,
2796 WindowOpenDisposition disposition) {
2797 if (render_manager_.dom_ui()) {
2798 // When we're a DOM UI, it will provide a page transition type for us (this
2799 // is so the new tab page can specify AUTO_BOOKMARK for automatically
2800 // generated suggestions).
2801 //
2802 // Note also that we hide the referrer for DOM UI pages. We don't really
[email protected]60e448982009-05-06 04:21:162803 // want web sites to see a referrer of "chrome://blah" (and some
2804 // chrome: URLs might have search terms or other stuff we don't want to
[email protected]420ae012009-04-24 05:16:322805 // send to the site), so we send no referrer.
2806 OpenURL(url, GURL(), disposition,
2807 render_manager_.dom_ui()->link_transition_type());
2808 } else {
2809 OpenURL(url, referrer, disposition, PageTransition::LINK);
2810 }
2811}
2812
[email protected]1ee614862009-07-06 20:29:182813void TabContents::DomOperationResponse(const std::string& json_string,
2814 int automation_id) {
2815}
2816
[email protected]8b8e7c92010-08-19 18:05:562817void TabContents::ProcessDOMUIMessage(
2818 const ViewHostMsg_DomMessage_Params& params) {
[email protected]420ae012009-04-24 05:16:322819 if (!render_manager_.dom_ui()) {
[email protected]c2e74fe82009-09-03 17:57:442820 // This can happen if someone uses window.open() to open an extension URL
2821 // from a non-extension context.
[email protected]8b8e7c92010-08-19 18:05:562822 render_view_host()->BlockExtensionRequest(params.request_id);
[email protected]420ae012009-04-24 05:16:322823 return;
2824 }
[email protected]8b8e7c92010-08-19 18:05:562825 render_manager_.dom_ui()->ProcessDOMUIMessage(params);
[email protected]420ae012009-04-24 05:16:322826}
2827
2828void TabContents::ProcessExternalHostMessage(const std::string& message,
2829 const std::string& origin,
2830 const std::string& target) {
2831 if (delegate())
2832 delegate()->ForwardMessageToExternalHost(message, origin, target);
2833}
2834
[email protected]420ae012009-04-24 05:16:322835void TabContents::RunJavaScriptMessage(
2836 const std::wstring& message,
2837 const std::wstring& default_prompt,
2838 const GURL& frame_url,
2839 const int flags,
2840 IPC::Message* reply_msg,
2841 bool* did_suppress_message) {
2842 // Suppress javascript messages when requested and when inside a constrained
2843 // popup window (because that activates them and breaks them out of the
2844 // constrained window jail).
[email protected]91ed49a2009-07-07 21:29:552845 // Also suppress messages when showing an interstitial. The interstitial is
2846 // shown over the previous page, we don't want the hidden page dialogs to
2847 // interfere with the interstitial.
[email protected]8f55e802010-12-06 18:11:502848 bool suppress_this_message =
2849 suppress_javascript_messages_ ||
2850 showing_interstitial_page() ||
2851 (delegate() && delegate()->ShouldSuppressDialogs());
[email protected]420ae012009-04-24 05:16:322852 if (delegate())
2853 suppress_this_message |=
[email protected]b6c874582009-05-08 19:38:312854 (delegate()->GetConstrainingContents(this) != this);
[email protected]420ae012009-04-24 05:16:322855
2856 *did_suppress_message = suppress_this_message;
2857
2858 if (!suppress_this_message) {
2859 base::TimeDelta time_since_last_message(
2860 base::TimeTicks::Now() - last_javascript_message_dismissal_);
2861 bool show_suppress_checkbox = false;
2862 // Show a checkbox offering to suppress further messages if this message is
2863 // being displayed within kJavascriptMessageExpectedDelay of the last one.
2864 if (time_since_last_message <
2865 base::TimeDelta::FromMilliseconds(kJavascriptMessageExpectedDelay))
2866 show_suppress_checkbox = true;
2867
[email protected]d6ad7442010-09-05 19:06:522868 RunJavascriptMessageBox(profile(), this, frame_url, flags, message,
2869 default_prompt, show_suppress_checkbox, reply_msg);
[email protected]420ae012009-04-24 05:16:322870 } else {
2871 // If we are suppressing messages, just reply as is if the user immediately
2872 // pressed "Cancel".
[email protected]beb440c2009-11-06 04:08:542873 OnMessageBoxClosed(reply_msg, false, std::wstring());
[email protected]420ae012009-04-24 05:16:322874 }
2875}
2876
2877void TabContents::RunBeforeUnloadConfirm(const std::wstring& message,
2878 IPC::Message* reply_msg) {
[email protected]5aab5e22010-12-08 22:13:292879 if (delegate())
2880 delegate()->WillRunBeforeUnloadConfirm();
[email protected]8f55e802010-12-06 18:11:502881 if (delegate() && delegate()->ShouldSuppressDialogs()) {
2882 render_view_host()->JavaScriptMessageBoxClosed(reply_msg, true,
2883 std::wstring());
2884 return;
2885 }
[email protected]7ab1e7d62009-10-14 23:32:012886 is_showing_before_unload_dialog_ = true;
[email protected]d82ed61e2009-06-16 02:46:222887 RunBeforeUnloadDialog(this, message, reply_msg);
[email protected]420ae012009-04-24 05:16:322888}
2889
2890void TabContents::ShowModalHTMLDialog(const GURL& url, int width, int height,
2891 const std::string& json_arguments,
2892 IPC::Message* reply_msg) {
2893 if (delegate()) {
2894 HtmlDialogUIDelegate* dialog_delegate =
2895 new ModalHtmlDialogDelegate(url, width, height, json_arguments,
[email protected]d82ed61e2009-06-16 02:46:222896 reply_msg, this);
[email protected]420ae012009-04-24 05:16:322897 delegate()->ShowHtmlDialog(dialog_delegate, NULL);
2898 }
2899}
2900
[email protected]c4aa86962010-11-24 23:08:042901void TabContents::PasswordFormsFound(
2902 const std::vector<webkit_glue::PasswordForm>& forms) {
2903 FOR_EACH_OBSERVER(WebNavigationObserver, web_navigation_observers_,
2904 PasswordFormsFound(forms));
2905}
2906
2907void TabContents::PasswordFormsVisible(
2908 const std::vector<webkit_glue::PasswordForm>& visible_forms) {
2909 FOR_EACH_OBSERVER(WebNavigationObserver, web_navigation_observers_,
2910 PasswordFormsVisible(visible_forms));
2911}
2912
[email protected]420ae012009-04-24 05:16:322913// Checks to see if we should generate a keyword based on the OSDD, and if
2914// necessary uses TemplateURLFetcher to download the OSDD and create a keyword.
[email protected]2de7e002010-10-11 22:58:202915void TabContents::PageHasOSDD(
2916 RenderViewHost* render_view_host,
2917 int32 page_id,
2918 const GURL& url,
2919 const ViewHostMsg_PageHasOSDD_Type& msg_provider_type) {
[email protected]0b5b85c2010-10-11 23:00:122920 // Make sure page_id is the current page and other basic checks.
[email protected]420ae012009-04-24 05:16:322921 DCHECK(url.is_valid());
2922 if (!IsActiveEntry(page_id))
2923 return;
[email protected]420ae012009-04-24 05:16:322924 if (!profile()->GetTemplateURLFetcher())
2925 return;
[email protected]420ae012009-04-24 05:16:322926 if (profile()->IsOffTheRecord())
2927 return;
2928
[email protected]2de7e002010-10-11 22:58:202929 TemplateURLFetcher::ProviderType provider_type;
2930 switch (msg_provider_type.type) {
2931 case ViewHostMsg_PageHasOSDD_Type::AUTODETECTED_PROVIDER:
2932 provider_type = TemplateURLFetcher::AUTODETECTED_PROVIDER;
2933 break;
2934
2935 case ViewHostMsg_PageHasOSDD_Type::EXPLICIT_DEFAULT_PROVIDER:
2936 provider_type = TemplateURLFetcher::EXPLICIT_DEFAULT_PROVIDER;
2937 break;
2938
2939 case ViewHostMsg_PageHasOSDD_Type::EXPLICIT_PROVIDER:
2940 provider_type = TemplateURLFetcher::EXPLICIT_PROVIDER;
2941 break;
2942
2943 default:
2944 NOTREACHED();
2945 return;
2946 }
2947
[email protected]420ae012009-04-24 05:16:322948 const NavigationEntry* entry = controller_.GetLastCommittedEntry();
2949 DCHECK(entry);
2950
2951 const NavigationEntry* base_entry = entry;
2952 if (IsFormSubmit(base_entry)) {
2953 // If the current page is a form submit, find the last page that was not
2954 // a form submit and use its url to generate the keyword from.
2955 int index = controller_.last_committed_entry_index() - 1;
2956 while (index >= 0 && IsFormSubmit(controller_.GetEntryAtIndex(index)))
2957 index--;
2958 if (index >= 0)
2959 base_entry = controller_.GetEntryAtIndex(index);
2960 else
2961 base_entry = NULL;
2962 }
2963
2964 // We want to use the user typed URL if available since that represents what
2965 // the user typed to get here, and fall back on the regular URL if not.
2966 if (!base_entry)
2967 return;
2968 GURL keyword_url = base_entry->user_typed_url().is_valid() ?
2969 base_entry->user_typed_url() : base_entry->url();
2970 if (!keyword_url.is_valid())
2971 return;
[email protected]2de7e002010-10-11 22:58:202972
[email protected]0b5b85c2010-10-11 23:00:122973 std::wstring keyword = TemplateURLModel::GenerateKeyword(
2974 keyword_url,
2975 provider_type == TemplateURLFetcher::AUTODETECTED_PROVIDER);
[email protected]a88dbdd52010-01-15 19:25:532976
[email protected]0b5b85c2010-10-11 23:00:122977 // Download the OpenSearch description document. If this is successful, a
[email protected]420ae012009-04-24 05:16:322978 // new keyword will be created when done.
[email protected]420ae012009-04-24 05:16:322979 profile()->GetTemplateURLFetcher()->ScheduleDownload(
2980 keyword,
2981 url,
2982 base_entry->favicon().url(),
[email protected]0b5b85c2010-10-11 23:00:122983 new TemplateURLFetcherUICallbacks(this),
[email protected]2de7e002010-10-11 22:58:202984 provider_type);
[email protected]420ae012009-04-24 05:16:322985}
2986
[email protected]420ae012009-04-24 05:16:322987GURL TabContents::GetAlternateErrorPageURL() const {
2988 GURL url;
2989 // Disable alternate error pages when in OffTheRecord/Incognito mode.
2990 if (profile()->IsOffTheRecord())
2991 return url;
2992
2993 PrefService* prefs = profile()->GetPrefs();
2994 DCHECK(prefs);
2995 if (prefs->GetBoolean(prefs::kAlternateErrorPagesEnabled)) {
[email protected]a33457b82009-09-21 23:07:152996 url = google_util::AppendGoogleLocaleParam(
2997 GURL(google_util::kLinkDoctorBaseURL));
[email protected]420ae012009-04-24 05:16:322998 url = google_util::AppendGoogleTLDParam(url);
2999 }
3000 return url;
3001}
3002
3003WebPreferences TabContents::GetWebkitPrefs() {
[email protected]6c32ce72010-03-08 05:18:063004 Profile* profile = render_view_host()->process()->profile();
[email protected]7ccddb8c2009-08-04 17:36:553005 bool is_dom_ui = false;
[email protected]447021c2010-09-08 21:29:083006 WebPreferences web_prefs =
3007 RenderViewHostDelegateHelper::GetWebkitPrefs(profile, is_dom_ui);
3008
[email protected]9beff752010-09-22 19:35:433009 // Force accelerated compositing and 2d canvas off for chrome: and
3010 // chrome-extension: pages.
[email protected]27c790d42010-10-22 09:28:433011 if (GetURL().SchemeIs(chrome::kChromeDevToolsScheme) ||
3012 GetURL().SchemeIs(chrome::kChromeUIScheme)) {
[email protected]447021c2010-09-08 21:29:083013 web_prefs.accelerated_compositing_enabled = false;
[email protected]9beff752010-09-22 19:35:433014 web_prefs.accelerated_2d_canvas_enabled = false;
[email protected]447021c2010-09-08 21:29:083015 }
3016
[email protected]39553442010-09-09 00:01:553017#if defined(OS_MACOSX)
3018 // Disable accelerated compositing if IOSurface's are not supported,
3019 // as is the case in 10.5.
3020 if (!IOSurfaceSupport::Initialize())
3021 web_prefs.accelerated_compositing_enabled = false;
3022#endif
3023
[email protected]447021c2010-09-08 21:29:083024 return web_prefs;
[email protected]420ae012009-04-24 05:16:323025}
3026
[email protected]fa1cf0b82010-01-15 21:49:443027void TabContents::OnIgnoredUIEvent() {
3028 if (constrained_window_count()) {
3029 ConstrainedWindow* window = *constrained_window_begin();
3030 window->FocusConstrainedWindow();
3031 }
3032}
3033
[email protected]420ae012009-04-24 05:16:323034void TabContents::OnJSOutOfMemory() {
3035 AddInfoBar(new SimpleAlertInfoBarDelegate(
[email protected]e23d3a32010-08-13 19:39:583036 this, l10n_util::GetStringUTF16(IDS_JS_OUT_OF_MEMORY_PROMPT),
3037 NULL, true));
[email protected]420ae012009-04-24 05:16:323038}
3039
[email protected]420ae012009-04-24 05:16:323040void TabContents::OnCrossSiteResponse(int new_render_process_host_id,
3041 int new_request_id) {
[email protected]57c6a652009-05-04 07:58:343042 // Allows the TabContents to react when a cross-site response is ready to be
[email protected]420ae012009-04-24 05:16:323043 // delivered to a pending RenderViewHost. We must first run the onunload
3044 // handler of the old RenderViewHost before we can allow it to proceed.
3045 render_manager_.OnCrossSiteResponse(new_render_process_host_id,
3046 new_request_id);
3047}
3048
[email protected]420ae012009-04-24 05:16:323049void TabContents::RendererUnresponsive(RenderViewHost* rvh,
3050 bool is_during_unload) {
3051 if (is_during_unload) {
3052 // Hang occurred while firing the beforeunload/unload handler.
3053 // Pretend the handler fired so tab closing continues as if it had.
[email protected]829e7612009-04-25 01:15:113054 rvh->set_sudden_termination_allowed(true);
[email protected]420ae012009-04-24 05:16:323055
3056 if (!render_manager_.ShouldCloseTabOnUnresponsiveRenderer())
3057 return;
3058
3059 // If the tab hangs in the beforeunload/unload handler there's really
3060 // nothing we can do to recover. Pretend the unload listeners have
3061 // all fired and close the tab. If the hang is in the beforeunload handler
3062 // then the user will not have the option of cancelling the close.
3063 Close(rvh);
3064 return;
3065 }
3066
3067 if (render_view_host() && render_view_host()->IsRenderViewLive())
[email protected]e7b8d832009-11-20 22:41:003068 hung_renderer_dialog::ShowForTabContents(this);
[email protected]420ae012009-04-24 05:16:323069}
3070
3071void TabContents::RendererResponsive(RenderViewHost* render_view_host) {
[email protected]e7b8d832009-11-20 22:41:003072 hung_renderer_dialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:323073}
3074
3075void TabContents::LoadStateChanged(const GURL& url,
[email protected]094e5b22009-09-25 04:23:563076 net::LoadState load_state,
3077 uint64 upload_position,
3078 uint64 upload_size) {
[email protected]420ae012009-04-24 05:16:323079 load_state_ = load_state;
[email protected]094e5b22009-09-25 04:23:563080 upload_position_ = upload_position;
3081 upload_size_ = upload_size;
[email protected]d686e812009-06-03 19:10:293082 std::wstring languages =
[email protected]ddd231e2010-06-29 20:35:193083 UTF8ToWide(profile()->GetPrefs()->GetString(prefs::kAcceptLanguages));
[email protected]d686e812009-06-03 19:10:293084 std::string host = url.host();
[email protected]ce85f602009-11-07 01:34:533085 load_state_host_ =
3086 net::IDNToUnicode(host.c_str(), host.size(), languages, NULL);
[email protected]420ae012009-04-24 05:16:323087 if (load_state_ == net::LOAD_STATE_READING_RESPONSE)
3088 SetNotWaitingForResponse();
3089 if (is_loading())
[email protected]c9cd2222009-05-06 05:16:503090 NotifyNavigationStateChanged(INVALIDATE_LOAD | INVALIDATE_TAB);
[email protected]420ae012009-04-24 05:16:323091}
3092
[email protected]1ee614862009-07-06 20:29:183093bool TabContents::IsExternalTabContainer() const {
3094 if (!delegate())
3095 return false;
3096
3097 return delegate()->IsExternalTabContainer();
3098}
3099
[email protected]1ee614862009-07-06 20:29:183100void TabContents::DidInsertCSS() {
3101 // This RVHDelegate function is used for extensions and not us.
[email protected]420ae012009-04-24 05:16:323102}
3103
[email protected]546ae4e02010-12-08 14:57:193104void TabContents::FocusedNodeChanged(bool is_editable_node) {
[email protected]130efb02009-09-18 18:54:353105 NotificationService::current()->Notify(
3106 NotificationType::FOCUS_CHANGED_IN_PAGE,
3107 Source<RenderViewHost>(render_view_host()),
[email protected]546ae4e02010-12-08 14:57:193108 Details<const bool>(&is_editable_node));
[email protected]130efb02009-09-18 18:54:353109}
3110
[email protected]b75b8292010-10-01 07:28:253111void TabContents::UpdateZoomLimits(int minimum_percent,
3112 int maximum_percent,
3113 bool remember) {
3114 minimum_zoom_percent_ = minimum_percent;
3115 maximum_zoom_percent_ = maximum_percent;
3116 temporary_zoom_settings_ = !remember;
[email protected]ebf40a72010-07-22 01:46:383117}
3118
[email protected]9e823662010-10-13 23:36:003119void TabContents::UpdateContentRestrictions(int restrictions) {
3120 content_restrictions_ = restrictions;
3121 delegate()->ContentRestrictionsChanged(this);
[email protected]166e3042010-10-12 22:42:323122}
3123
[email protected]420ae012009-04-24 05:16:323124void TabContents::BeforeUnloadFiredFromRenderManager(
3125 bool proceed,
3126 bool* proceed_to_fire_unload) {
3127 if (delegate())
3128 delegate()->BeforeUnloadFired(this, proceed, proceed_to_fire_unload);
3129}
3130
[email protected]3a3d47472010-07-15 21:03:543131void TabContents::DidStartLoadingFromRenderManager(
3132 RenderViewHost* render_view_host) {
3133 DidStartLoading();
3134}
3135
3136void TabContents::RenderViewGoneFromRenderManager(
3137 RenderViewHost* render_view_host) {
[email protected]443b80e2010-12-14 00:42:233138 DCHECK(crashed_status_ != base::TERMINATION_STATUS_STILL_RUNNING);
3139 RenderViewGone(render_view_host, crashed_status_, crashed_error_code_);
[email protected]3a3d47472010-07-15 21:03:543140}
3141
[email protected]420ae012009-04-24 05:16:323142void TabContents::UpdateRenderViewSizeForRenderManager() {
3143 // TODO(brettw) this is a hack. See TabContentsView::SizeContents.
[email protected]0323ee42010-02-17 22:03:263144 gfx::Size size = view_->GetContainerSize();
3145 // 0x0 isn't a valid window size (minimal window size is 1x1) but it may be
3146 // here during container initialization and normal window size will be set
3147 // later. In case of tab duplication this resizing to 0x0 prevents setting
3148 // normal size later so just ignore it.
3149 if (!size.IsEmpty())
3150 view_->SizeContents(size);
[email protected]420ae012009-04-24 05:16:323151}
3152
[email protected]3a3d47472010-07-15 21:03:543153void TabContents::NotifySwappedFromRenderManager() {
3154 NotifySwapped();
3155}
3156
3157NavigationController& TabContents::GetControllerForRenderManager() {
3158 return controller();
3159}
3160
[email protected]420ae012009-04-24 05:16:323161DOMUI* TabContents::CreateDOMUIForRenderManager(const GURL& url) {
[email protected]d82ed61e2009-06-16 02:46:223162 return DOMUIFactory::CreateDOMUIForURL(this, url);
[email protected]420ae012009-04-24 05:16:323163}
3164
3165NavigationEntry*
3166TabContents::GetLastCommittedNavigationEntryForRenderManager() {
3167 return controller_.GetLastCommittedEntry();
3168}
3169
3170bool TabContents::CreateRenderViewForRenderManager(
3171 RenderViewHost* render_view_host) {
[email protected]420ae012009-04-24 05:16:323172 RenderWidgetHostView* rwh_view = view_->CreateViewForWidget(render_view_host);
[email protected]1a98a932009-11-17 00:12:523173
[email protected]674b3822010-08-04 04:02:513174 if (!render_view_host->CreateRenderView(string16()))
[email protected]420ae012009-04-24 05:16:323175 return false;
3176
3177 // Now that the RenderView has been created, we need to tell it its size.
3178 rwh_view->SetSize(view_->GetContainerSize());
3179
3180 UpdateMaxPageIDIfNecessary(render_view_host->site_instance(),
3181 render_view_host);
3182 return true;
3183}
3184
3185void TabContents::Observe(NotificationType type,
3186 const NotificationSource& source,
3187 const NotificationDetails& details) {
3188 switch (type.value) {
3189 case NotificationType::BOOKMARK_MODEL_LOADED:
3190 // BookmarkModel finished loading, fall through to update starred state.
3191 case NotificationType::URLS_STARRED: {
3192 // Somewhere, a URL has been starred.
3193 // Ignore notifications for profiles other than our current one.
3194 Profile* source_profile = Source<Profile>(source).ptr();
3195 if (!source_profile || !source_profile->IsSameProfile(profile()))
3196 return;
3197
3198 UpdateStarredStateForCurrentURL();
3199 break;
3200 }
3201 case NotificationType::PREF_CHANGED: {
[email protected]57ecc4b2010-08-11 03:02:513202 std::string* pref_name_in = Details<std::string>(details).ptr();
[email protected]420ae012009-04-24 05:16:323203 DCHECK(Source<PrefService>(source).ptr() == profile()->GetPrefs());
3204 if (*pref_name_in == prefs::kAlternateErrorPagesEnabled) {
3205 UpdateAlternateErrorPageURL();
[email protected]88e14382010-10-14 21:45:533206 } else if ((*pref_name_in == prefs::kDefaultCharset) ||
3207 StartsWithASCII(*pref_name_in, "webkit.webprefs.", true)) {
[email protected]420ae012009-04-24 05:16:323208 UpdateWebPreferences();
[email protected]d0b8d092010-10-25 04:05:173209 } else if (*pref_name_in == prefs::kDefaultZoomLevel) {
3210 UpdateZoomLevel();
[email protected]420ae012009-04-24 05:16:323211 } else {
3212 NOTREACHED() << "unexpected pref change notification" << *pref_name_in;
3213 }
3214 break;
3215 }
3216 case NotificationType::RENDER_WIDGET_HOST_DESTROYED:
3217 view_->RenderWidgetHostDestroyed(Source<RenderWidgetHost>(source).ptr());
3218 break;
3219
3220 case NotificationType::NAV_ENTRY_COMMITTED: {
3221 DCHECK(&controller_ == Source<NavigationController>(source).ptr());
3222
3223 NavigationController::LoadCommittedDetails& committed_details =
3224 *(Details<NavigationController::LoadCommittedDetails>(details).ptr());
3225 ExpireInfoBars(committed_details);
3226 break;
3227 }
3228
[email protected]d3ba77272009-09-03 00:06:093229#if defined(OS_LINUX)
3230 case NotificationType::BROWSER_THEME_CHANGED: {
[email protected]7ff431e2010-01-07 18:18:543231 renderer_preferences_util::UpdateFromSystemSettings(
3232 &renderer_preferences_, profile());
[email protected]d3ba77272009-09-03 00:06:093233 render_view_host()->SyncRendererPrefs();
3234 break;
3235 }
3236#endif
3237
[email protected]6c32ce72010-03-08 05:18:063238 case NotificationType::USER_STYLE_SHEET_UPDATED:
3239 UpdateWebPreferences();
3240 break;
3241
[email protected]79580c62010-02-02 02:36:253242 case NotificationType::CONTENT_SETTINGS_CHANGED: {
[email protected]5f24f212010-12-02 14:47:453243 Details<const ContentSettingsDetails> settings_details(details);
[email protected]79580c62010-02-02 02:36:253244 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]da0c8e92010-02-09 23:25:493245 GURL entry_url;
[email protected]0314ae02010-04-08 09:18:293246 if (entry)
[email protected]da0c8e92010-02-09 23:25:493247 entry_url = entry->url();
[email protected]0314ae02010-04-08 09:18:293248 if (settings_details.ptr()->update_all() ||
3249 settings_details.ptr()->pattern().Matches(entry_url)) {
3250 render_view_host()->SendContentSettings(entry_url,
[email protected]b9a4ea7f2010-06-25 07:11:233251 profile()->GetHostContentSettingsMap()->
3252 GetContentSettings(entry_url));
[email protected]79580c62010-02-02 02:36:253253 }
3254 break;
3255 }
3256
[email protected]e5adff72010-06-14 18:13:063257 case NotificationType::EXTENSION_LOADED:
[email protected]a888b29e62010-04-01 13:38:573258 break;
[email protected]a888b29e62010-04-01 13:38:573259
3260 case NotificationType::EXTENSION_UNLOADED:
[email protected]e5adff72010-06-14 18:13:063261 case NotificationType::EXTENSION_UNLOADED_DISABLED:
[email protected]a888b29e62010-04-01 13:38:573262 break;
[email protected]a888b29e62010-04-01 13:38:573263
[email protected]88152052010-10-01 04:05:183264 case NotificationType::GOOGLE_URL_UPDATED:
3265 UpdateAlternateErrorPageURL();
3266 break;
3267
[email protected]420ae012009-04-24 05:16:323268 default:
3269 NOTREACHED();
3270 }
3271}
[email protected]aacd7cc2009-09-02 14:51:103272
[email protected]9adb9692010-10-29 23:14:023273void TabContents::UpdateExtensionAppIcon(const Extension* extension) {
[email protected]98aa0b52010-05-06 17:03:083274 extension_app_icon_.reset();
[email protected]f075fe0a22010-04-26 04:06:433275
[email protected]a888b29e62010-04-01 13:38:573276 if (extension) {
[email protected]98aa0b52010-05-06 17:03:083277 extension_app_image_loader_.reset(new ImageLoadingTracker(this));
3278 extension_app_image_loader_->LoadImage(
[email protected]a888b29e62010-04-01 13:38:573279 extension,
[email protected]807871f2010-09-16 01:04:483280 extension->GetIconResource(Extension::EXTENSION_ICON_SMALLISH,
3281 ExtensionIconSet::MATCH_EXACTLY),
[email protected]a888b29e62010-04-01 13:38:573282 gfx::Size(Extension::EXTENSION_ICON_SMALLISH,
3283 Extension::EXTENSION_ICON_SMALLISH),
3284 ImageLoadingTracker::CACHE);
3285 } else {
[email protected]98aa0b52010-05-06 17:03:083286 extension_app_image_loader_.reset(NULL);
[email protected]a888b29e62010-04-01 13:38:573287 }
3288}
3289
[email protected]9adb9692010-10-29 23:14:023290const Extension* TabContents::GetExtensionContaining(const GURL& url) {
[email protected]eaa7dd182010-12-14 11:09:003291 ExtensionService* extensions_service = profile()->GetExtensionService();
[email protected]a888b29e62010-04-01 13:38:573292 if (!extensions_service)
3293 return NULL;
3294
[email protected]9adb9692010-10-29 23:14:023295 const Extension* extension = extensions_service->GetExtensionByURL(url);
[email protected]a888b29e62010-04-01 13:38:573296 return extension ?
3297 extension : extensions_service->GetExtensionByWebExtent(url);
3298}
3299
[email protected]a888b29e62010-04-01 13:38:573300void TabContents::OnImageLoaded(SkBitmap* image, ExtensionResource resource,
3301 int index) {
3302 if (image) {
[email protected]98aa0b52010-05-06 17:03:083303 extension_app_icon_ = *image;
[email protected]a888b29e62010-04-01 13:38:573304 NotifyNavigationStateChanged(INVALIDATE_TAB);
3305 }
3306}
3307
[email protected]beb440c2009-11-06 04:08:543308gfx::NativeWindow TabContents::GetMessageBoxRootWindow() {
3309 return view_->GetTopLevelNativeWindow();
3310}
3311
3312void TabContents::OnMessageBoxClosed(IPC::Message* reply_msg,
3313 bool success,
3314 const std::wstring& prompt) {
3315 last_javascript_message_dismissal_ = base::TimeTicks::Now();
3316 if (is_showing_before_unload_dialog_ && !success) {
3317 // If a beforeunload dialog is canceled, we need to stop the throbber from
3318 // spinning, since we forced it to start spinning in Navigate.
3319 DidStopLoading();
3320
3321 tab_close_start_time_ = base::TimeTicks();
3322 }
3323 is_showing_before_unload_dialog_ = false;
3324 render_view_host()->JavaScriptMessageBoxClosed(reply_msg, success, prompt);
3325}
3326
3327void TabContents::SetSuppressMessageBoxes(bool suppress_message_boxes) {
3328 set_suppress_javascript_messages(suppress_message_boxes);
3329}
3330
[email protected]3a3d47472010-07-15 21:03:543331TabContents* TabContents::AsTabContents() {
3332 return this;
3333}
3334
3335ExtensionHost* TabContents::AsExtensionHost() {
3336 return NULL;
3337}
3338
[email protected]41fc0322009-09-04 22:23:403339void TabContents::set_encoding(const std::string& encoding) {
[email protected]aacd7cc2009-09-02 14:51:103340 encoding_ = CharacterEncoding::GetCanonicalEncodingNameByAliasName(encoding);
3341}
[email protected]f45d2a72010-03-08 23:28:353342
3343void TabContents::SetAppIcon(const SkBitmap& app_icon) {
3344 app_icon_ = app_icon;
3345 NotifyNavigationStateChanged(INVALIDATE_TITLE);
3346}
[email protected]33f74972010-12-08 16:40:363347
3348void TabContents::SwapInRenderViewHost(RenderViewHost* rvh) {
3349 render_manager_.SwapInRenderViewHost(rvh);
3350}
3351
3352void TabContents::CreateViewAndSetSizeForRVH(RenderViewHost* rvh) {
3353 RenderWidgetHostView* rwh_view = view()->CreateViewForWidget(rvh);
3354 rwh_view->SetSize(view()->GetContainerSize());
3355}