blob: 8b5b74c3dd1f71ba883b95a01b1b736739fbf326 [file] [log] [blame]
[email protected]61d68ef12011-01-13 14:02:561// Copyright (c) 2011 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]f3ec7742009-01-15 00:59:165#include "chrome/browser/tab_contents/tab_contents.h"
initial.commit09911bf2008-07-26 23:55:296
[email protected]b75b8292010-10-01 07:28:257#include <cmath>
8
[email protected]8cc8d492010-02-02 10:40:499#include "base/auto_reset.h"
[email protected]835d7c82010-10-14 04:38:3810#include "base/metrics/histogram.h"
[email protected]724159a2010-12-30 01:11:1811#include "base/metrics/stats_counters.h"
[email protected]96d185d2009-04-24 03:28:5412#include "base/string16.h"
[email protected]996fd702009-09-04 19:12:3713#include "base/string_util.h"
[email protected]96d185d2009-04-24 03:28:5414#include "base/time.h"
[email protected]3c733bde2010-12-21 19:56:3115#include "base/utf_string_conversions.h"
[email protected]6db0def92010-03-02 19:55:4516#include "chrome/browser/autocomplete_history_manager.h"
[email protected]21674df2009-10-28 19:51:2117#include "chrome/browser/autofill/autofill_manager.h"
[email protected]c65e2f152010-10-14 15:30:4018#include "chrome/browser/blocked_content_container.h"
[email protected]420ae012009-04-24 05:16:3219#include "chrome/browser/bookmarks/bookmark_model.h"
[email protected]96d185d2009-04-24 03:28:5420#include "chrome/browser/browser_process.h"
[email protected]cf68b092010-01-15 20:05:5521#include "chrome/browser/browser_shutdown.h"
[email protected]aacd7cc2009-09-02 14:51:1022#include "chrome/browser/character_encoding.h"
[email protected]724159a2010-12-30 01:11:1823#include "chrome/browser/child_process_security_policy.h"
[email protected]5f24f212010-12-02 14:47:4524#include "chrome/browser/content_settings/content_settings_details.h"
[email protected]9d01a6a2010-11-30 12:03:3325#include "chrome/browser/content_settings/host_content_settings_map.h"
[email protected]96d185d2009-04-24 03:28:5426#include "chrome/browser/debugger/devtools_manager.h"
[email protected]c8f74942010-09-03 10:04:1427#include "chrome/browser/defaults.h"
[email protected]420ae012009-04-24 05:16:3228#include "chrome/browser/dom_operation_notification_details.h"
[email protected]96d185d2009-04-24 03:28:5429#include "chrome/browser/dom_ui/dom_ui.h"
[email protected]bcef0dc02009-02-28 00:35:0230#include "chrome/browser/download/download_item_model.h"
[email protected]420ae012009-04-24 05:16:3231#include "chrome/browser/download/download_manager.h"
[email protected]41291322010-07-15 17:09:0132#include "chrome/browser/download/download_request_limiter.h"
[email protected]eaa7dd182010-12-14 11:09:0033#include "chrome/browser/extensions/extension_service.h"
[email protected]3c733bde2010-12-21 19:56:3134#include "chrome/browser/external_protocol_handler.h"
35#include "chrome/browser/favicon_service.h"
36#include "chrome/browser/file_select_helper.h"
37#include "chrome/browser/google/google_util.h"
[email protected]ef0d04c2010-12-15 23:34:2138#include "chrome/browser/history/history.h"
[email protected]ec0b6c42010-08-26 03:16:5839#include "chrome/browser/history/history_types.h"
[email protected]523623c2010-05-19 16:03:4240#include "chrome/browser/history/top_sites.h"
[email protected]b75b8292010-10-01 07:28:2541#include "chrome/browser/host_zoom_map.h"
[email protected]8897c382009-05-06 17:53:2642#include "chrome/browser/hung_renderer_dialog.h"
[email protected]6ee12c42010-09-14 09:36:0743#include "chrome/browser/in_process_webkit/session_storage_namespace.h"
[email protected]420ae012009-04-24 05:16:3244#include "chrome/browser/load_from_memory_cache_details.h"
45#include "chrome/browser/load_notification_details.h"
[email protected]84abba62009-10-07 17:01:4446#include "chrome/browser/metrics/metric_event_duration_details.h"
[email protected]724159a2010-12-30 01:11:1847#include "chrome/browser/metrics/user_metrics.h"
[email protected]ea0e80892009-08-22 00:36:0548#include "chrome/browser/modal_html_dialog_delegate.h"
[email protected]c8865482009-07-23 20:40:1049#include "chrome/browser/omnibox_search_hint.h"
[email protected]c8f73ab2011-01-22 00:05:1750#include "chrome/browser/pdf_unsupported_feature.h"
[email protected]14a000d2010-04-29 21:44:2451#include "chrome/browser/platform_util.h"
[email protected]f4f50ef2011-01-21 19:01:1952#include "chrome/browser/plugin_installer_infobar_delegate.h"
[email protected]37858e52010-08-26 00:22:0253#include "chrome/browser/prefs/pref_service.h"
[email protected]33f74972010-12-08 16:40:3654#include "chrome/browser/prerender/prerender_manager.h"
[email protected]dbeebd52010-11-16 20:34:1655#include "chrome/browser/printing/print_preview_tab_controller.h"
[email protected]12802702010-07-09 19:43:0956#include "chrome/browser/printing/print_view_manager.h"
[email protected]8ecad5e2010-12-02 21:18:3357#include "chrome/browser/profiles/profile.h"
[email protected]1db6ff152009-10-12 15:32:0758#include "chrome/browser/renderer_host/render_process_host.h"
[email protected]8cb5d5b2010-02-09 11:36:1659#include "chrome/browser/renderer_host/render_view_host.h"
[email protected]96d185d2009-04-24 03:28:5460#include "chrome/browser/renderer_host/render_widget_host_view.h"
[email protected]fd694982009-07-03 17:32:3761#include "chrome/browser/renderer_host/resource_request_details.h"
[email protected]1db6ff152009-10-12 15:32:0762#include "chrome/browser/renderer_host/site_instance.h"
[email protected]96d185d2009-04-24 03:28:5463#include "chrome/browser/renderer_host/web_cache_manager.h"
[email protected]93623c5d2009-12-10 21:40:3264#include "chrome/browser/renderer_preferences_util.h"
[email protected]8b62334b2010-08-31 22:37:1165#include "chrome/browser/search_engines/template_url.h"
[email protected]152b8ba32010-03-29 19:40:1666#include "chrome/browser/search_engines/template_url_fetcher.h"
[email protected]0b5b85c2010-10-11 23:00:1267#include "chrome/browser/search_engines/template_url_fetcher_ui_callbacks.h"
[email protected]152b8ba32010-03-29 19:40:1668#include "chrome/browser/search_engines/template_url_model.h"
[email protected]c37e3b62010-01-22 00:15:1769#include "chrome/browser/sessions/session_types.h"
[email protected]bd1ad682009-05-15 22:19:1770#include "chrome/browser/tab_contents/infobar_delegate.h"
[email protected]7e383692009-06-12 19:14:5471#include "chrome/browser/tab_contents/interstitial_page.h"
[email protected]f3ec7742009-01-15 00:59:1672#include "chrome/browser/tab_contents/navigation_entry.h"
[email protected]de34a962009-06-26 23:22:4373#include "chrome/browser/tab_contents/provisional_load_details.h"
[email protected]f3ec7742009-01-15 00:59:1674#include "chrome/browser/tab_contents/tab_contents_delegate.h"
[email protected]1f18184a2010-07-21 19:34:4975#include "chrome/browser/tab_contents/tab_contents_ssl_helper.h"
[email protected]fdd61c62009-04-22 19:22:5776#include "chrome/browser/tab_contents/tab_contents_view.h"
[email protected]d54169e92011-01-21 09:19:5277#include "chrome/browser/tab_contents/thumbnail_generator.h"
[email protected]3c9e1872010-11-18 16:17:4978#include "chrome/browser/tab_contents/web_navigation_observer.h"
[email protected]152b8ba32010-03-29 19:40:1679#include "chrome/browser/translate/page_translated_details.h"
[email protected]1caa1522010-12-01 20:23:3580#include "chrome/browser/ui/app_modal_dialogs/message_box_handler.h"
[email protected]45300ad42010-12-02 15:51:1481#include "chrome/browser/ui/find_bar/find_bar_state.h"
[email protected]54ec6472010-04-09 19:39:5882#include "chrome/common/bindings_policy.h"
[email protected]35f7d212009-04-29 21:19:2783#include "chrome/common/chrome_switches.h"
[email protected]de4761b2010-12-07 21:05:2184#include "chrome/common/content_restriction.h"
[email protected]3b355642010-02-05 16:01:4985#include "chrome/common/extensions/extension.h"
[email protected]5d246db22009-10-27 06:17:5786#include "chrome/common/extensions/extension_action.h"
[email protected]807871f2010-09-16 01:04:4887#include "chrome/common/extensions/extension_icon_set.h"
[email protected]942690b132010-05-11 06:42:1488#include "chrome/common/extensions/extension_resource.h"
[email protected]12802702010-07-09 19:43:0989#include "chrome/common/extensions/url_pattern.h"
90#include "chrome/common/navigation_types.h"
91#include "chrome/common/net/url_request_context_getter.h"
[email protected]bfd04a62009-02-01 18:16:5692#include "chrome/common/notification_service.h"
initial.commit09911bf2008-07-26 23:55:2993#include "chrome/common/pref_names.h"
[email protected]420ae012009-04-24 05:16:3294#include "chrome/common/render_messages.h"
[email protected]939856a2010-08-24 20:29:0295#include "chrome/common/render_messages_params.h"
[email protected]96d185d2009-04-24 03:28:5496#include "chrome/common/url_constants.h"
[email protected]ef0d04c2010-12-15 23:34:2197#include "gfx/codec/png_codec.h"
[email protected]ea68c06a2010-04-23 17:25:3798#include "grit/chromium_strings.h"
[email protected]8897c382009-05-06 17:53:2699#include "grit/generated_resources.h"
[email protected]420ae012009-04-24 05:16:32100#include "grit/locale_settings.h"
[email protected]fd626782010-01-22 05:26:06101#include "grit/platform_locale_settings.h"
[email protected]ea68c06a2010-04-23 17:25:37102#include "grit/theme_resources.h"
[email protected]d686e812009-06-03 19:10:29103#include "net/base/net_util.h"
[email protected]420ae012009-04-24 05:16:32104#include "net/base/registry_controlled_domain.h"
[email protected]8bd0fe62011-01-17 06:44:37105#include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
[email protected]c051a1b2011-01-21 23:30:17106#include "ui/base/l10n/l10n_util.h"
[email protected]42ce29d2011-01-20 23:19:46107#include "ui/base/resource/resource_bundle.h"
[email protected]d5e311b2010-07-20 17:15:03108#include "webkit/glue/password_form.h"
[email protected]191eb3f72010-12-21 06:27:50109#include "webkit/plugins/npapi/plugin_list.h"
[email protected]3c733bde2010-12-21 19:56:31110#include "webkit/glue/webpreferences.h"
111
112#if defined(OS_MACOSX)
113#include "app/surface/io_surface_support_mac.h"
114#endif // defined(OS_MACOSX)
[email protected]3e45ba92009-02-20 21:09:00115
[email protected]61d68ef12011-01-13 14:02:56116#if defined(OS_CHROMEOS)
117#include "chrome/browser/chromeos/locale_change_guard.h"
118#endif // defined(OS_CHROMEOS)
119
[email protected]420ae012009-04-24 05:16:32120// Cross-Site Navigations
121//
122// If a TabContents is told to navigate to a different web site (as determined
123// by SiteInstance), it will replace its current RenderViewHost with a new
124// RenderViewHost dedicated to the new SiteInstance. This works as follows:
125//
126// - Navigate determines whether the destination is cross-site, and if so,
127// it creates a pending_render_view_host_ and moves into the PENDING
128// RendererState.
129// - The pending RVH is "suspended," so that no navigation messages are sent to
130// its renderer until the onbeforeunload JavaScript handler has a chance to
131// run in the current RVH.
132// - The pending RVH tells CrossSiteRequestManager (a thread-safe singleton)
133// that it has a pending cross-site request. ResourceDispatcherHost will
134// check for this when the response arrives.
135// - The current RVH runs its onbeforeunload handler. If it returns false, we
136// cancel all the pending logic and go back to NORMAL. Otherwise we allow
137// the pending RVH to send the navigation request to its renderer.
138// - ResourceDispatcherHost receives a ResourceRequest on the IO thread. It
139// checks CrossSiteRequestManager to see that the RVH responsible has a
140// pending cross-site request, and then installs a CrossSiteEventHandler.
141// - When RDH receives a response, the BufferedEventHandler determines whether
142// it is a download. If so, it sends a message to the new renderer causing
143// it to cancel the request, and the download proceeds in the download
144// thread. For now, we stay in a PENDING state (with a pending RVH) until
[email protected]57c6a652009-05-04 07:58:34145// the next DidNavigate event for this TabContents. This isn't ideal, but it
[email protected]420ae012009-04-24 05:16:32146// doesn't affect any functionality.
147// - After RDH receives a response and determines that it is safe and not a
148// download, it pauses the response to first run the old page's onunload
149// handler. It does this by asynchronously calling the OnCrossSiteResponse
[email protected]57c6a652009-05-04 07:58:34150// method of TabContents on the UI thread, which sends a ClosePage message
[email protected]420ae012009-04-24 05:16:32151// to the current RVH.
152// - Once the onunload handler is finished, a ClosePage_ACK message is sent to
153// the ResourceDispatcherHost, who unpauses the response. Data is then sent
154// to the pending RVH.
155// - The pending renderer sends a FrameNavigate message that invokes the
156// DidNavigate method. This replaces the current RVH with the
157// pending RVH and goes back to the NORMAL RendererState.
158
159namespace {
160
161// Amount of time we wait between when a key event is received and the renderer
162// is queried for its state and pushed to the NavigationEntry.
163const int kQueryStateDelay = 5000;
164
[email protected]6ebdc9b2010-09-27 16:55:57165const int kSyncWaitDelay = 40;
166
[email protected]420ae012009-04-24 05:16:32167// If another javascript message box is displayed within
168// kJavascriptMessageExpectedDelay of a previous javascript message box being
169// dismissed, display an option to suppress future message boxes from this
170// contents.
171const int kJavascriptMessageExpectedDelay = 1000;
172
[email protected]420ae012009-04-24 05:16:32173// The list of prefs we want to observe.
[email protected]57ecc4b2010-08-11 03:02:51174const char* kPrefsToObserve[] = {
[email protected]420ae012009-04-24 05:16:32175 prefs::kAlternateErrorPagesEnabled,
[email protected]d0b8d092010-10-25 04:05:17176 prefs::kDefaultZoomLevel,
[email protected]420ae012009-04-24 05:16:32177 prefs::kWebKitJavaEnabled,
178 prefs::kWebKitJavascriptEnabled,
179 prefs::kWebKitLoadsImagesAutomatically,
180 prefs::kWebKitPluginsEnabled,
181 prefs::kWebKitUsesUniversalDetector,
182 prefs::kWebKitSerifFontFamily,
183 prefs::kWebKitSansSerifFontFamily,
184 prefs::kWebKitFixedFontFamily,
185 prefs::kWebKitDefaultFontSize,
186 prefs::kWebKitDefaultFixedFontSize,
[email protected]6bdf05b2010-10-13 20:34:13187 prefs::kWebKitMinimumFontSize,
188 prefs::kWebKitMinimumLogicalFontSize,
[email protected]1661a3c02010-05-20 16:32:36189 prefs::kWebkitTabsToLinks,
[email protected]420ae012009-04-24 05:16:32190 prefs::kDefaultCharset
191 // kWebKitStandardFontIsSerif needs to be added
192 // if we let users pick which font to use, serif or sans-serif when
193 // no font is specified or a CSS generic family (serif or sans-serif)
194 // is not specified.
195};
196
197const int kPrefsToObserveLength = arraysize(kPrefsToObserve);
198
[email protected]420ae012009-04-24 05:16:32199// Returns true if the entry's transition type is FORM_SUBMIT.
200bool IsFormSubmit(const NavigationEntry* entry) {
201 return (PageTransition::StripQualifier(entry->transition_type()) ==
202 PageTransition::FORM_SUBMIT);
203}
204
205#if defined(OS_WIN)
206
207BOOL CALLBACK InvalidateWindow(HWND hwnd, LPARAM lparam) {
208 // Note: erase is required to properly paint some widgets borders. This can
209 // be seen with textfields.
210 InvalidateRect(hwnd, NULL, TRUE);
211 return TRUE;
212}
213#endif
214
[email protected]5e369672009-11-03 23:48:30215ViewMsg_Navigate_Params::NavigationType GetNavigationType(
[email protected]1ccb3568d2010-02-19 10:51:16216 Profile* profile, const NavigationEntry& entry,
217 NavigationController::ReloadType reload_type) {
218 switch (reload_type) {
219 case NavigationController::RELOAD:
220 return ViewMsg_Navigate_Params::RELOAD;
221 case NavigationController::RELOAD_IGNORING_CACHE:
222 return ViewMsg_Navigate_Params::RELOAD_IGNORING_CACHE;
223 case NavigationController::NO_RELOAD:
224 break; // Fall through to rest of function.
225 }
[email protected]5e369672009-11-03 23:48:30226
227 if (entry.restore_type() == NavigationEntry::RESTORE_LAST_SESSION &&
228 profile->DidLastSessionExitCleanly())
229 return ViewMsg_Navigate_Params::RESTORE;
230
231 return ViewMsg_Navigate_Params::NORMAL;
232}
233
[email protected]876bc832010-09-07 16:29:54234void MakeNavigateParams(const NavigationEntry& entry,
235 const NavigationController& controller,
[email protected]1ccb3568d2010-02-19 10:51:16236 NavigationController::ReloadType reload_type,
237 ViewMsg_Navigate_Params* params) {
[email protected]056de2d2009-06-26 16:41:34238 params->page_id = entry.page_id();
[email protected]876bc832010-09-07 16:29:54239 params->pending_history_list_offset = controller.GetIndexOfEntry(&entry);
[email protected]3cc72b12010-03-18 23:03:00240 params->current_history_list_offset = controller.last_committed_entry_index();
241 params->current_history_list_length = controller.entry_count();
[email protected]056de2d2009-06-26 16:41:34242 params->url = entry.url();
243 params->referrer = entry.referrer();
244 params->transition = entry.transition_type();
245 params->state = entry.content_state();
[email protected]3cc72b12010-03-18 23:03:00246 params->navigation_type =
247 GetNavigationType(controller.profile(), entry, reload_type);
[email protected]056de2d2009-06-26 16:41:34248 params->request_time = base::Time::Now();
249}
250
[email protected]f4f50ef2011-01-21 19:01:19251
252// OutdatedPluginInfoBar ------------------------------------------------------
253
[email protected]3c2826852010-12-16 19:09:14254class OutdatedPluginInfoBar : public ConfirmInfoBarDelegate {
[email protected]851b1eb2010-08-09 13:32:29255 public:
[email protected]3c2826852010-12-16 19:09:14256 OutdatedPluginInfoBar(TabContents* tab_contents,
[email protected]851b1eb2010-08-09 13:32:29257 const string16& name,
[email protected]f4f50ef2011-01-21 19:01:19258 const GURL& update_url);
[email protected]f5ec1ba2010-12-22 17:47:40259
[email protected]851b1eb2010-08-09 13:32:29260 private:
[email protected]f4f50ef2011-01-21 19:01:19261 virtual ~OutdatedPluginInfoBar();
262
263 // ConfirmInfoBarDelegate:
264 virtual void InfoBarClosed();
265 virtual SkBitmap* GetIcon() const;
266 virtual string16 GetMessageText() const;
267 virtual int GetButtons() const;
268 virtual string16 GetButtonLabel(InfoBarButton button) const;
269 virtual bool Accept();
270 virtual bool Cancel();
271 virtual string16 GetLinkText();
272 virtual bool LinkClicked(WindowOpenDisposition disposition);
273
[email protected]851b1eb2010-08-09 13:32:29274 TabContents* tab_contents_;
275 string16 name_;
276 GURL update_url_;
277};
278
[email protected]f4f50ef2011-01-21 19:01:19279OutdatedPluginInfoBar::OutdatedPluginInfoBar(TabContents* tab_contents,
280 const string16& name,
281 const GURL& update_url)
282 : ConfirmInfoBarDelegate(tab_contents),
283 tab_contents_(tab_contents),
284 name_(name),
285 update_url_(update_url) {
286 UserMetrics::RecordAction(UserMetricsAction("OutdatedPluginInfobar.Shown"));
287 tab_contents->AddInfoBar(this);
288}
289
290OutdatedPluginInfoBar::~OutdatedPluginInfoBar() {
291}
292
293void OutdatedPluginInfoBar::InfoBarClosed() {
294 UserMetrics::RecordAction(UserMetricsAction("OutdatedPluginInfobar.Closed"));
295 delete this;
296}
297
298SkBitmap* OutdatedPluginInfoBar::GetIcon() const {
299 return ResourceBundle::GetSharedInstance().GetBitmapNamed(
300 IDR_INFOBAR_PLUGIN_INSTALL);
301}
302
303string16 OutdatedPluginInfoBar::GetMessageText() const {
304 return l10n_util::GetStringFUTF16(IDS_PLUGIN_OUTDATED_PROMPT, name_);
305}
306
307int OutdatedPluginInfoBar::GetButtons() const {
308 return BUTTON_OK | BUTTON_CANCEL;
309}
310
311string16 OutdatedPluginInfoBar::GetButtonLabel(InfoBarButton button) const {
312 return l10n_util::GetStringUTF16((button == BUTTON_OK) ?
313 IDS_PLUGIN_UPDATE : IDS_PLUGIN_ENABLE_TEMPORARILY);
314}
315
316bool OutdatedPluginInfoBar::Accept() {
317 UserMetrics::RecordAction(UserMetricsAction("OutdatedPluginInfobar.Update"));
318 tab_contents_->OpenURL(update_url_, GURL(), NEW_FOREGROUND_TAB,
319 PageTransition::LINK);
320 return false;
321}
322
323bool OutdatedPluginInfoBar::Cancel() {
324 UserMetrics::RecordAction(
325 UserMetricsAction("OutdatedPluginInfobar.AllowThisTime"));
326 tab_contents_->render_view_host()->LoadBlockedPlugins();
327 return false;
328}
329
330string16 OutdatedPluginInfoBar::GetLinkText() {
331 return l10n_util::GetStringUTF16(IDS_LEARN_MORE);
332}
333
334bool OutdatedPluginInfoBar::LinkClicked(WindowOpenDisposition disposition) {
335 UserMetrics::RecordAction(
336 UserMetricsAction("OutdatedPluginInfobar.LearnMore"));
337 // TODO(bauerb): Navigate to a help page explaining why we disabled
338 // the plugin, once we have one.
339 return false;
340}
341
[email protected]420ae012009-04-24 05:16:32342} // namespace
343
[email protected]f4f50ef2011-01-21 19:01:19344
345// TabContents ----------------------------------------------------------------
[email protected]420ae012009-04-24 05:16:32346
[email protected]fdd61c62009-04-22 19:22:57347// static
348int TabContents::find_request_id_counter_ = -1;
349
[email protected]420ae012009-04-24 05:16:32350
[email protected]420ae012009-04-24 05:16:32351TabContents::TabContents(Profile* profile,
352 SiteInstance* site_instance,
353 int routing_id,
[email protected]6ee12c42010-09-14 09:36:07354 const TabContents* base_tab_contents,
355 SessionStorageNamespace* session_storage_namespace)
[email protected]b680ad22009-04-15 23:19:42356 : delegate_(NULL),
[email protected]6ee12c42010-09-14 09:36:07357 ALLOW_THIS_IN_INITIALIZER_LIST(controller_(
358 this, profile, session_storage_namespace)),
[email protected]66ba4932009-06-04 19:22:13359 ALLOW_THIS_IN_INITIALIZER_LIST(view_(
[email protected]d82ed61e2009-06-16 02:46:22360 TabContentsView::Create(this))),
361 ALLOW_THIS_IN_INITIALIZER_LIST(render_manager_(this, this)),
[email protected]fdd61c62009-04-22 19:22:57362 property_bag_(),
363 registrar_(),
[email protected]12802702010-07-09 19:43:09364 ALLOW_THIS_IN_INITIALIZER_LIST(printing_(
365 new printing::PrintViewManager(*this))),
[email protected]fdd61c62009-04-22 19:22:57366 save_package_(),
[email protected]6db0def92010-03-02 19:55:45367 autocomplete_history_manager_(),
[email protected]21674df2009-10-28 19:51:21368 autofill_manager_(),
[email protected]fdd61c62009-04-22 19:22:57369 plugin_installer_(),
[email protected]6f821dd2010-02-23 00:54:06370 bookmark_drag_(NULL),
[email protected]d82ed61e2009-06-16 02:46:22371 ALLOW_THIS_IN_INITIALIZER_LIST(fav_icon_helper_(this)),
[email protected]d5f942ba2008-09-26 19:30:34372 is_loading_(false),
[email protected]443b80e2010-12-14 00:42:23373 crashed_status_(base::TERMINATION_STATUS_STILL_RUNNING),
374 crashed_error_code_(0),
[email protected]d5f942ba2008-09-26 19:30:34375 waiting_for_response_(false),
[email protected]d5f942ba2008-09-26 19:30:34376 max_page_id_(-1),
[email protected]fdd61c62009-04-22 19:22:57377 current_load_start_(),
378 load_state_(net::LOAD_STATE_IDLE),
379 load_state_host_(),
[email protected]094e5b22009-09-25 04:23:56380 upload_size_(0),
381 upload_position_(0),
[email protected]fdd61c62009-04-22 19:22:57382 received_page_title_(false),
383 is_starred_(false),
384 contents_mime_type_(),
385 encoding_(),
[email protected]c65e2f152010-10-14 15:30:40386 blocked_contents_(NULL),
387 all_contents_blocked_(false),
[email protected]8cc8d492010-02-02 10:40:49388 dont_notify_render_view_(false),
[email protected]f17a0ee2010-05-17 17:38:47389 displayed_insecure_content_(false),
[email protected]fdd61c62009-04-22 19:22:57390 infobar_delegates_(),
[email protected]fdd61c62009-04-22 19:22:57391 find_ui_active_(false),
392 find_op_aborted_(false),
393 current_find_request_id_(find_request_id_counter_++),
[email protected]e491f1c2009-05-22 20:28:12394 last_search_case_sensitive_(false),
[email protected]e491f1c2009-05-22 20:28:12395 last_search_result_(),
[email protected]98aa0b52010-05-06 17:03:08396 extension_app_(NULL),
[email protected]fdd61c62009-04-22 19:22:57397 capturing_contents_(false),
398 is_being_destroyed_(false),
399 notify_disconnection_(false),
400 history_requests_(),
401#if defined(OS_WIN)
402 message_box_active_(CreateEvent(NULL, TRUE, FALSE, NULL)),
403#endif
404 last_javascript_message_dismissal_(),
[email protected]c2e74fe82009-09-03 17:57:44405 suppress_javascript_messages_(false),
[email protected]7ab1e7d62009-10-14 23:32:01406 is_showing_before_unload_dialog_(false),
[email protected]7ff431e2010-01-07 18:18:54407 renderer_preferences_(),
[email protected]1a242c32010-02-02 21:20:54408 opener_dom_ui_type_(DOMUIFactory::kNoDOMUI),
[email protected]777a7b62010-05-11 13:28:16409 language_state_(&controller_),
[email protected]ebf40a72010-07-22 01:46:38410 closed_by_user_gesture_(false),
[email protected]b75b8292010-10-01 07:28:25411 minimum_zoom_percent_(
412 static_cast<int>(WebKit::WebView::minTextSizeMultiplier * 100)),
413 maximum_zoom_percent_(
414 static_cast<int>(WebKit::WebView::maxTextSizeMultiplier * 100)),
[email protected]9e823662010-10-13 23:36:00415 temporary_zoom_settings_(false),
416 content_restrictions_(0) {
[email protected]7ff431e2010-01-07 18:18:54417 renderer_preferences_util::UpdateFromSystemSettings(
418 &renderer_preferences_, profile);
419
[email protected]9fb83e82010-07-02 18:24:55420 content_settings_delegate_.reset(
421 new TabSpecificContentSettings(this, profile));
422
[email protected]12636df2009-09-28 22:32:21423 render_manager_.Init(profile, site_instance, routing_id);
[email protected]420ae012009-04-24 05:16:32424
[email protected]34ac70502009-09-25 17:07:23425 // We have the initial size of the view be based on the size of the passed in
426 // tab contents (normally a tab from the same window).
427 view_->CreateView(base_tab_contents ?
428 base_tab_contents->view()->GetContainerSize() : gfx::Size());
[email protected]420ae012009-04-24 05:16:32429
430 // Register for notifications about all interested prefs change.
431 PrefService* prefs = profile->GetPrefs();
[email protected]2fb7dc982010-09-29 12:24:28432 pref_change_registrar_.Init(prefs);
[email protected]420ae012009-04-24 05:16:32433 if (prefs) {
434 for (int i = 0; i < kPrefsToObserveLength; ++i)
[email protected]2fb7dc982010-09-29 12:24:28435 pref_change_registrar_.Add(kPrefsToObserve[i], this);
[email protected]420ae012009-04-24 05:16:32436 }
437
438 // Register for notifications about URL starredness changing on any profile.
439 registrar_.Add(this, NotificationType::URLS_STARRED,
440 NotificationService::AllSources());
441 registrar_.Add(this, NotificationType::BOOKMARK_MODEL_LOADED,
442 NotificationService::AllSources());
443 registrar_.Add(this, NotificationType::RENDER_WIDGET_HOST_DESTROYED,
444 NotificationService::AllSources());
[email protected]d3ba77272009-09-03 00:06:09445#if defined(OS_LINUX)
446 registrar_.Add(this, NotificationType::BROWSER_THEME_CHANGED,
447 NotificationService::AllSources());
448#endif
[email protected]420ae012009-04-24 05:16:32449
[email protected]6c32ce72010-03-08 05:18:06450 registrar_.Add(this, NotificationType::USER_STYLE_SHEET_UPDATED,
451 NotificationService::AllSources());
452
[email protected]79580c62010-02-02 02:36:25453 // Register for notifications about content setting changes.
454 registrar_.Add(this, NotificationType::CONTENT_SETTINGS_CHANGED,
455 NotificationService::AllSources());
456
[email protected]a888b29e62010-04-01 13:38:57457 // Listen for extension changes so we can update extension_for_current_page_.
458 registrar_.Add(this, NotificationType::EXTENSION_LOADED,
459 NotificationService::AllSources());
460 registrar_.Add(this, NotificationType::EXTENSION_UNLOADED,
461 NotificationService::AllSources());
[email protected]a888b29e62010-04-01 13:38:57462
[email protected]88152052010-10-01 04:05:18463 // Listen for Google URL changes
464 registrar_.Add(this, NotificationType::GOOGLE_URL_UPDATED,
465 NotificationService::AllSources());
466
[email protected]c8865482009-07-23 20:40:10467 // Set-up the showing of the omnibox search infobar if applicable.
468 if (OmniboxSearchHint::IsEnabled(profile))
469 omnibox_search_hint_.reset(new OmniboxSearchHint(this));
[email protected]403415a2011-01-10 18:57:53470
471 autofill_manager_.reset(new AutoFillManager(this));
[email protected]0f180ee2011-01-21 18:23:10472 AddNavigationObserver(autofill_manager_.get());
[email protected]403415a2011-01-10 18:57:53473 autocomplete_history_manager_.reset(new AutocompleteHistoryManager(this));
[email protected]0f180ee2011-01-21 18:23:10474 AddNavigationObserver(autocomplete_history_manager_.get());
[email protected]332af7732009-03-11 13:21:35475}
initial.commit09911bf2008-07-26 23:55:29476
477TabContents::~TabContents() {
[email protected]420ae012009-04-24 05:16:32478 is_being_destroyed_ = true;
479
[email protected]bfe4c1582009-10-02 18:11:09480 // We don't want any notifications while we're running our destructor.
[email protected]420ae012009-04-24 05:16:32481 registrar_.RemoveAll();
[email protected]2fb7dc982010-09-29 12:24:28482 pref_change_registrar_.RemoveAll();
[email protected]420ae012009-04-24 05:16:32483
[email protected]420ae012009-04-24 05:16:32484 NotifyDisconnected();
[email protected]e7b8d832009-11-20 22:41:00485 hung_renderer_dialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:32486
[email protected]420ae012009-04-24 05:16:32487 // First cleanly close all child windows.
488 // TODO(mpcomplete): handle case if MaybeCloseChildWindows() already asked
489 // some of these to close. CloseWindows is async, so it might get called
490 // twice before it runs.
[email protected]8cc8d492010-02-02 10:40:49491 CloseConstrainedWindows();
[email protected]420ae012009-04-24 05:16:32492
[email protected]c65e2f152010-10-14 15:30:40493 // Close all blocked contents.
494 if (blocked_contents_)
495 blocked_contents_->Destroy();
[email protected]66ba4932009-06-04 19:22:13496
[email protected]420ae012009-04-24 05:16:32497 // Notify any observer that have a reference on this tab contents.
498 NotificationService::current()->Notify(
499 NotificationType::TAB_CONTENTS_DESTROYED,
500 Source<TabContents>(this),
501 NotificationService::NoDetails());
502
[email protected]1e5e74f2009-05-27 20:55:12503 // Notify any lasting InfobarDelegates that have not yet been removed that
504 // whatever infobar they were handling in this TabContents has closed,
505 // because the TabContents is going away entirely.
506 // This must happen after the TAB_CONTENTS_DESTROYED notification as the
507 // notification may trigger infobars calls that access their delegate. (and
508 // some implementations of InfoBarDelegate do delete themselves on
509 // InfoBarClosed()).
510 for (int i = 0; i < infobar_delegate_count(); ++i) {
511 InfoBarDelegate* delegate = GetInfoBarDelegateAt(i);
512 delegate->InfoBarClosed();
513 }
514 infobar_delegates_.clear();
515
[email protected]420ae012009-04-24 05:16:32516 // TODO(brettw) this should be moved to the view.
517#if defined(OS_WIN)
518 // If we still have a window handle, destroy it. GetNativeView can return
519 // NULL if this contents was part of a window that closed.
[email protected]50bd6452010-11-27 19:39:42520 if (GetNativeView()) {
521 RenderViewHost* host = render_view_host();
522 if (host && host->view()) {
523 host->view()->WillWmDestroy();
524 }
[email protected]420ae012009-04-24 05:16:32525 ::DestroyWindow(GetNativeView());
[email protected]50bd6452010-11-27 19:39:42526 }
[email protected]420ae012009-04-24 05:16:32527#endif
[email protected]7ab1e7d62009-10-14 23:32:01528
529 // OnCloseStarted isn't called in unit tests.
530 if (!tab_close_start_time_.is_null()) {
531 UMA_HISTOGRAM_TIMES("Tab.Close",
532 base::TimeTicks::Now() - tab_close_start_time_);
533 }
initial.commit09911bf2008-07-26 23:55:29534}
535
[email protected]d5f942ba2008-09-26 19:30:34536// static
537void TabContents::RegisterUserPrefs(PrefService* prefs) {
[email protected]420ae012009-04-24 05:16:32538 prefs->RegisterBooleanPref(prefs::kAlternateErrorPagesEnabled, true);
539
540 WebPreferences pref_defaults;
541 prefs->RegisterBooleanPref(prefs::kWebKitJavascriptEnabled,
542 pref_defaults.javascript_enabled);
543 prefs->RegisterBooleanPref(prefs::kWebKitWebSecurityEnabled,
544 pref_defaults.web_security_enabled);
545 prefs->RegisterBooleanPref(
546 prefs::kWebKitJavascriptCanOpenWindowsAutomatically, true);
547 prefs->RegisterBooleanPref(prefs::kWebKitLoadsImagesAutomatically,
548 pref_defaults.loads_images_automatically);
549 prefs->RegisterBooleanPref(prefs::kWebKitPluginsEnabled,
550 pref_defaults.plugins_enabled);
551 prefs->RegisterBooleanPref(prefs::kWebKitDomPasteEnabled,
552 pref_defaults.dom_paste_enabled);
553 prefs->RegisterBooleanPref(prefs::kWebKitShrinksStandaloneImagesToFit,
554 pref_defaults.shrinks_standalone_images_to_fit);
[email protected]c4e98902010-06-01 10:20:14555 prefs->RegisterDictionaryPref(prefs::kWebKitInspectorSettings);
[email protected]420ae012009-04-24 05:16:32556 prefs->RegisterBooleanPref(prefs::kWebKitTextAreasAreResizable,
557 pref_defaults.text_areas_are_resizable);
558 prefs->RegisterBooleanPref(prefs::kWebKitJavaEnabled,
559 pref_defaults.java_enabled);
[email protected]1661a3c02010-05-20 16:32:36560 prefs->RegisterBooleanPref(prefs::kWebkitTabsToLinks,
561 pref_defaults.tabs_to_links);
[email protected]420ae012009-04-24 05:16:32562
563 prefs->RegisterLocalizedStringPref(prefs::kAcceptLanguages,
564 IDS_ACCEPT_LANGUAGES);
565 prefs->RegisterLocalizedStringPref(prefs::kDefaultCharset,
566 IDS_DEFAULT_ENCODING);
567 prefs->RegisterLocalizedBooleanPref(prefs::kWebKitStandardFontIsSerif,
568 IDS_STANDARD_FONT_IS_SERIF);
569 prefs->RegisterLocalizedStringPref(prefs::kWebKitFixedFontFamily,
570 IDS_FIXED_FONT_FAMILY);
571 prefs->RegisterLocalizedStringPref(prefs::kWebKitSerifFontFamily,
572 IDS_SERIF_FONT_FAMILY);
573 prefs->RegisterLocalizedStringPref(prefs::kWebKitSansSerifFontFamily,
574 IDS_SANS_SERIF_FONT_FAMILY);
575 prefs->RegisterLocalizedStringPref(prefs::kWebKitCursiveFontFamily,
576 IDS_CURSIVE_FONT_FAMILY);
577 prefs->RegisterLocalizedStringPref(prefs::kWebKitFantasyFontFamily,
578 IDS_FANTASY_FONT_FAMILY);
579 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitDefaultFontSize,
580 IDS_DEFAULT_FONT_SIZE);
581 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitDefaultFixedFontSize,
582 IDS_DEFAULT_FIXED_FONT_SIZE);
583 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitMinimumFontSize,
584 IDS_MINIMUM_FONT_SIZE);
585 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitMinimumLogicalFontSize,
586 IDS_MINIMUM_LOGICAL_FONT_SIZE);
587 prefs->RegisterLocalizedBooleanPref(prefs::kWebKitUsesUniversalDetector,
588 IDS_USES_UNIVERSAL_DETECTOR);
589 prefs->RegisterLocalizedStringPref(prefs::kStaticEncodings,
590 IDS_STATIC_ENCODING_LIST);
initial.commit09911bf2008-07-26 23:55:29591}
592
[email protected]724159a2010-12-30 01:11:18593bool TabContents::OnMessageReceived(const IPC::Message& message) {
[email protected]0f180ee2011-01-21 18:23:10594 ObserverListBase<WebNavigationObserver>::Iterator it(
595 web_navigation_observers_);
596 WebNavigationObserver* observer;
597 while ((observer = it.GetNext()) != NULL)
598 if (observer->OnMessageReceived(message))
[email protected]403415a2011-01-10 18:57:53599 return true;
[email protected]403415a2011-01-10 18:57:53600
[email protected]724159a2010-12-30 01:11:18601 bool handled = true;
602 bool message_is_ok = true;
603 IPC_BEGIN_MESSAGE_MAP_EX(TabContents, message, message_is_ok)
604 IPC_MESSAGE_HANDLER(ViewHostMsg_DidStartProvisionalLoadForFrame,
605 OnDidStartProvisionalLoadForFrame)
606 IPC_MESSAGE_HANDLER(ViewHostMsg_DidRedirectProvisionalLoad,
607 OnDidRedirectProvisionalLoad)
608 IPC_MESSAGE_HANDLER(ViewHostMsg_DidFailProvisionalLoadWithError,
609 OnDidFailProvisionalLoadWithError)
610 IPC_MESSAGE_HANDLER(ViewHostMsg_DidLoadResourceFromMemoryCache,
611 OnDidLoadResourceFromMemoryCache)
612 IPC_MESSAGE_HANDLER(ViewHostMsg_DidDisplayInsecureContent,
613 OnDidDisplayInsecureContent)
614 IPC_MESSAGE_HANDLER(ViewHostMsg_DidRunInsecureContent,
615 OnDidRunInsecureContent)
616 IPC_MESSAGE_HANDLER(ViewHostMsg_DocumentLoadedInFrame,
617 OnDocumentLoadedInFrame)
618 IPC_MESSAGE_HANDLER(ViewHostMsg_DidFinishLoad, OnDidFinishLoad)
[email protected]c8f73ab2011-01-22 00:05:17619 IPC_MESSAGE_HANDLER(ViewHostMsg_UpdateContentRestrictions,
620 OnUpdateContentRestrictions)
621 IPC_MESSAGE_HANDLER(ViewHostMsg_PDFHasUnsupportedFeature,
622 OnPDFHasUnsupportedFeature)
[email protected]7d472472011-01-22 01:30:25623 IPC_MESSAGE_HANDLER(ViewHostMsg_Find_Reply, OnFindReply)
624 IPC_MESSAGE_HANDLER(ViewHostMsg_GoToEntryAtOffset, OnGoToEntryAtOffset)
625 IPC_MESSAGE_HANDLER(ViewHostMsg_MissingPluginStatus, OnMissingPluginStatus)
626 IPC_MESSAGE_HANDLER(ViewHostMsg_CrashedPlugin, OnCrashedPlugin)
627 IPC_MESSAGE_HANDLER(ViewHostMsg_BlockedOutdatedPlugin,
628 OnBlockedOutdatedPlugin)
629 IPC_MESSAGE_HANDLER(ViewHostMsg_DidGetApplicationInfo,
630 OnDidGetApplicationInfo)
631 IPC_MESSAGE_HANDLER(ViewHostMsg_InstallApplication,
632 OnInstallApplication)
633 IPC_MESSAGE_HANDLER(ViewHostMsg_PageContents, OnPageContents)
634 IPC_MESSAGE_HANDLER(ViewHostMsg_PageTranslated, OnPageTranslated)
635 IPC_MESSAGE_HANDLER(ViewHostMsg_SetSuggestions, OnSetSuggestions)
636 IPC_MESSAGE_HANDLER(ViewHostMsg_InstantSupportDetermined,
637 OnInstantSupportDetermined)
[email protected]724159a2010-12-30 01:11:18638 IPC_MESSAGE_UNHANDLED(handled = false)
639 IPC_END_MESSAGE_MAP_EX()
640
641 if (!message_is_ok) {
642 UserMetrics::RecordAction(UserMetricsAction("BadMessageTerminate_RVD"));
643 GetRenderProcessHost()->ReceivedBadMessage();
644 }
645
646 return handled;
647}
648
[email protected]13367f72009-07-08 16:39:02649// Returns true if contains content rendered by an extension.
650bool TabContents::HostsExtension() const {
651 return GetURL().SchemeIs(chrome::kExtensionScheme);
[email protected]d5f942ba2008-09-26 19:30:34652}
653
[email protected]f4f50ef2011-01-21 19:01:19654PluginInstallerInfoBarDelegate* TabContents::GetPluginInstaller() {
[email protected]6e95706d2009-04-23 22:55:01655 if (plugin_installer_.get() == NULL)
[email protected]f4f50ef2011-01-21 19:01:19656 plugin_installer_.reset(new PluginInstallerInfoBarDelegate(this));
[email protected]6e95706d2009-04-23 22:55:01657 return plugin_installer_.get();
658}
659
[email protected]1f18184a2010-07-21 19:34:49660TabContentsSSLHelper* TabContents::GetSSLHelper() {
661 if (ssl_helper_.get() == NULL)
662 ssl_helper_.reset(new TabContentsSSLHelper(this));
663 return ssl_helper_.get();
664}
665
[email protected]8cb5d5b2010-02-09 11:36:16666RenderProcessHost* TabContents::GetRenderProcessHost() const {
667 return render_manager_.current_host()->process();
668}
669
[email protected]9adb9692010-10-29 23:14:02670void TabContents::SetExtensionApp(const Extension* extension) {
[email protected]867a73e12010-03-19 20:45:46671 DCHECK(!extension || extension->GetFullLaunchURL().is_valid());
[email protected]98aa0b52010-05-06 17:03:08672 extension_app_ = extension;
[email protected]fca656c2010-02-10 20:30:10673
[email protected]98aa0b52010-05-06 17:03:08674 UpdateExtensionAppIcon(extension_app_);
[email protected]a888b29e62010-04-01 13:38:57675
[email protected]fca656c2010-02-10 20:30:10676 NotificationService::current()->Notify(
677 NotificationType::TAB_CONTENTS_APPLICATION_EXTENSION_CHANGED,
678 Source<TabContents>(this),
679 NotificationService::NoDetails());
[email protected]3b355642010-02-05 16:01:49680}
681
[email protected]98aa0b52010-05-06 17:03:08682void TabContents::SetExtensionAppById(const std::string& extension_app_id) {
683 if (extension_app_id.empty())
[email protected]911f8152010-03-18 16:46:40684 return;
685
[email protected]eaa7dd182010-12-14 11:09:00686 ExtensionService* extension_service = profile()->GetExtensionService();
[email protected]fa6a9102010-11-22 15:38:50687 if (!extension_service || !extension_service->is_ready())
688 return;
689
690 const Extension* extension =
691 extension_service->GetExtensionById(extension_app_id, false);
692 if (extension)
693 SetExtensionApp(extension);
[email protected]911f8152010-03-18 16:46:40694}
695
[email protected]98aa0b52010-05-06 17:03:08696SkBitmap* TabContents::GetExtensionAppIcon() {
[email protected]98aa0b52010-05-06 17:03:08697 if (extension_app_icon_.empty())
[email protected]8eb043b2010-04-30 07:19:51698 return NULL;
699
[email protected]98aa0b52010-05-06 17:03:08700 return &extension_app_icon_;
[email protected]8eb043b2010-04-30 07:19:51701}
702
[email protected]d5f942ba2008-09-26 19:30:34703const GURL& TabContents::GetURL() const {
704 // We may not have a navigation entry yet
[email protected]ce3fa3c2009-04-20 19:55:57705 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]ebe89e062009-08-13 23:16:54706 return entry ? entry->virtual_url() : GURL::EmptyGURL();
[email protected]d5f942ba2008-09-26 19:30:34707}
708
[email protected]96d185d2009-04-24 03:28:54709const string16& TabContents::GetTitle() const {
[email protected]4c6092c5b2009-06-06 00:23:55710 // Transient entries take precedence. They are used for interstitial pages
711 // that are shown on top of existing pages.
712 NavigationEntry* entry = controller_.GetTransientEntry();
[email protected]45d0ef7f2011-01-05 13:46:23713 if (entry) {
714 return entry->GetTitleForDisplay(profile()->GetPrefs()->
715 GetString(prefs::kAcceptLanguages));
716 }
[email protected]96d185d2009-04-24 03:28:54717 DOMUI* our_dom_ui = render_manager_.pending_dom_ui() ?
718 render_manager_.pending_dom_ui() : render_manager_.dom_ui();
719 if (our_dom_ui) {
720 // Don't override the title in view source mode.
[email protected]4c6092c5b2009-06-06 00:23:55721 entry = controller_.GetActiveEntry();
[email protected]96d185d2009-04-24 03:28:54722 if (!(entry && entry->IsViewSourceMode())) {
723 // Give the DOM UI the chance to override our title.
724 const string16& title = our_dom_ui->overridden_title();
725 if (!title.empty())
726 return title;
727 }
728 }
729
730 // We use the title for the last committed entry rather than a pending
731 // navigation entry. For example, when the user types in a URL, we want to
732 // keep the old page's title until the new load has committed and we get a new
733 // title.
[email protected]96d185d2009-04-24 03:28:54734 entry = controller_.GetLastCommittedEntry();
[email protected]45d0ef7f2011-01-05 13:46:23735 if (entry) {
736 return entry->GetTitleForDisplay(profile()->GetPrefs()->
737 GetString(prefs::kAcceptLanguages));
738 }
[email protected]96d185d2009-04-24 03:28:54739 return EmptyString16();
740}
741
[email protected]bbbc1ef2010-02-12 18:03:41742// static
743string16 TabContents::GetDefaultTitle() {
744 return l10n_util::GetStringUTF16(IDS_DEFAULT_TAB_TITLE);
745}
746
[email protected]d5f942ba2008-09-26 19:30:34747int32 TabContents::GetMaxPageID() {
748 if (GetSiteInstance())
749 return GetSiteInstance()->max_page_id();
750 else
751 return max_page_id_;
752}
753
754void TabContents::UpdateMaxPageID(int32 page_id) {
755 // Ensure both the SiteInstance and RenderProcessHost update their max page
[email protected]57c6a652009-05-04 07:58:34756 // IDs in sync. Only TabContents will also have site instances, except during
[email protected]d5f942ba2008-09-26 19:30:34757 // testing.
758 if (GetSiteInstance())
759 GetSiteInstance()->UpdateMaxPageID(page_id);
[email protected]8cb5d5b2010-02-09 11:36:16760 GetRenderProcessHost()->UpdateMaxPageID(page_id);
[email protected]d5f942ba2008-09-26 19:30:34761}
762
[email protected]96d185d2009-04-24 03:28:54763SiteInstance* TabContents::GetSiteInstance() const {
764 return render_manager_.current_host()->site_instance();
765}
766
[email protected]96d185d2009-04-24 03:28:54767bool TabContents::ShouldDisplayURL() {
[email protected]8ab24cc2009-11-13 20:56:15768 // Don't hide the url in view source mode and with interstitials.
[email protected]96d185d2009-04-24 03:28:54769 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]8ab24cc2009-11-13 20:56:15770 if (entry && (entry->IsViewSourceMode() ||
[email protected]cccd3762010-11-12 18:40:01771 entry->page_type() == INTERSTITIAL_PAGE)) {
[email protected]96d185d2009-04-24 03:28:54772 return true;
[email protected]8ab24cc2009-11-13 20:56:15773 }
[email protected]83e3895b2009-06-11 00:07:16774
[email protected]96d185d2009-04-24 03:28:54775 DOMUI* dom_ui = GetDOMUIForCurrentState();
776 if (dom_ui)
777 return !dom_ui->should_hide_url();
778 return true;
779}
780
[email protected]d5f942ba2008-09-26 19:30:34781SkBitmap TabContents::GetFavIcon() const {
782 // Like GetTitle(), we also want to use the favicon for the last committed
783 // entry rather than a pending navigation entry.
[email protected]ce3fa3c2009-04-20 19:55:57784 NavigationEntry* entry = controller_.GetTransientEntry();
[email protected]cbab76d2008-10-13 22:42:47785 if (entry)
786 return entry->favicon().bitmap();
787
[email protected]ce3fa3c2009-04-20 19:55:57788 entry = controller_.GetLastCommittedEntry();
[email protected]d5f942ba2008-09-26 19:30:34789 if (entry)
790 return entry->favicon().bitmap();
[email protected]d5f942ba2008-09-26 19:30:34791 return SkBitmap();
792}
793
[email protected]0b303cc2009-09-28 22:35:15794bool TabContents::FavIconIsValid() const {
795 NavigationEntry* entry = controller_.GetTransientEntry();
796 if (entry)
797 return entry->favicon().is_valid();
798
799 entry = controller_.GetLastCommittedEntry();
800 if (entry)
801 return entry->favicon().is_valid();
802
803 return false;
804}
805
[email protected]96d185d2009-04-24 03:28:54806bool TabContents::ShouldDisplayFavIcon() {
807 // Always display a throbber during pending loads.
808 if (controller_.GetLastCommittedEntry() && controller_.pending_entry())
809 return true;
810
811 DOMUI* dom_ui = GetDOMUIForCurrentState();
812 if (dom_ui)
813 return !dom_ui->hide_favicon();
814 return true;
815}
816
[email protected]42197a22010-12-28 23:29:42817string16 TabContents::GetStatusText() const {
[email protected]96d185d2009-04-24 03:28:54818 if (!is_loading() || load_state_ == net::LOAD_STATE_IDLE)
[email protected]42197a22010-12-28 23:29:42819 return string16();
[email protected]96d185d2009-04-24 03:28:54820
821 switch (load_state_) {
822 case net::LOAD_STATE_WAITING_FOR_CACHE:
[email protected]42197a22010-12-28 23:29:42823 return l10n_util::GetStringUTF16(IDS_LOAD_STATE_WAITING_FOR_CACHE);
[email protected]8a1f3312010-05-25 19:25:04824 case net::LOAD_STATE_ESTABLISHING_PROXY_TUNNEL:
[email protected]42197a22010-12-28 23:29:42825 return
826 l10n_util::GetStringUTF16(IDS_LOAD_STATE_ESTABLISHING_PROXY_TUNNEL);
[email protected]96d185d2009-04-24 03:28:54827 case net::LOAD_STATE_RESOLVING_PROXY_FOR_URL:
[email protected]42197a22010-12-28 23:29:42828 return l10n_util::GetStringUTF16(IDS_LOAD_STATE_RESOLVING_PROXY_FOR_URL);
[email protected]96d185d2009-04-24 03:28:54829 case net::LOAD_STATE_RESOLVING_HOST:
[email protected]42197a22010-12-28 23:29:42830 return l10n_util::GetStringUTF16(IDS_LOAD_STATE_RESOLVING_HOST);
[email protected]96d185d2009-04-24 03:28:54831 case net::LOAD_STATE_CONNECTING:
[email protected]42197a22010-12-28 23:29:42832 return l10n_util::GetStringUTF16(IDS_LOAD_STATE_CONNECTING);
[email protected]375e3112010-05-27 20:32:06833 case net::LOAD_STATE_SSL_HANDSHAKE:
[email protected]42197a22010-12-28 23:29:42834 return l10n_util::GetStringUTF16(IDS_LOAD_STATE_SSL_HANDSHAKE);
[email protected]96d185d2009-04-24 03:28:54835 case net::LOAD_STATE_SENDING_REQUEST:
[email protected]094e5b22009-09-25 04:23:56836 if (upload_size_)
[email protected]42197a22010-12-28 23:29:42837 return l10n_util::GetStringFUTF16Int(
[email protected]094e5b22009-09-25 04:23:56838 IDS_LOAD_STATE_SENDING_REQUEST_WITH_PROGRESS,
839 static_cast<int>((100 * upload_position_) / upload_size_));
840 else
[email protected]42197a22010-12-28 23:29:42841 return l10n_util::GetStringUTF16(IDS_LOAD_STATE_SENDING_REQUEST);
[email protected]96d185d2009-04-24 03:28:54842 case net::LOAD_STATE_WAITING_FOR_RESPONSE:
[email protected]42197a22010-12-28 23:29:42843 return l10n_util::GetStringFUTF16(IDS_LOAD_STATE_WAITING_FOR_RESPONSE,
844 load_state_host_);
[email protected]96d185d2009-04-24 03:28:54845 // Ignore net::LOAD_STATE_READING_RESPONSE and net::LOAD_STATE_IDLE
846 case net::LOAD_STATE_IDLE:
847 case net::LOAD_STATE_READING_RESPONSE:
848 break;
849 }
850
[email protected]42197a22010-12-28 23:29:42851 return string16();
[email protected]96d185d2009-04-24 03:28:54852}
853
[email protected]3c9e1872010-11-18 16:17:49854void TabContents::AddNavigationObserver(WebNavigationObserver* observer) {
855 web_navigation_observers_.AddObserver(observer);
856}
857
858void TabContents::RemoveNavigationObserver(WebNavigationObserver* observer) {
859 web_navigation_observers_.RemoveObserver(observer);
860}
861
[email protected]443b80e2010-12-14 00:42:23862void TabContents::SetIsCrashed(base::TerminationStatus status, int error_code) {
863 if (status == crashed_status_)
[email protected]d5f942ba2008-09-26 19:30:34864 return;
865
[email protected]443b80e2010-12-14 00:42:23866 crashed_status_ = status;
867 crashed_error_code_ = error_code;
[email protected]c9cd2222009-05-06 05:16:50868 NotifyNavigationStateChanged(INVALIDATE_TAB);
[email protected]d5f942ba2008-09-26 19:30:34869}
870
[email protected]744ef172009-10-16 21:53:46871void TabContents::PageActionStateChanged() {
872 NotifyNavigationStateChanged(TabContents::INVALIDATE_PAGE_ACTIONS);
[email protected]d7eaf572009-07-01 21:57:00873}
[email protected]ce5c4502009-05-06 16:46:11874
[email protected]d5f942ba2008-09-26 19:30:34875void TabContents::NotifyNavigationStateChanged(unsigned changed_flags) {
876 if (delegate_)
877 delegate_->NavigationStateChanged(this, changed_flags);
878}
879
[email protected]96d185d2009-04-24 03:28:54880void TabContents::DidBecomeSelected() {
881 controller_.SetActive(true);
[email protected]8cb5d5b2010-02-09 11:36:16882 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
883 if (rwhv) {
884 rwhv->DidBecomeSelected();
[email protected]789e9152009-08-04 21:59:43885#if defined(OS_MACOSX)
[email protected]8cb5d5b2010-02-09 11:36:16886 rwhv->SetActive(true);
[email protected]789e9152009-08-04 21:59:43887#endif
888 }
[email protected]96d185d2009-04-24 03:28:54889
[email protected]8cb5d5b2010-02-09 11:36:16890 WebCacheManager::GetInstance()->ObserveActivity(GetRenderProcessHost()->id());
[email protected]5ac20162010-11-24 23:33:11891 last_selected_time_ = base::TimeTicks::Now();
[email protected]61d68ef12011-01-13 14:02:56892#if defined(OS_CHROMEOS)
893 chromeos::LocaleChangeGuard::Check(this);
894#endif
[email protected]96d185d2009-04-24 03:28:54895}
896
[email protected]a94454b2010-12-16 04:16:03897void TabContents::FadeForInstant(bool animate) {
[email protected]1b072562010-12-13 19:37:45898 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
899 SkColor whitish = SkColorSetARGB(192, 255, 255, 255);
900 if (rwhv)
[email protected]a94454b2010-12-16 04:16:03901 rwhv->SetVisuallyDeemphasized(&whitish, animate);
[email protected]1b072562010-12-13 19:37:45902}
903
904void TabContents::CancelInstantFade() {
905 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
906 if (rwhv)
907 rwhv->SetVisuallyDeemphasized(NULL, false);
908}
909
[email protected]96d185d2009-04-24 03:28:54910void TabContents::WasHidden() {
911 if (!capturing_contents()) {
912 // |render_view_host()| can be NULL if the user middle clicks a link to open
913 // a tab in then background, then closes the tab before selecting it. This
914 // is because closing the tab calls TabContents::Destroy(), which removes
915 // the |render_view_host()|; then when we actually destroy the window,
916 // OnWindowPosChanged() notices and calls HideContents() (which calls us).
[email protected]8cb5d5b2010-02-09 11:36:16917 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
918 if (rwhv)
919 rwhv->WasHidden();
[email protected]96d185d2009-04-24 03:28:54920 }
921
922 NotificationService::current()->Notify(
923 NotificationType::TAB_CONTENTS_HIDDEN,
924 Source<TabContents>(this),
925 NotificationService::NoDetails());
926}
927
[email protected]d5f942ba2008-09-26 19:30:34928void TabContents::Activate() {
929 if (delegate_)
930 delegate_->ActivateContents(this);
931}
932
[email protected]ea42e7782010-08-23 23:58:12933void TabContents::Deactivate() {
934 if (delegate_)
935 delegate_->DeactivateContents(this);
936}
937
[email protected]96d185d2009-04-24 03:28:54938void TabContents::ShowContents() {
[email protected]8cb5d5b2010-02-09 11:36:16939 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
940 if (rwhv)
941 rwhv->DidBecomeSelected();
[email protected]96d185d2009-04-24 03:28:54942}
943
944void TabContents::HideContents() {
945 // TODO(pkasting): http://b/1239839 Right now we purposefully don't call
946 // our superclass HideContents(), because some callers want to be very picky
947 // about the order in which these get called. In addition to making the code
948 // here practically impossible to understand, this also means we end up
949 // calling TabContents::WasHidden() twice if callers call both versions of
[email protected]57c6a652009-05-04 07:58:34950 // HideContents() on a TabContents.
[email protected]96d185d2009-04-24 03:28:54951 WasHidden();
952}
953
[email protected]5aab5e22010-12-08 22:13:29954bool TabContents::NeedToFireBeforeUnload() {
955 // TODO(creis): Should we fire even for interstitial pages?
956 return notify_disconnection() &&
957 !showing_interstitial_page() &&
958 !render_view_host()->SuddenTerminationAllowed();
959}
960
[email protected]c0588052008-10-27 23:01:50961void TabContents::OpenURL(const GURL& url, const GURL& referrer,
[email protected]d5f942ba2008-09-26 19:30:34962 WindowOpenDisposition disposition,
963 PageTransition::Type transition) {
964 if (delegate_)
[email protected]c0588052008-10-27 23:01:50965 delegate_->OpenURLFromTab(this, url, referrer, disposition, transition);
[email protected]d5f942ba2008-09-26 19:30:34966}
967
[email protected]1ccb3568d2010-02-19 10:51:16968bool TabContents::NavigateToPendingEntry(
969 NavigationController::ReloadType reload_type) {
[email protected]876bc832010-09-07 16:29:54970 return NavigateToEntry(*controller_.pending_entry(), reload_type);
971}
[email protected]96d185d2009-04-24 03:28:54972
[email protected]876bc832010-09-07 16:29:54973bool TabContents::NavigateToEntry(
974 const NavigationEntry& entry,
975 NavigationController::ReloadType reload_type) {
[email protected]96d185d2009-04-24 03:28:54976 RenderViewHost* dest_render_view_host = render_manager_.Navigate(entry);
977 if (!dest_render_view_host)
978 return false; // Unable to create the desired render view host.
979
[email protected]770dd8b2010-05-24 18:11:39980 if (delegate_ && delegate_->ShouldEnablePreferredSizeNotifications()) {
981 dest_render_view_host->EnablePreferredSizeChangedMode(
982 kPreferredSizeWidth | kPreferredSizeHeightThisIsSlow);
983 }
[email protected]9fb325e2010-05-06 18:23:24984
[email protected]05fdd492010-11-15 17:52:07985 // For security, we should never send non-DOM-UI URLs to a DOM UI renderer.
986 // Double check that here.
[email protected]54ec6472010-04-09 19:39:58987 int enabled_bindings = dest_render_view_host->enabled_bindings();
[email protected]0352e5472010-04-29 00:11:40988 bool is_allowed_in_dom_ui_renderer =
[email protected]05fdd492010-11-15 17:52:07989 DOMUIFactory::IsURLAcceptableForDOMUI(profile(), entry.url());
[email protected]54ec6472010-04-09 19:39:58990 CHECK(!BindingsPolicy::is_dom_ui_enabled(enabled_bindings) ||
[email protected]0352e5472010-04-29 00:11:40991 is_allowed_in_dom_ui_renderer);
[email protected]54ec6472010-04-09 19:39:58992
[email protected]96d185d2009-04-24 03:28:54993 // Tell DevTools agent that it is attached prior to the navigation.
[email protected]d9f9b792009-06-24 13:17:12994 DevToolsManager* devtools_manager = DevToolsManager::GetInstance();
[email protected]7e8e6b62009-05-08 11:28:32995 if (devtools_manager) { // NULL in unit tests.
[email protected]3cc72b12010-03-18 23:03:00996 devtools_manager->OnNavigatingToPendingEntry(render_view_host(),
997 dest_render_view_host,
998 entry.url());
[email protected]7e8e6b62009-05-08 11:28:32999 }
[email protected]96d185d2009-04-24 03:28:541000
1001 // Used for page load time metrics.
1002 current_load_start_ = base::TimeTicks::Now();
1003
1004 // Navigate in the desired RenderViewHost.
[email protected]056de2d2009-06-26 16:41:341005 ViewMsg_Navigate_Params navigate_params;
[email protected]876bc832010-09-07 16:29:541006 MakeNavigateParams(entry, controller_, reload_type, &navigate_params);
[email protected]52c68652010-12-07 17:47:041007 if (delegate_) {
1008 navigate_params.extra_headers =
1009 delegate_->GetNavigationHeaders(navigate_params.url);
1010 }
[email protected]056de2d2009-06-26 16:41:341011 dest_render_view_host->Navigate(navigate_params);
[email protected]96d185d2009-04-24 03:28:541012
1013 if (entry.page_id() == -1) {
1014 // HACK!! This code suppresses javascript: URLs from being added to
1015 // session history, which is what we want to do for javascript: URLs that
1016 // do not generate content. What we really need is a message from the
1017 // renderer telling us that a new page was not created. The same message
1018 // could be used for mailto: URLs and the like.
1019 if (entry.url().SchemeIs(chrome::kJavaScriptScheme))
1020 return false;
1021 }
1022
[email protected]3c9e1872010-11-18 16:17:491023 // Notify observers about navigation.
1024 FOR_EACH_OBSERVER(WebNavigationObserver, web_navigation_observers_,
1025 NavigateToPendingEntry());
[email protected]96d185d2009-04-24 03:28:541026
[email protected]1ccb3568d2010-02-19 10:51:161027 if (reload_type != NavigationController::NO_RELOAD &&
1028 !profile()->IsOffTheRecord()) {
[email protected]0189bc722009-08-28 21:56:481029 FaviconService* favicon_service =
1030 profile()->GetFaviconService(Profile::IMPLICIT_ACCESS);
1031 if (favicon_service)
1032 favicon_service->SetFaviconOutOfDateForPage(entry.url());
[email protected]96d185d2009-04-24 03:28:541033 }
1034
[email protected]d5f942ba2008-09-26 19:30:341035 return true;
1036}
1037
[email protected]96d185d2009-04-24 03:28:541038void TabContents::Stop() {
1039 render_manager_.Stop();
[email protected]12802702010-07-09 19:43:091040 printing_->Stop();
[email protected]96d185d2009-04-24 03:28:541041}
1042
[email protected]96d185d2009-04-24 03:28:541043void TabContents::DisassociateFromPopupCount() {
1044 render_view_host()->DisassociateFromPopupCount();
1045}
1046
1047TabContents* TabContents::Clone() {
1048 // We create a new SiteInstance so that the new tab won't share processes
1049 // with the old one. This can be changed in the future if we need it to share
1050 // processes for some reason.
[email protected]420ae012009-04-24 05:16:321051 TabContents* tc = new TabContents(profile(),
[email protected]96d185d2009-04-24 03:28:541052 SiteInstance::CreateSiteInstance(profile()),
[email protected]6ee12c42010-09-14 09:36:071053 MSG_ROUTING_NONE, this, NULL);
[email protected]96d185d2009-04-24 03:28:541054 tc->controller().CopyStateFrom(controller_);
[email protected]98aa0b52010-05-06 17:03:081055 tc->extension_app_ = extension_app_;
[email protected]98aa0b52010-05-06 17:03:081056 tc->extension_app_icon_ = extension_app_icon_;
[email protected]96d185d2009-04-24 03:28:541057 return tc;
1058}
1059
[email protected]4d677202009-07-19 07:37:121060void TabContents::ShowPageInfo(const GURL& url,
1061 const NavigationEntry::SSLStatus& ssl,
1062 bool show_history) {
1063 if (!delegate_)
1064 return;
1065
[email protected]bb678332009-07-21 00:15:501066 delegate_->ShowPageInfo(profile(), url, ssl, show_history);
[email protected]4d677202009-07-19 07:37:121067}
1068
[email protected]ef0d04c2010-12-15 23:34:211069void TabContents::SaveFavicon() {
1070 NavigationEntry* entry = controller_.GetActiveEntry();
1071 if (!entry || entry->url().is_empty())
1072 return;
1073
1074 // Make sure the page is in history, otherwise adding the favicon does
1075 // nothing.
1076 HistoryService* history = profile()->GetOriginalProfile()->GetHistoryService(
1077 Profile::IMPLICIT_ACCESS);
1078 if (!history)
1079 return;
1080 history->AddPageNoVisitForBookmark(entry->url());
1081
1082 FaviconService* service = profile()->GetOriginalProfile()->GetFaviconService(
1083 Profile::IMPLICIT_ACCESS);
1084 if (!service)
1085 return;
1086 const NavigationEntry::FaviconStatus& favicon(entry->favicon());
1087 if (!favicon.is_valid() || favicon.url().is_empty() ||
1088 favicon.bitmap().empty()) {
1089 return;
1090 }
1091 std::vector<unsigned char> image_data;
1092 gfx::PNGCodec::EncodeBGRASkBitmap(favicon.bitmap(), false, &image_data);
1093 service->SetFavicon(entry->url(), favicon.url(), image_data);
1094}
1095
initial.commit09911bf2008-07-26 23:55:291096ConstrainedWindow* TabContents::CreateConstrainedDialog(
[email protected]e8382172009-06-19 22:16:281097 ConstrainedWindowDelegate* delegate) {
initial.commit09911bf2008-07-26 23:55:291098 ConstrainedWindow* window =
[email protected]e8382172009-06-19 22:16:281099 ConstrainedWindow::CreateConstrainedDialog(this, delegate);
initial.commit09911bf2008-07-26 23:55:291100 child_windows_.push_back(window);
[email protected]fa1cf0b82010-01-15 21:49:441101
1102 if (child_windows_.size() == 1) {
1103 window->ShowConstrainedWindow();
1104 BlockTabContent(true);
1105 }
1106
initial.commit09911bf2008-07-26 23:55:291107 return window;
1108}
1109
[email protected]fa1cf0b82010-01-15 21:49:441110void TabContents::BlockTabContent(bool blocked) {
[email protected]8dccd7cb2010-02-25 05:19:551111 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
[email protected]1b072562010-12-13 19:37:451112 // 70% opaque grey.
1113 SkColor greyish = SkColorSetARGB(178, 0, 0, 0);
[email protected]8dccd7cb2010-02-25 05:19:551114 if (rwhv)
[email protected]1b072562010-12-13 19:37:451115 rwhv->SetVisuallyDeemphasized(blocked ? &greyish : NULL, false);
[email protected]fa1cf0b82010-01-15 21:49:441116 render_view_host()->set_ignore_input_events(blocked);
1117 if (delegate_)
1118 delegate_->SetTabContentBlocked(this, blocked);
1119}
1120
initial.commit09911bf2008-07-26 23:55:291121void TabContents::AddNewContents(TabContents* new_contents,
1122 WindowOpenDisposition disposition,
1123 const gfx::Rect& initial_pos,
[email protected]7e7414ae2010-01-26 20:19:291124 bool user_gesture) {
[email protected]c65e2f152010-10-14 15:30:401125 if (all_contents_blocked_) {
1126 if (!blocked_contents_)
1127 blocked_contents_ = new BlockedContentContainer(this);
1128 blocked_contents_->AddTabContents(
1129 new_contents, disposition, initial_pos, user_gesture);
1130 return;
1131 }
1132
initial.commit09911bf2008-07-26 23:55:291133 if (!delegate_)
1134 return;
1135
[email protected]35f7d212009-04-29 21:19:271136 if ((disposition == NEW_POPUP) && !user_gesture &&
1137 !CommandLine::ForCurrentProcess()->HasSwitch(
1138 switches::kDisablePopupBlocking)) {
[email protected]2c4410d2009-05-06 23:46:221139 // Unrequested popups from normal pages are constrained unless they're in
[email protected]b6c874582009-05-08 19:38:311140 // the whitelist. The popup owner will handle checking this.
[email protected]7e7414ae2010-01-26 20:19:291141 delegate_->GetConstrainingContents(this)->AddPopup(
1142 new_contents, initial_pos);
[email protected]b6c874582009-05-08 19:38:311143 } else {
[email protected]0aa55312008-10-17 21:53:081144 new_contents->DisassociateFromPopupCount();
initial.commit09911bf2008-07-26 23:55:291145 delegate_->AddNewContents(this, new_contents, disposition, initial_pos,
1146 user_gesture);
[email protected]7f37dd92009-09-15 16:35:491147 NotificationService::current()->Notify(
1148 NotificationType::TAB_ADDED,
1149 Source<TabContentsDelegate>(delegate_),
1150 Details<TabContents>(this));
initial.commit09911bf2008-07-26 23:55:291151 }
initial.commit09911bf2008-07-26 23:55:291152
[email protected]8cc8d492010-02-02 10:40:491153 // TODO(pkasting): Why is this necessary?
[email protected]c65e2f152010-10-14 15:30:401154 PopupNotificationVisibilityChanged(blocked_contents_ != NULL);
initial.commit09911bf2008-07-26 23:55:291155}
1156
[email protected]bcd9dc42010-01-23 16:38:181157bool TabContents::ExecuteCode(int request_id, const std::string& extension_id,
[email protected]20ad2692009-11-20 18:27:201158 bool is_js_code, const std::string& code_string,
1159 bool all_frames) {
[email protected]ebd0eae72010-02-16 23:50:511160 RenderViewHost* host = render_view_host();
1161 if (!host)
1162 return false;
1163
1164 return host->Send(new ViewMsg_ExecuteCode(host->routing_id(),
[email protected]be7e5cb2010-10-04 12:53:171165 ViewMsg_ExecuteCode_Params(request_id, extension_id,
[email protected]ebd0eae72010-02-16 23:50:511166 is_js_code, code_string, all_frames)));
[email protected]912256b32009-09-18 09:47:351167}
1168
[email protected]96d185d2009-04-24 03:28:541169void TabContents::PopupNotificationVisibilityChanged(bool visible) {
[email protected]8cc8d492010-02-02 10:40:491170 if (is_being_destroyed_)
1171 return;
[email protected]9fb83e82010-07-02 18:24:551172 content_settings_delegate_->SetPopupsBlocked(visible);
[email protected]8cc8d492010-02-02 10:40:491173 if (!dont_notify_render_view_)
[email protected]e8345242010-05-06 03:00:401174 render_view_host()->AllowScriptToClose(!visible);
[email protected]96d185d2009-04-24 03:28:541175}
1176
[email protected]5c9e97a2009-09-09 23:48:301177gfx::NativeView TabContents::GetContentNativeView() const {
[email protected]96d185d2009-04-24 03:28:541178 return view_->GetContentNativeView();
1179}
1180
1181gfx::NativeView TabContents::GetNativeView() const {
1182 return view_->GetNativeView();
1183}
1184
1185void TabContents::GetContainerBounds(gfx::Rect *out) const {
1186 view_->GetContainerBounds(out);
1187}
1188
1189void TabContents::Focus() {
1190 view_->Focus();
1191}
1192
[email protected]90daadb42009-06-08 21:27:281193void TabContents::FocusThroughTabTraversal(bool reverse) {
[email protected]7e383692009-06-12 19:14:541194 if (showing_interstitial_page()) {
1195 render_manager_.interstitial_page()->FocusThroughTabTraversal(reverse);
1196 return;
1197 }
[email protected]96d185d2009-04-24 03:28:541198 render_view_host()->SetInitialFocus(reverse);
1199}
1200
1201bool TabContents::FocusLocationBarByDefault() {
1202 DOMUI* dom_ui = GetDOMUIForCurrentState();
1203 if (dom_ui)
1204 return dom_ui->focus_location_bar_by_default();
[email protected]3e3f0eb2009-06-22 18:33:431205 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]e0d481582009-09-15 21:06:251206 if (entry && entry->url() == GURL(chrome::kAboutBlankURL))
[email protected]3e3f0eb2009-06-22 18:33:431207 return true;
[email protected]96d185d2009-04-24 03:28:541208 return false;
1209}
1210
[email protected]a26dc362010-04-23 01:48:581211void TabContents::SetFocusToLocationBar(bool select_all) {
[email protected]a11aaf22010-03-30 00:03:381212 if (delegate())
[email protected]a26dc362010-04-23 01:48:581213 delegate()->SetFocusToLocationBar(select_all);
[email protected]a11aaf22010-03-30 00:03:381214}
1215
[email protected]616ed5a2008-11-21 22:27:241216void TabContents::AddInfoBar(InfoBarDelegate* delegate) {
[email protected]f7019302010-03-26 19:58:321217 if (delegate_ && !delegate_->infobars_enabled()) {
1218 delegate->InfoBarClosed();
1219 return;
1220 }
1221
[email protected]616ed5a2008-11-21 22:27:241222 // Look through the existing InfoBarDelegates we have for a match. If we've
1223 // already got one that matches, then we don't add the new one.
[email protected]f86a07022008-11-25 01:06:051224 for (int i = 0; i < infobar_delegate_count(); ++i) {
[email protected]6d27a7b2009-12-18 23:25:451225 if (GetInfoBarDelegateAt(i)->EqualsDelegate(delegate)) {
1226 // Tell the new infobar to close so that it can clean itself up.
1227 delegate->InfoBarClosed();
[email protected]616ed5a2008-11-21 22:27:241228 return;
[email protected]6d27a7b2009-12-18 23:25:451229 }
[email protected]616ed5a2008-11-21 22:27:241230 }
1231
1232 infobar_delegates_.push_back(delegate);
[email protected]bfd04a62009-02-01 18:16:561233 NotificationService::current()->Notify(
[email protected]f4f50ef2011-01-21 19:01:191234 NotificationType::TAB_CONTENTS_INFOBAR_ADDED, Source<TabContents>(this),
[email protected]bfd04a62009-02-01 18:16:561235 Details<InfoBarDelegate>(delegate));
[email protected]616ed5a2008-11-21 22:27:241236
1237 // Add ourselves as an observer for navigations the first time a delegate is
1238 // added. We use this notification to expire InfoBars that need to expire on
1239 // page transitions.
1240 if (infobar_delegates_.size() == 1) {
[email protected]bfd04a62009-02-01 18:16:561241 registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED,
[email protected]ce3fa3c2009-04-20 19:55:571242 Source<NavigationController>(&controller_));
[email protected]616ed5a2008-11-21 22:27:241243 }
1244}
1245
1246void TabContents::RemoveInfoBar(InfoBarDelegate* delegate) {
[email protected]f7019302010-03-26 19:58:321247 if (delegate_ && !delegate_->infobars_enabled()) {
1248 return;
1249 }
1250
[email protected]616ed5a2008-11-21 22:27:241251 std::vector<InfoBarDelegate*>::iterator it =
1252 find(infobar_delegates_.begin(), infobar_delegates_.end(), delegate);
1253 if (it != infobar_delegates_.end()) {
1254 InfoBarDelegate* delegate = *it;
[email protected]bfd04a62009-02-01 18:16:561255 NotificationService::current()->Notify(
1256 NotificationType::TAB_CONTENTS_INFOBAR_REMOVED,
1257 Source<TabContents>(this),
1258 Details<InfoBarDelegate>(delegate));
[email protected]616ed5a2008-11-21 22:27:241259
[email protected]f4f50ef2011-01-21 19:01:191260 infobar_delegates_.erase(it);
1261 // Remove ourselves as an observer if we are tracking no more InfoBars.
1262 if (infobar_delegates_.empty()) {
1263 registrar_.Remove(this, NotificationType::NAV_ENTRY_COMMITTED,
1264 Source<NavigationController>(&controller_));
[email protected]6a02963e2009-01-06 16:58:031265 }
[email protected]616ed5a2008-11-21 22:27:241266 }
1267}
[email protected]3edd9522009-03-04 22:19:411268
[email protected]f81821c2009-08-07 01:29:471269void TabContents::ReplaceInfoBar(InfoBarDelegate* old_delegate,
1270 InfoBarDelegate* new_delegate) {
[email protected]f7019302010-03-26 19:58:321271 if (delegate_ && !delegate_->infobars_enabled()) {
1272 new_delegate->InfoBarClosed();
1273 return;
1274 }
1275
[email protected]f81821c2009-08-07 01:29:471276 std::vector<InfoBarDelegate*>::iterator it =
1277 find(infobar_delegates_.begin(), infobar_delegates_.end(), old_delegate);
1278 DCHECK(it != infobar_delegates_.end());
1279
1280 // Notify the container about the change of plans.
1281 scoped_ptr<std::pair<InfoBarDelegate*, InfoBarDelegate*> > details(
1282 new std::pair<InfoBarDelegate*, InfoBarDelegate*>(
1283 old_delegate, new_delegate));
1284 NotificationService::current()->Notify(
1285 NotificationType::TAB_CONTENTS_INFOBAR_REPLACED,
1286 Source<TabContents>(this),
1287 Details<std::pair<InfoBarDelegate*, InfoBarDelegate*> >(details.get()));
1288
[email protected]f4f50ef2011-01-21 19:01:191289 // Remove the old one.
1290 infobar_delegates_.erase(it);
[email protected]f81821c2009-08-07 01:29:471291
1292 // Add the new one.
[email protected]cdadf6f2010-08-20 16:36:081293 DCHECK(find(infobar_delegates_.begin(),
1294 infobar_delegates_.end(), new_delegate) ==
1295 infobar_delegates_.end());
[email protected]f81821c2009-08-07 01:29:471296 infobar_delegates_.push_back(new_delegate);
1297}
1298
[email protected]20ede072009-10-06 00:28:351299bool TabContents::ShouldShowBookmarkBar() {
[email protected]aa9196f2009-10-22 16:20:271300 if (showing_interstitial_page())
1301 return false;
1302
[email protected]c8f74942010-09-03 10:04:141303 // Do not show bookmarks bar if bookmarks aren't enabled.
1304 if (!browser_defaults::bookmarks_enabled)
1305 return false;
1306
[email protected]96d185d2009-04-24 03:28:541307 // See GetDOMUIForCurrentState() comment for more info. This case is very
1308 // similar, but for non-first loads, we want to use the committed entry. This
1309 // is so the bookmarks bar disappears at the same time the page does.
1310 if (controller_.GetLastCommittedEntry()) {
1311 // Not the first load, always use the committed DOM UI.
[email protected]d6219912010-11-17 19:47:371312 return (render_manager_.dom_ui() == NULL) ?
1313 false : render_manager_.dom_ui()->force_bookmark_bar_visible();
[email protected]96d185d2009-04-24 03:28:541314 }
1315
1316 // When it's the first load, we know either the pending one or the committed
1317 // one will have the DOM UI in it (see GetDOMUIForCurrentState), and only one
1318 // of them will be valid, so we can just check both.
1319 if (render_manager_.pending_dom_ui())
1320 return render_manager_.pending_dom_ui()->force_bookmark_bar_visible();
[email protected]d6219912010-11-17 19:47:371321 return (render_manager_.dom_ui() == NULL) ?
1322 false : render_manager_.dom_ui()->force_bookmark_bar_visible();
[email protected]96d185d2009-04-24 03:28:541323}
1324
[email protected]3edd9522009-03-04 22:19:411325void TabContents::ToolbarSizeChanged(bool is_animating) {
1326 TabContentsDelegate* d = delegate();
1327 if (d)
1328 d->ToolbarSizeChanged(this, is_animating);
1329}
[email protected]616ed5a2008-11-21 22:27:241330
[email protected]fc6fb7fb2009-11-07 02:35:041331bool TabContents::CanDownload(int request_id) {
1332 TabContentsDelegate* d = delegate();
1333 if (d)
1334 return d->CanDownload(request_id);
1335 return true;
1336}
1337
[email protected]d5f942ba2008-09-26 19:30:341338void TabContents::OnStartDownload(DownloadItem* download) {
1339 DCHECK(download);
[email protected]d5f942ba2008-09-26 19:30:341340
1341 // Download in a constrained popup is shown in the tab that opened it.
[email protected]b6c874582009-05-08 19:38:311342 TabContents* tab_contents = delegate()->GetConstrainingContents(this);
[email protected]d5f942ba2008-09-26 19:30:341343
[email protected]5b928b62010-08-03 11:46:511344 if (tab_contents && tab_contents->delegate())
1345 tab_contents->delegate()->OnStartDownload(download, this);
[email protected]3edd9522009-03-04 22:19:411346}
[email protected]3edd9522009-03-04 22:19:411347
[email protected]d5f942ba2008-09-26 19:30:341348void TabContents::WillClose(ConstrainedWindow* window) {
[email protected]d6219912010-11-17 19:47:371349 ConstrainedWindowList::iterator i(
1350 std::find(child_windows_.begin(), child_windows_.end(), window));
1351 bool removed_topmost_window = i == child_windows_.begin();
1352 if (i != child_windows_.end())
1353 child_windows_.erase(i);
1354 if (child_windows_.empty()) {
[email protected]fa1cf0b82010-01-15 21:49:441355 BlockTabContent(false);
[email protected]d6219912010-11-17 19:47:371356 } else {
1357 if (removed_topmost_window)
1358 child_windows_[0]->ShowConstrainedWindow();
1359 BlockTabContent(true);
[email protected]fa1cf0b82010-01-15 21:49:441360 }
[email protected]66ba4932009-06-04 19:22:131361}
[email protected]d5f942ba2008-09-26 19:30:341362
[email protected]c65e2f152010-10-14 15:30:401363void TabContents::WillCloseBlockedContentContainer(
1364 BlockedContentContainer* container) {
1365 DCHECK(blocked_contents_ == container);
1366 blocked_contents_ = NULL;
[email protected]8cc8d492010-02-02 10:40:491367 PopupNotificationVisibilityChanged(false);
[email protected]d5f942ba2008-09-26 19:30:341368}
1369
[email protected]d5f942ba2008-09-26 19:30:341370void TabContents::DidMoveOrResize(ConstrainedWindow* window) {
[email protected]332af7732009-03-11 13:21:351371#if defined(OS_WIN)
[email protected]92edc472009-02-10 20:32:061372 UpdateWindow(GetNativeView());
[email protected]332af7732009-03-11 13:21:351373#endif
[email protected]d5f942ba2008-09-26 19:30:341374}
1375
[email protected]c1dc9f132009-11-20 23:13:211376void TabContents::StartFinding(string16 search_string,
[email protected]e491f1c2009-05-22 20:28:121377 bool forward_direction,
1378 bool case_sensitive) {
[email protected]c1dc9f132009-11-20 23:13:211379 // If search_string is empty, it means FindNext was pressed with a keyboard
[email protected]6e95706d2009-04-23 22:55:011380 // shortcut so unless we have something to search for we return early.
[email protected]c1dc9f132009-11-20 23:13:211381 if (search_string.empty() && find_text_.empty()) {
[email protected]e5d2f7132010-04-09 18:13:531382 string16 last_search_prepopulate_text =
1383 FindBarState::GetLastPrepopulateText(profile());
1384
[email protected]87fae512009-12-11 02:20:211385 // Try the last thing we searched for on this tab, then the last thing
1386 // searched for on any tab.
1387 if (!previous_find_text_.empty())
1388 search_string = previous_find_text_;
[email protected]e5d2f7132010-04-09 18:13:531389 else if (!last_search_prepopulate_text.empty())
1390 search_string = last_search_prepopulate_text;
[email protected]87fae512009-12-11 02:20:211391 else
[email protected]e491f1c2009-05-22 20:28:121392 return;
[email protected]e491f1c2009-05-22 20:28:121393 }
[email protected]6e95706d2009-04-23 22:55:011394
[email protected]101c90e2009-10-26 21:14:441395 // Keep track of the previous search.
1396 previous_find_text_ = find_text_;
1397
[email protected]6e95706d2009-04-23 22:55:011398 // This is a FindNext operation if we are searching for the same text again,
1399 // or if the passed in search text is empty (FindNext keyboard shortcut). The
1400 // exception to this is if the Find was aborted (then we don't want FindNext
1401 // because the highlighting has been cleared and we need it to reappear). We
1402 // therefore treat FindNext after an aborted Find operation as a full fledged
1403 // Find.
[email protected]c1dc9f132009-11-20 23:13:211404 bool find_next = (find_text_ == search_string || search_string.empty()) &&
[email protected]e491f1c2009-05-22 20:28:121405 (last_search_case_sensitive_ == case_sensitive) &&
[email protected]6e95706d2009-04-23 22:55:011406 !find_op_aborted_;
1407 if (!find_next)
1408 current_find_request_id_ = find_request_id_counter_++;
1409
[email protected]c1dc9f132009-11-20 23:13:211410 if (!search_string.empty())
1411 find_text_ = search_string;
[email protected]e491f1c2009-05-22 20:28:121412 last_search_case_sensitive_ = case_sensitive;
[email protected]6e95706d2009-04-23 22:55:011413
1414 find_op_aborted_ = false;
1415
1416 // Keep track of what the last search was across the tabs.
[email protected]e5d2f7132010-04-09 18:13:531417 FindBarState* find_bar_state = profile()->GetFindBarState();
1418 find_bar_state->set_last_prepopulate_text(find_text_);
[email protected]6e95706d2009-04-23 22:55:011419 render_view_host()->StartFinding(current_find_request_id_,
1420 find_text_,
1421 forward_direction,
[email protected]e491f1c2009-05-22 20:28:121422 case_sensitive,
[email protected]6e95706d2009-04-23 22:55:011423 find_next);
1424}
1425
[email protected]5bc8fe92010-03-11 18:19:001426void TabContents::StopFinding(
1427 FindBarController::SelectionAction selection_action) {
[email protected]d6e8d332010-06-04 22:26:011428 if (selection_action == FindBarController::kClearSelection) {
1429 // kClearSelection means the find string has been cleared by the user, but
1430 // the UI has not been dismissed. In that case we want to clear the
1431 // previously remembered search (http://crbug.com/42639).
1432 previous_find_text_ = string16();
1433 } else {
[email protected]2d2dffc2009-06-09 21:41:001434 find_ui_active_ = false;
[email protected]d6e8d332010-06-04 22:26:011435 if (!find_text_.empty())
1436 previous_find_text_ = find_text_;
1437 }
[email protected]101c90e2009-10-26 21:14:441438 find_text_.clear();
[email protected]6e95706d2009-04-23 22:55:011439 find_op_aborted_ = true;
[email protected]e491f1c2009-05-22 20:28:121440 last_search_result_ = FindNotificationDetails();
[email protected]5bc8fe92010-03-11 18:19:001441 render_view_host()->StopFinding(selection_action);
[email protected]6e95706d2009-04-23 22:55:011442}
1443
[email protected]420ae012009-04-24 05:16:321444void TabContents::OnSavePage() {
1445 // If we can not save the page, try to download it.
1446 if (!SavePackage::IsSavableContents(contents_mime_type())) {
1447 DownloadManager* dlm = profile()->GetDownloadManager();
1448 const GURL& current_page_url = GetURL();
1449 if (dlm && current_page_url.is_valid())
[email protected]d82ed61e2009-06-16 02:46:221450 dlm->DownloadUrl(current_page_url, GURL(), "", this);
[email protected]420ae012009-04-24 05:16:321451 return;
1452 }
1453
1454 Stop();
1455
1456 // Create the save package and possibly prompt the user for the name to save
1457 // the page as. The user prompt is an asynchronous operation that runs on
1458 // another thread.
[email protected]d82ed61e2009-06-16 02:46:221459 save_package_ = new SavePackage(this);
[email protected]420ae012009-04-24 05:16:321460 save_package_->GetSaveInfo();
1461}
1462
1463// Used in automated testing to bypass prompting the user for file names.
1464// Instead, the names and paths are hard coded rather than running them through
1465// file name sanitation and extension / mime checking.
[email protected]828cabe2009-09-26 22:47:111466bool TabContents::SavePage(const FilePath& main_file, const FilePath& dir_path,
[email protected]420ae012009-04-24 05:16:321467 SavePackage::SavePackageType save_type) {
1468 // Stop the page from navigating.
1469 Stop();
1470
[email protected]828cabe2009-09-26 22:47:111471 save_package_ = new SavePackage(this, save_type, main_file, dir_path);
[email protected]cdf6cc82009-08-04 21:48:591472 return save_package_->Init();
[email protected]420ae012009-04-24 05:16:321473}
1474
[email protected]89d6e6e2009-12-03 23:06:511475void TabContents::EmailPageLocation() {
1476 std::string title = EscapeQueryParamValue(UTF16ToUTF8(GetTitle()), false);
1477 std::string page_url = EscapeQueryParamValue(GetURL().spec(), false);
1478 std::string mailto = std::string("mailto:?subject=Fwd:%20") +
1479 title + "&body=%0A%0A" + page_url;
1480 platform_util::OpenExternal(GURL(mailto));
1481}
1482
[email protected]420ae012009-04-24 05:16:321483void TabContents::PrintPreview() {
[email protected]dbeebd52010-11-16 20:34:161484 if (CommandLine::ForCurrentProcess()->HasSwitch(
1485 switches::kEnablePrintPreview)) {
1486 if (showing_interstitial_page())
1487 return;
1488 render_view_host()->PrintPreview();
1489 } else {
1490 PrintNow();
1491 }
[email protected]420ae012009-04-24 05:16:321492}
1493
1494bool TabContents::PrintNow() {
1495 // We can't print interstitial page for now.
1496 if (showing_interstitial_page())
1497 return false;
1498
1499 return render_view_host()->PrintPages();
1500}
1501
[email protected]82270452009-06-19 15:58:011502void TabContents::PrintingDone(int document_cookie, bool success) {
1503 render_view_host()->PrintingDone(document_cookie, success);
1504}
1505
[email protected]420ae012009-04-24 05:16:321506bool TabContents::IsActiveEntry(int32 page_id) {
1507 NavigationEntry* active_entry = controller_.GetActiveEntry();
1508 return (active_entry != NULL &&
1509 active_entry->site_instance() == GetSiteInstance() &&
1510 active_entry->page_id() == page_id);
1511}
1512
[email protected]b2fe07d12010-02-09 14:38:081513void TabContents::SetOverrideEncoding(const std::string& encoding) {
[email protected]8cb5d5b2010-02-09 11:36:161514 set_encoding(encoding);
1515 render_view_host()->SetPageEncoding(encoding);
1516}
1517
[email protected]b2fe07d12010-02-09 14:38:081518void TabContents::ResetOverrideEncoding() {
[email protected]8cb5d5b2010-02-09 11:36:161519 reset_encoding();
1520 render_view_host()->ResetPageEncodingToDefault();
1521}
1522
1523void TabContents::WindowMoveOrResizeStarted() {
1524 render_view_host()->WindowMoveOrResizeStarted();
1525}
1526
[email protected]c65e2f152010-10-14 15:30:401527void TabContents::SetAllContentsBlocked(bool value) {
1528 if (all_contents_blocked_ == value)
1529 return;
1530
1531 all_contents_blocked_ = value;
1532 if (!all_contents_blocked_ && blocked_contents_) {
1533 std::vector<TabContents*> blocked;
1534 blocked_contents_->GetBlockedContents(&blocked);
1535 for (size_t i = 0; i < blocked.size(); ++i)
1536 blocked_contents_->LaunchForContents(blocked[i]);
1537 }
1538}
1539
[email protected]3f8eb7f2009-10-02 23:58:051540void TabContents::LogNewTabTime(const std::string& event_name) {
1541 // Not all new tab pages get timed. In those cases, we don't have a
1542 // new_tab_start_time_.
1543 if (new_tab_start_time_.is_null())
1544 return;
1545
1546 base::TimeDelta duration = base::TimeTicks::Now() - new_tab_start_time_;
[email protected]84abba62009-10-07 17:01:441547 MetricEventDurationDetails details(event_name,
1548 static_cast<int>(duration.InMilliseconds()));
1549
[email protected]7ab1e7d62009-10-14 23:32:011550 if (event_name == "Tab.NewTabScriptStart") {
1551 UMA_HISTOGRAM_TIMES("Tab.NewTabScriptStart", duration);
1552 } else if (event_name == "Tab.NewTabDOMContentLoaded") {
1553 UMA_HISTOGRAM_TIMES("Tab.NewTabDOMContentLoaded", duration);
1554 } else if (event_name == "Tab.NewTabOnload") {
1555 UMA_HISTOGRAM_TIMES("Tab.NewTabOnload", duration);
[email protected]3f8eb7f2009-10-02 23:58:051556 // The new tab page has finished loading; reset it.
1557 new_tab_start_time_ = base::TimeTicks();
[email protected]84abba62009-10-07 17:01:441558 } else {
1559 NOTREACHED();
[email protected]3f8eb7f2009-10-02 23:58:051560 }
[email protected]84abba62009-10-07 17:01:441561 NotificationService::current()->Notify(
1562 NotificationType::METRIC_EVENT_DURATION,
1563 Source<TabContents>(this),
1564 Details<MetricEventDurationDetails>(&details));
[email protected]3f8eb7f2009-10-02 23:58:051565}
1566
[email protected]7ab1e7d62009-10-14 23:32:011567void TabContents::OnCloseStarted() {
1568 if (tab_close_start_time_.is_null())
1569 tab_close_start_time_ = base::TimeTicks::Now();
1570}
1571
[email protected]46624bf2010-06-09 16:04:191572bool TabContents::ShouldAcceptDragAndDrop() const {
1573#if defined(OS_CHROMEOS)
1574 // ChromeOS panels (pop-ups) do not take drag-n-drop.
1575 // See http://crosbug.com/2413
[email protected]b83af492010-10-09 03:21:201576 if (delegate() && delegate()->IsPopup(this))
1577 return false;
1578 return true;
[email protected]46624bf2010-06-09 16:04:191579#else
1580 return true;
1581#endif
1582}
1583
[email protected]ec0b6c42010-08-26 03:16:581584void TabContents::UpdateHistoryForNavigation(
1585 scoped_refptr<history::HistoryAddPageArgs> add_page_args) {
1586 if (profile()->IsOffTheRecord())
1587 return;
1588
1589 // Add to history service.
1590 HistoryService* hs = profile()->GetHistoryService(Profile::IMPLICIT_ACCESS);
1591 if (hs)
1592 hs->AddPage(*add_page_args);
1593}
1594
1595void TabContents::UpdateHistoryPageTitle(const NavigationEntry& entry) {
1596 if (profile()->IsOffTheRecord())
1597 return;
1598
1599 HistoryService* hs = profile()->GetHistoryService(Profile::IMPLICIT_ACCESS);
1600 if (hs)
1601 hs->SetPageTitle(entry.virtual_url(), entry.title());
1602}
1603
[email protected]d0b8d092010-10-25 04:05:171604double TabContents::GetZoomLevel() const {
[email protected]b75b8292010-10-01 07:28:251605 HostZoomMap* zoom_map = profile()->GetHostZoomMap();
1606 if (!zoom_map)
[email protected]d0b8d092010-10-25 04:05:171607 return 0;
[email protected]b75b8292010-10-01 07:28:251608
1609 double zoom_level;
1610 if (temporary_zoom_settings_) {
1611 zoom_level = zoom_map->GetTemporaryZoomLevel(
1612 GetRenderProcessHost()->id(), render_view_host()->routing_id());
1613 } else {
1614 zoom_level = zoom_map->GetZoomLevel(GetURL());
1615 }
[email protected]d0b8d092010-10-25 04:05:171616 return zoom_level;
1617}
[email protected]b75b8292010-10-01 07:28:251618
[email protected]d0b8d092010-10-25 04:05:171619int TabContents::GetZoomPercent(bool* enable_increment,
1620 bool* enable_decrement) {
1621 *enable_decrement = *enable_increment = false;
[email protected]b75b8292010-10-01 07:28:251622 int percent = static_cast<int>(
[email protected]d0b8d092010-10-25 04:05:171623 WebKit::WebView::zoomLevelToZoomFactor(GetZoomLevel()) * 100);
[email protected]b75b8292010-10-01 07:28:251624 *enable_decrement = percent > minimum_zoom_percent_;
1625 *enable_increment = percent < maximum_zoom_percent_;
1626 return percent;
1627}
1628
[email protected]3c733bde2010-12-21 19:56:311629void TabContents::ViewSource() {
[email protected]1788e772010-12-15 16:40:501630 if (!delegate_)
1631 return;
1632
1633 NavigationEntry* active_entry = controller().GetActiveEntry();
1634 if (!active_entry)
1635 return;
1636
1637 delegate_->ViewSourceForTab(this, active_entry->url());
[email protected]77d8d622010-12-15 10:30:121638}
1639
[email protected]724159a2010-12-30 01:11:181640void TabContents::OnDidStartProvisionalLoadForFrame(int64 frame_id,
1641 bool is_main_frame,
1642 const GURL& url) {
1643 bool is_error_page = (url.spec() == chrome::kUnreachableWebDataURL);
1644 GURL validated_url(url);
1645 render_view_host()->FilterURL(ChildProcessSecurityPolicy::GetInstance(),
1646 GetRenderProcessHost()->id(), &validated_url);
1647
1648 ProvisionalLoadDetails details(
1649 is_main_frame,
1650 controller_.IsURLInPageNavigation(validated_url),
1651 validated_url, std::string(), false, is_error_page, frame_id);
1652 NotificationService::current()->Notify(
1653 NotificationType::FRAME_PROVISIONAL_LOAD_START,
1654 Source<NavigationController>(&controller_),
1655 Details<ProvisionalLoadDetails>(&details));
1656 if (is_main_frame) {
1657 // If we're displaying a network error page do not reset the content
1658 // settings delegate's cookies so the user has a chance to modify cookie
1659 // settings.
1660 if (!is_error_page)
1661 content_settings_delegate_->ClearCookieSpecificContentSettings();
1662 content_settings_delegate_->ClearGeolocationContentSettings();
[email protected]e7d50892011-01-19 21:47:381663
1664 // Check if the URL we are about to load has been prerendered by any chance,
1665 // and use it if possible.
1666 MaybeUsePreloadedPage(url);
[email protected]724159a2010-12-30 01:11:181667 }
1668}
1669
1670void TabContents::OnDidRedirectProvisionalLoad(int32 page_id,
1671 const GURL& source_url,
1672 const GURL& target_url) {
1673 NavigationEntry* entry;
1674 if (page_id == -1)
1675 entry = controller_.pending_entry();
1676 else
1677 entry = controller_.GetEntryWithPageID(GetSiteInstance(), page_id);
1678 if (!entry || entry->url() != source_url)
1679 return;
1680 entry->set_url(target_url);
[email protected]e7d50892011-01-19 21:47:381681
1682 // Check if the URL we are about to load has been prerendered by any chance,
1683 // and use it if possible.
1684 MaybeUsePreloadedPage(target_url);
[email protected]724159a2010-12-30 01:11:181685}
1686
1687void TabContents::OnDidFailProvisionalLoadWithError(
1688 int64 frame_id,
1689 bool is_main_frame,
1690 int error_code,
1691 const GURL& url,
1692 bool showing_repost_interstitial) {
1693 VLOG(1) << "Failed Provisional Load: " << url.possibly_invalid_spec()
1694 << ", error_code: " << error_code
1695 << " is_main_frame: " << is_main_frame
1696 << " showing_repost_interstitial: " << showing_repost_interstitial
1697 << " frame_id: " << frame_id;
1698 GURL validated_url(url);
1699 render_view_host()->FilterURL(ChildProcessSecurityPolicy::GetInstance(),
1700 GetRenderProcessHost()->id(), &validated_url);
1701
1702 if (net::ERR_ABORTED == error_code) {
1703 // EVIL HACK ALERT! Ignore failed loads when we're showing interstitials.
1704 // This means that the interstitial won't be torn down properly, which is
1705 // bad. But if we have an interstitial, go back to another tab type, and
1706 // then load the same interstitial again, we could end up getting the first
1707 // interstitial's "failed" message (as a result of the cancel) when we're on
1708 // the second one.
1709 //
1710 // We can't tell this apart, so we think we're tearing down the current page
1711 // which will cause a crash later one. There is also some code in
1712 // RenderViewHostManager::RendererAbortedProvisionalLoad that is commented
1713 // out because of this problem.
1714 //
1715 // http://code.google.com/p/chromium/issues/detail?id=2855
1716 // Because this will not tear down the interstitial properly, if "back" is
1717 // back to another tab type, the interstitial will still be somewhat alive
1718 // in the previous tab type. If you navigate somewhere that activates the
1719 // tab with the interstitial again, you'll see a flash before the new load
1720 // commits of the interstitial page.
1721 if (showing_interstitial_page()) {
1722 LOG(WARNING) << "Discarding message during interstitial.";
1723 return;
1724 }
1725
1726 // This will discard our pending entry if we cancelled the load (e.g., if we
1727 // decided to download the file instead of load it). Only discard the
1728 // pending entry if the URLs match, otherwise the user initiated a navigate
1729 // before the page loaded so that the discard would discard the wrong entry.
1730 NavigationEntry* pending_entry = controller_.pending_entry();
1731 if (pending_entry && pending_entry->url() == validated_url) {
1732 controller_.DiscardNonCommittedEntries();
1733 // Update the URL display.
1734 NotifyNavigationStateChanged(TabContents::INVALIDATE_URL);
1735 }
1736
1737 render_manager_.RendererAbortedProvisionalLoad(render_view_host());
1738 }
1739
1740 // Send out a notification that we failed a provisional load with an error.
1741 ProvisionalLoadDetails details(
1742 is_main_frame, controller_.IsURLInPageNavigation(validated_url),
1743 validated_url, std::string(), false, false, frame_id);
1744 details.set_error_code(error_code);
1745
1746 NotificationService::current()->Notify(
1747 NotificationType::FAIL_PROVISIONAL_LOAD_WITH_ERROR,
1748 Source<NavigationController>(&controller_),
1749 Details<ProvisionalLoadDetails>(&details));
1750}
1751
1752void TabContents::OnDidLoadResourceFromMemoryCache(
1753 const GURL& url,
1754 const std::string& frame_origin,
1755 const std::string& main_frame_origin,
1756 const std::string& security_info) {
1757 static base::StatsCounter cache("WebKit.CacheHit");
1758 cache.Increment();
1759
1760 // Send out a notification that we loaded a resource from our memory cache.
1761 int cert_id = 0, cert_status = 0, security_bits = -1, connection_status = 0;
1762 SSLManager::DeserializeSecurityInfo(security_info,
1763 &cert_id, &cert_status,
1764 &security_bits,
1765 &connection_status);
1766 LoadFromMemoryCacheDetails details(url, frame_origin, main_frame_origin,
1767 GetRenderProcessHost()->id(), cert_id,
1768 cert_status);
1769
1770 NotificationService::current()->Notify(
1771 NotificationType::LOAD_FROM_MEMORY_CACHE,
1772 Source<NavigationController>(&controller_),
1773 Details<LoadFromMemoryCacheDetails>(&details));
1774}
1775
1776void TabContents::OnDidDisplayInsecureContent() {
1777 displayed_insecure_content_ = true;
1778 SSLManager::NotifySSLInternalStateChanged();
1779}
1780
1781void TabContents::OnDidRunInsecureContent(
[email protected]92771112011-01-20 00:13:021782 const std::string& security_origin, const GURL& target_url) {
[email protected]724159a2010-12-30 01:11:181783 controller_.ssl_manager()->DidRunInsecureContent(security_origin);
1784}
1785
1786void TabContents::OnDocumentLoadedInFrame(int64 frame_id) {
1787 controller_.DocumentLoadedInFrame();
1788 NotificationService::current()->Notify(
1789 NotificationType::FRAME_DOM_CONTENT_LOADED,
1790 Source<NavigationController>(&controller_),
1791 Details<int64>(&frame_id));
1792}
1793
1794void TabContents::OnDidFinishLoad(int64 frame_id) {
1795 NotificationService::current()->Notify(
1796 NotificationType::FRAME_DID_FINISH_LOAD,
1797 Source<NavigationController>(&controller_),
1798 Details<int64>(&frame_id));
1799}
1800
[email protected]c8f73ab2011-01-22 00:05:171801void TabContents::OnUpdateContentRestrictions(int restrictions) {
1802 content_restrictions_ = restrictions;
1803 delegate()->ContentRestrictionsChanged(this);
1804}
1805
1806void TabContents::OnPDFHasUnsupportedFeature() {
1807 PDFHasUnsupportedFeature(this);
1808}
1809
[email protected]96d185d2009-04-24 03:28:541810// Notifies the RenderWidgetHost instance about the fact that the page is
1811// loading, or done loading and calls the base implementation.
1812void TabContents::SetIsLoading(bool is_loading,
1813 LoadNotificationDetails* details) {
1814 if (is_loading == is_loading_)
1815 return;
1816
1817 if (!is_loading) {
1818 load_state_ = net::LOAD_STATE_IDLE;
1819 load_state_host_.clear();
[email protected]094e5b22009-09-25 04:23:561820 upload_size_ = 0;
1821 upload_position_ = 0;
[email protected]96d185d2009-04-24 03:28:541822 }
1823
1824 render_manager_.SetIsLoading(is_loading);
1825
1826 is_loading_ = is_loading;
1827 waiting_for_response_ = is_loading;
1828
[email protected]6ebdc9b2010-09-27 16:55:571829 if (delegate_)
1830 delegate_->LoadingStateChanged(this);
[email protected]329581b2009-04-28 06:52:351831 NotifyNavigationStateChanged(INVALIDATE_LOAD);
[email protected]96d185d2009-04-24 03:28:541832
1833 NotificationType type = is_loading ? NotificationType::LOAD_START :
1834 NotificationType::LOAD_STOP;
[email protected]ce5c4502009-05-06 16:46:111835 NotificationDetails det = NotificationService::NoDetails();
[email protected]96d185d2009-04-24 03:28:541836 if (details)
1837 det = Details<LoadNotificationDetails>(details);
1838 NotificationService::current()->Notify(type,
1839 Source<NavigationController>(&controller_),
1840 det);
1841}
1842
[email protected]b6c874582009-05-08 19:38:311843void TabContents::AddPopup(TabContents* new_contents,
[email protected]7e7414ae2010-01-26 20:19:291844 const gfx::Rect& initial_pos) {
[email protected]57eda82a2010-05-05 21:28:061845 // A page can't spawn popups (or do anything else, either) until its load
1846 // commits, so when we reach here, the popup was spawned by the
1847 // NavigationController's last committed entry, not the active entry. For
1848 // example, if a page opens a popup in an onunload() handler, then the active
1849 // entry is the page to be loaded as we navigate away from the unloading
1850 // page. For this reason, we can't use GetURL() to get the opener URL,
1851 // because it returns the active entry.
1852 NavigationEntry* entry = controller_.GetLastCommittedEntry();
1853 GURL creator = entry ? entry->virtual_url() : GURL::EmptyGURL();
1854
1855 if (creator.is_valid() &&
[email protected]8cc8d492010-02-02 10:40:491856 profile()->GetHostContentSettingsMap()->GetContentSetting(
[email protected]ca352452010-08-06 11:14:091857 creator, CONTENT_SETTINGS_TYPE_POPUPS, "") == CONTENT_SETTING_ALLOW) {
[email protected]8cc8d492010-02-02 10:40:491858 AddNewContents(new_contents, NEW_POPUP, initial_pos, true);
1859 } else {
[email protected]c65e2f152010-10-14 15:30:401860 if (!blocked_contents_)
1861 blocked_contents_ = new BlockedContentContainer(this);
1862 blocked_contents_->AddTabContents(new_contents, NEW_POPUP, initial_pos,
1863 true);
[email protected]55126132010-08-19 14:53:281864 content_settings_delegate_->OnContentBlocked(CONTENT_SETTINGS_TYPE_POPUPS,
1865 std::string());
[email protected]8cc8d492010-02-02 10:40:491866 }
[email protected]b9681312008-11-07 00:08:261867}
[email protected]616ed5a2008-11-21 22:27:241868
[email protected]616ed5a2008-11-21 22:27:241869void TabContents::ExpireInfoBars(
1870 const NavigationController::LoadCommittedDetails& details) {
1871 // Only hide InfoBars when the user has done something that makes the main
1872 // frame load. We don't want various automatic or subframe navigations making
1873 // it disappear.
1874 if (!details.is_user_initiated_main_frame_load())
1875 return;
1876
[email protected]f86a07022008-11-25 01:06:051877 for (int i = infobar_delegate_count() - 1; i >= 0; --i) {
[email protected]616ed5a2008-11-21 22:27:241878 InfoBarDelegate* delegate = GetInfoBarDelegateAt(i);
[email protected]f86a07022008-11-25 01:06:051879 if (delegate->ShouldExpire(details))
[email protected]616ed5a2008-11-21 22:27:241880 RemoveInfoBar(delegate);
[email protected]616ed5a2008-11-21 22:27:241881 }
1882}
[email protected]fdd61c62009-04-22 19:22:571883
[email protected]96d185d2009-04-24 03:28:541884DOMUI* TabContents::GetDOMUIForCurrentState() {
1885 // When there is a pending navigation entry, we want to use the pending DOMUI
1886 // that goes along with it to control the basic flags. For example, we want to
1887 // show the pending URL in the URL bar, so we want the display_url flag to
1888 // be from the pending entry.
1889 //
1890 // The confusion comes because there are multiple possibilities for the
1891 // initial load in a tab as a side effect of the way the RenderViewHostManager
1892 // works.
1893 //
1894 // - For the very first tab the load looks "normal". The new tab DOM UI is
1895 // the pending one, and we want it to apply here.
1896 //
1897 // - For subsequent new tabs, they'll get a new SiteInstance which will then
1898 // get switched to the one previously associated with the new tab pages.
1899 // This switching will cause the manager to commit the RVH/DOMUI. So we'll
1900 // have a committed DOM UI in this case.
1901 //
1902 // This condition handles all of these cases:
1903 //
1904 // - First load in first tab: no committed nav entry + pending nav entry +
1905 // pending dom ui:
1906 // -> Use pending DOM UI if any.
1907 //
1908 // - First load in second tab: no committed nav entry + pending nav entry +
1909 // no pending DOM UI:
1910 // -> Use the committed DOM UI if any.
1911 //
1912 // - Second navigation in any tab: committed nav entry + pending nav entry:
1913 // -> Use pending DOM UI if any.
1914 //
1915 // - Normal state with no load: committed nav entry + no pending nav entry:
1916 // -> Use committed DOM UI.
1917 if (controller_.pending_entry() &&
1918 (controller_.GetLastCommittedEntry() ||
1919 render_manager_.pending_dom_ui()))
1920 return render_manager_.pending_dom_ui();
1921 return render_manager_.dom_ui();
1922}
[email protected]420ae012009-04-24 05:16:321923
1924void TabContents::DidNavigateMainFramePostCommit(
1925 const NavigationController::LoadCommittedDetails& details,
1926 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]c2e74fe82009-09-03 17:57:441927 if (opener_dom_ui_type_ != DOMUIFactory::kNoDOMUI) {
1928 // If this is a window.open navigation, use the same DOMUI as the renderer
1929 // that opened the window, as long as both renderers have the same
1930 // privileges.
[email protected]583d45c12010-08-31 02:48:121931 if (opener_dom_ui_type_ ==
1932 DOMUIFactory::GetDOMUIType(profile(), GetURL())) {
[email protected]c2e74fe82009-09-03 17:57:441933 DOMUI* dom_ui = DOMUIFactory::CreateDOMUIForURL(this, GetURL());
1934 // dom_ui might be NULL if the URL refers to a non-existent extension.
1935 if (dom_ui) {
1936 render_manager_.SetDOMUIPostCommit(dom_ui);
1937 dom_ui->RenderViewCreated(render_view_host());
1938 }
1939 }
1940 opener_dom_ui_type_ = DOMUIFactory::kNoDOMUI;
1941 }
1942
[email protected]420ae012009-04-24 05:16:321943 if (details.is_user_initiated_main_frame_load()) {
1944 // Clear the status bubble. This is a workaround for a bug where WebKit
1945 // doesn't let us know that the cursor left an element during a
1946 // transition (this is also why the mouse cursor remains as a hand after
1947 // clicking on a link); see bugs 1184641 and 980803. We don't want to
1948 // clear the bubble when a user navigates to a named anchor in the same
1949 // page.
1950 UpdateTargetURL(details.entry->page_id(), GURL());
[email protected]420ae012009-04-24 05:16:321951 }
1952
1953 // The keyword generator uses the navigation entries, so must be called after
1954 // the commit.
1955 GenerateKeywordIfNecessary(params);
1956
1957 // Allow the new page to set the title again.
1958 received_page_title_ = false;
1959
1960 // Get the favicon, either from history or request it from the net.
1961 fav_icon_helper_.FetchFavIcon(details.entry->url());
1962
[email protected]0de80162010-02-03 04:52:351963 // Clear all page actions, blocked content notifications and browser actions
1964 // for this tab, unless this is an in-page navigation.
[email protected]a6e82fc2010-02-24 22:28:141965 if (!details.is_in_page) {
[email protected]eaa7dd182010-12-14 11:09:001966 ExtensionService* service = profile()->GetExtensionService();
[email protected]92c6f9b92009-10-24 04:35:081967 if (service) {
1968 for (size_t i = 0; i < service->extensions()->size(); ++i) {
[email protected]5d246db22009-10-27 06:17:571969 ExtensionAction* browser_action =
[email protected]92c6f9b92009-10-24 04:35:081970 service->extensions()->at(i)->browser_action();
[email protected]56ce6e52009-10-27 00:10:521971 if (browser_action) {
1972 browser_action->ClearAllValuesForTab(controller().session_id().id());
1973 NotificationService::current()->Notify(
1974 NotificationType::EXTENSION_BROWSER_ACTION_UPDATED,
[email protected]5d246db22009-10-27 06:17:571975 Source<ExtensionAction>(browser_action),
[email protected]56ce6e52009-10-27 00:10:521976 NotificationService::NoDetails());
1977 }
[email protected]92c6f9b92009-10-24 04:35:081978
[email protected]5d246db22009-10-27 06:17:571979 ExtensionAction* page_action =
[email protected]56ce6e52009-10-27 00:10:521980 service->extensions()->at(i)->page_action();
1981 if (page_action) {
1982 page_action->ClearAllValuesForTab(controller().session_id().id());
1983 PageActionStateChanged();
1984 }
[email protected]92c6f9b92009-10-24 04:35:081985 }
[email protected]aece2c7f2009-08-27 20:43:171986 }
[email protected]0de80162010-02-03 04:52:351987
1988 // Close blocked popups.
[email protected]c65e2f152010-10-14 15:30:401989 if (blocked_contents_) {
[email protected]0fbd70332010-06-01 19:28:341990 AutoReset<bool> auto_reset(&dont_notify_render_view_, true);
[email protected]c65e2f152010-10-14 15:30:401991 blocked_contents_->Destroy();
1992 blocked_contents_ = NULL;
[email protected]0de80162010-02-03 04:52:351993 }
1994
1995 // Clear "blocked" flags.
[email protected]da4f8a12010-08-19 08:33:411996 content_settings_delegate_->ClearBlockedContentSettingsExceptForCookies();
[email protected]9fb83e82010-07-02 18:24:551997 content_settings_delegate_->GeolocationDidNavigate(details);
[email protected]f17a0ee2010-05-17 17:38:471998
1999 // Once the main frame is navigated, we're no longer considered to have
2000 // displayed insecure content.
2001 displayed_insecure_content_ = false;
[email protected]aece2c7f2009-08-27 20:43:172002 }
[email protected]ce5c4502009-05-06 16:46:112003
[email protected]8cc8d492010-02-02 10:40:492004 // Close constrained windows if necessary.
2005 if (!net::RegistryControlledDomainService::SameDomainOrHost(
2006 details.previous_url, details.entry->url()))
2007 CloseConstrainedWindows();
2008
[email protected]420ae012009-04-24 05:16:322009 // Update the starred state.
2010 UpdateStarredStateForCurrentURL();
[email protected]c07c9752010-03-12 05:57:532011
[email protected]3c9e1872010-11-18 16:17:492012 // Notify observers about navigation.
2013 FOR_EACH_OBSERVER(WebNavigationObserver, web_navigation_observers_,
2014 DidNavigateMainFramePostCommit(details, params));
[email protected]420ae012009-04-24 05:16:322015}
2016
2017void TabContents::DidNavigateAnyFramePostCommit(
2018 RenderViewHost* render_view_host,
2019 const NavigationController::LoadCommittedDetails& details,
2020 const ViewHostMsg_FrameNavigate_Params& params) {
2021 // If we navigate, start showing messages again. This does nothing to prevent
2022 // a malicious script from spamming messages, since the script could just
2023 // reload the page to stop blocking.
2024 suppress_javascript_messages_ = false;
2025
[email protected]3c9e1872010-11-18 16:17:492026 // Notify observers about navigation.
2027 FOR_EACH_OBSERVER(WebNavigationObserver, web_navigation_observers_,
2028 DidNavigateAnyFramePostCommit(details, params));
[email protected]3b1c1812010-02-12 21:53:552029
2030 // Let the LanguageState clear its state.
[email protected]80fc6112010-08-13 20:10:582031 language_state_.DidNavigate(details);
[email protected]420ae012009-04-24 05:16:322032}
2033
[email protected]8cc8d492010-02-02 10:40:492034void TabContents::CloseConstrainedWindows() {
2035 // Clear out any constrained windows since we are leaving this page entirely.
[email protected]420ae012009-04-24 05:16:322036 // We use indices instead of iterators in case CloseWindow does something
2037 // that may invalidate an iterator.
[email protected]61d68ef12011-01-13 14:02:562038 for (size_t i = 0; i < child_windows_.size(); ++i) {
2039 ConstrainedWindow* window = child_windows_[child_windows_.size() - 1 - i];
[email protected]fa1cf0b82010-01-15 21:49:442040 if (window) {
[email protected]420ae012009-04-24 05:16:322041 window->CloseConstrainedWindow();
[email protected]fa1cf0b82010-01-15 21:49:442042 BlockTabContent(false);
2043 }
[email protected]420ae012009-04-24 05:16:322044 }
2045}
2046
2047void TabContents::UpdateStarredStateForCurrentURL() {
2048 BookmarkModel* model = profile()->GetBookmarkModel();
2049 const bool old_state = is_starred_;
2050 is_starred_ = (model && model->IsBookmarked(GetURL()));
2051
2052 if (is_starred_ != old_state && delegate())
2053 delegate()->URLStarredChanged(this, is_starred_);
2054}
2055
2056void TabContents::UpdateAlternateErrorPageURL() {
2057 GURL url = GetAlternateErrorPageURL();
2058 render_view_host()->SetAlternateErrorPageURL(url);
2059}
2060
2061void TabContents::UpdateWebPreferences() {
2062 render_view_host()->UpdateWebPreferences(GetWebkitPrefs());
2063}
2064
[email protected]d0b8d092010-10-25 04:05:172065void TabContents::UpdateZoomLevel() {
2066 render_view_host()->SetZoomLevel(GetZoomLevel());
2067}
2068
[email protected]420ae012009-04-24 05:16:322069void TabContents::UpdateMaxPageIDIfNecessary(SiteInstance* site_instance,
2070 RenderViewHost* rvh) {
2071 // If we are creating a RVH for a restored controller, then we might
2072 // have more page IDs than the SiteInstance's current max page ID. We must
2073 // make sure that the max page ID is larger than any restored page ID.
2074 // Note that it is ok for conflicting page IDs to exist in another tab
2075 // (i.e., NavigationController), but if any page ID is larger than the max,
2076 // the back/forward list will get confused.
2077 int max_restored_page_id = controller_.max_restored_page_id();
2078 if (max_restored_page_id > 0) {
2079 int curr_max_page_id = site_instance->max_page_id();
2080 if (max_restored_page_id > curr_max_page_id) {
2081 // Need to update the site instance immediately.
2082 site_instance->UpdateMaxPageID(max_restored_page_id);
2083
2084 // Also tell the renderer to update its internal representation. We
2085 // need to reserve enough IDs to make all restored page IDs less than
2086 // the max.
2087 if (curr_max_page_id < 0)
2088 curr_max_page_id = 0;
2089 rvh->ReservePageIDRange(max_restored_page_id - curr_max_page_id);
2090 }
2091 }
2092}
2093
[email protected]ec0b6c42010-08-26 03:16:582094scoped_refptr<history::HistoryAddPageArgs>
2095TabContents::CreateHistoryAddPageArgs(
[email protected]ebe89e062009-08-13 23:16:542096 const GURL& virtual_url,
[email protected]befd8d822009-07-01 04:51:472097 const NavigationController::LoadCommittedDetails& details,
[email protected]420ae012009-04-24 05:16:322098 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]ec0b6c42010-08-26 03:16:582099 scoped_refptr<history::HistoryAddPageArgs> add_page_args(
2100 new history::HistoryAddPageArgs(
2101 params.url, base::Time::Now(), this, params.page_id, params.referrer,
2102 params.redirects, params.transition, history::SOURCE_BROWSED,
2103 details.did_replace_entry));
2104 if (PageTransition::IsMainFrame(params.transition) &&
2105 virtual_url != params.url) {
2106 // Hack on the "virtual" URL so that it will appear in history. For some
2107 // types of URLs, we will display a magic URL that is different from where
2108 // the page is actually navigated. We want the user to see in history what
2109 // they saw in the URL bar, so we add the virtual URL as a redirect. This
2110 // only applies to the main frame, as the virtual URL doesn't apply to
2111 // sub-frames.
2112 add_page_args->url = virtual_url;
2113 if (!add_page_args->redirects.empty())
2114 add_page_args->redirects.back() = virtual_url;
[email protected]420ae012009-04-24 05:16:322115 }
[email protected]ec0b6c42010-08-26 03:16:582116 return add_page_args;
[email protected]420ae012009-04-24 05:16:322117}
2118
2119bool TabContents::UpdateTitleForEntry(NavigationEntry* entry,
2120 const std::wstring& title) {
2121 // For file URLs without a title, use the pathname instead. In the case of a
2122 // synthesized title, we don't want the update to count toward the "one set
2123 // per page of the title to history."
[email protected]e53668962010-06-23 15:35:252124 string16 final_title;
[email protected]420ae012009-04-24 05:16:322125 bool explicit_set;
2126 if (entry->url().SchemeIsFile() && title.empty()) {
[email protected]e53668962010-06-23 15:35:252127 final_title = UTF8ToUTF16(entry->url().ExtractFileName());
[email protected]420ae012009-04-24 05:16:322128 explicit_set = false; // Don't count synthetic titles toward the set limit.
2129 } else {
[email protected]e53668962010-06-23 15:35:252130 TrimWhitespace(WideToUTF16Hack(title), TRIM_ALL, &final_title);
[email protected]420ae012009-04-24 05:16:322131 explicit_set = true;
2132 }
2133
[email protected]e53668962010-06-23 15:35:252134 if (final_title == entry->title())
[email protected]420ae012009-04-24 05:16:322135 return false; // Nothing changed, don't bother.
2136
[email protected]e53668962010-06-23 15:35:252137 entry->set_title(final_title);
[email protected]420ae012009-04-24 05:16:322138
[email protected]ec0b6c42010-08-26 03:16:582139 if (!received_page_title_) {
2140 UpdateHistoryPageTitle(*entry);
[email protected]420ae012009-04-24 05:16:322141 received_page_title_ = explicit_set;
2142 }
2143
2144 // Lastly, set the title for the view.
[email protected]e53668962010-06-23 15:35:252145 view_->SetPageTitle(UTF16ToWideHack(final_title));
[email protected]420ae012009-04-24 05:16:322146
[email protected]cbc0e1b2010-04-12 18:33:042147 NotificationService::current()->Notify(
2148 NotificationType::TAB_CONTENTS_TITLE_UPDATED,
2149 Source<TabContents>(this),
2150 NotificationService::NoDetails());
2151
[email protected]420ae012009-04-24 05:16:322152 return true;
2153}
2154
2155void TabContents::NotifySwapped() {
2156 // After sending out a swap notification, we need to send a disconnect
2157 // notification so that clients that pick up a pointer to |this| can NULL the
2158 // pointer. See Bug 1230284.
2159 notify_disconnection_ = true;
2160 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:342161 NotificationType::TAB_CONTENTS_SWAPPED,
[email protected]d82ed61e2009-06-16 02:46:222162 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:322163 NotificationService::NoDetails());
2164}
2165
2166void TabContents::NotifyConnected() {
2167 notify_disconnection_ = true;
2168 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:342169 NotificationType::TAB_CONTENTS_CONNECTED,
[email protected]d82ed61e2009-06-16 02:46:222170 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:322171 NotificationService::NoDetails());
2172}
2173
2174void TabContents::NotifyDisconnected() {
2175 if (!notify_disconnection_)
2176 return;
2177
2178 notify_disconnection_ = false;
2179 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:342180 NotificationType::TAB_CONTENTS_DISCONNECTED,
[email protected]d82ed61e2009-06-16 02:46:222181 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:322182 NotificationService::NoDetails());
2183}
2184
2185void TabContents::GenerateKeywordIfNecessary(
2186 const ViewHostMsg_FrameNavigate_Params& params) {
2187 if (!params.searchable_form_url.is_valid())
2188 return;
2189
2190 if (profile()->IsOffTheRecord())
2191 return;
2192
2193 int last_index = controller_.last_committed_entry_index();
2194 // When there was no previous page, the last index will be 0. This is
2195 // normally due to a form submit that opened in a new tab.
2196 // TODO(brettw) bug 916126: we should support keywords when form submits
2197 // happen in new tabs.
2198 if (last_index <= 0)
2199 return;
2200 const NavigationEntry* previous_entry =
2201 controller_.GetEntryAtIndex(last_index - 1);
2202 if (IsFormSubmit(previous_entry)) {
2203 // Only generate a keyword if the previous page wasn't itself a form
2204 // submit.
2205 return;
2206 }
2207
2208 GURL keyword_url = previous_entry->user_typed_url().is_valid() ?
2209 previous_entry->user_typed_url() : previous_entry->url();
[email protected]400b133f2011-01-19 18:32:302210 string16 keyword =
[email protected]420ae012009-04-24 05:16:322211 TemplateURLModel::GenerateKeyword(keyword_url, true); // autodetected
2212 if (keyword.empty())
2213 return;
2214
2215 TemplateURLModel* url_model = profile()->GetTemplateURLModel();
2216 if (!url_model)
2217 return;
2218
2219 if (!url_model->loaded()) {
2220 url_model->Load();
2221 return;
2222 }
2223
2224 const TemplateURL* current_url;
[email protected]ddd231e2010-06-29 20:35:192225 GURL url = params.searchable_form_url;
[email protected]420ae012009-04-24 05:16:322226 if (!url_model->CanReplaceKeyword(keyword, url, &current_url))
2227 return;
2228
2229 if (current_url) {
2230 if (current_url->originating_url().is_valid()) {
2231 // The existing keyword was generated from an OpenSearch description
2232 // document, don't regenerate.
2233 return;
2234 }
2235 url_model->Remove(current_url);
2236 }
2237 TemplateURL* new_url = new TemplateURL();
2238 new_url->set_keyword(keyword);
2239 new_url->set_short_name(keyword);
[email protected]ddd231e2010-06-29 20:35:192240 new_url->SetURL(url.spec(), 0, 0);
[email protected]420ae012009-04-24 05:16:322241 new_url->add_input_encoding(params.searchable_form_encoding);
2242 DCHECK(controller_.GetLastCommittedEntry());
2243 const GURL& favicon_url =
2244 controller_.GetLastCommittedEntry()->favicon().url();
2245 if (favicon_url.is_valid()) {
2246 new_url->SetFavIconURL(favicon_url);
2247 } else {
2248 // The favicon url isn't valid. This means there really isn't a favicon,
2249 // or the favicon url wasn't obtained before the load started. This assumes
2250 // the later.
2251 // TODO(sky): Need a way to set the favicon that doesn't involve generating
2252 // its url.
2253 new_url->SetFavIconURL(TemplateURL::GenerateFaviconURL(params.referrer));
2254 }
2255 new_url->set_safe_for_autoreplace(true);
2256 url_model->Add(new_url);
2257}
2258
[email protected]bf5c2ff392009-07-08 16:24:332259void TabContents::OnFindReply(int request_id,
2260 int number_of_matches,
2261 const gfx::Rect& selection_rect,
2262 int active_match_ordinal,
2263 bool final_update) {
[email protected]3ce7ced2009-07-24 18:52:422264 // Ignore responses for requests that have been aborted.
[email protected]bf5c2ff392009-07-08 16:24:332265 // Ignore responses for requests other than the one we have most recently
2266 // issued. That way we won't act on stale results when the user has
2267 // already typed in another query.
[email protected]7d472472011-01-22 01:30:252268 if (!find_op_aborted_ && request_id == current_find_request_id_) {
2269 if (number_of_matches == -1)
2270 number_of_matches = last_search_result_.number_of_matches();
2271 if (active_match_ordinal == -1)
2272 active_match_ordinal = last_search_result_.active_match_ordinal();
[email protected]bf5c2ff392009-07-08 16:24:332273
[email protected]7d472472011-01-22 01:30:252274 gfx::Rect selection = selection_rect;
2275 if (selection.IsEmpty())
2276 selection = last_search_result_.selection_rect();
[email protected]bf5c2ff392009-07-08 16:24:332277
[email protected]7d472472011-01-22 01:30:252278 // Notify the UI, automation and any other observers that a find result was
2279 // found.
2280 last_search_result_ = FindNotificationDetails(
2281 request_id, number_of_matches, selection, active_match_ordinal,
2282 final_update);
2283 NotificationService::current()->Notify(
2284 NotificationType::FIND_RESULT_AVAILABLE,
2285 Source<TabContents>(this),
2286 Details<FindNotificationDetails>(&last_search_result_));
2287 }
[email protected]bf5c2ff392009-07-08 16:24:332288
[email protected]7d472472011-01-22 01:30:252289 // Send a notification to the renderer that we are ready to receive more
2290 // results from the scoping effort of the Find operation. The FindInPage
2291 // scoping is asynchronous and periodically sends results back up to the
2292 // browser using IPC. In an effort to not spam the browser we have the
2293 // browser send an ACK for each FindReply message and have the renderer
2294 // queue up the latest status message while waiting for this ACK.
2295 render_view_host()->Send(new ViewMsg_FindReplyACK(
2296 render_view_host()->routing_id()));
[email protected]bf5c2ff392009-07-08 16:24:332297}
2298
[email protected]7d472472011-01-22 01:30:252299void TabContents::OnGoToEntryAtOffset(int offset) {
[email protected]876bc832010-09-07 16:29:542300 if (!delegate_ || delegate_->OnGoToEntryOffset(offset)) {
2301 NavigationEntry* entry = controller_.GetEntryAtOffset(offset);
2302 if (!entry)
2303 return;
2304 // Note that we don't call NavigationController::GotToOffset() as we don't
2305 // want to create a pending navigation entry (it might end up lingering
2306 // http://crbug.com/51680).
[email protected]784688a62010-09-13 07:06:522307 entry->set_transition_type(entry->transition_type() |
2308 PageTransition::FORWARD_BACK);
[email protected]876bc832010-09-07 16:29:542309 NavigateToEntry(*entry, NavigationController::NO_RELOAD);
2310 }
[email protected]bf5c2ff392009-07-08 16:24:332311}
2312
[email protected]bf5c2ff392009-07-08 16:24:332313void TabContents::OnMissingPluginStatus(int status) {
2314#if defined(OS_WIN)
2315// TODO(PORT): pull in when plug-ins work
2316 GetPluginInstaller()->OnMissingPluginStatus(status);
2317#endif
2318}
2319
2320void TabContents::OnCrashedPlugin(const FilePath& plugin_path) {
[email protected]bf5c2ff392009-07-08 16:24:332321 DCHECK(!plugin_path.value().empty());
2322
2323 std::wstring plugin_name = plugin_path.ToWStringHack();
[email protected]191eb3f72010-12-21 06:27:502324 webkit::npapi::WebPluginInfo plugin_info;
2325 if (webkit::npapi::PluginList::Singleton()->GetPluginInfoByPath(
[email protected]45446a52010-11-04 17:41:002326 plugin_path, &plugin_info) &&
2327 !plugin_info.name.empty()) {
2328 plugin_name = UTF16ToWide(plugin_info.name);
[email protected]996fd702009-09-04 19:12:372329#if defined(OS_MACOSX)
[email protected]45446a52010-11-04 17:41:002330 // Many plugins on the Mac have .plugin in the actual name, which looks
2331 // terrible, so look for that and strip it off if present.
2332 const std::wstring plugin_extension(L".plugin");
2333 if (EndsWith(plugin_name, plugin_extension, true))
2334 plugin_name.erase(plugin_name.length() - plugin_extension.length());
[email protected]996fd702009-09-04 19:12:372335#endif // OS_MACOSX
[email protected]bf5c2ff392009-07-08 16:24:332336 }
[email protected]2ae8a922010-06-28 20:27:532337 SkBitmap* crash_icon = ResourceBundle::GetSharedInstance().GetBitmapNamed(
2338 IDR_INFOBAR_PLUGIN_CRASHED);
[email protected]f4f50ef2011-01-21 19:01:192339 AddInfoBar(new SimpleAlertInfoBarDelegate(this, crash_icon,
2340 l10n_util::GetStringFUTF16(IDS_PLUGIN_CRASHED_PROMPT,
2341 WideToUTF16Hack(plugin_name)), true));
[email protected]bf5c2ff392009-07-08 16:24:332342}
2343
[email protected]38789d82010-11-17 06:03:442344void TabContents::OnDidGetApplicationInfo(int32 page_id,
2345 const WebApplicationInfo& info) {
[email protected]ed5431872009-11-17 08:39:512346 web_app_info_ = info;
[email protected]bf5c2ff392009-07-08 16:24:332347
[email protected]ed5431872009-11-17 08:39:512348 if (delegate())
2349 delegate()->OnDidGetApplicationInfo(this, page_id);
[email protected]bf5c2ff392009-07-08 16:24:332350}
2351
[email protected]bb461532010-11-26 21:50:232352void TabContents::OnInstallApplication(const WebApplicationInfo& info) {
2353 if (delegate())
2354 delegate()->OnInstallApplication(this, info);
2355}
2356
[email protected]3c2826852010-12-16 19:09:142357void TabContents::OnBlockedOutdatedPlugin(const string16& name,
2358 const GURL& update_url) {
2359 new OutdatedPluginInfoBar(this, name, update_url);
[email protected]851b1eb2010-08-09 13:32:292360}
2361
[email protected]a2cf65eb2010-01-18 08:20:382362void TabContents::OnPageContents(const GURL& url,
[email protected]a2cf65eb2010-01-18 08:20:382363 int32 page_id,
[email protected]e5106202010-06-11 21:12:362364 const string16& contents,
[email protected]1c57b7b2010-07-12 17:38:102365 const std::string& language,
2366 bool page_translatable) {
[email protected]a2cf65eb2010-01-18 08:20:382367 // Don't index any https pages. People generally don't want their bank
2368 // accounts, etc. indexed on their computer, especially since some of these
2369 // things are not marked cachable.
2370 // TODO(brettw) we may want to consider more elaborate heuristics such as
2371 // the cachability of the page. We may also want to consider subframes (this
2372 // test will still index subframes if the subframe is SSL).
[email protected]ccd40a52010-05-11 16:15:272373 // TODO(zelidrag) bug chromium-os:2808 - figure out if we want to reenable
2374 // content indexing for chromeos in some future releases.
2375#if !defined(OS_CHROMEOS)
[email protected]a2cf65eb2010-01-18 08:20:382376 if (!url.SchemeIsSecure()) {
2377 Profile* p = profile();
2378 if (p && !p->IsOffTheRecord()) {
2379 HistoryService* hs = p->GetHistoryService(Profile::IMPLICIT_ACCESS);
2380 if (hs)
2381 hs->SetPageContents(url, contents);
2382 }
2383 }
[email protected]ccd40a52010-05-11 16:15:272384#endif
[email protected]a8a81292010-01-21 00:32:452385
[email protected]1c57b7b2010-07-12 17:38:102386 language_state_.LanguageDetermined(language, page_translatable);
[email protected]a8a81292010-01-21 00:32:452387
2388 std::string lang = language;
2389 NotificationService::current()->Notify(
2390 NotificationType::TAB_LANGUAGE_DETERMINED,
[email protected]7e0831b2010-02-01 08:45:152391 Source<TabContents>(this),
[email protected]a8a81292010-01-21 00:32:452392 Details<std::string>(&lang));
[email protected]d54169e92011-01-21 09:19:522393
2394 // Generate the thumbnail here if the in-browser thumbnailing is enabled.
2395 if (CommandLine::ForCurrentProcess()->HasSwitch(
2396 switches::kEnableInBrowserThumbnailing)) {
2397 ThumbnailGenerator::UpdateThumbnailIfNecessary(this, url);
2398 }
[email protected]a2cf65eb2010-01-18 08:20:382399}
2400
[email protected]7e0831b2010-02-01 08:45:152401void TabContents::OnPageTranslated(int32 page_id,
2402 const std::string& original_lang,
[email protected]152b8ba32010-03-29 19:40:162403 const std::string& translated_lang,
2404 TranslateErrors::Type error_type) {
[email protected]3b1c1812010-02-12 21:53:552405 language_state_.set_current_language(translated_lang);
2406 language_state_.set_translation_pending(false);
[email protected]152b8ba32010-03-29 19:40:162407 PageTranslatedDetails details(original_lang, translated_lang, error_type);
[email protected]7e0831b2010-02-01 08:45:152408 NotificationService::current()->Notify(
2409 NotificationType::PAGE_TRANSLATED,
2410 Source<TabContents>(this),
[email protected]152b8ba32010-03-29 19:40:162411 Details<PageTranslatedDetails>(&details));
[email protected]7e0831b2010-02-01 08:45:152412}
2413
[email protected]ce833282010-11-04 15:48:392414void TabContents::OnSetSuggestions(
2415 int32 page_id,
2416 const std::vector<std::string>& suggestions) {
[email protected]15d9c0c2010-09-10 22:36:262417 if (delegate())
[email protected]ce833282010-11-04 15:48:392418 delegate()->OnSetSuggestions(page_id, suggestions);
2419}
2420
2421void TabContents::OnInstantSupportDetermined(int32 page_id, bool result) {
2422 if (delegate())
2423 delegate()->OnInstantSupportDetermined(page_id, result);
[email protected]15d9c0c2010-09-10 22:36:262424}
2425
[email protected]dcd5b332010-08-11 08:55:182426void TabContents::OnContentSettingsAccessed(bool content_was_blocked) {
[email protected]614c0e12010-03-25 10:12:562427 if (delegate_)
[email protected]20ad6a92010-04-21 14:07:242428 delegate_->OnContentSettingsChange(this);
[email protected]614c0e12010-03-25 10:12:562429}
2430
[email protected]8d3347f2009-07-09 22:00:212431RenderViewHostDelegate::View* TabContents::GetViewDelegate() {
[email protected]420ae012009-04-24 05:16:322432 return view_.get();
2433}
2434
[email protected]8d3347f2009-07-09 22:00:212435RenderViewHostDelegate::RendererManagement*
2436TabContents::GetRendererManagementDelegate() {
2437 return &render_manager_;
2438}
2439
[email protected]46ba9082010-06-22 10:00:582440RenderViewHostDelegate::ContentSettings*
2441TabContents::GetContentSettingsDelegate() {
[email protected]9fb83e82010-07-02 18:24:552442 return content_settings_delegate_.get();
[email protected]46ba9082010-06-22 10:00:582443}
2444
[email protected]8d3347f2009-07-09 22:00:212445RenderViewHostDelegate::Save* TabContents::GetSaveDelegate() {
[email protected]420ae012009-04-24 05:16:322446 return save_package_.get(); // May be NULL, but we can return NULL.
2447}
2448
[email protected]8d3347f2009-07-09 22:00:212449RenderViewHostDelegate::Printing* TabContents::GetPrintingDelegate() {
[email protected]12802702010-07-09 19:43:092450 return printing_.get();
[email protected]8d3347f2009-07-09 22:00:212451}
2452
2453RenderViewHostDelegate::FavIcon* TabContents::GetFavIconDelegate() {
2454 return &fav_icon_helper_;
2455}
2456
[email protected]1f18184a2010-07-21 19:34:492457RenderViewHostDelegate::SSL* TabContents::GetSSLDelegate() {
2458 return GetSSLHelper();
2459}
2460
[email protected]b5977a0c2010-08-24 19:46:262461RenderViewHostDelegate::FileSelect* TabContents::GetFileSelectDelegate() {
2462 if (file_select_helper_.get() == NULL)
[email protected]ba70d082010-09-10 16:54:492463 file_select_helper_.reset(new FileSelectHelper(profile()));
[email protected]b5977a0c2010-08-24 19:46:262464 return file_select_helper_.get();
2465}
2466
[email protected]fd571bb2010-03-19 01:23:542467AutomationResourceRoutingDelegate*
2468TabContents::GetAutomationResourceRoutingDelegate() {
2469 return delegate();
2470}
2471
[email protected]ced90ae12010-02-20 02:06:162472RenderViewHostDelegate::BookmarkDrag* TabContents::GetBookmarkDragDelegate() {
2473 return bookmark_drag_;
2474}
2475
2476void TabContents::SetBookmarkDragDelegate(
2477 RenderViewHostDelegate::BookmarkDrag* bookmark_drag) {
2478 bookmark_drag_ = bookmark_drag;
2479}
2480
[email protected]9fb83e82010-07-02 18:24:552481TabSpecificContentSettings* TabContents::GetTabSpecificContentSettings() const {
2482 return content_settings_delegate_.get();
2483}
2484
[email protected]93623c5d2009-12-10 21:40:322485RendererPreferences TabContents::GetRendererPrefs(Profile* profile) const {
[email protected]840b1512009-07-21 16:53:462486 return renderer_preferences_;
[email protected]80d96fa2009-06-10 22:34:512487}
2488
[email protected]57c6a652009-05-04 07:58:342489TabContents* TabContents::GetAsTabContents() {
2490 return this;
2491}
2492
[email protected]7b291f92009-08-14 05:43:532493ViewType::Type TabContents::GetRenderViewType() const {
2494 return ViewType::TAB_CONTENTS;
2495}
2496
2497int TabContents::GetBrowserWindowID() const {
2498 return controller().window_id().id();
2499}
2500
[email protected]420ae012009-04-24 05:16:322501void TabContents::RenderViewCreated(RenderViewHost* render_view_host) {
[email protected]2e4633c2009-07-09 16:58:062502 NotificationService::current()->Notify(
2503 NotificationType::RENDER_VIEW_HOST_CREATED_FOR_TAB,
2504 Source<TabContents>(this),
2505 Details<RenderViewHost>(render_view_host));
[email protected]420ae012009-04-24 05:16:322506 NavigationEntry* entry = controller_.GetActiveEntry();
2507 if (!entry)
2508 return;
2509
2510 // When we're creating views, we're still doing initial setup, so we always
2511 // use the pending DOM UI rather than any possibly existing committed one.
2512 if (render_manager_.pending_dom_ui()) {
2513 render_manager_.pending_dom_ui()->RenderViewCreated(render_view_host);
2514 }
2515
2516 if (entry->IsViewSourceMode()) {
2517 // Put the renderer in view source mode.
2518 render_view_host->Send(
2519 new ViewMsg_EnableViewSourceMode(render_view_host->routing_id()));
2520 }
[email protected]0666aef2009-05-13 19:48:082521
2522 view()->RenderViewCreated(render_view_host);
[email protected]420ae012009-04-24 05:16:322523}
2524
2525void TabContents::RenderViewReady(RenderViewHost* rvh) {
2526 if (rvh != render_view_host()) {
2527 // Don't notify the world, since this came from a renderer in the
2528 // background.
2529 return;
2530 }
2531
2532 NotifyConnected();
[email protected]9d8a4642009-07-29 17:25:302533 bool was_crashed = is_crashed();
[email protected]443b80e2010-12-14 00:42:232534 SetIsCrashed(base::TERMINATION_STATUS_STILL_RUNNING, 0);
[email protected]9d8a4642009-07-29 17:25:302535
2536 // Restore the focus to the tab (otherwise the focus will be on the top
2537 // window).
[email protected]484ae5912010-09-29 19:16:142538 if (was_crashed && !FocusLocationBarByDefault() &&
2539 (!delegate_ || delegate_->ShouldFocusPageAfterCrash())) {
[email protected]9d8a4642009-07-29 17:25:302540 Focus();
[email protected]484ae5912010-09-29 19:16:142541 }
[email protected]420ae012009-04-24 05:16:322542}
2543
[email protected]443b80e2010-12-14 00:42:232544void TabContents::RenderViewGone(RenderViewHost* rvh,
2545 base::TerminationStatus status,
2546 int error_code) {
[email protected]420ae012009-04-24 05:16:322547 // Ask the print preview if this renderer was valuable.
[email protected]12802702010-07-09 19:43:092548 if (!printing_->OnRenderViewGone(rvh))
[email protected]420ae012009-04-24 05:16:322549 return;
2550 if (rvh != render_view_host()) {
2551 // The pending page's RenderViewHost is gone.
2552 return;
2553 }
2554
2555 SetIsLoading(false, NULL);
2556 NotifyDisconnected();
[email protected]443b80e2010-12-14 00:42:232557 SetIsCrashed(status, error_code);
[email protected]420ae012009-04-24 05:16:322558
[email protected]ff9efc62010-02-18 22:10:542559 // Remove all infobars.
[email protected]e5430c92010-02-18 22:39:212560 for (int i = infobar_delegate_count() - 1; i >=0 ; --i)
[email protected]ff9efc62010-02-18 22:10:542561 RemoveInfoBar(GetInfoBarDelegateAt(i));
2562
[email protected]8e537302009-06-10 21:19:582563 // Tell the view that we've crashed so it can prepare the sad tab page.
[email protected]cf68b092010-01-15 20:05:552564 // Only do this if we're not in browser shutdown, so that TabContents
2565 // objects that are not in a browser (e.g., HTML dialogs) and thus are
2566 // visible do not flash a sad tab page.
2567 if (browser_shutdown::GetShutdownType() == browser_shutdown::NOT_VALID)
[email protected]a8f024392011-01-13 21:50:162568 view_->OnTabCrashed(status, error_code);
[email protected]420ae012009-04-24 05:16:322569
2570 // Hide any visible hung renderer warning for this web contents' process.
[email protected]e7b8d832009-11-20 22:41:002571 hung_renderer_dialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:322572}
2573
[email protected]2e4633c2009-07-09 16:58:062574void TabContents::RenderViewDeleted(RenderViewHost* rvh) {
2575 NotificationService::current()->Notify(
2576 NotificationType::RENDER_VIEW_HOST_DELETED,
2577 Source<TabContents>(this),
2578 Details<RenderViewHost>(rvh));
[email protected]fb7b79f72009-11-06 18:00:482579 render_manager_.RenderViewDeleted(rvh);
[email protected]2e4633c2009-07-09 16:58:062580}
2581
[email protected]420ae012009-04-24 05:16:322582void TabContents::DidNavigate(RenderViewHost* rvh,
2583 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]8030f012009-09-25 18:09:372584 int extra_invalidate_flags = 0;
2585
2586 if (PageTransition::IsMainFrame(params.transition)) {
[email protected]e7d50892011-01-19 21:47:382587 if (MaybeUsePreloadedPage(params.url)) {
2588 return;
[email protected]33f74972010-12-08 16:40:362589 }
2590
[email protected]20ede072009-10-06 00:28:352591 bool was_bookmark_bar_visible = ShouldShowBookmarkBar();
[email protected]8030f012009-09-25 18:09:372592
[email protected]420ae012009-04-24 05:16:322593 render_manager_.DidNavigateMainFrame(rvh);
2594
[email protected]20ede072009-10-06 00:28:352595 if (was_bookmark_bar_visible != ShouldShowBookmarkBar())
[email protected]8030f012009-09-25 18:09:372596 extra_invalidate_flags |= INVALIDATE_BOOKMARK_BAR;
[email protected]8030f012009-09-25 18:09:372597 }
2598
[email protected]420ae012009-04-24 05:16:322599 // Update the site of the SiteInstance if it doesn't have one yet.
2600 if (!GetSiteInstance()->has_site())
2601 GetSiteInstance()->SetSite(params.url);
2602
2603 // Need to update MIME type here because it's referred to in
2604 // UpdateNavigationCommands() called by RendererDidNavigate() to
2605 // determine whether or not to enable the encoding menu.
2606 // It's updated only for the main frame. For a subframe,
2607 // RenderView::UpdateURL does not set params.contents_mime_type.
2608 // (see http://code.google.com/p/chromium/issues/detail?id=2929 )
2609 // TODO(jungshik): Add a test for the encoding menu to avoid
2610 // regressing it again.
2611 if (PageTransition::IsMainFrame(params.transition))
2612 contents_mime_type_ = params.contents_mime_type;
2613
2614 NavigationController::LoadCommittedDetails details;
[email protected]8030f012009-09-25 18:09:372615 bool did_navigate = controller_.RendererDidNavigate(
2616 params, extra_invalidate_flags, &details);
[email protected]3e90d4a2009-07-03 17:38:392617
[email protected]a9c0bfe2010-09-17 08:35:222618 // Send notification about committed provisional loads. This notification is
2619 // different from the NAV_ENTRY_COMMITTED notification which doesn't include
2620 // the actual URL navigated to and isn't sent for AUTO_SUBFRAME navigations.
2621 if (details.type != NavigationType::NAV_IGNORE) {
[email protected]dabb0d12010-10-05 12:50:072622 // For AUTO_SUBFRAME navigations, an event for the main frame is generated
2623 // that is not recorded in the navigation history. For the purpose of
2624 // tracking navigation events, we treat this event as a sub frame navigation
2625 // event.
2626 bool is_main_frame = did_navigate ? details.is_main_frame : false;
2627 ProvisionalLoadDetails load_details(
2628 is_main_frame, details.is_in_page, params.url, std::string(), false,
[email protected]dd0aafd2010-12-02 13:13:412629 false, params.frame_id);
[email protected]a9c0bfe2010-09-17 08:35:222630 load_details.set_transition_type(params.transition);
2631 // Whether or not a page transition was triggered by going backward or
2632 // forward in the history is only stored in the navigation controller's
2633 // entry list.
2634 if (did_navigate &&
2635 (controller_.GetActiveEntry()->transition_type() &
2636 PageTransition::FORWARD_BACK)) {
2637 load_details.set_transition_type(
2638 params.transition | PageTransition::FORWARD_BACK);
2639 }
2640 NotificationService::current()->Notify(
2641 NotificationType::FRAME_PROVISIONAL_LOAD_COMMITTED,
2642 Source<NavigationController>(&controller_),
2643 Details<ProvisionalLoadDetails>(&load_details));
[email protected]a9c0bfe2010-09-17 08:35:222644 }
2645
[email protected]3e90d4a2009-07-03 17:38:392646 // Update history. Note that this needs to happen after the entry is complete,
2647 // which WillNavigate[Main,Sub]Frame will do before this function is called.
[email protected]ec0b6c42010-08-26 03:16:582648 if (params.should_update_history) {
[email protected]3e90d4a2009-07-03 17:38:392649 // Most of the time, the displayURL matches the loaded URL, but for about:
[email protected]ec0b6c42010-08-26 03:16:582650 // URLs, we use a data: URL as the real value. We actually want to save the
2651 // about: URL to the history db and keep the data: URL hidden. This is what
2652 // the TabContents' URL getter does.
2653 scoped_refptr<history::HistoryAddPageArgs> add_page_args(
2654 CreateHistoryAddPageArgs(GetURL(), details, params));
2655 if (!delegate() ||
2656 delegate()->ShouldAddNavigationToHistory(*add_page_args,
2657 details.type)) {
2658 UpdateHistoryForNavigation(add_page_args);
2659 }
[email protected]3e90d4a2009-07-03 17:38:392660 }
2661
[email protected]76543b92009-08-31 17:27:452662 if (!did_navigate)
2663 return; // No navigation happened.
[email protected]420ae012009-04-24 05:16:322664
2665 // DO NOT ADD MORE STUFF TO THIS FUNCTION! Your component should either listen
2666 // for the appropriate notification (best) or you can add it to
2667 // DidNavigateMainFramePostCommit / DidNavigateAnyFramePostCommit (only if
2668 // necessary, please).
2669
2670 // Run post-commit tasks.
2671 if (details.is_main_frame)
2672 DidNavigateMainFramePostCommit(details, params);
2673 DidNavigateAnyFramePostCommit(rvh, details, params);
2674}
2675
2676void TabContents::UpdateState(RenderViewHost* rvh,
2677 int32 page_id,
2678 const std::string& state) {
2679 DCHECK(rvh == render_view_host());
2680
2681 // We must be prepared to handle state updates for any page, these occur
2682 // when the user is scrolling and entering form data, as well as when we're
2683 // leaving a page, in which case our state may have already been moved to
2684 // the next page. The navigation controller will look up the appropriate
2685 // NavigationEntry and update it when it is notified via the delegate.
2686
2687 int entry_index = controller_.GetEntryIndexWithPageID(
2688 GetSiteInstance(), page_id);
2689 if (entry_index < 0)
2690 return;
2691 NavigationEntry* entry = controller_.GetEntryAtIndex(entry_index);
2692
2693 if (state == entry->content_state())
2694 return; // Nothing to update.
2695 entry->set_content_state(state);
2696 controller_.NotifyEntryChanged(entry, entry_index);
2697}
2698
2699void TabContents::UpdateTitle(RenderViewHost* rvh,
2700 int32 page_id, const std::wstring& title) {
2701 // If we have a title, that's a pretty good indication that we've started
2702 // getting useful data.
2703 SetNotWaitingForResponse();
2704
2705 DCHECK(rvh == render_view_host());
[email protected]371cc8b2010-09-01 16:03:482706 NavigationEntry* entry = controller_.GetEntryWithPageID(rvh->site_instance(),
[email protected]ec0b6c42010-08-26 03:16:582707 page_id);
[email protected]420ae012009-04-24 05:16:322708 if (!entry || !UpdateTitleForEntry(entry, title))
2709 return;
2710
2711 // Broadcast notifications when the UI should be updated.
2712 if (entry == controller_.GetEntryAtOffset(0))
[email protected]f1cd5e82009-10-23 17:19:032713 NotifyNavigationStateChanged(INVALIDATE_TITLE);
[email protected]420ae012009-04-24 05:16:322714}
2715
[email protected]420ae012009-04-24 05:16:322716void TabContents::UpdateEncoding(RenderViewHost* render_view_host,
[email protected]41fc0322009-09-04 22:23:402717 const std::string& encoding) {
[email protected]420ae012009-04-24 05:16:322718 set_encoding(encoding);
2719}
2720
2721void TabContents::UpdateTargetURL(int32 page_id, const GURL& url) {
2722 if (delegate())
2723 delegate()->UpdateTargetURL(this, url);
2724}
2725
2726void TabContents::UpdateThumbnail(const GURL& url,
2727 const SkBitmap& bitmap,
2728 const ThumbnailScore& score) {
[email protected]e6baed562010-11-02 22:33:442729 if (profile()->IsOffTheRecord())
2730 return;
2731
[email protected]420ae012009-04-24 05:16:322732 // Tell History about this thumbnail
[email protected]54097492f2011-01-18 08:32:072733 history::TopSites* ts = profile()->GetTopSites();
2734 if (ts)
2735 ts->SetPageThumbnail(url, bitmap, score);
[email protected]420ae012009-04-24 05:16:322736}
2737
[email protected]c4e98902010-06-01 10:20:142738void TabContents::UpdateInspectorSetting(const std::string& key,
2739 const std::string& value) {
[email protected]24d27d72010-10-12 16:30:052740 RenderViewHostDelegateHelper::UpdateInspectorSetting(profile(), key, value);
[email protected]0df30122009-06-03 12:13:082741}
2742
[email protected]45aafc92010-06-07 14:18:532743void TabContents::ClearInspectorSettings() {
[email protected]24d27d72010-10-12 16:30:052744 RenderViewHostDelegateHelper::ClearInspectorSettings(profile());
[email protected]45aafc92010-06-07 14:18:532745}
2746
[email protected]420ae012009-04-24 05:16:322747void TabContents::Close(RenderViewHost* rvh) {
[email protected]07707302009-11-06 00:50:292748 // The UI may be in an event-tracking loop, such as between the
2749 // mouse-down and mouse-up in text selection or a button click.
2750 // Defer the close until after tracking is complete, so that we
2751 // don't free objects out from under the UI.
2752 // TODO(shess): This could probably be integrated with the
2753 // IsDoingDrag() test below. Punting for now because I need more
2754 // research to understand how this impacts platforms other than Mac.
2755 // TODO(shess): This could get more fine-grained. For instance,
2756 // closing a tab in another window while selecting text in the
2757 // current window's Omnibox should be just fine.
2758 if (view()->IsEventTracking()) {
2759 view()->CloseTabAfterEventTracking();
2760 return;
2761 }
2762
[email protected]24a4d1062009-07-10 23:10:422763 // If we close the tab while we're in the middle of a drag, we'll crash.
2764 // Instead, cancel the drag and close it as soon as the drag ends.
2765 if (view()->IsDoingDrag()) {
2766 view()->CancelDragAndCloseTab();
2767 return;
2768 }
2769
[email protected]420ae012009-04-24 05:16:322770 // Ignore this if it comes from a RenderViewHost that we aren't showing.
2771 if (delegate() && rvh == render_view_host())
2772 delegate()->CloseContents(this);
2773}
2774
2775void TabContents::RequestMove(const gfx::Rect& new_bounds) {
2776 if (delegate() && delegate()->IsPopup(this))
2777 delegate()->MoveContents(this, new_bounds);
2778}
2779
[email protected]7ab1e7d62009-10-14 23:32:012780void TabContents::DidStartLoading() {
[email protected]420ae012009-04-24 05:16:322781 SetIsLoading(true, NULL);
[email protected]3c9e1872010-11-18 16:17:492782
[email protected]de4761b2010-12-07 21:05:212783 if (delegate()) {
2784 bool is_print_preview_tab =
2785 printing::PrintPreviewTabController::IsPrintPreviewTab(this);
2786 if (content_restrictions_ || is_print_preview_tab) {
2787 content_restrictions_= is_print_preview_tab ?
2788 CONTENT_RESTRICTION_PRINT : 0;
2789 delegate()->ContentRestrictionsChanged(this);
2790 }
[email protected]9e823662010-10-13 23:36:002791 }
[email protected]3c9e1872010-11-18 16:17:492792
2793 // Notify observers about navigation.
2794 FOR_EACH_OBSERVER(WebNavigationObserver, web_navigation_observers_,
2795 DidStartLoading());
[email protected]420ae012009-04-24 05:16:322796}
2797
[email protected]7ab1e7d62009-10-14 23:32:012798void TabContents::DidStopLoading() {
[email protected]420ae012009-04-24 05:16:322799 scoped_ptr<LoadNotificationDetails> details;
2800
2801 NavigationEntry* entry = controller_.GetActiveEntry();
2802 // An entry may not exist for a stop when loading an initial blank page or
2803 // if an iframe injected by script into a blank page finishes loading.
2804 if (entry) {
[email protected]420ae012009-04-24 05:16:322805 base::TimeDelta elapsed = base::TimeTicks::Now() - current_load_start_;
2806
2807 details.reset(new LoadNotificationDetails(
[email protected]ebe89e062009-08-13 23:16:542808 entry->virtual_url(),
[email protected]420ae012009-04-24 05:16:322809 entry->transition_type(),
2810 elapsed,
2811 &controller_,
2812 controller_.GetCurrentEntryIndex()));
2813 }
2814
[email protected]420ae012009-04-24 05:16:322815 SetIsLoading(false, details.get());
[email protected]3c9e1872010-11-18 16:17:492816
2817 // Notify observers about navigation.
2818 FOR_EACH_OBSERVER(WebNavigationObserver, web_navigation_observers_,
2819 DidStopLoading());
[email protected]420ae012009-04-24 05:16:322820}
2821
[email protected]1a3c3cb2010-12-16 21:03:402822void TabContents::DidChangeLoadProgress(double progress) {
2823 if (delegate())
2824 delegate()->LoadProgressChanged(progress);
2825}
2826
[email protected]25497492010-09-11 15:15:082827void TabContents::DocumentOnLoadCompletedInMainFrame(
2828 RenderViewHost* render_view_host,
2829 int32 page_id) {
2830 NotificationService::current()->Notify(
2831 NotificationType::LOAD_COMPLETED_MAIN_FRAME,
2832 Source<TabContents>(this),
2833 Details<int>(&page_id));
2834}
2835
[email protected]420ae012009-04-24 05:16:322836void TabContents::RequestOpenURL(const GURL& url, const GURL& referrer,
2837 WindowOpenDisposition disposition) {
2838 if (render_manager_.dom_ui()) {
2839 // When we're a DOM UI, it will provide a page transition type for us (this
2840 // is so the new tab page can specify AUTO_BOOKMARK for automatically
2841 // generated suggestions).
2842 //
2843 // Note also that we hide the referrer for DOM UI pages. We don't really
[email protected]60e448982009-05-06 04:21:162844 // want web sites to see a referrer of "chrome://blah" (and some
2845 // chrome: URLs might have search terms or other stuff we don't want to
[email protected]420ae012009-04-24 05:16:322846 // send to the site), so we send no referrer.
2847 OpenURL(url, GURL(), disposition,
2848 render_manager_.dom_ui()->link_transition_type());
2849 } else {
2850 OpenURL(url, referrer, disposition, PageTransition::LINK);
2851 }
2852}
2853
[email protected]1ee614862009-07-06 20:29:182854void TabContents::DomOperationResponse(const std::string& json_string,
2855 int automation_id) {
2856}
2857
[email protected]8b8e7c92010-08-19 18:05:562858void TabContents::ProcessDOMUIMessage(
2859 const ViewHostMsg_DomMessage_Params& params) {
[email protected]420ae012009-04-24 05:16:322860 if (!render_manager_.dom_ui()) {
[email protected]c2e74fe82009-09-03 17:57:442861 // This can happen if someone uses window.open() to open an extension URL
2862 // from a non-extension context.
[email protected]8b8e7c92010-08-19 18:05:562863 render_view_host()->BlockExtensionRequest(params.request_id);
[email protected]420ae012009-04-24 05:16:322864 return;
2865 }
[email protected]8b8e7c92010-08-19 18:05:562866 render_manager_.dom_ui()->ProcessDOMUIMessage(params);
[email protected]420ae012009-04-24 05:16:322867}
2868
2869void TabContents::ProcessExternalHostMessage(const std::string& message,
2870 const std::string& origin,
2871 const std::string& target) {
2872 if (delegate())
2873 delegate()->ForwardMessageToExternalHost(message, origin, target);
2874}
2875
[email protected]420ae012009-04-24 05:16:322876void TabContents::RunJavaScriptMessage(
2877 const std::wstring& message,
2878 const std::wstring& default_prompt,
2879 const GURL& frame_url,
2880 const int flags,
2881 IPC::Message* reply_msg,
2882 bool* did_suppress_message) {
2883 // Suppress javascript messages when requested and when inside a constrained
2884 // popup window (because that activates them and breaks them out of the
2885 // constrained window jail).
[email protected]91ed49a2009-07-07 21:29:552886 // Also suppress messages when showing an interstitial. The interstitial is
2887 // shown over the previous page, we don't want the hidden page dialogs to
2888 // interfere with the interstitial.
[email protected]8f55e802010-12-06 18:11:502889 bool suppress_this_message =
2890 suppress_javascript_messages_ ||
2891 showing_interstitial_page() ||
2892 (delegate() && delegate()->ShouldSuppressDialogs());
[email protected]420ae012009-04-24 05:16:322893 if (delegate())
2894 suppress_this_message |=
[email protected]b6c874582009-05-08 19:38:312895 (delegate()->GetConstrainingContents(this) != this);
[email protected]420ae012009-04-24 05:16:322896
2897 *did_suppress_message = suppress_this_message;
2898
2899 if (!suppress_this_message) {
2900 base::TimeDelta time_since_last_message(
2901 base::TimeTicks::Now() - last_javascript_message_dismissal_);
2902 bool show_suppress_checkbox = false;
2903 // Show a checkbox offering to suppress further messages if this message is
2904 // being displayed within kJavascriptMessageExpectedDelay of the last one.
2905 if (time_since_last_message <
2906 base::TimeDelta::FromMilliseconds(kJavascriptMessageExpectedDelay))
2907 show_suppress_checkbox = true;
2908
[email protected]d6ad7442010-09-05 19:06:522909 RunJavascriptMessageBox(profile(), this, frame_url, flags, message,
2910 default_prompt, show_suppress_checkbox, reply_msg);
[email protected]420ae012009-04-24 05:16:322911 } else {
2912 // If we are suppressing messages, just reply as is if the user immediately
2913 // pressed "Cancel".
[email protected]beb440c2009-11-06 04:08:542914 OnMessageBoxClosed(reply_msg, false, std::wstring());
[email protected]420ae012009-04-24 05:16:322915 }
2916}
2917
2918void TabContents::RunBeforeUnloadConfirm(const std::wstring& message,
2919 IPC::Message* reply_msg) {
[email protected]5aab5e22010-12-08 22:13:292920 if (delegate())
2921 delegate()->WillRunBeforeUnloadConfirm();
[email protected]8f55e802010-12-06 18:11:502922 if (delegate() && delegate()->ShouldSuppressDialogs()) {
2923 render_view_host()->JavaScriptMessageBoxClosed(reply_msg, true,
2924 std::wstring());
2925 return;
2926 }
[email protected]7ab1e7d62009-10-14 23:32:012927 is_showing_before_unload_dialog_ = true;
[email protected]d82ed61e2009-06-16 02:46:222928 RunBeforeUnloadDialog(this, message, reply_msg);
[email protected]420ae012009-04-24 05:16:322929}
2930
2931void TabContents::ShowModalHTMLDialog(const GURL& url, int width, int height,
2932 const std::string& json_arguments,
2933 IPC::Message* reply_msg) {
2934 if (delegate()) {
2935 HtmlDialogUIDelegate* dialog_delegate =
2936 new ModalHtmlDialogDelegate(url, width, height, json_arguments,
[email protected]d82ed61e2009-06-16 02:46:222937 reply_msg, this);
[email protected]420ae012009-04-24 05:16:322938 delegate()->ShowHtmlDialog(dialog_delegate, NULL);
2939 }
2940}
2941
[email protected]420ae012009-04-24 05:16:322942// Checks to see if we should generate a keyword based on the OSDD, and if
2943// necessary uses TemplateURLFetcher to download the OSDD and create a keyword.
[email protected]2de7e002010-10-11 22:58:202944void TabContents::PageHasOSDD(
2945 RenderViewHost* render_view_host,
2946 int32 page_id,
2947 const GURL& url,
2948 const ViewHostMsg_PageHasOSDD_Type& msg_provider_type) {
[email protected]0b5b85c2010-10-11 23:00:122949 // Make sure page_id is the current page and other basic checks.
[email protected]420ae012009-04-24 05:16:322950 DCHECK(url.is_valid());
2951 if (!IsActiveEntry(page_id))
2952 return;
[email protected]420ae012009-04-24 05:16:322953 if (!profile()->GetTemplateURLFetcher())
2954 return;
[email protected]420ae012009-04-24 05:16:322955 if (profile()->IsOffTheRecord())
2956 return;
2957
[email protected]2de7e002010-10-11 22:58:202958 TemplateURLFetcher::ProviderType provider_type;
2959 switch (msg_provider_type.type) {
2960 case ViewHostMsg_PageHasOSDD_Type::AUTODETECTED_PROVIDER:
2961 provider_type = TemplateURLFetcher::AUTODETECTED_PROVIDER;
2962 break;
2963
2964 case ViewHostMsg_PageHasOSDD_Type::EXPLICIT_DEFAULT_PROVIDER:
2965 provider_type = TemplateURLFetcher::EXPLICIT_DEFAULT_PROVIDER;
2966 break;
2967
2968 case ViewHostMsg_PageHasOSDD_Type::EXPLICIT_PROVIDER:
2969 provider_type = TemplateURLFetcher::EXPLICIT_PROVIDER;
2970 break;
2971
2972 default:
2973 NOTREACHED();
2974 return;
2975 }
2976
[email protected]420ae012009-04-24 05:16:322977 const NavigationEntry* entry = controller_.GetLastCommittedEntry();
2978 DCHECK(entry);
2979
2980 const NavigationEntry* base_entry = entry;
2981 if (IsFormSubmit(base_entry)) {
2982 // If the current page is a form submit, find the last page that was not
2983 // a form submit and use its url to generate the keyword from.
2984 int index = controller_.last_committed_entry_index() - 1;
2985 while (index >= 0 && IsFormSubmit(controller_.GetEntryAtIndex(index)))
2986 index--;
2987 if (index >= 0)
2988 base_entry = controller_.GetEntryAtIndex(index);
2989 else
2990 base_entry = NULL;
2991 }
2992
2993 // We want to use the user typed URL if available since that represents what
2994 // the user typed to get here, and fall back on the regular URL if not.
2995 if (!base_entry)
2996 return;
2997 GURL keyword_url = base_entry->user_typed_url().is_valid() ?
2998 base_entry->user_typed_url() : base_entry->url();
2999 if (!keyword_url.is_valid())
3000 return;
[email protected]2de7e002010-10-11 22:58:203001
[email protected]400b133f2011-01-19 18:32:303002 string16 keyword = TemplateURLModel::GenerateKeyword(
[email protected]0b5b85c2010-10-11 23:00:123003 keyword_url,
3004 provider_type == TemplateURLFetcher::AUTODETECTED_PROVIDER);
[email protected]a88dbdd52010-01-15 19:25:533005
[email protected]0b5b85c2010-10-11 23:00:123006 // Download the OpenSearch description document. If this is successful, a
[email protected]420ae012009-04-24 05:16:323007 // new keyword will be created when done.
[email protected]420ae012009-04-24 05:16:323008 profile()->GetTemplateURLFetcher()->ScheduleDownload(
3009 keyword,
3010 url,
3011 base_entry->favicon().url(),
[email protected]0b5b85c2010-10-11 23:00:123012 new TemplateURLFetcherUICallbacks(this),
[email protected]2de7e002010-10-11 22:58:203013 provider_type);
[email protected]420ae012009-04-24 05:16:323014}
3015
[email protected]420ae012009-04-24 05:16:323016GURL TabContents::GetAlternateErrorPageURL() const {
3017 GURL url;
3018 // Disable alternate error pages when in OffTheRecord/Incognito mode.
3019 if (profile()->IsOffTheRecord())
3020 return url;
3021
3022 PrefService* prefs = profile()->GetPrefs();
3023 DCHECK(prefs);
3024 if (prefs->GetBoolean(prefs::kAlternateErrorPagesEnabled)) {
[email protected]a33457b82009-09-21 23:07:153025 url = google_util::AppendGoogleLocaleParam(
3026 GURL(google_util::kLinkDoctorBaseURL));
[email protected]420ae012009-04-24 05:16:323027 url = google_util::AppendGoogleTLDParam(url);
3028 }
3029 return url;
3030}
3031
3032WebPreferences TabContents::GetWebkitPrefs() {
[email protected]6c32ce72010-03-08 05:18:063033 Profile* profile = render_view_host()->process()->profile();
[email protected]7ccddb8c2009-08-04 17:36:553034 bool is_dom_ui = false;
[email protected]447021c2010-09-08 21:29:083035 WebPreferences web_prefs =
3036 RenderViewHostDelegateHelper::GetWebkitPrefs(profile, is_dom_ui);
3037
[email protected]9beff752010-09-22 19:35:433038 // Force accelerated compositing and 2d canvas off for chrome: and
3039 // chrome-extension: pages.
[email protected]27c790d42010-10-22 09:28:433040 if (GetURL().SchemeIs(chrome::kChromeDevToolsScheme) ||
3041 GetURL().SchemeIs(chrome::kChromeUIScheme)) {
[email protected]447021c2010-09-08 21:29:083042 web_prefs.accelerated_compositing_enabled = false;
[email protected]9beff752010-09-22 19:35:433043 web_prefs.accelerated_2d_canvas_enabled = false;
[email protected]447021c2010-09-08 21:29:083044 }
3045
[email protected]39553442010-09-09 00:01:553046#if defined(OS_MACOSX)
3047 // Disable accelerated compositing if IOSurface's are not supported,
3048 // as is the case in 10.5.
3049 if (!IOSurfaceSupport::Initialize())
3050 web_prefs.accelerated_compositing_enabled = false;
3051#endif
3052
[email protected]447021c2010-09-08 21:29:083053 return web_prefs;
[email protected]420ae012009-04-24 05:16:323054}
3055
[email protected]7d472472011-01-22 01:30:253056void TabContents::OnUserGesture() {
3057 // See comment in RenderViewHostDelegate::OnUserGesture as to why we do this.
3058 DownloadRequestLimiter* limiter =
3059 g_browser_process->download_request_limiter();
3060 if (limiter)
3061 limiter->OnUserGesture(this);
3062 ExternalProtocolHandler::PermitLaunchUrl();
3063}
3064
[email protected]fa1cf0b82010-01-15 21:49:443065void TabContents::OnIgnoredUIEvent() {
3066 if (constrained_window_count()) {
3067 ConstrainedWindow* window = *constrained_window_begin();
3068 window->FocusConstrainedWindow();
3069 }
3070}
3071
[email protected]420ae012009-04-24 05:16:323072void TabContents::OnJSOutOfMemory() {
[email protected]f4f50ef2011-01-21 19:01:193073 AddInfoBar(new SimpleAlertInfoBarDelegate(this, NULL,
3074 l10n_util::GetStringUTF16(IDS_JS_OUT_OF_MEMORY_PROMPT), true));
[email protected]420ae012009-04-24 05:16:323075}
3076
[email protected]420ae012009-04-24 05:16:323077void TabContents::OnCrossSiteResponse(int new_render_process_host_id,
3078 int new_request_id) {
[email protected]57c6a652009-05-04 07:58:343079 // Allows the TabContents to react when a cross-site response is ready to be
[email protected]420ae012009-04-24 05:16:323080 // delivered to a pending RenderViewHost. We must first run the onunload
3081 // handler of the old RenderViewHost before we can allow it to proceed.
3082 render_manager_.OnCrossSiteResponse(new_render_process_host_id,
3083 new_request_id);
3084}
3085
[email protected]420ae012009-04-24 05:16:323086void TabContents::RendererUnresponsive(RenderViewHost* rvh,
3087 bool is_during_unload) {
3088 if (is_during_unload) {
3089 // Hang occurred while firing the beforeunload/unload handler.
3090 // Pretend the handler fired so tab closing continues as if it had.
[email protected]829e7612009-04-25 01:15:113091 rvh->set_sudden_termination_allowed(true);
[email protected]420ae012009-04-24 05:16:323092
3093 if (!render_manager_.ShouldCloseTabOnUnresponsiveRenderer())
3094 return;
3095
3096 // If the tab hangs in the beforeunload/unload handler there's really
3097 // nothing we can do to recover. Pretend the unload listeners have
3098 // all fired and close the tab. If the hang is in the beforeunload handler
3099 // then the user will not have the option of cancelling the close.
3100 Close(rvh);
3101 return;
3102 }
3103
3104 if (render_view_host() && render_view_host()->IsRenderViewLive())
[email protected]e7b8d832009-11-20 22:41:003105 hung_renderer_dialog::ShowForTabContents(this);
[email protected]420ae012009-04-24 05:16:323106}
3107
3108void TabContents::RendererResponsive(RenderViewHost* render_view_host) {
[email protected]e7b8d832009-11-20 22:41:003109 hung_renderer_dialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:323110}
3111
3112void TabContents::LoadStateChanged(const GURL& url,
[email protected]094e5b22009-09-25 04:23:563113 net::LoadState load_state,
3114 uint64 upload_position,
3115 uint64 upload_size) {
[email protected]420ae012009-04-24 05:16:323116 load_state_ = load_state;
[email protected]094e5b22009-09-25 04:23:563117 upload_position_ = upload_position;
3118 upload_size_ = upload_size;
[email protected]d686e812009-06-03 19:10:293119 std::wstring languages =
[email protected]ddd231e2010-06-29 20:35:193120 UTF8ToWide(profile()->GetPrefs()->GetString(prefs::kAcceptLanguages));
[email protected]d686e812009-06-03 19:10:293121 std::string host = url.host();
[email protected]42197a22010-12-28 23:29:423122 load_state_host_ = WideToUTF16Hack(
3123 net::IDNToUnicode(host.c_str(), host.size(), languages, NULL));
[email protected]420ae012009-04-24 05:16:323124 if (load_state_ == net::LOAD_STATE_READING_RESPONSE)
3125 SetNotWaitingForResponse();
3126 if (is_loading())
[email protected]c9cd2222009-05-06 05:16:503127 NotifyNavigationStateChanged(INVALIDATE_LOAD | INVALIDATE_TAB);
[email protected]420ae012009-04-24 05:16:323128}
3129
[email protected]1ee614862009-07-06 20:29:183130bool TabContents::IsExternalTabContainer() const {
3131 if (!delegate())
3132 return false;
3133
3134 return delegate()->IsExternalTabContainer();
3135}
3136
[email protected]1ee614862009-07-06 20:29:183137void TabContents::DidInsertCSS() {
3138 // This RVHDelegate function is used for extensions and not us.
[email protected]420ae012009-04-24 05:16:323139}
3140
[email protected]546ae4e02010-12-08 14:57:193141void TabContents::FocusedNodeChanged(bool is_editable_node) {
[email protected]130efb02009-09-18 18:54:353142 NotificationService::current()->Notify(
3143 NotificationType::FOCUS_CHANGED_IN_PAGE,
3144 Source<RenderViewHost>(render_view_host()),
[email protected]546ae4e02010-12-08 14:57:193145 Details<const bool>(&is_editable_node));
[email protected]130efb02009-09-18 18:54:353146}
3147
[email protected]b75b8292010-10-01 07:28:253148void TabContents::UpdateZoomLimits(int minimum_percent,
3149 int maximum_percent,
3150 bool remember) {
3151 minimum_zoom_percent_ = minimum_percent;
3152 maximum_zoom_percent_ = maximum_percent;
3153 temporary_zoom_settings_ = !remember;
[email protected]ebf40a72010-07-22 01:46:383154}
3155
[email protected]7d472472011-01-22 01:30:253156void TabContents::WorkerCrashed() {
3157 AddInfoBar(new SimpleAlertInfoBarDelegate(this, NULL,
3158 l10n_util::GetStringUTF16(IDS_WEBWORKER_CRASHED_PROMPT), true));
3159}
3160
[email protected]420ae012009-04-24 05:16:323161void TabContents::BeforeUnloadFiredFromRenderManager(
3162 bool proceed,
3163 bool* proceed_to_fire_unload) {
3164 if (delegate())
3165 delegate()->BeforeUnloadFired(this, proceed, proceed_to_fire_unload);
3166}
3167
[email protected]3a3d47472010-07-15 21:03:543168void TabContents::DidStartLoadingFromRenderManager(
3169 RenderViewHost* render_view_host) {
3170 DidStartLoading();
3171}
3172
3173void TabContents::RenderViewGoneFromRenderManager(
3174 RenderViewHost* render_view_host) {
[email protected]443b80e2010-12-14 00:42:233175 DCHECK(crashed_status_ != base::TERMINATION_STATUS_STILL_RUNNING);
3176 RenderViewGone(render_view_host, crashed_status_, crashed_error_code_);
[email protected]3a3d47472010-07-15 21:03:543177}
3178
[email protected]420ae012009-04-24 05:16:323179void TabContents::UpdateRenderViewSizeForRenderManager() {
3180 // TODO(brettw) this is a hack. See TabContentsView::SizeContents.
[email protected]0323ee42010-02-17 22:03:263181 gfx::Size size = view_->GetContainerSize();
3182 // 0x0 isn't a valid window size (minimal window size is 1x1) but it may be
3183 // here during container initialization and normal window size will be set
3184 // later. In case of tab duplication this resizing to 0x0 prevents setting
3185 // normal size later so just ignore it.
3186 if (!size.IsEmpty())
3187 view_->SizeContents(size);
[email protected]420ae012009-04-24 05:16:323188}
3189
[email protected]3a3d47472010-07-15 21:03:543190void TabContents::NotifySwappedFromRenderManager() {
3191 NotifySwapped();
3192}
3193
3194NavigationController& TabContents::GetControllerForRenderManager() {
3195 return controller();
3196}
3197
[email protected]420ae012009-04-24 05:16:323198DOMUI* TabContents::CreateDOMUIForRenderManager(const GURL& url) {
[email protected]d82ed61e2009-06-16 02:46:223199 return DOMUIFactory::CreateDOMUIForURL(this, url);
[email protected]420ae012009-04-24 05:16:323200}
3201
3202NavigationEntry*
3203TabContents::GetLastCommittedNavigationEntryForRenderManager() {
3204 return controller_.GetLastCommittedEntry();
3205}
3206
3207bool TabContents::CreateRenderViewForRenderManager(
3208 RenderViewHost* render_view_host) {
[email protected]420ae012009-04-24 05:16:323209 RenderWidgetHostView* rwh_view = view_->CreateViewForWidget(render_view_host);
[email protected]1a98a932009-11-17 00:12:523210
[email protected]674b3822010-08-04 04:02:513211 if (!render_view_host->CreateRenderView(string16()))
[email protected]420ae012009-04-24 05:16:323212 return false;
3213
3214 // Now that the RenderView has been created, we need to tell it its size.
3215 rwh_view->SetSize(view_->GetContainerSize());
3216
3217 UpdateMaxPageIDIfNecessary(render_view_host->site_instance(),
3218 render_view_host);
3219 return true;
3220}
3221
3222void TabContents::Observe(NotificationType type,
3223 const NotificationSource& source,
3224 const NotificationDetails& details) {
3225 switch (type.value) {
3226 case NotificationType::BOOKMARK_MODEL_LOADED:
3227 // BookmarkModel finished loading, fall through to update starred state.
3228 case NotificationType::URLS_STARRED: {
3229 // Somewhere, a URL has been starred.
3230 // Ignore notifications for profiles other than our current one.
3231 Profile* source_profile = Source<Profile>(source).ptr();
3232 if (!source_profile || !source_profile->IsSameProfile(profile()))
3233 return;
3234
3235 UpdateStarredStateForCurrentURL();
3236 break;
3237 }
3238 case NotificationType::PREF_CHANGED: {
[email protected]57ecc4b2010-08-11 03:02:513239 std::string* pref_name_in = Details<std::string>(details).ptr();
[email protected]420ae012009-04-24 05:16:323240 DCHECK(Source<PrefService>(source).ptr() == profile()->GetPrefs());
3241 if (*pref_name_in == prefs::kAlternateErrorPagesEnabled) {
3242 UpdateAlternateErrorPageURL();
[email protected]88e14382010-10-14 21:45:533243 } else if ((*pref_name_in == prefs::kDefaultCharset) ||
3244 StartsWithASCII(*pref_name_in, "webkit.webprefs.", true)) {
[email protected]420ae012009-04-24 05:16:323245 UpdateWebPreferences();
[email protected]d0b8d092010-10-25 04:05:173246 } else if (*pref_name_in == prefs::kDefaultZoomLevel) {
3247 UpdateZoomLevel();
[email protected]420ae012009-04-24 05:16:323248 } else {
3249 NOTREACHED() << "unexpected pref change notification" << *pref_name_in;
3250 }
3251 break;
3252 }
3253 case NotificationType::RENDER_WIDGET_HOST_DESTROYED:
3254 view_->RenderWidgetHostDestroyed(Source<RenderWidgetHost>(source).ptr());
3255 break;
3256
3257 case NotificationType::NAV_ENTRY_COMMITTED: {
3258 DCHECK(&controller_ == Source<NavigationController>(source).ptr());
3259
3260 NavigationController::LoadCommittedDetails& committed_details =
3261 *(Details<NavigationController::LoadCommittedDetails>(details).ptr());
3262 ExpireInfoBars(committed_details);
3263 break;
3264 }
3265
[email protected]d3ba77272009-09-03 00:06:093266#if defined(OS_LINUX)
3267 case NotificationType::BROWSER_THEME_CHANGED: {
[email protected]7ff431e2010-01-07 18:18:543268 renderer_preferences_util::UpdateFromSystemSettings(
3269 &renderer_preferences_, profile());
[email protected]d3ba77272009-09-03 00:06:093270 render_view_host()->SyncRendererPrefs();
3271 break;
3272 }
3273#endif
3274
[email protected]6c32ce72010-03-08 05:18:063275 case NotificationType::USER_STYLE_SHEET_UPDATED:
3276 UpdateWebPreferences();
3277 break;
3278
[email protected]79580c62010-02-02 02:36:253279 case NotificationType::CONTENT_SETTINGS_CHANGED: {
[email protected]5f24f212010-12-02 14:47:453280 Details<const ContentSettingsDetails> settings_details(details);
[email protected]79580c62010-02-02 02:36:253281 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]da0c8e92010-02-09 23:25:493282 GURL entry_url;
[email protected]0314ae02010-04-08 09:18:293283 if (entry)
[email protected]da0c8e92010-02-09 23:25:493284 entry_url = entry->url();
[email protected]0314ae02010-04-08 09:18:293285 if (settings_details.ptr()->update_all() ||
3286 settings_details.ptr()->pattern().Matches(entry_url)) {
3287 render_view_host()->SendContentSettings(entry_url,
[email protected]b9a4ea7f2010-06-25 07:11:233288 profile()->GetHostContentSettingsMap()->
3289 GetContentSettings(entry_url));
[email protected]79580c62010-02-02 02:36:253290 }
3291 break;
3292 }
3293
[email protected]e5adff72010-06-14 18:13:063294 case NotificationType::EXTENSION_LOADED:
[email protected]a888b29e62010-04-01 13:38:573295 break;
[email protected]a888b29e62010-04-01 13:38:573296
3297 case NotificationType::EXTENSION_UNLOADED:
[email protected]a888b29e62010-04-01 13:38:573298 break;
[email protected]a888b29e62010-04-01 13:38:573299
[email protected]88152052010-10-01 04:05:183300 case NotificationType::GOOGLE_URL_UPDATED:
3301 UpdateAlternateErrorPageURL();
3302 break;
3303
[email protected]420ae012009-04-24 05:16:323304 default:
3305 NOTREACHED();
3306 }
3307}
[email protected]aacd7cc2009-09-02 14:51:103308
[email protected]9adb9692010-10-29 23:14:023309void TabContents::UpdateExtensionAppIcon(const Extension* extension) {
[email protected]98aa0b52010-05-06 17:03:083310 extension_app_icon_.reset();
[email protected]f075fe0a22010-04-26 04:06:433311
[email protected]a888b29e62010-04-01 13:38:573312 if (extension) {
[email protected]98aa0b52010-05-06 17:03:083313 extension_app_image_loader_.reset(new ImageLoadingTracker(this));
3314 extension_app_image_loader_->LoadImage(
[email protected]a888b29e62010-04-01 13:38:573315 extension,
[email protected]807871f2010-09-16 01:04:483316 extension->GetIconResource(Extension::EXTENSION_ICON_SMALLISH,
3317 ExtensionIconSet::MATCH_EXACTLY),
[email protected]a888b29e62010-04-01 13:38:573318 gfx::Size(Extension::EXTENSION_ICON_SMALLISH,
3319 Extension::EXTENSION_ICON_SMALLISH),
3320 ImageLoadingTracker::CACHE);
3321 } else {
[email protected]98aa0b52010-05-06 17:03:083322 extension_app_image_loader_.reset(NULL);
[email protected]a888b29e62010-04-01 13:38:573323 }
3324}
3325
[email protected]9adb9692010-10-29 23:14:023326const Extension* TabContents::GetExtensionContaining(const GURL& url) {
[email protected]eaa7dd182010-12-14 11:09:003327 ExtensionService* extensions_service = profile()->GetExtensionService();
[email protected]a888b29e62010-04-01 13:38:573328 if (!extensions_service)
3329 return NULL;
3330
[email protected]9adb9692010-10-29 23:14:023331 const Extension* extension = extensions_service->GetExtensionByURL(url);
[email protected]a888b29e62010-04-01 13:38:573332 return extension ?
3333 extension : extensions_service->GetExtensionByWebExtent(url);
3334}
3335
[email protected]a888b29e62010-04-01 13:38:573336void TabContents::OnImageLoaded(SkBitmap* image, ExtensionResource resource,
3337 int index) {
3338 if (image) {
[email protected]98aa0b52010-05-06 17:03:083339 extension_app_icon_ = *image;
[email protected]a888b29e62010-04-01 13:38:573340 NotifyNavigationStateChanged(INVALIDATE_TAB);
3341 }
3342}
3343
[email protected]beb440c2009-11-06 04:08:543344gfx::NativeWindow TabContents::GetMessageBoxRootWindow() {
3345 return view_->GetTopLevelNativeWindow();
3346}
3347
3348void TabContents::OnMessageBoxClosed(IPC::Message* reply_msg,
3349 bool success,
3350 const std::wstring& prompt) {
3351 last_javascript_message_dismissal_ = base::TimeTicks::Now();
3352 if (is_showing_before_unload_dialog_ && !success) {
3353 // If a beforeunload dialog is canceled, we need to stop the throbber from
3354 // spinning, since we forced it to start spinning in Navigate.
3355 DidStopLoading();
3356
3357 tab_close_start_time_ = base::TimeTicks();
3358 }
3359 is_showing_before_unload_dialog_ = false;
3360 render_view_host()->JavaScriptMessageBoxClosed(reply_msg, success, prompt);
3361}
3362
3363void TabContents::SetSuppressMessageBoxes(bool suppress_message_boxes) {
3364 set_suppress_javascript_messages(suppress_message_boxes);
3365}
3366
[email protected]3a3d47472010-07-15 21:03:543367TabContents* TabContents::AsTabContents() {
3368 return this;
3369}
3370
3371ExtensionHost* TabContents::AsExtensionHost() {
3372 return NULL;
3373}
3374
[email protected]41fc0322009-09-04 22:23:403375void TabContents::set_encoding(const std::string& encoding) {
[email protected]aacd7cc2009-09-02 14:51:103376 encoding_ = CharacterEncoding::GetCanonicalEncodingNameByAliasName(encoding);
3377}
[email protected]f45d2a72010-03-08 23:28:353378
3379void TabContents::SetAppIcon(const SkBitmap& app_icon) {
3380 app_icon_ = app_icon;
3381 NotifyNavigationStateChanged(INVALIDATE_TITLE);
3382}
[email protected]33f74972010-12-08 16:40:363383
3384void TabContents::SwapInRenderViewHost(RenderViewHost* rvh) {
3385 render_manager_.SwapInRenderViewHost(rvh);
3386}
3387
3388void TabContents::CreateViewAndSetSizeForRVH(RenderViewHost* rvh) {
3389 RenderWidgetHostView* rwh_view = view()->CreateViewForWidget(rvh);
3390 rwh_view->SetSize(view()->GetContainerSize());
3391}
[email protected]e7d50892011-01-19 21:47:383392
3393bool TabContents::MaybeUsePreloadedPage(const GURL& url) {
3394 PrerenderManager* pm = profile()->GetPrerenderManager();
3395 if (pm != NULL) {
3396 if (pm->MaybeUsePreloadedPage(this, url)) {
3397 // TODO(tburkard): If the preloaded page has not finished preloading
3398 // yet, we should not do this.
3399 DidStopLoading();
3400 return true;
3401 }
3402 }
3403 return false;
3404}