blob: 758a1d09ec9bde27c418ed5fa34241bb2eb27f3a [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]14a000d2010-04-29 21:44:2450#include "chrome/browser/platform_util.h"
[email protected]f4f50ef2011-01-21 19:01:1951#include "chrome/browser/plugin_installer_infobar_delegate.h"
[email protected]37858e52010-08-26 00:22:0252#include "chrome/browser/prefs/pref_service.h"
[email protected]33f74972010-12-08 16:40:3653#include "chrome/browser/prerender/prerender_manager.h"
[email protected]dbeebd52010-11-16 20:34:1654#include "chrome/browser/printing/print_preview_tab_controller.h"
[email protected]12802702010-07-09 19:43:0955#include "chrome/browser/printing/print_view_manager.h"
[email protected]8ecad5e2010-12-02 21:18:3356#include "chrome/browser/profiles/profile.h"
[email protected]1db6ff152009-10-12 15:32:0757#include "chrome/browser/renderer_host/render_process_host.h"
[email protected]8cb5d5b2010-02-09 11:36:1658#include "chrome/browser/renderer_host/render_view_host.h"
[email protected]96d185d2009-04-24 03:28:5459#include "chrome/browser/renderer_host/render_widget_host_view.h"
[email protected]fd694982009-07-03 17:32:3760#include "chrome/browser/renderer_host/resource_request_details.h"
[email protected]1db6ff152009-10-12 15:32:0761#include "chrome/browser/renderer_host/site_instance.h"
[email protected]96d185d2009-04-24 03:28:5462#include "chrome/browser/renderer_host/web_cache_manager.h"
[email protected]93623c5d2009-12-10 21:40:3263#include "chrome/browser/renderer_preferences_util.h"
[email protected]8b62334b2010-08-31 22:37:1164#include "chrome/browser/search_engines/template_url.h"
[email protected]152b8ba32010-03-29 19:40:1665#include "chrome/browser/search_engines/template_url_fetcher.h"
[email protected]0b5b85c2010-10-11 23:00:1266#include "chrome/browser/search_engines/template_url_fetcher_ui_callbacks.h"
[email protected]152b8ba32010-03-29 19:40:1667#include "chrome/browser/search_engines/template_url_model.h"
[email protected]c37e3b62010-01-22 00:15:1768#include "chrome/browser/sessions/session_types.h"
[email protected]bd1ad682009-05-15 22:19:1769#include "chrome/browser/tab_contents/infobar_delegate.h"
[email protected]7e383692009-06-12 19:14:5470#include "chrome/browser/tab_contents/interstitial_page.h"
[email protected]f3ec7742009-01-15 00:59:1671#include "chrome/browser/tab_contents/navigation_entry.h"
[email protected]de34a962009-06-26 23:22:4372#include "chrome/browser/tab_contents/provisional_load_details.h"
[email protected]f3ec7742009-01-15 00:59:1673#include "chrome/browser/tab_contents/tab_contents_delegate.h"
[email protected]1f18184a2010-07-21 19:34:4974#include "chrome/browser/tab_contents/tab_contents_ssl_helper.h"
[email protected]fdd61c62009-04-22 19:22:5775#include "chrome/browser/tab_contents/tab_contents_view.h"
[email protected]d54169e92011-01-21 09:19:5276#include "chrome/browser/tab_contents/thumbnail_generator.h"
[email protected]3c9e1872010-11-18 16:17:4977#include "chrome/browser/tab_contents/web_navigation_observer.h"
[email protected]152b8ba32010-03-29 19:40:1678#include "chrome/browser/translate/page_translated_details.h"
[email protected]1caa1522010-12-01 20:23:3579#include "chrome/browser/ui/app_modal_dialogs/message_box_handler.h"
[email protected]45300ad42010-12-02 15:51:1480#include "chrome/browser/ui/find_bar/find_bar_state.h"
[email protected]54ec6472010-04-09 19:39:5881#include "chrome/common/bindings_policy.h"
[email protected]35f7d212009-04-29 21:19:2782#include "chrome/common/chrome_switches.h"
[email protected]de4761b2010-12-07 21:05:2183#include "chrome/common/content_restriction.h"
[email protected]3b355642010-02-05 16:01:4984#include "chrome/common/extensions/extension.h"
[email protected]5d246db22009-10-27 06:17:5785#include "chrome/common/extensions/extension_action.h"
[email protected]807871f2010-09-16 01:04:4886#include "chrome/common/extensions/extension_icon_set.h"
[email protected]942690b132010-05-11 06:42:1487#include "chrome/common/extensions/extension_resource.h"
[email protected]12802702010-07-09 19:43:0988#include "chrome/common/extensions/url_pattern.h"
89#include "chrome/common/navigation_types.h"
90#include "chrome/common/net/url_request_context_getter.h"
[email protected]bfd04a62009-02-01 18:16:5691#include "chrome/common/notification_service.h"
initial.commit09911bf2008-07-26 23:55:2992#include "chrome/common/pref_names.h"
[email protected]420ae012009-04-24 05:16:3293#include "chrome/common/render_messages.h"
[email protected]939856a2010-08-24 20:29:0294#include "chrome/common/render_messages_params.h"
[email protected]96d185d2009-04-24 03:28:5495#include "chrome/common/url_constants.h"
[email protected]ef0d04c2010-12-15 23:34:2196#include "gfx/codec/png_codec.h"
[email protected]ea68c06a2010-04-23 17:25:3797#include "grit/chromium_strings.h"
[email protected]8897c382009-05-06 17:53:2698#include "grit/generated_resources.h"
[email protected]420ae012009-04-24 05:16:3299#include "grit/locale_settings.h"
[email protected]fd626782010-01-22 05:26:06100#include "grit/platform_locale_settings.h"
[email protected]ea68c06a2010-04-23 17:25:37101#include "grit/theme_resources.h"
[email protected]d686e812009-06-03 19:10:29102#include "net/base/net_util.h"
[email protected]420ae012009-04-24 05:16:32103#include "net/base/registry_controlled_domain.h"
[email protected]8bd0fe62011-01-17 06:44:37104#include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
[email protected]c051a1b2011-01-21 23:30:17105#include "ui/base/l10n/l10n_util.h"
[email protected]42ce29d2011-01-20 23:19:46106#include "ui/base/resource/resource_bundle.h"
[email protected]d5e311b2010-07-20 17:15:03107#include "webkit/glue/password_form.h"
[email protected]191eb3f72010-12-21 06:27:50108#include "webkit/plugins/npapi/plugin_list.h"
[email protected]3c733bde2010-12-21 19:56:31109#include "webkit/glue/webpreferences.h"
110
111#if defined(OS_MACOSX)
112#include "app/surface/io_surface_support_mac.h"
113#endif // defined(OS_MACOSX)
[email protected]3e45ba92009-02-20 21:09:00114
[email protected]61d68ef12011-01-13 14:02:56115#if defined(OS_CHROMEOS)
116#include "chrome/browser/chromeos/locale_change_guard.h"
117#endif // defined(OS_CHROMEOS)
118
[email protected]420ae012009-04-24 05:16:32119// Cross-Site Navigations
120//
121// If a TabContents is told to navigate to a different web site (as determined
122// by SiteInstance), it will replace its current RenderViewHost with a new
123// RenderViewHost dedicated to the new SiteInstance. This works as follows:
124//
125// - Navigate determines whether the destination is cross-site, and if so,
126// it creates a pending_render_view_host_ and moves into the PENDING
127// RendererState.
128// - The pending RVH is "suspended," so that no navigation messages are sent to
129// its renderer until the onbeforeunload JavaScript handler has a chance to
130// run in the current RVH.
131// - The pending RVH tells CrossSiteRequestManager (a thread-safe singleton)
132// that it has a pending cross-site request. ResourceDispatcherHost will
133// check for this when the response arrives.
134// - The current RVH runs its onbeforeunload handler. If it returns false, we
135// cancel all the pending logic and go back to NORMAL. Otherwise we allow
136// the pending RVH to send the navigation request to its renderer.
137// - ResourceDispatcherHost receives a ResourceRequest on the IO thread. It
138// checks CrossSiteRequestManager to see that the RVH responsible has a
139// pending cross-site request, and then installs a CrossSiteEventHandler.
140// - When RDH receives a response, the BufferedEventHandler determines whether
141// it is a download. If so, it sends a message to the new renderer causing
142// it to cancel the request, and the download proceeds in the download
143// thread. For now, we stay in a PENDING state (with a pending RVH) until
[email protected]57c6a652009-05-04 07:58:34144// the next DidNavigate event for this TabContents. This isn't ideal, but it
[email protected]420ae012009-04-24 05:16:32145// doesn't affect any functionality.
146// - After RDH receives a response and determines that it is safe and not a
147// download, it pauses the response to first run the old page's onunload
148// handler. It does this by asynchronously calling the OnCrossSiteResponse
[email protected]57c6a652009-05-04 07:58:34149// method of TabContents on the UI thread, which sends a ClosePage message
[email protected]420ae012009-04-24 05:16:32150// to the current RVH.
151// - Once the onunload handler is finished, a ClosePage_ACK message is sent to
152// the ResourceDispatcherHost, who unpauses the response. Data is then sent
153// to the pending RVH.
154// - The pending renderer sends a FrameNavigate message that invokes the
155// DidNavigate method. This replaces the current RVH with the
156// pending RVH and goes back to the NORMAL RendererState.
157
158namespace {
159
160// Amount of time we wait between when a key event is received and the renderer
161// is queried for its state and pushed to the NavigationEntry.
162const int kQueryStateDelay = 5000;
163
[email protected]6ebdc9b2010-09-27 16:55:57164const int kSyncWaitDelay = 40;
165
[email protected]420ae012009-04-24 05:16:32166// If another javascript message box is displayed within
167// kJavascriptMessageExpectedDelay of a previous javascript message box being
168// dismissed, display an option to suppress future message boxes from this
169// contents.
170const int kJavascriptMessageExpectedDelay = 1000;
171
[email protected]420ae012009-04-24 05:16:32172// The list of prefs we want to observe.
[email protected]57ecc4b2010-08-11 03:02:51173const char* kPrefsToObserve[] = {
[email protected]420ae012009-04-24 05:16:32174 prefs::kAlternateErrorPagesEnabled,
[email protected]d0b8d092010-10-25 04:05:17175 prefs::kDefaultZoomLevel,
[email protected]420ae012009-04-24 05:16:32176 prefs::kWebKitJavaEnabled,
177 prefs::kWebKitJavascriptEnabled,
178 prefs::kWebKitLoadsImagesAutomatically,
179 prefs::kWebKitPluginsEnabled,
180 prefs::kWebKitUsesUniversalDetector,
181 prefs::kWebKitSerifFontFamily,
182 prefs::kWebKitSansSerifFontFamily,
183 prefs::kWebKitFixedFontFamily,
184 prefs::kWebKitDefaultFontSize,
185 prefs::kWebKitDefaultFixedFontSize,
[email protected]6bdf05b2010-10-13 20:34:13186 prefs::kWebKitMinimumFontSize,
187 prefs::kWebKitMinimumLogicalFontSize,
[email protected]1661a3c02010-05-20 16:32:36188 prefs::kWebkitTabsToLinks,
[email protected]420ae012009-04-24 05:16:32189 prefs::kDefaultCharset
190 // kWebKitStandardFontIsSerif needs to be added
191 // if we let users pick which font to use, serif or sans-serif when
192 // no font is specified or a CSS generic family (serif or sans-serif)
193 // is not specified.
194};
195
196const int kPrefsToObserveLength = arraysize(kPrefsToObserve);
197
[email protected]420ae012009-04-24 05:16:32198// Returns true if the entry's transition type is FORM_SUBMIT.
199bool IsFormSubmit(const NavigationEntry* entry) {
200 return (PageTransition::StripQualifier(entry->transition_type()) ==
201 PageTransition::FORM_SUBMIT);
202}
203
204#if defined(OS_WIN)
205
206BOOL CALLBACK InvalidateWindow(HWND hwnd, LPARAM lparam) {
207 // Note: erase is required to properly paint some widgets borders. This can
208 // be seen with textfields.
209 InvalidateRect(hwnd, NULL, TRUE);
210 return TRUE;
211}
212#endif
213
[email protected]5e369672009-11-03 23:48:30214ViewMsg_Navigate_Params::NavigationType GetNavigationType(
[email protected]1ccb3568d2010-02-19 10:51:16215 Profile* profile, const NavigationEntry& entry,
216 NavigationController::ReloadType reload_type) {
217 switch (reload_type) {
218 case NavigationController::RELOAD:
219 return ViewMsg_Navigate_Params::RELOAD;
220 case NavigationController::RELOAD_IGNORING_CACHE:
221 return ViewMsg_Navigate_Params::RELOAD_IGNORING_CACHE;
222 case NavigationController::NO_RELOAD:
223 break; // Fall through to rest of function.
224 }
[email protected]5e369672009-11-03 23:48:30225
226 if (entry.restore_type() == NavigationEntry::RESTORE_LAST_SESSION &&
227 profile->DidLastSessionExitCleanly())
228 return ViewMsg_Navigate_Params::RESTORE;
229
230 return ViewMsg_Navigate_Params::NORMAL;
231}
232
[email protected]876bc832010-09-07 16:29:54233void MakeNavigateParams(const NavigationEntry& entry,
234 const NavigationController& controller,
[email protected]1ccb3568d2010-02-19 10:51:16235 NavigationController::ReloadType reload_type,
236 ViewMsg_Navigate_Params* params) {
[email protected]056de2d2009-06-26 16:41:34237 params->page_id = entry.page_id();
[email protected]876bc832010-09-07 16:29:54238 params->pending_history_list_offset = controller.GetIndexOfEntry(&entry);
[email protected]3cc72b12010-03-18 23:03:00239 params->current_history_list_offset = controller.last_committed_entry_index();
240 params->current_history_list_length = controller.entry_count();
[email protected]056de2d2009-06-26 16:41:34241 params->url = entry.url();
242 params->referrer = entry.referrer();
243 params->transition = entry.transition_type();
244 params->state = entry.content_state();
[email protected]3cc72b12010-03-18 23:03:00245 params->navigation_type =
246 GetNavigationType(controller.profile(), entry, reload_type);
[email protected]056de2d2009-06-26 16:41:34247 params->request_time = base::Time::Now();
248}
249
[email protected]f4f50ef2011-01-21 19:01:19250
251// OutdatedPluginInfoBar ------------------------------------------------------
252
[email protected]3c2826852010-12-16 19:09:14253class OutdatedPluginInfoBar : public ConfirmInfoBarDelegate {
[email protected]851b1eb2010-08-09 13:32:29254 public:
[email protected]3c2826852010-12-16 19:09:14255 OutdatedPluginInfoBar(TabContents* tab_contents,
[email protected]851b1eb2010-08-09 13:32:29256 const string16& name,
[email protected]f4f50ef2011-01-21 19:01:19257 const GURL& update_url);
[email protected]f5ec1ba2010-12-22 17:47:40258
[email protected]851b1eb2010-08-09 13:32:29259 private:
[email protected]f4f50ef2011-01-21 19:01:19260 virtual ~OutdatedPluginInfoBar();
261
262 // ConfirmInfoBarDelegate:
263 virtual void InfoBarClosed();
264 virtual SkBitmap* GetIcon() const;
265 virtual string16 GetMessageText() const;
266 virtual int GetButtons() const;
267 virtual string16 GetButtonLabel(InfoBarButton button) const;
268 virtual bool Accept();
269 virtual bool Cancel();
270 virtual string16 GetLinkText();
271 virtual bool LinkClicked(WindowOpenDisposition disposition);
272
[email protected]851b1eb2010-08-09 13:32:29273 TabContents* tab_contents_;
274 string16 name_;
275 GURL update_url_;
276};
277
[email protected]f4f50ef2011-01-21 19:01:19278OutdatedPluginInfoBar::OutdatedPluginInfoBar(TabContents* tab_contents,
279 const string16& name,
280 const GURL& update_url)
281 : ConfirmInfoBarDelegate(tab_contents),
282 tab_contents_(tab_contents),
283 name_(name),
284 update_url_(update_url) {
285 UserMetrics::RecordAction(UserMetricsAction("OutdatedPluginInfobar.Shown"));
286 tab_contents->AddInfoBar(this);
287}
288
289OutdatedPluginInfoBar::~OutdatedPluginInfoBar() {
290}
291
292void OutdatedPluginInfoBar::InfoBarClosed() {
293 UserMetrics::RecordAction(UserMetricsAction("OutdatedPluginInfobar.Closed"));
294 delete this;
295}
296
297SkBitmap* OutdatedPluginInfoBar::GetIcon() const {
298 return ResourceBundle::GetSharedInstance().GetBitmapNamed(
299 IDR_INFOBAR_PLUGIN_INSTALL);
300}
301
302string16 OutdatedPluginInfoBar::GetMessageText() const {
303 return l10n_util::GetStringFUTF16(IDS_PLUGIN_OUTDATED_PROMPT, name_);
304}
305
306int OutdatedPluginInfoBar::GetButtons() const {
307 return BUTTON_OK | BUTTON_CANCEL;
308}
309
310string16 OutdatedPluginInfoBar::GetButtonLabel(InfoBarButton button) const {
311 return l10n_util::GetStringUTF16((button == BUTTON_OK) ?
312 IDS_PLUGIN_UPDATE : IDS_PLUGIN_ENABLE_TEMPORARILY);
313}
314
315bool OutdatedPluginInfoBar::Accept() {
316 UserMetrics::RecordAction(UserMetricsAction("OutdatedPluginInfobar.Update"));
317 tab_contents_->OpenURL(update_url_, GURL(), NEW_FOREGROUND_TAB,
318 PageTransition::LINK);
319 return false;
320}
321
322bool OutdatedPluginInfoBar::Cancel() {
323 UserMetrics::RecordAction(
324 UserMetricsAction("OutdatedPluginInfobar.AllowThisTime"));
325 tab_contents_->render_view_host()->LoadBlockedPlugins();
326 return false;
327}
328
329string16 OutdatedPluginInfoBar::GetLinkText() {
330 return l10n_util::GetStringUTF16(IDS_LEARN_MORE);
331}
332
333bool OutdatedPluginInfoBar::LinkClicked(WindowOpenDisposition disposition) {
334 UserMetrics::RecordAction(
335 UserMetricsAction("OutdatedPluginInfobar.LearnMore"));
336 // TODO(bauerb): Navigate to a help page explaining why we disabled
337 // the plugin, once we have one.
338 return false;
339}
340
[email protected]420ae012009-04-24 05:16:32341} // namespace
342
[email protected]f4f50ef2011-01-21 19:01:19343
344// TabContents ----------------------------------------------------------------
[email protected]420ae012009-04-24 05:16:32345
[email protected]fdd61c62009-04-22 19:22:57346// static
347int TabContents::find_request_id_counter_ = -1;
348
[email protected]420ae012009-04-24 05:16:32349
[email protected]420ae012009-04-24 05:16:32350TabContents::TabContents(Profile* profile,
351 SiteInstance* site_instance,
352 int routing_id,
[email protected]6ee12c42010-09-14 09:36:07353 const TabContents* base_tab_contents,
354 SessionStorageNamespace* session_storage_namespace)
[email protected]b680ad22009-04-15 23:19:42355 : delegate_(NULL),
[email protected]6ee12c42010-09-14 09:36:07356 ALLOW_THIS_IN_INITIALIZER_LIST(controller_(
357 this, profile, session_storage_namespace)),
[email protected]66ba4932009-06-04 19:22:13358 ALLOW_THIS_IN_INITIALIZER_LIST(view_(
[email protected]d82ed61e2009-06-16 02:46:22359 TabContentsView::Create(this))),
360 ALLOW_THIS_IN_INITIALIZER_LIST(render_manager_(this, this)),
[email protected]fdd61c62009-04-22 19:22:57361 property_bag_(),
362 registrar_(),
[email protected]12802702010-07-09 19:43:09363 ALLOW_THIS_IN_INITIALIZER_LIST(printing_(
364 new printing::PrintViewManager(*this))),
[email protected]fdd61c62009-04-22 19:22:57365 save_package_(),
[email protected]6db0def92010-03-02 19:55:45366 autocomplete_history_manager_(),
[email protected]21674df2009-10-28 19:51:21367 autofill_manager_(),
[email protected]fdd61c62009-04-22 19:22:57368 plugin_installer_(),
[email protected]6f821dd2010-02-23 00:54:06369 bookmark_drag_(NULL),
[email protected]d82ed61e2009-06-16 02:46:22370 ALLOW_THIS_IN_INITIALIZER_LIST(fav_icon_helper_(this)),
[email protected]d5f942ba2008-09-26 19:30:34371 is_loading_(false),
[email protected]443b80e2010-12-14 00:42:23372 crashed_status_(base::TERMINATION_STATUS_STILL_RUNNING),
373 crashed_error_code_(0),
[email protected]d5f942ba2008-09-26 19:30:34374 waiting_for_response_(false),
[email protected]d5f942ba2008-09-26 19:30:34375 max_page_id_(-1),
[email protected]fdd61c62009-04-22 19:22:57376 current_load_start_(),
377 load_state_(net::LOAD_STATE_IDLE),
378 load_state_host_(),
[email protected]094e5b22009-09-25 04:23:56379 upload_size_(0),
380 upload_position_(0),
[email protected]fdd61c62009-04-22 19:22:57381 received_page_title_(false),
382 is_starred_(false),
383 contents_mime_type_(),
384 encoding_(),
[email protected]c65e2f152010-10-14 15:30:40385 blocked_contents_(NULL),
386 all_contents_blocked_(false),
[email protected]8cc8d492010-02-02 10:40:49387 dont_notify_render_view_(false),
[email protected]f17a0ee2010-05-17 17:38:47388 displayed_insecure_content_(false),
[email protected]fdd61c62009-04-22 19:22:57389 infobar_delegates_(),
[email protected]fdd61c62009-04-22 19:22:57390 find_ui_active_(false),
391 find_op_aborted_(false),
392 current_find_request_id_(find_request_id_counter_++),
[email protected]e491f1c2009-05-22 20:28:12393 last_search_case_sensitive_(false),
[email protected]e491f1c2009-05-22 20:28:12394 last_search_result_(),
[email protected]98aa0b52010-05-06 17:03:08395 extension_app_(NULL),
[email protected]fdd61c62009-04-22 19:22:57396 capturing_contents_(false),
397 is_being_destroyed_(false),
398 notify_disconnection_(false),
399 history_requests_(),
400#if defined(OS_WIN)
401 message_box_active_(CreateEvent(NULL, TRUE, FALSE, NULL)),
402#endif
403 last_javascript_message_dismissal_(),
[email protected]c2e74fe82009-09-03 17:57:44404 suppress_javascript_messages_(false),
[email protected]7ab1e7d62009-10-14 23:32:01405 is_showing_before_unload_dialog_(false),
[email protected]7ff431e2010-01-07 18:18:54406 renderer_preferences_(),
[email protected]1a242c32010-02-02 21:20:54407 opener_dom_ui_type_(DOMUIFactory::kNoDOMUI),
[email protected]777a7b62010-05-11 13:28:16408 language_state_(&controller_),
[email protected]ebf40a72010-07-22 01:46:38409 closed_by_user_gesture_(false),
[email protected]b75b8292010-10-01 07:28:25410 minimum_zoom_percent_(
411 static_cast<int>(WebKit::WebView::minTextSizeMultiplier * 100)),
412 maximum_zoom_percent_(
413 static_cast<int>(WebKit::WebView::maxTextSizeMultiplier * 100)),
[email protected]9e823662010-10-13 23:36:00414 temporary_zoom_settings_(false),
415 content_restrictions_(0) {
[email protected]7ff431e2010-01-07 18:18:54416 renderer_preferences_util::UpdateFromSystemSettings(
417 &renderer_preferences_, profile);
418
[email protected]9fb83e82010-07-02 18:24:55419 content_settings_delegate_.reset(
420 new TabSpecificContentSettings(this, profile));
421
[email protected]12636df2009-09-28 22:32:21422 render_manager_.Init(profile, site_instance, routing_id);
[email protected]420ae012009-04-24 05:16:32423
[email protected]34ac70502009-09-25 17:07:23424 // We have the initial size of the view be based on the size of the passed in
425 // tab contents (normally a tab from the same window).
426 view_->CreateView(base_tab_contents ?
427 base_tab_contents->view()->GetContainerSize() : gfx::Size());
[email protected]420ae012009-04-24 05:16:32428
429 // Register for notifications about all interested prefs change.
430 PrefService* prefs = profile->GetPrefs();
[email protected]2fb7dc982010-09-29 12:24:28431 pref_change_registrar_.Init(prefs);
[email protected]420ae012009-04-24 05:16:32432 if (prefs) {
433 for (int i = 0; i < kPrefsToObserveLength; ++i)
[email protected]2fb7dc982010-09-29 12:24:28434 pref_change_registrar_.Add(kPrefsToObserve[i], this);
[email protected]420ae012009-04-24 05:16:32435 }
436
437 // Register for notifications about URL starredness changing on any profile.
438 registrar_.Add(this, NotificationType::URLS_STARRED,
439 NotificationService::AllSources());
440 registrar_.Add(this, NotificationType::BOOKMARK_MODEL_LOADED,
441 NotificationService::AllSources());
442 registrar_.Add(this, NotificationType::RENDER_WIDGET_HOST_DESTROYED,
443 NotificationService::AllSources());
[email protected]d3ba77272009-09-03 00:06:09444#if defined(OS_LINUX)
445 registrar_.Add(this, NotificationType::BROWSER_THEME_CHANGED,
446 NotificationService::AllSources());
447#endif
[email protected]420ae012009-04-24 05:16:32448
[email protected]6c32ce72010-03-08 05:18:06449 registrar_.Add(this, NotificationType::USER_STYLE_SHEET_UPDATED,
450 NotificationService::AllSources());
451
[email protected]79580c62010-02-02 02:36:25452 // Register for notifications about content setting changes.
453 registrar_.Add(this, NotificationType::CONTENT_SETTINGS_CHANGED,
454 NotificationService::AllSources());
455
[email protected]a888b29e62010-04-01 13:38:57456 // Listen for extension changes so we can update extension_for_current_page_.
457 registrar_.Add(this, NotificationType::EXTENSION_LOADED,
458 NotificationService::AllSources());
459 registrar_.Add(this, NotificationType::EXTENSION_UNLOADED,
460 NotificationService::AllSources());
[email protected]a888b29e62010-04-01 13:38:57461
[email protected]88152052010-10-01 04:05:18462 // Listen for Google URL changes
463 registrar_.Add(this, NotificationType::GOOGLE_URL_UPDATED,
464 NotificationService::AllSources());
465
[email protected]c8865482009-07-23 20:40:10466 // Set-up the showing of the omnibox search infobar if applicable.
467 if (OmniboxSearchHint::IsEnabled(profile))
468 omnibox_search_hint_.reset(new OmniboxSearchHint(this));
[email protected]403415a2011-01-10 18:57:53469
470 autofill_manager_.reset(new AutoFillManager(this));
[email protected]0f180ee2011-01-21 18:23:10471 AddNavigationObserver(autofill_manager_.get());
[email protected]403415a2011-01-10 18:57:53472 autocomplete_history_manager_.reset(new AutocompleteHistoryManager(this));
[email protected]0f180ee2011-01-21 18:23:10473 AddNavigationObserver(autocomplete_history_manager_.get());
[email protected]332af7732009-03-11 13:21:35474}
initial.commit09911bf2008-07-26 23:55:29475
476TabContents::~TabContents() {
[email protected]420ae012009-04-24 05:16:32477 is_being_destroyed_ = true;
478
[email protected]bfe4c1582009-10-02 18:11:09479 // We don't want any notifications while we're running our destructor.
[email protected]420ae012009-04-24 05:16:32480 registrar_.RemoveAll();
[email protected]2fb7dc982010-09-29 12:24:28481 pref_change_registrar_.RemoveAll();
[email protected]420ae012009-04-24 05:16:32482
[email protected]420ae012009-04-24 05:16:32483 NotifyDisconnected();
[email protected]e7b8d832009-11-20 22:41:00484 hung_renderer_dialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:32485
[email protected]420ae012009-04-24 05:16:32486 // First cleanly close all child windows.
487 // TODO(mpcomplete): handle case if MaybeCloseChildWindows() already asked
488 // some of these to close. CloseWindows is async, so it might get called
489 // twice before it runs.
[email protected]8cc8d492010-02-02 10:40:49490 CloseConstrainedWindows();
[email protected]420ae012009-04-24 05:16:32491
[email protected]c65e2f152010-10-14 15:30:40492 // Close all blocked contents.
493 if (blocked_contents_)
494 blocked_contents_->Destroy();
[email protected]66ba4932009-06-04 19:22:13495
[email protected]420ae012009-04-24 05:16:32496 // Notify any observer that have a reference on this tab contents.
497 NotificationService::current()->Notify(
498 NotificationType::TAB_CONTENTS_DESTROYED,
499 Source<TabContents>(this),
500 NotificationService::NoDetails());
501
[email protected]1e5e74f2009-05-27 20:55:12502 // Notify any lasting InfobarDelegates that have not yet been removed that
503 // whatever infobar they were handling in this TabContents has closed,
504 // because the TabContents is going away entirely.
505 // This must happen after the TAB_CONTENTS_DESTROYED notification as the
506 // notification may trigger infobars calls that access their delegate. (and
507 // some implementations of InfoBarDelegate do delete themselves on
508 // InfoBarClosed()).
509 for (int i = 0; i < infobar_delegate_count(); ++i) {
510 InfoBarDelegate* delegate = GetInfoBarDelegateAt(i);
511 delegate->InfoBarClosed();
512 }
513 infobar_delegates_.clear();
514
[email protected]420ae012009-04-24 05:16:32515 // TODO(brettw) this should be moved to the view.
516#if defined(OS_WIN)
517 // If we still have a window handle, destroy it. GetNativeView can return
518 // NULL if this contents was part of a window that closed.
[email protected]50bd6452010-11-27 19:39:42519 if (GetNativeView()) {
520 RenderViewHost* host = render_view_host();
521 if (host && host->view()) {
522 host->view()->WillWmDestroy();
523 }
[email protected]420ae012009-04-24 05:16:32524 ::DestroyWindow(GetNativeView());
[email protected]50bd6452010-11-27 19:39:42525 }
[email protected]420ae012009-04-24 05:16:32526#endif
[email protected]7ab1e7d62009-10-14 23:32:01527
528 // OnCloseStarted isn't called in unit tests.
529 if (!tab_close_start_time_.is_null()) {
530 UMA_HISTOGRAM_TIMES("Tab.Close",
531 base::TimeTicks::Now() - tab_close_start_time_);
532 }
initial.commit09911bf2008-07-26 23:55:29533}
534
[email protected]d5f942ba2008-09-26 19:30:34535// static
536void TabContents::RegisterUserPrefs(PrefService* prefs) {
[email protected]420ae012009-04-24 05:16:32537 prefs->RegisterBooleanPref(prefs::kAlternateErrorPagesEnabled, true);
538
539 WebPreferences pref_defaults;
540 prefs->RegisterBooleanPref(prefs::kWebKitJavascriptEnabled,
541 pref_defaults.javascript_enabled);
542 prefs->RegisterBooleanPref(prefs::kWebKitWebSecurityEnabled,
543 pref_defaults.web_security_enabled);
544 prefs->RegisterBooleanPref(
545 prefs::kWebKitJavascriptCanOpenWindowsAutomatically, true);
546 prefs->RegisterBooleanPref(prefs::kWebKitLoadsImagesAutomatically,
547 pref_defaults.loads_images_automatically);
548 prefs->RegisterBooleanPref(prefs::kWebKitPluginsEnabled,
549 pref_defaults.plugins_enabled);
550 prefs->RegisterBooleanPref(prefs::kWebKitDomPasteEnabled,
551 pref_defaults.dom_paste_enabled);
552 prefs->RegisterBooleanPref(prefs::kWebKitShrinksStandaloneImagesToFit,
553 pref_defaults.shrinks_standalone_images_to_fit);
[email protected]c4e98902010-06-01 10:20:14554 prefs->RegisterDictionaryPref(prefs::kWebKitInspectorSettings);
[email protected]420ae012009-04-24 05:16:32555 prefs->RegisterBooleanPref(prefs::kWebKitTextAreasAreResizable,
556 pref_defaults.text_areas_are_resizable);
557 prefs->RegisterBooleanPref(prefs::kWebKitJavaEnabled,
558 pref_defaults.java_enabled);
[email protected]1661a3c02010-05-20 16:32:36559 prefs->RegisterBooleanPref(prefs::kWebkitTabsToLinks,
560 pref_defaults.tabs_to_links);
[email protected]420ae012009-04-24 05:16:32561
562 prefs->RegisterLocalizedStringPref(prefs::kAcceptLanguages,
563 IDS_ACCEPT_LANGUAGES);
564 prefs->RegisterLocalizedStringPref(prefs::kDefaultCharset,
565 IDS_DEFAULT_ENCODING);
566 prefs->RegisterLocalizedBooleanPref(prefs::kWebKitStandardFontIsSerif,
567 IDS_STANDARD_FONT_IS_SERIF);
568 prefs->RegisterLocalizedStringPref(prefs::kWebKitFixedFontFamily,
569 IDS_FIXED_FONT_FAMILY);
570 prefs->RegisterLocalizedStringPref(prefs::kWebKitSerifFontFamily,
571 IDS_SERIF_FONT_FAMILY);
572 prefs->RegisterLocalizedStringPref(prefs::kWebKitSansSerifFontFamily,
573 IDS_SANS_SERIF_FONT_FAMILY);
574 prefs->RegisterLocalizedStringPref(prefs::kWebKitCursiveFontFamily,
575 IDS_CURSIVE_FONT_FAMILY);
576 prefs->RegisterLocalizedStringPref(prefs::kWebKitFantasyFontFamily,
577 IDS_FANTASY_FONT_FAMILY);
578 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitDefaultFontSize,
579 IDS_DEFAULT_FONT_SIZE);
580 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitDefaultFixedFontSize,
581 IDS_DEFAULT_FIXED_FONT_SIZE);
582 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitMinimumFontSize,
583 IDS_MINIMUM_FONT_SIZE);
584 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitMinimumLogicalFontSize,
585 IDS_MINIMUM_LOGICAL_FONT_SIZE);
586 prefs->RegisterLocalizedBooleanPref(prefs::kWebKitUsesUniversalDetector,
587 IDS_USES_UNIVERSAL_DETECTOR);
588 prefs->RegisterLocalizedStringPref(prefs::kStaticEncodings,
589 IDS_STATIC_ENCODING_LIST);
initial.commit09911bf2008-07-26 23:55:29590}
591
[email protected]724159a2010-12-30 01:11:18592bool TabContents::OnMessageReceived(const IPC::Message& message) {
[email protected]0f180ee2011-01-21 18:23:10593 ObserverListBase<WebNavigationObserver>::Iterator it(
594 web_navigation_observers_);
595 WebNavigationObserver* observer;
596 while ((observer = it.GetNext()) != NULL)
597 if (observer->OnMessageReceived(message))
[email protected]403415a2011-01-10 18:57:53598 return true;
[email protected]403415a2011-01-10 18:57:53599
[email protected]724159a2010-12-30 01:11:18600 bool handled = true;
601 bool message_is_ok = true;
602 IPC_BEGIN_MESSAGE_MAP_EX(TabContents, message, message_is_ok)
603 IPC_MESSAGE_HANDLER(ViewHostMsg_DidStartProvisionalLoadForFrame,
604 OnDidStartProvisionalLoadForFrame)
605 IPC_MESSAGE_HANDLER(ViewHostMsg_DidRedirectProvisionalLoad,
606 OnDidRedirectProvisionalLoad)
607 IPC_MESSAGE_HANDLER(ViewHostMsg_DidFailProvisionalLoadWithError,
608 OnDidFailProvisionalLoadWithError)
609 IPC_MESSAGE_HANDLER(ViewHostMsg_DidLoadResourceFromMemoryCache,
610 OnDidLoadResourceFromMemoryCache)
611 IPC_MESSAGE_HANDLER(ViewHostMsg_DidDisplayInsecureContent,
612 OnDidDisplayInsecureContent)
613 IPC_MESSAGE_HANDLER(ViewHostMsg_DidRunInsecureContent,
614 OnDidRunInsecureContent)
615 IPC_MESSAGE_HANDLER(ViewHostMsg_DocumentLoadedInFrame,
616 OnDocumentLoadedInFrame)
617 IPC_MESSAGE_HANDLER(ViewHostMsg_DidFinishLoad, OnDidFinishLoad)
618 IPC_MESSAGE_UNHANDLED(handled = false)
619 IPC_END_MESSAGE_MAP_EX()
620
621 if (!message_is_ok) {
622 UserMetrics::RecordAction(UserMetricsAction("BadMessageTerminate_RVD"));
623 GetRenderProcessHost()->ReceivedBadMessage();
624 }
625
626 return handled;
627}
628
[email protected]13367f72009-07-08 16:39:02629// Returns true if contains content rendered by an extension.
630bool TabContents::HostsExtension() const {
631 return GetURL().SchemeIs(chrome::kExtensionScheme);
[email protected]d5f942ba2008-09-26 19:30:34632}
633
[email protected]f4f50ef2011-01-21 19:01:19634PluginInstallerInfoBarDelegate* TabContents::GetPluginInstaller() {
[email protected]6e95706d2009-04-23 22:55:01635 if (plugin_installer_.get() == NULL)
[email protected]f4f50ef2011-01-21 19:01:19636 plugin_installer_.reset(new PluginInstallerInfoBarDelegate(this));
[email protected]6e95706d2009-04-23 22:55:01637 return plugin_installer_.get();
638}
639
[email protected]1f18184a2010-07-21 19:34:49640TabContentsSSLHelper* TabContents::GetSSLHelper() {
641 if (ssl_helper_.get() == NULL)
642 ssl_helper_.reset(new TabContentsSSLHelper(this));
643 return ssl_helper_.get();
644}
645
[email protected]8cb5d5b2010-02-09 11:36:16646RenderProcessHost* TabContents::GetRenderProcessHost() const {
647 return render_manager_.current_host()->process();
648}
649
[email protected]9adb9692010-10-29 23:14:02650void TabContents::SetExtensionApp(const Extension* extension) {
[email protected]867a73e12010-03-19 20:45:46651 DCHECK(!extension || extension->GetFullLaunchURL().is_valid());
[email protected]98aa0b52010-05-06 17:03:08652 extension_app_ = extension;
[email protected]fca656c2010-02-10 20:30:10653
[email protected]98aa0b52010-05-06 17:03:08654 UpdateExtensionAppIcon(extension_app_);
[email protected]a888b29e62010-04-01 13:38:57655
[email protected]fca656c2010-02-10 20:30:10656 NotificationService::current()->Notify(
657 NotificationType::TAB_CONTENTS_APPLICATION_EXTENSION_CHANGED,
658 Source<TabContents>(this),
659 NotificationService::NoDetails());
[email protected]3b355642010-02-05 16:01:49660}
661
[email protected]98aa0b52010-05-06 17:03:08662void TabContents::SetExtensionAppById(const std::string& extension_app_id) {
663 if (extension_app_id.empty())
[email protected]911f8152010-03-18 16:46:40664 return;
665
[email protected]eaa7dd182010-12-14 11:09:00666 ExtensionService* extension_service = profile()->GetExtensionService();
[email protected]fa6a9102010-11-22 15:38:50667 if (!extension_service || !extension_service->is_ready())
668 return;
669
670 const Extension* extension =
671 extension_service->GetExtensionById(extension_app_id, false);
672 if (extension)
673 SetExtensionApp(extension);
[email protected]911f8152010-03-18 16:46:40674}
675
[email protected]98aa0b52010-05-06 17:03:08676SkBitmap* TabContents::GetExtensionAppIcon() {
[email protected]98aa0b52010-05-06 17:03:08677 if (extension_app_icon_.empty())
[email protected]8eb043b2010-04-30 07:19:51678 return NULL;
679
[email protected]98aa0b52010-05-06 17:03:08680 return &extension_app_icon_;
[email protected]8eb043b2010-04-30 07:19:51681}
682
[email protected]d5f942ba2008-09-26 19:30:34683const GURL& TabContents::GetURL() const {
684 // We may not have a navigation entry yet
[email protected]ce3fa3c2009-04-20 19:55:57685 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]ebe89e062009-08-13 23:16:54686 return entry ? entry->virtual_url() : GURL::EmptyGURL();
[email protected]d5f942ba2008-09-26 19:30:34687}
688
[email protected]96d185d2009-04-24 03:28:54689const string16& TabContents::GetTitle() const {
[email protected]4c6092c5b2009-06-06 00:23:55690 // Transient entries take precedence. They are used for interstitial pages
691 // that are shown on top of existing pages.
692 NavigationEntry* entry = controller_.GetTransientEntry();
[email protected]45d0ef7f2011-01-05 13:46:23693 if (entry) {
694 return entry->GetTitleForDisplay(profile()->GetPrefs()->
695 GetString(prefs::kAcceptLanguages));
696 }
[email protected]96d185d2009-04-24 03:28:54697 DOMUI* our_dom_ui = render_manager_.pending_dom_ui() ?
698 render_manager_.pending_dom_ui() : render_manager_.dom_ui();
699 if (our_dom_ui) {
700 // Don't override the title in view source mode.
[email protected]4c6092c5b2009-06-06 00:23:55701 entry = controller_.GetActiveEntry();
[email protected]96d185d2009-04-24 03:28:54702 if (!(entry && entry->IsViewSourceMode())) {
703 // Give the DOM UI the chance to override our title.
704 const string16& title = our_dom_ui->overridden_title();
705 if (!title.empty())
706 return title;
707 }
708 }
709
710 // We use the title for the last committed entry rather than a pending
711 // navigation entry. For example, when the user types in a URL, we want to
712 // keep the old page's title until the new load has committed and we get a new
713 // title.
[email protected]96d185d2009-04-24 03:28:54714 entry = controller_.GetLastCommittedEntry();
[email protected]45d0ef7f2011-01-05 13:46:23715 if (entry) {
716 return entry->GetTitleForDisplay(profile()->GetPrefs()->
717 GetString(prefs::kAcceptLanguages));
718 }
[email protected]96d185d2009-04-24 03:28:54719 return EmptyString16();
720}
721
[email protected]bbbc1ef2010-02-12 18:03:41722// static
723string16 TabContents::GetDefaultTitle() {
724 return l10n_util::GetStringUTF16(IDS_DEFAULT_TAB_TITLE);
725}
726
[email protected]d5f942ba2008-09-26 19:30:34727int32 TabContents::GetMaxPageID() {
728 if (GetSiteInstance())
729 return GetSiteInstance()->max_page_id();
730 else
731 return max_page_id_;
732}
733
734void TabContents::UpdateMaxPageID(int32 page_id) {
735 // Ensure both the SiteInstance and RenderProcessHost update their max page
[email protected]57c6a652009-05-04 07:58:34736 // IDs in sync. Only TabContents will also have site instances, except during
[email protected]d5f942ba2008-09-26 19:30:34737 // testing.
738 if (GetSiteInstance())
739 GetSiteInstance()->UpdateMaxPageID(page_id);
[email protected]8cb5d5b2010-02-09 11:36:16740 GetRenderProcessHost()->UpdateMaxPageID(page_id);
[email protected]d5f942ba2008-09-26 19:30:34741}
742
[email protected]96d185d2009-04-24 03:28:54743SiteInstance* TabContents::GetSiteInstance() const {
744 return render_manager_.current_host()->site_instance();
745}
746
[email protected]96d185d2009-04-24 03:28:54747bool TabContents::ShouldDisplayURL() {
[email protected]8ab24cc2009-11-13 20:56:15748 // Don't hide the url in view source mode and with interstitials.
[email protected]96d185d2009-04-24 03:28:54749 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]8ab24cc2009-11-13 20:56:15750 if (entry && (entry->IsViewSourceMode() ||
[email protected]cccd3762010-11-12 18:40:01751 entry->page_type() == INTERSTITIAL_PAGE)) {
[email protected]96d185d2009-04-24 03:28:54752 return true;
[email protected]8ab24cc2009-11-13 20:56:15753 }
[email protected]83e3895b2009-06-11 00:07:16754
[email protected]96d185d2009-04-24 03:28:54755 DOMUI* dom_ui = GetDOMUIForCurrentState();
756 if (dom_ui)
757 return !dom_ui->should_hide_url();
758 return true;
759}
760
[email protected]d5f942ba2008-09-26 19:30:34761SkBitmap TabContents::GetFavIcon() const {
762 // Like GetTitle(), we also want to use the favicon for the last committed
763 // entry rather than a pending navigation entry.
[email protected]ce3fa3c2009-04-20 19:55:57764 NavigationEntry* entry = controller_.GetTransientEntry();
[email protected]cbab76d2008-10-13 22:42:47765 if (entry)
766 return entry->favicon().bitmap();
767
[email protected]ce3fa3c2009-04-20 19:55:57768 entry = controller_.GetLastCommittedEntry();
[email protected]d5f942ba2008-09-26 19:30:34769 if (entry)
770 return entry->favicon().bitmap();
[email protected]d5f942ba2008-09-26 19:30:34771 return SkBitmap();
772}
773
[email protected]0b303cc2009-09-28 22:35:15774bool TabContents::FavIconIsValid() const {
775 NavigationEntry* entry = controller_.GetTransientEntry();
776 if (entry)
777 return entry->favicon().is_valid();
778
779 entry = controller_.GetLastCommittedEntry();
780 if (entry)
781 return entry->favicon().is_valid();
782
783 return false;
784}
785
[email protected]96d185d2009-04-24 03:28:54786bool TabContents::ShouldDisplayFavIcon() {
787 // Always display a throbber during pending loads.
788 if (controller_.GetLastCommittedEntry() && controller_.pending_entry())
789 return true;
790
791 DOMUI* dom_ui = GetDOMUIForCurrentState();
792 if (dom_ui)
793 return !dom_ui->hide_favicon();
794 return true;
795}
796
[email protected]42197a22010-12-28 23:29:42797string16 TabContents::GetStatusText() const {
[email protected]96d185d2009-04-24 03:28:54798 if (!is_loading() || load_state_ == net::LOAD_STATE_IDLE)
[email protected]42197a22010-12-28 23:29:42799 return string16();
[email protected]96d185d2009-04-24 03:28:54800
801 switch (load_state_) {
802 case net::LOAD_STATE_WAITING_FOR_CACHE:
[email protected]42197a22010-12-28 23:29:42803 return l10n_util::GetStringUTF16(IDS_LOAD_STATE_WAITING_FOR_CACHE);
[email protected]8a1f3312010-05-25 19:25:04804 case net::LOAD_STATE_ESTABLISHING_PROXY_TUNNEL:
[email protected]42197a22010-12-28 23:29:42805 return
806 l10n_util::GetStringUTF16(IDS_LOAD_STATE_ESTABLISHING_PROXY_TUNNEL);
[email protected]96d185d2009-04-24 03:28:54807 case net::LOAD_STATE_RESOLVING_PROXY_FOR_URL:
[email protected]42197a22010-12-28 23:29:42808 return l10n_util::GetStringUTF16(IDS_LOAD_STATE_RESOLVING_PROXY_FOR_URL);
[email protected]96d185d2009-04-24 03:28:54809 case net::LOAD_STATE_RESOLVING_HOST:
[email protected]42197a22010-12-28 23:29:42810 return l10n_util::GetStringUTF16(IDS_LOAD_STATE_RESOLVING_HOST);
[email protected]96d185d2009-04-24 03:28:54811 case net::LOAD_STATE_CONNECTING:
[email protected]42197a22010-12-28 23:29:42812 return l10n_util::GetStringUTF16(IDS_LOAD_STATE_CONNECTING);
[email protected]375e3112010-05-27 20:32:06813 case net::LOAD_STATE_SSL_HANDSHAKE:
[email protected]42197a22010-12-28 23:29:42814 return l10n_util::GetStringUTF16(IDS_LOAD_STATE_SSL_HANDSHAKE);
[email protected]96d185d2009-04-24 03:28:54815 case net::LOAD_STATE_SENDING_REQUEST:
[email protected]094e5b22009-09-25 04:23:56816 if (upload_size_)
[email protected]42197a22010-12-28 23:29:42817 return l10n_util::GetStringFUTF16Int(
[email protected]094e5b22009-09-25 04:23:56818 IDS_LOAD_STATE_SENDING_REQUEST_WITH_PROGRESS,
819 static_cast<int>((100 * upload_position_) / upload_size_));
820 else
[email protected]42197a22010-12-28 23:29:42821 return l10n_util::GetStringUTF16(IDS_LOAD_STATE_SENDING_REQUEST);
[email protected]96d185d2009-04-24 03:28:54822 case net::LOAD_STATE_WAITING_FOR_RESPONSE:
[email protected]42197a22010-12-28 23:29:42823 return l10n_util::GetStringFUTF16(IDS_LOAD_STATE_WAITING_FOR_RESPONSE,
824 load_state_host_);
[email protected]96d185d2009-04-24 03:28:54825 // Ignore net::LOAD_STATE_READING_RESPONSE and net::LOAD_STATE_IDLE
826 case net::LOAD_STATE_IDLE:
827 case net::LOAD_STATE_READING_RESPONSE:
828 break;
829 }
830
[email protected]42197a22010-12-28 23:29:42831 return string16();
[email protected]96d185d2009-04-24 03:28:54832}
833
[email protected]3c9e1872010-11-18 16:17:49834void TabContents::AddNavigationObserver(WebNavigationObserver* observer) {
835 web_navigation_observers_.AddObserver(observer);
836}
837
838void TabContents::RemoveNavigationObserver(WebNavigationObserver* observer) {
839 web_navigation_observers_.RemoveObserver(observer);
840}
841
[email protected]443b80e2010-12-14 00:42:23842void TabContents::SetIsCrashed(base::TerminationStatus status, int error_code) {
843 if (status == crashed_status_)
[email protected]d5f942ba2008-09-26 19:30:34844 return;
845
[email protected]443b80e2010-12-14 00:42:23846 crashed_status_ = status;
847 crashed_error_code_ = error_code;
[email protected]c9cd2222009-05-06 05:16:50848 NotifyNavigationStateChanged(INVALIDATE_TAB);
[email protected]d5f942ba2008-09-26 19:30:34849}
850
[email protected]744ef172009-10-16 21:53:46851void TabContents::PageActionStateChanged() {
852 NotifyNavigationStateChanged(TabContents::INVALIDATE_PAGE_ACTIONS);
[email protected]d7eaf572009-07-01 21:57:00853}
[email protected]ce5c4502009-05-06 16:46:11854
[email protected]d5f942ba2008-09-26 19:30:34855void TabContents::NotifyNavigationStateChanged(unsigned changed_flags) {
856 if (delegate_)
857 delegate_->NavigationStateChanged(this, changed_flags);
858}
859
[email protected]96d185d2009-04-24 03:28:54860void TabContents::DidBecomeSelected() {
861 controller_.SetActive(true);
[email protected]8cb5d5b2010-02-09 11:36:16862 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
863 if (rwhv) {
864 rwhv->DidBecomeSelected();
[email protected]789e9152009-08-04 21:59:43865#if defined(OS_MACOSX)
[email protected]8cb5d5b2010-02-09 11:36:16866 rwhv->SetActive(true);
[email protected]789e9152009-08-04 21:59:43867#endif
868 }
[email protected]96d185d2009-04-24 03:28:54869
[email protected]8cb5d5b2010-02-09 11:36:16870 WebCacheManager::GetInstance()->ObserveActivity(GetRenderProcessHost()->id());
[email protected]5ac20162010-11-24 23:33:11871 last_selected_time_ = base::TimeTicks::Now();
[email protected]61d68ef12011-01-13 14:02:56872#if defined(OS_CHROMEOS)
873 chromeos::LocaleChangeGuard::Check(this);
874#endif
[email protected]96d185d2009-04-24 03:28:54875}
876
[email protected]a94454b2010-12-16 04:16:03877void TabContents::FadeForInstant(bool animate) {
[email protected]1b072562010-12-13 19:37:45878 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
879 SkColor whitish = SkColorSetARGB(192, 255, 255, 255);
880 if (rwhv)
[email protected]a94454b2010-12-16 04:16:03881 rwhv->SetVisuallyDeemphasized(&whitish, animate);
[email protected]1b072562010-12-13 19:37:45882}
883
884void TabContents::CancelInstantFade() {
885 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
886 if (rwhv)
887 rwhv->SetVisuallyDeemphasized(NULL, false);
888}
889
[email protected]96d185d2009-04-24 03:28:54890void TabContents::WasHidden() {
891 if (!capturing_contents()) {
892 // |render_view_host()| can be NULL if the user middle clicks a link to open
893 // a tab in then background, then closes the tab before selecting it. This
894 // is because closing the tab calls TabContents::Destroy(), which removes
895 // the |render_view_host()|; then when we actually destroy the window,
896 // OnWindowPosChanged() notices and calls HideContents() (which calls us).
[email protected]8cb5d5b2010-02-09 11:36:16897 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
898 if (rwhv)
899 rwhv->WasHidden();
[email protected]96d185d2009-04-24 03:28:54900 }
901
902 NotificationService::current()->Notify(
903 NotificationType::TAB_CONTENTS_HIDDEN,
904 Source<TabContents>(this),
905 NotificationService::NoDetails());
906}
907
[email protected]d5f942ba2008-09-26 19:30:34908void TabContents::Activate() {
909 if (delegate_)
910 delegate_->ActivateContents(this);
911}
912
[email protected]ea42e7782010-08-23 23:58:12913void TabContents::Deactivate() {
914 if (delegate_)
915 delegate_->DeactivateContents(this);
916}
917
[email protected]96d185d2009-04-24 03:28:54918void TabContents::ShowContents() {
[email protected]8cb5d5b2010-02-09 11:36:16919 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
920 if (rwhv)
921 rwhv->DidBecomeSelected();
[email protected]96d185d2009-04-24 03:28:54922}
923
924void TabContents::HideContents() {
925 // TODO(pkasting): http://b/1239839 Right now we purposefully don't call
926 // our superclass HideContents(), because some callers want to be very picky
927 // about the order in which these get called. In addition to making the code
928 // here practically impossible to understand, this also means we end up
929 // calling TabContents::WasHidden() twice if callers call both versions of
[email protected]57c6a652009-05-04 07:58:34930 // HideContents() on a TabContents.
[email protected]96d185d2009-04-24 03:28:54931 WasHidden();
932}
933
[email protected]5aab5e22010-12-08 22:13:29934bool TabContents::NeedToFireBeforeUnload() {
935 // TODO(creis): Should we fire even for interstitial pages?
936 return notify_disconnection() &&
937 !showing_interstitial_page() &&
938 !render_view_host()->SuddenTerminationAllowed();
939}
940
[email protected]c0588052008-10-27 23:01:50941void TabContents::OpenURL(const GURL& url, const GURL& referrer,
[email protected]d5f942ba2008-09-26 19:30:34942 WindowOpenDisposition disposition,
943 PageTransition::Type transition) {
944 if (delegate_)
[email protected]c0588052008-10-27 23:01:50945 delegate_->OpenURLFromTab(this, url, referrer, disposition, transition);
[email protected]d5f942ba2008-09-26 19:30:34946}
947
[email protected]1ccb3568d2010-02-19 10:51:16948bool TabContents::NavigateToPendingEntry(
949 NavigationController::ReloadType reload_type) {
[email protected]876bc832010-09-07 16:29:54950 return NavigateToEntry(*controller_.pending_entry(), reload_type);
951}
[email protected]96d185d2009-04-24 03:28:54952
[email protected]876bc832010-09-07 16:29:54953bool TabContents::NavigateToEntry(
954 const NavigationEntry& entry,
955 NavigationController::ReloadType reload_type) {
[email protected]96d185d2009-04-24 03:28:54956 RenderViewHost* dest_render_view_host = render_manager_.Navigate(entry);
957 if (!dest_render_view_host)
958 return false; // Unable to create the desired render view host.
959
[email protected]770dd8b2010-05-24 18:11:39960 if (delegate_ && delegate_->ShouldEnablePreferredSizeNotifications()) {
961 dest_render_view_host->EnablePreferredSizeChangedMode(
962 kPreferredSizeWidth | kPreferredSizeHeightThisIsSlow);
963 }
[email protected]9fb325e2010-05-06 18:23:24964
[email protected]05fdd492010-11-15 17:52:07965 // For security, we should never send non-DOM-UI URLs to a DOM UI renderer.
966 // Double check that here.
[email protected]54ec6472010-04-09 19:39:58967 int enabled_bindings = dest_render_view_host->enabled_bindings();
[email protected]0352e5472010-04-29 00:11:40968 bool is_allowed_in_dom_ui_renderer =
[email protected]05fdd492010-11-15 17:52:07969 DOMUIFactory::IsURLAcceptableForDOMUI(profile(), entry.url());
[email protected]54ec6472010-04-09 19:39:58970 CHECK(!BindingsPolicy::is_dom_ui_enabled(enabled_bindings) ||
[email protected]0352e5472010-04-29 00:11:40971 is_allowed_in_dom_ui_renderer);
[email protected]54ec6472010-04-09 19:39:58972
[email protected]96d185d2009-04-24 03:28:54973 // Tell DevTools agent that it is attached prior to the navigation.
[email protected]d9f9b792009-06-24 13:17:12974 DevToolsManager* devtools_manager = DevToolsManager::GetInstance();
[email protected]7e8e6b62009-05-08 11:28:32975 if (devtools_manager) { // NULL in unit tests.
[email protected]3cc72b12010-03-18 23:03:00976 devtools_manager->OnNavigatingToPendingEntry(render_view_host(),
977 dest_render_view_host,
978 entry.url());
[email protected]7e8e6b62009-05-08 11:28:32979 }
[email protected]96d185d2009-04-24 03:28:54980
981 // Used for page load time metrics.
982 current_load_start_ = base::TimeTicks::Now();
983
984 // Navigate in the desired RenderViewHost.
[email protected]056de2d2009-06-26 16:41:34985 ViewMsg_Navigate_Params navigate_params;
[email protected]876bc832010-09-07 16:29:54986 MakeNavigateParams(entry, controller_, reload_type, &navigate_params);
[email protected]52c68652010-12-07 17:47:04987 if (delegate_) {
988 navigate_params.extra_headers =
989 delegate_->GetNavigationHeaders(navigate_params.url);
990 }
[email protected]056de2d2009-06-26 16:41:34991 dest_render_view_host->Navigate(navigate_params);
[email protected]96d185d2009-04-24 03:28:54992
993 if (entry.page_id() == -1) {
994 // HACK!! This code suppresses javascript: URLs from being added to
995 // session history, which is what we want to do for javascript: URLs that
996 // do not generate content. What we really need is a message from the
997 // renderer telling us that a new page was not created. The same message
998 // could be used for mailto: URLs and the like.
999 if (entry.url().SchemeIs(chrome::kJavaScriptScheme))
1000 return false;
1001 }
1002
[email protected]3c9e1872010-11-18 16:17:491003 // Notify observers about navigation.
1004 FOR_EACH_OBSERVER(WebNavigationObserver, web_navigation_observers_,
1005 NavigateToPendingEntry());
[email protected]96d185d2009-04-24 03:28:541006
[email protected]1ccb3568d2010-02-19 10:51:161007 if (reload_type != NavigationController::NO_RELOAD &&
1008 !profile()->IsOffTheRecord()) {
[email protected]0189bc722009-08-28 21:56:481009 FaviconService* favicon_service =
1010 profile()->GetFaviconService(Profile::IMPLICIT_ACCESS);
1011 if (favicon_service)
1012 favicon_service->SetFaviconOutOfDateForPage(entry.url());
[email protected]96d185d2009-04-24 03:28:541013 }
1014
[email protected]d5f942ba2008-09-26 19:30:341015 return true;
1016}
1017
[email protected]96d185d2009-04-24 03:28:541018void TabContents::Stop() {
1019 render_manager_.Stop();
[email protected]12802702010-07-09 19:43:091020 printing_->Stop();
[email protected]96d185d2009-04-24 03:28:541021}
1022
[email protected]96d185d2009-04-24 03:28:541023void TabContents::DisassociateFromPopupCount() {
1024 render_view_host()->DisassociateFromPopupCount();
1025}
1026
1027TabContents* TabContents::Clone() {
1028 // We create a new SiteInstance so that the new tab won't share processes
1029 // with the old one. This can be changed in the future if we need it to share
1030 // processes for some reason.
[email protected]420ae012009-04-24 05:16:321031 TabContents* tc = new TabContents(profile(),
[email protected]96d185d2009-04-24 03:28:541032 SiteInstance::CreateSiteInstance(profile()),
[email protected]6ee12c42010-09-14 09:36:071033 MSG_ROUTING_NONE, this, NULL);
[email protected]96d185d2009-04-24 03:28:541034 tc->controller().CopyStateFrom(controller_);
[email protected]98aa0b52010-05-06 17:03:081035 tc->extension_app_ = extension_app_;
[email protected]98aa0b52010-05-06 17:03:081036 tc->extension_app_icon_ = extension_app_icon_;
[email protected]96d185d2009-04-24 03:28:541037 return tc;
1038}
1039
[email protected]4d677202009-07-19 07:37:121040void TabContents::ShowPageInfo(const GURL& url,
1041 const NavigationEntry::SSLStatus& ssl,
1042 bool show_history) {
1043 if (!delegate_)
1044 return;
1045
[email protected]bb678332009-07-21 00:15:501046 delegate_->ShowPageInfo(profile(), url, ssl, show_history);
[email protected]4d677202009-07-19 07:37:121047}
1048
[email protected]ef0d04c2010-12-15 23:34:211049void TabContents::SaveFavicon() {
1050 NavigationEntry* entry = controller_.GetActiveEntry();
1051 if (!entry || entry->url().is_empty())
1052 return;
1053
1054 // Make sure the page is in history, otherwise adding the favicon does
1055 // nothing.
1056 HistoryService* history = profile()->GetOriginalProfile()->GetHistoryService(
1057 Profile::IMPLICIT_ACCESS);
1058 if (!history)
1059 return;
1060 history->AddPageNoVisitForBookmark(entry->url());
1061
1062 FaviconService* service = profile()->GetOriginalProfile()->GetFaviconService(
1063 Profile::IMPLICIT_ACCESS);
1064 if (!service)
1065 return;
1066 const NavigationEntry::FaviconStatus& favicon(entry->favicon());
1067 if (!favicon.is_valid() || favicon.url().is_empty() ||
1068 favicon.bitmap().empty()) {
1069 return;
1070 }
1071 std::vector<unsigned char> image_data;
1072 gfx::PNGCodec::EncodeBGRASkBitmap(favicon.bitmap(), false, &image_data);
1073 service->SetFavicon(entry->url(), favicon.url(), image_data);
1074}
1075
initial.commit09911bf2008-07-26 23:55:291076ConstrainedWindow* TabContents::CreateConstrainedDialog(
[email protected]e8382172009-06-19 22:16:281077 ConstrainedWindowDelegate* delegate) {
initial.commit09911bf2008-07-26 23:55:291078 ConstrainedWindow* window =
[email protected]e8382172009-06-19 22:16:281079 ConstrainedWindow::CreateConstrainedDialog(this, delegate);
initial.commit09911bf2008-07-26 23:55:291080 child_windows_.push_back(window);
[email protected]fa1cf0b82010-01-15 21:49:441081
1082 if (child_windows_.size() == 1) {
1083 window->ShowConstrainedWindow();
1084 BlockTabContent(true);
1085 }
1086
initial.commit09911bf2008-07-26 23:55:291087 return window;
1088}
1089
[email protected]fa1cf0b82010-01-15 21:49:441090void TabContents::BlockTabContent(bool blocked) {
[email protected]8dccd7cb2010-02-25 05:19:551091 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
[email protected]1b072562010-12-13 19:37:451092 // 70% opaque grey.
1093 SkColor greyish = SkColorSetARGB(178, 0, 0, 0);
[email protected]8dccd7cb2010-02-25 05:19:551094 if (rwhv)
[email protected]1b072562010-12-13 19:37:451095 rwhv->SetVisuallyDeemphasized(blocked ? &greyish : NULL, false);
[email protected]fa1cf0b82010-01-15 21:49:441096 render_view_host()->set_ignore_input_events(blocked);
1097 if (delegate_)
1098 delegate_->SetTabContentBlocked(this, blocked);
1099}
1100
initial.commit09911bf2008-07-26 23:55:291101void TabContents::AddNewContents(TabContents* new_contents,
1102 WindowOpenDisposition disposition,
1103 const gfx::Rect& initial_pos,
[email protected]7e7414ae2010-01-26 20:19:291104 bool user_gesture) {
[email protected]c65e2f152010-10-14 15:30:401105 if (all_contents_blocked_) {
1106 if (!blocked_contents_)
1107 blocked_contents_ = new BlockedContentContainer(this);
1108 blocked_contents_->AddTabContents(
1109 new_contents, disposition, initial_pos, user_gesture);
1110 return;
1111 }
1112
initial.commit09911bf2008-07-26 23:55:291113 if (!delegate_)
1114 return;
1115
[email protected]35f7d212009-04-29 21:19:271116 if ((disposition == NEW_POPUP) && !user_gesture &&
1117 !CommandLine::ForCurrentProcess()->HasSwitch(
1118 switches::kDisablePopupBlocking)) {
[email protected]2c4410d2009-05-06 23:46:221119 // Unrequested popups from normal pages are constrained unless they're in
[email protected]b6c874582009-05-08 19:38:311120 // the whitelist. The popup owner will handle checking this.
[email protected]7e7414ae2010-01-26 20:19:291121 delegate_->GetConstrainingContents(this)->AddPopup(
1122 new_contents, initial_pos);
[email protected]b6c874582009-05-08 19:38:311123 } else {
[email protected]0aa55312008-10-17 21:53:081124 new_contents->DisassociateFromPopupCount();
initial.commit09911bf2008-07-26 23:55:291125 delegate_->AddNewContents(this, new_contents, disposition, initial_pos,
1126 user_gesture);
[email protected]7f37dd92009-09-15 16:35:491127 NotificationService::current()->Notify(
1128 NotificationType::TAB_ADDED,
1129 Source<TabContentsDelegate>(delegate_),
1130 Details<TabContents>(this));
initial.commit09911bf2008-07-26 23:55:291131 }
initial.commit09911bf2008-07-26 23:55:291132
[email protected]8cc8d492010-02-02 10:40:491133 // TODO(pkasting): Why is this necessary?
[email protected]c65e2f152010-10-14 15:30:401134 PopupNotificationVisibilityChanged(blocked_contents_ != NULL);
initial.commit09911bf2008-07-26 23:55:291135}
1136
[email protected]bcd9dc42010-01-23 16:38:181137bool TabContents::ExecuteCode(int request_id, const std::string& extension_id,
[email protected]20ad2692009-11-20 18:27:201138 bool is_js_code, const std::string& code_string,
1139 bool all_frames) {
[email protected]ebd0eae72010-02-16 23:50:511140 RenderViewHost* host = render_view_host();
1141 if (!host)
1142 return false;
1143
1144 return host->Send(new ViewMsg_ExecuteCode(host->routing_id(),
[email protected]be7e5cb2010-10-04 12:53:171145 ViewMsg_ExecuteCode_Params(request_id, extension_id,
[email protected]ebd0eae72010-02-16 23:50:511146 is_js_code, code_string, all_frames)));
[email protected]912256b32009-09-18 09:47:351147}
1148
[email protected]96d185d2009-04-24 03:28:541149void TabContents::PopupNotificationVisibilityChanged(bool visible) {
[email protected]8cc8d492010-02-02 10:40:491150 if (is_being_destroyed_)
1151 return;
[email protected]9fb83e82010-07-02 18:24:551152 content_settings_delegate_->SetPopupsBlocked(visible);
[email protected]8cc8d492010-02-02 10:40:491153 if (!dont_notify_render_view_)
[email protected]e8345242010-05-06 03:00:401154 render_view_host()->AllowScriptToClose(!visible);
[email protected]96d185d2009-04-24 03:28:541155}
1156
[email protected]5c9e97a2009-09-09 23:48:301157gfx::NativeView TabContents::GetContentNativeView() const {
[email protected]96d185d2009-04-24 03:28:541158 return view_->GetContentNativeView();
1159}
1160
1161gfx::NativeView TabContents::GetNativeView() const {
1162 return view_->GetNativeView();
1163}
1164
1165void TabContents::GetContainerBounds(gfx::Rect *out) const {
1166 view_->GetContainerBounds(out);
1167}
1168
1169void TabContents::Focus() {
1170 view_->Focus();
1171}
1172
[email protected]90daadb42009-06-08 21:27:281173void TabContents::FocusThroughTabTraversal(bool reverse) {
[email protected]7e383692009-06-12 19:14:541174 if (showing_interstitial_page()) {
1175 render_manager_.interstitial_page()->FocusThroughTabTraversal(reverse);
1176 return;
1177 }
[email protected]96d185d2009-04-24 03:28:541178 render_view_host()->SetInitialFocus(reverse);
1179}
1180
1181bool TabContents::FocusLocationBarByDefault() {
1182 DOMUI* dom_ui = GetDOMUIForCurrentState();
1183 if (dom_ui)
1184 return dom_ui->focus_location_bar_by_default();
[email protected]3e3f0eb2009-06-22 18:33:431185 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]e0d481582009-09-15 21:06:251186 if (entry && entry->url() == GURL(chrome::kAboutBlankURL))
[email protected]3e3f0eb2009-06-22 18:33:431187 return true;
[email protected]96d185d2009-04-24 03:28:541188 return false;
1189}
1190
[email protected]a26dc362010-04-23 01:48:581191void TabContents::SetFocusToLocationBar(bool select_all) {
[email protected]a11aaf22010-03-30 00:03:381192 if (delegate())
[email protected]a26dc362010-04-23 01:48:581193 delegate()->SetFocusToLocationBar(select_all);
[email protected]a11aaf22010-03-30 00:03:381194}
1195
[email protected]616ed5a2008-11-21 22:27:241196void TabContents::AddInfoBar(InfoBarDelegate* delegate) {
[email protected]f7019302010-03-26 19:58:321197 if (delegate_ && !delegate_->infobars_enabled()) {
1198 delegate->InfoBarClosed();
1199 return;
1200 }
1201
[email protected]616ed5a2008-11-21 22:27:241202 // Look through the existing InfoBarDelegates we have for a match. If we've
1203 // already got one that matches, then we don't add the new one.
[email protected]f86a07022008-11-25 01:06:051204 for (int i = 0; i < infobar_delegate_count(); ++i) {
[email protected]6d27a7b2009-12-18 23:25:451205 if (GetInfoBarDelegateAt(i)->EqualsDelegate(delegate)) {
1206 // Tell the new infobar to close so that it can clean itself up.
1207 delegate->InfoBarClosed();
[email protected]616ed5a2008-11-21 22:27:241208 return;
[email protected]6d27a7b2009-12-18 23:25:451209 }
[email protected]616ed5a2008-11-21 22:27:241210 }
1211
1212 infobar_delegates_.push_back(delegate);
[email protected]bfd04a62009-02-01 18:16:561213 NotificationService::current()->Notify(
[email protected]f4f50ef2011-01-21 19:01:191214 NotificationType::TAB_CONTENTS_INFOBAR_ADDED, Source<TabContents>(this),
[email protected]bfd04a62009-02-01 18:16:561215 Details<InfoBarDelegate>(delegate));
[email protected]616ed5a2008-11-21 22:27:241216
1217 // Add ourselves as an observer for navigations the first time a delegate is
1218 // added. We use this notification to expire InfoBars that need to expire on
1219 // page transitions.
1220 if (infobar_delegates_.size() == 1) {
[email protected]bfd04a62009-02-01 18:16:561221 registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED,
[email protected]ce3fa3c2009-04-20 19:55:571222 Source<NavigationController>(&controller_));
[email protected]616ed5a2008-11-21 22:27:241223 }
1224}
1225
1226void TabContents::RemoveInfoBar(InfoBarDelegate* delegate) {
[email protected]f7019302010-03-26 19:58:321227 if (delegate_ && !delegate_->infobars_enabled()) {
1228 return;
1229 }
1230
[email protected]616ed5a2008-11-21 22:27:241231 std::vector<InfoBarDelegate*>::iterator it =
1232 find(infobar_delegates_.begin(), infobar_delegates_.end(), delegate);
1233 if (it != infobar_delegates_.end()) {
1234 InfoBarDelegate* delegate = *it;
[email protected]bfd04a62009-02-01 18:16:561235 NotificationService::current()->Notify(
1236 NotificationType::TAB_CONTENTS_INFOBAR_REMOVED,
1237 Source<TabContents>(this),
1238 Details<InfoBarDelegate>(delegate));
[email protected]616ed5a2008-11-21 22:27:241239
[email protected]f4f50ef2011-01-21 19:01:191240 infobar_delegates_.erase(it);
1241 // Remove ourselves as an observer if we are tracking no more InfoBars.
1242 if (infobar_delegates_.empty()) {
1243 registrar_.Remove(this, NotificationType::NAV_ENTRY_COMMITTED,
1244 Source<NavigationController>(&controller_));
[email protected]6a02963e2009-01-06 16:58:031245 }
[email protected]616ed5a2008-11-21 22:27:241246 }
1247}
[email protected]3edd9522009-03-04 22:19:411248
[email protected]f81821c2009-08-07 01:29:471249void TabContents::ReplaceInfoBar(InfoBarDelegate* old_delegate,
1250 InfoBarDelegate* new_delegate) {
[email protected]f7019302010-03-26 19:58:321251 if (delegate_ && !delegate_->infobars_enabled()) {
1252 new_delegate->InfoBarClosed();
1253 return;
1254 }
1255
[email protected]f81821c2009-08-07 01:29:471256 std::vector<InfoBarDelegate*>::iterator it =
1257 find(infobar_delegates_.begin(), infobar_delegates_.end(), old_delegate);
1258 DCHECK(it != infobar_delegates_.end());
1259
1260 // Notify the container about the change of plans.
1261 scoped_ptr<std::pair<InfoBarDelegate*, InfoBarDelegate*> > details(
1262 new std::pair<InfoBarDelegate*, InfoBarDelegate*>(
1263 old_delegate, new_delegate));
1264 NotificationService::current()->Notify(
1265 NotificationType::TAB_CONTENTS_INFOBAR_REPLACED,
1266 Source<TabContents>(this),
1267 Details<std::pair<InfoBarDelegate*, InfoBarDelegate*> >(details.get()));
1268
[email protected]f4f50ef2011-01-21 19:01:191269 // Remove the old one.
1270 infobar_delegates_.erase(it);
[email protected]f81821c2009-08-07 01:29:471271
1272 // Add the new one.
[email protected]cdadf6f2010-08-20 16:36:081273 DCHECK(find(infobar_delegates_.begin(),
1274 infobar_delegates_.end(), new_delegate) ==
1275 infobar_delegates_.end());
[email protected]f81821c2009-08-07 01:29:471276 infobar_delegates_.push_back(new_delegate);
1277}
1278
[email protected]20ede072009-10-06 00:28:351279bool TabContents::ShouldShowBookmarkBar() {
[email protected]aa9196f2009-10-22 16:20:271280 if (showing_interstitial_page())
1281 return false;
1282
[email protected]c8f74942010-09-03 10:04:141283 // Do not show bookmarks bar if bookmarks aren't enabled.
1284 if (!browser_defaults::bookmarks_enabled)
1285 return false;
1286
[email protected]96d185d2009-04-24 03:28:541287 // See GetDOMUIForCurrentState() comment for more info. This case is very
1288 // similar, but for non-first loads, we want to use the committed entry. This
1289 // is so the bookmarks bar disappears at the same time the page does.
1290 if (controller_.GetLastCommittedEntry()) {
1291 // Not the first load, always use the committed DOM UI.
[email protected]d6219912010-11-17 19:47:371292 return (render_manager_.dom_ui() == NULL) ?
1293 false : render_manager_.dom_ui()->force_bookmark_bar_visible();
[email protected]96d185d2009-04-24 03:28:541294 }
1295
1296 // When it's the first load, we know either the pending one or the committed
1297 // one will have the DOM UI in it (see GetDOMUIForCurrentState), and only one
1298 // of them will be valid, so we can just check both.
1299 if (render_manager_.pending_dom_ui())
1300 return render_manager_.pending_dom_ui()->force_bookmark_bar_visible();
[email protected]d6219912010-11-17 19:47:371301 return (render_manager_.dom_ui() == NULL) ?
1302 false : render_manager_.dom_ui()->force_bookmark_bar_visible();
[email protected]96d185d2009-04-24 03:28:541303}
1304
[email protected]3edd9522009-03-04 22:19:411305void TabContents::ToolbarSizeChanged(bool is_animating) {
1306 TabContentsDelegate* d = delegate();
1307 if (d)
1308 d->ToolbarSizeChanged(this, is_animating);
1309}
[email protected]616ed5a2008-11-21 22:27:241310
[email protected]fc6fb7fb2009-11-07 02:35:041311bool TabContents::CanDownload(int request_id) {
1312 TabContentsDelegate* d = delegate();
1313 if (d)
1314 return d->CanDownload(request_id);
1315 return true;
1316}
1317
[email protected]d5f942ba2008-09-26 19:30:341318void TabContents::OnStartDownload(DownloadItem* download) {
1319 DCHECK(download);
[email protected]d5f942ba2008-09-26 19:30:341320
1321 // Download in a constrained popup is shown in the tab that opened it.
[email protected]b6c874582009-05-08 19:38:311322 TabContents* tab_contents = delegate()->GetConstrainingContents(this);
[email protected]d5f942ba2008-09-26 19:30:341323
[email protected]5b928b62010-08-03 11:46:511324 if (tab_contents && tab_contents->delegate())
1325 tab_contents->delegate()->OnStartDownload(download, this);
[email protected]3edd9522009-03-04 22:19:411326}
[email protected]3edd9522009-03-04 22:19:411327
[email protected]d5f942ba2008-09-26 19:30:341328void TabContents::WillClose(ConstrainedWindow* window) {
[email protected]d6219912010-11-17 19:47:371329 ConstrainedWindowList::iterator i(
1330 std::find(child_windows_.begin(), child_windows_.end(), window));
1331 bool removed_topmost_window = i == child_windows_.begin();
1332 if (i != child_windows_.end())
1333 child_windows_.erase(i);
1334 if (child_windows_.empty()) {
[email protected]fa1cf0b82010-01-15 21:49:441335 BlockTabContent(false);
[email protected]d6219912010-11-17 19:47:371336 } else {
1337 if (removed_topmost_window)
1338 child_windows_[0]->ShowConstrainedWindow();
1339 BlockTabContent(true);
[email protected]fa1cf0b82010-01-15 21:49:441340 }
[email protected]66ba4932009-06-04 19:22:131341}
[email protected]d5f942ba2008-09-26 19:30:341342
[email protected]c65e2f152010-10-14 15:30:401343void TabContents::WillCloseBlockedContentContainer(
1344 BlockedContentContainer* container) {
1345 DCHECK(blocked_contents_ == container);
1346 blocked_contents_ = NULL;
[email protected]8cc8d492010-02-02 10:40:491347 PopupNotificationVisibilityChanged(false);
[email protected]d5f942ba2008-09-26 19:30:341348}
1349
[email protected]d5f942ba2008-09-26 19:30:341350void TabContents::DidMoveOrResize(ConstrainedWindow* window) {
[email protected]332af7732009-03-11 13:21:351351#if defined(OS_WIN)
[email protected]92edc472009-02-10 20:32:061352 UpdateWindow(GetNativeView());
[email protected]332af7732009-03-11 13:21:351353#endif
[email protected]d5f942ba2008-09-26 19:30:341354}
1355
[email protected]c1dc9f132009-11-20 23:13:211356void TabContents::StartFinding(string16 search_string,
[email protected]e491f1c2009-05-22 20:28:121357 bool forward_direction,
1358 bool case_sensitive) {
[email protected]c1dc9f132009-11-20 23:13:211359 // If search_string is empty, it means FindNext was pressed with a keyboard
[email protected]6e95706d2009-04-23 22:55:011360 // shortcut so unless we have something to search for we return early.
[email protected]c1dc9f132009-11-20 23:13:211361 if (search_string.empty() && find_text_.empty()) {
[email protected]e5d2f7132010-04-09 18:13:531362 string16 last_search_prepopulate_text =
1363 FindBarState::GetLastPrepopulateText(profile());
1364
[email protected]87fae512009-12-11 02:20:211365 // Try the last thing we searched for on this tab, then the last thing
1366 // searched for on any tab.
1367 if (!previous_find_text_.empty())
1368 search_string = previous_find_text_;
[email protected]e5d2f7132010-04-09 18:13:531369 else if (!last_search_prepopulate_text.empty())
1370 search_string = last_search_prepopulate_text;
[email protected]87fae512009-12-11 02:20:211371 else
[email protected]e491f1c2009-05-22 20:28:121372 return;
[email protected]e491f1c2009-05-22 20:28:121373 }
[email protected]6e95706d2009-04-23 22:55:011374
[email protected]101c90e2009-10-26 21:14:441375 // Keep track of the previous search.
1376 previous_find_text_ = find_text_;
1377
[email protected]6e95706d2009-04-23 22:55:011378 // This is a FindNext operation if we are searching for the same text again,
1379 // or if the passed in search text is empty (FindNext keyboard shortcut). The
1380 // exception to this is if the Find was aborted (then we don't want FindNext
1381 // because the highlighting has been cleared and we need it to reappear). We
1382 // therefore treat FindNext after an aborted Find operation as a full fledged
1383 // Find.
[email protected]c1dc9f132009-11-20 23:13:211384 bool find_next = (find_text_ == search_string || search_string.empty()) &&
[email protected]e491f1c2009-05-22 20:28:121385 (last_search_case_sensitive_ == case_sensitive) &&
[email protected]6e95706d2009-04-23 22:55:011386 !find_op_aborted_;
1387 if (!find_next)
1388 current_find_request_id_ = find_request_id_counter_++;
1389
[email protected]c1dc9f132009-11-20 23:13:211390 if (!search_string.empty())
1391 find_text_ = search_string;
[email protected]e491f1c2009-05-22 20:28:121392 last_search_case_sensitive_ = case_sensitive;
[email protected]6e95706d2009-04-23 22:55:011393
1394 find_op_aborted_ = false;
1395
1396 // Keep track of what the last search was across the tabs.
[email protected]e5d2f7132010-04-09 18:13:531397 FindBarState* find_bar_state = profile()->GetFindBarState();
1398 find_bar_state->set_last_prepopulate_text(find_text_);
[email protected]6e95706d2009-04-23 22:55:011399 render_view_host()->StartFinding(current_find_request_id_,
1400 find_text_,
1401 forward_direction,
[email protected]e491f1c2009-05-22 20:28:121402 case_sensitive,
[email protected]6e95706d2009-04-23 22:55:011403 find_next);
1404}
1405
[email protected]5bc8fe92010-03-11 18:19:001406void TabContents::StopFinding(
1407 FindBarController::SelectionAction selection_action) {
[email protected]d6e8d332010-06-04 22:26:011408 if (selection_action == FindBarController::kClearSelection) {
1409 // kClearSelection means the find string has been cleared by the user, but
1410 // the UI has not been dismissed. In that case we want to clear the
1411 // previously remembered search (http://crbug.com/42639).
1412 previous_find_text_ = string16();
1413 } else {
[email protected]2d2dffc2009-06-09 21:41:001414 find_ui_active_ = false;
[email protected]d6e8d332010-06-04 22:26:011415 if (!find_text_.empty())
1416 previous_find_text_ = find_text_;
1417 }
[email protected]101c90e2009-10-26 21:14:441418 find_text_.clear();
[email protected]6e95706d2009-04-23 22:55:011419 find_op_aborted_ = true;
[email protected]e491f1c2009-05-22 20:28:121420 last_search_result_ = FindNotificationDetails();
[email protected]5bc8fe92010-03-11 18:19:001421 render_view_host()->StopFinding(selection_action);
[email protected]6e95706d2009-04-23 22:55:011422}
1423
[email protected]420ae012009-04-24 05:16:321424void TabContents::OnSavePage() {
1425 // If we can not save the page, try to download it.
1426 if (!SavePackage::IsSavableContents(contents_mime_type())) {
1427 DownloadManager* dlm = profile()->GetDownloadManager();
1428 const GURL& current_page_url = GetURL();
1429 if (dlm && current_page_url.is_valid())
[email protected]d82ed61e2009-06-16 02:46:221430 dlm->DownloadUrl(current_page_url, GURL(), "", this);
[email protected]420ae012009-04-24 05:16:321431 return;
1432 }
1433
1434 Stop();
1435
1436 // Create the save package and possibly prompt the user for the name to save
1437 // the page as. The user prompt is an asynchronous operation that runs on
1438 // another thread.
[email protected]d82ed61e2009-06-16 02:46:221439 save_package_ = new SavePackage(this);
[email protected]420ae012009-04-24 05:16:321440 save_package_->GetSaveInfo();
1441}
1442
1443// Used in automated testing to bypass prompting the user for file names.
1444// Instead, the names and paths are hard coded rather than running them through
1445// file name sanitation and extension / mime checking.
[email protected]828cabe2009-09-26 22:47:111446bool TabContents::SavePage(const FilePath& main_file, const FilePath& dir_path,
[email protected]420ae012009-04-24 05:16:321447 SavePackage::SavePackageType save_type) {
1448 // Stop the page from navigating.
1449 Stop();
1450
[email protected]828cabe2009-09-26 22:47:111451 save_package_ = new SavePackage(this, save_type, main_file, dir_path);
[email protected]cdf6cc82009-08-04 21:48:591452 return save_package_->Init();
[email protected]420ae012009-04-24 05:16:321453}
1454
[email protected]89d6e6e2009-12-03 23:06:511455void TabContents::EmailPageLocation() {
1456 std::string title = EscapeQueryParamValue(UTF16ToUTF8(GetTitle()), false);
1457 std::string page_url = EscapeQueryParamValue(GetURL().spec(), false);
1458 std::string mailto = std::string("mailto:?subject=Fwd:%20") +
1459 title + "&body=%0A%0A" + page_url;
1460 platform_util::OpenExternal(GURL(mailto));
1461}
1462
[email protected]420ae012009-04-24 05:16:321463void TabContents::PrintPreview() {
[email protected]dbeebd52010-11-16 20:34:161464 if (CommandLine::ForCurrentProcess()->HasSwitch(
1465 switches::kEnablePrintPreview)) {
1466 if (showing_interstitial_page())
1467 return;
1468 render_view_host()->PrintPreview();
1469 } else {
1470 PrintNow();
1471 }
[email protected]420ae012009-04-24 05:16:321472}
1473
1474bool TabContents::PrintNow() {
1475 // We can't print interstitial page for now.
1476 if (showing_interstitial_page())
1477 return false;
1478
1479 return render_view_host()->PrintPages();
1480}
1481
[email protected]82270452009-06-19 15:58:011482void TabContents::PrintingDone(int document_cookie, bool success) {
1483 render_view_host()->PrintingDone(document_cookie, success);
1484}
1485
[email protected]420ae012009-04-24 05:16:321486bool TabContents::IsActiveEntry(int32 page_id) {
1487 NavigationEntry* active_entry = controller_.GetActiveEntry();
1488 return (active_entry != NULL &&
1489 active_entry->site_instance() == GetSiteInstance() &&
1490 active_entry->page_id() == page_id);
1491}
1492
[email protected]b2fe07d12010-02-09 14:38:081493void TabContents::SetOverrideEncoding(const std::string& encoding) {
[email protected]8cb5d5b2010-02-09 11:36:161494 set_encoding(encoding);
1495 render_view_host()->SetPageEncoding(encoding);
1496}
1497
[email protected]b2fe07d12010-02-09 14:38:081498void TabContents::ResetOverrideEncoding() {
[email protected]8cb5d5b2010-02-09 11:36:161499 reset_encoding();
1500 render_view_host()->ResetPageEncodingToDefault();
1501}
1502
1503void TabContents::WindowMoveOrResizeStarted() {
1504 render_view_host()->WindowMoveOrResizeStarted();
1505}
1506
[email protected]c65e2f152010-10-14 15:30:401507void TabContents::SetAllContentsBlocked(bool value) {
1508 if (all_contents_blocked_ == value)
1509 return;
1510
1511 all_contents_blocked_ = value;
1512 if (!all_contents_blocked_ && blocked_contents_) {
1513 std::vector<TabContents*> blocked;
1514 blocked_contents_->GetBlockedContents(&blocked);
1515 for (size_t i = 0; i < blocked.size(); ++i)
1516 blocked_contents_->LaunchForContents(blocked[i]);
1517 }
1518}
1519
[email protected]3f8eb7f2009-10-02 23:58:051520void TabContents::LogNewTabTime(const std::string& event_name) {
1521 // Not all new tab pages get timed. In those cases, we don't have a
1522 // new_tab_start_time_.
1523 if (new_tab_start_time_.is_null())
1524 return;
1525
1526 base::TimeDelta duration = base::TimeTicks::Now() - new_tab_start_time_;
[email protected]84abba62009-10-07 17:01:441527 MetricEventDurationDetails details(event_name,
1528 static_cast<int>(duration.InMilliseconds()));
1529
[email protected]7ab1e7d62009-10-14 23:32:011530 if (event_name == "Tab.NewTabScriptStart") {
1531 UMA_HISTOGRAM_TIMES("Tab.NewTabScriptStart", duration);
1532 } else if (event_name == "Tab.NewTabDOMContentLoaded") {
1533 UMA_HISTOGRAM_TIMES("Tab.NewTabDOMContentLoaded", duration);
1534 } else if (event_name == "Tab.NewTabOnload") {
1535 UMA_HISTOGRAM_TIMES("Tab.NewTabOnload", duration);
[email protected]3f8eb7f2009-10-02 23:58:051536 // The new tab page has finished loading; reset it.
1537 new_tab_start_time_ = base::TimeTicks();
[email protected]84abba62009-10-07 17:01:441538 } else {
1539 NOTREACHED();
[email protected]3f8eb7f2009-10-02 23:58:051540 }
[email protected]84abba62009-10-07 17:01:441541 NotificationService::current()->Notify(
1542 NotificationType::METRIC_EVENT_DURATION,
1543 Source<TabContents>(this),
1544 Details<MetricEventDurationDetails>(&details));
[email protected]3f8eb7f2009-10-02 23:58:051545}
1546
[email protected]7ab1e7d62009-10-14 23:32:011547void TabContents::OnCloseStarted() {
1548 if (tab_close_start_time_.is_null())
1549 tab_close_start_time_ = base::TimeTicks::Now();
1550}
1551
[email protected]46624bf2010-06-09 16:04:191552bool TabContents::ShouldAcceptDragAndDrop() const {
1553#if defined(OS_CHROMEOS)
1554 // ChromeOS panels (pop-ups) do not take drag-n-drop.
1555 // See http://crosbug.com/2413
[email protected]b83af492010-10-09 03:21:201556 if (delegate() && delegate()->IsPopup(this))
1557 return false;
1558 return true;
[email protected]46624bf2010-06-09 16:04:191559#else
1560 return true;
1561#endif
1562}
1563
[email protected]ec0b6c42010-08-26 03:16:581564void TabContents::UpdateHistoryForNavigation(
1565 scoped_refptr<history::HistoryAddPageArgs> add_page_args) {
1566 if (profile()->IsOffTheRecord())
1567 return;
1568
1569 // Add to history service.
1570 HistoryService* hs = profile()->GetHistoryService(Profile::IMPLICIT_ACCESS);
1571 if (hs)
1572 hs->AddPage(*add_page_args);
1573}
1574
1575void TabContents::UpdateHistoryPageTitle(const NavigationEntry& entry) {
1576 if (profile()->IsOffTheRecord())
1577 return;
1578
1579 HistoryService* hs = profile()->GetHistoryService(Profile::IMPLICIT_ACCESS);
1580 if (hs)
1581 hs->SetPageTitle(entry.virtual_url(), entry.title());
1582}
1583
[email protected]d0b8d092010-10-25 04:05:171584double TabContents::GetZoomLevel() const {
[email protected]b75b8292010-10-01 07:28:251585 HostZoomMap* zoom_map = profile()->GetHostZoomMap();
1586 if (!zoom_map)
[email protected]d0b8d092010-10-25 04:05:171587 return 0;
[email protected]b75b8292010-10-01 07:28:251588
1589 double zoom_level;
1590 if (temporary_zoom_settings_) {
1591 zoom_level = zoom_map->GetTemporaryZoomLevel(
1592 GetRenderProcessHost()->id(), render_view_host()->routing_id());
1593 } else {
1594 zoom_level = zoom_map->GetZoomLevel(GetURL());
1595 }
[email protected]d0b8d092010-10-25 04:05:171596 return zoom_level;
1597}
[email protected]b75b8292010-10-01 07:28:251598
[email protected]d0b8d092010-10-25 04:05:171599int TabContents::GetZoomPercent(bool* enable_increment,
1600 bool* enable_decrement) {
1601 *enable_decrement = *enable_increment = false;
[email protected]b75b8292010-10-01 07:28:251602 int percent = static_cast<int>(
[email protected]d0b8d092010-10-25 04:05:171603 WebKit::WebView::zoomLevelToZoomFactor(GetZoomLevel()) * 100);
[email protected]b75b8292010-10-01 07:28:251604 *enable_decrement = percent > minimum_zoom_percent_;
1605 *enable_increment = percent < maximum_zoom_percent_;
1606 return percent;
1607}
1608
[email protected]3c733bde2010-12-21 19:56:311609void TabContents::ViewSource() {
[email protected]1788e772010-12-15 16:40:501610 if (!delegate_)
1611 return;
1612
1613 NavigationEntry* active_entry = controller().GetActiveEntry();
1614 if (!active_entry)
1615 return;
1616
1617 delegate_->ViewSourceForTab(this, active_entry->url());
[email protected]77d8d622010-12-15 10:30:121618}
1619
[email protected]724159a2010-12-30 01:11:181620void TabContents::OnDidStartProvisionalLoadForFrame(int64 frame_id,
1621 bool is_main_frame,
1622 const GURL& url) {
1623 bool is_error_page = (url.spec() == chrome::kUnreachableWebDataURL);
1624 GURL validated_url(url);
1625 render_view_host()->FilterURL(ChildProcessSecurityPolicy::GetInstance(),
1626 GetRenderProcessHost()->id(), &validated_url);
1627
1628 ProvisionalLoadDetails details(
1629 is_main_frame,
1630 controller_.IsURLInPageNavigation(validated_url),
1631 validated_url, std::string(), false, is_error_page, frame_id);
1632 NotificationService::current()->Notify(
1633 NotificationType::FRAME_PROVISIONAL_LOAD_START,
1634 Source<NavigationController>(&controller_),
1635 Details<ProvisionalLoadDetails>(&details));
1636 if (is_main_frame) {
1637 // If we're displaying a network error page do not reset the content
1638 // settings delegate's cookies so the user has a chance to modify cookie
1639 // settings.
1640 if (!is_error_page)
1641 content_settings_delegate_->ClearCookieSpecificContentSettings();
1642 content_settings_delegate_->ClearGeolocationContentSettings();
[email protected]e7d50892011-01-19 21:47:381643
1644 // Check if the URL we are about to load has been prerendered by any chance,
1645 // and use it if possible.
1646 MaybeUsePreloadedPage(url);
[email protected]724159a2010-12-30 01:11:181647 }
1648}
1649
1650void TabContents::OnDidRedirectProvisionalLoad(int32 page_id,
1651 const GURL& source_url,
1652 const GURL& target_url) {
1653 NavigationEntry* entry;
1654 if (page_id == -1)
1655 entry = controller_.pending_entry();
1656 else
1657 entry = controller_.GetEntryWithPageID(GetSiteInstance(), page_id);
1658 if (!entry || entry->url() != source_url)
1659 return;
1660 entry->set_url(target_url);
[email protected]e7d50892011-01-19 21:47:381661
1662 // Check if the URL we are about to load has been prerendered by any chance,
1663 // and use it if possible.
1664 MaybeUsePreloadedPage(target_url);
[email protected]724159a2010-12-30 01:11:181665}
1666
1667void TabContents::OnDidFailProvisionalLoadWithError(
1668 int64 frame_id,
1669 bool is_main_frame,
1670 int error_code,
1671 const GURL& url,
1672 bool showing_repost_interstitial) {
1673 VLOG(1) << "Failed Provisional Load: " << url.possibly_invalid_spec()
1674 << ", error_code: " << error_code
1675 << " is_main_frame: " << is_main_frame
1676 << " showing_repost_interstitial: " << showing_repost_interstitial
1677 << " frame_id: " << frame_id;
1678 GURL validated_url(url);
1679 render_view_host()->FilterURL(ChildProcessSecurityPolicy::GetInstance(),
1680 GetRenderProcessHost()->id(), &validated_url);
1681
1682 if (net::ERR_ABORTED == error_code) {
1683 // EVIL HACK ALERT! Ignore failed loads when we're showing interstitials.
1684 // This means that the interstitial won't be torn down properly, which is
1685 // bad. But if we have an interstitial, go back to another tab type, and
1686 // then load the same interstitial again, we could end up getting the first
1687 // interstitial's "failed" message (as a result of the cancel) when we're on
1688 // the second one.
1689 //
1690 // We can't tell this apart, so we think we're tearing down the current page
1691 // which will cause a crash later one. There is also some code in
1692 // RenderViewHostManager::RendererAbortedProvisionalLoad that is commented
1693 // out because of this problem.
1694 //
1695 // http://code.google.com/p/chromium/issues/detail?id=2855
1696 // Because this will not tear down the interstitial properly, if "back" is
1697 // back to another tab type, the interstitial will still be somewhat alive
1698 // in the previous tab type. If you navigate somewhere that activates the
1699 // tab with the interstitial again, you'll see a flash before the new load
1700 // commits of the interstitial page.
1701 if (showing_interstitial_page()) {
1702 LOG(WARNING) << "Discarding message during interstitial.";
1703 return;
1704 }
1705
1706 // This will discard our pending entry if we cancelled the load (e.g., if we
1707 // decided to download the file instead of load it). Only discard the
1708 // pending entry if the URLs match, otherwise the user initiated a navigate
1709 // before the page loaded so that the discard would discard the wrong entry.
1710 NavigationEntry* pending_entry = controller_.pending_entry();
1711 if (pending_entry && pending_entry->url() == validated_url) {
1712 controller_.DiscardNonCommittedEntries();
1713 // Update the URL display.
1714 NotifyNavigationStateChanged(TabContents::INVALIDATE_URL);
1715 }
1716
1717 render_manager_.RendererAbortedProvisionalLoad(render_view_host());
1718 }
1719
1720 // Send out a notification that we failed a provisional load with an error.
1721 ProvisionalLoadDetails details(
1722 is_main_frame, controller_.IsURLInPageNavigation(validated_url),
1723 validated_url, std::string(), false, false, frame_id);
1724 details.set_error_code(error_code);
1725
1726 NotificationService::current()->Notify(
1727 NotificationType::FAIL_PROVISIONAL_LOAD_WITH_ERROR,
1728 Source<NavigationController>(&controller_),
1729 Details<ProvisionalLoadDetails>(&details));
1730}
1731
1732void TabContents::OnDidLoadResourceFromMemoryCache(
1733 const GURL& url,
1734 const std::string& frame_origin,
1735 const std::string& main_frame_origin,
1736 const std::string& security_info) {
1737 static base::StatsCounter cache("WebKit.CacheHit");
1738 cache.Increment();
1739
1740 // Send out a notification that we loaded a resource from our memory cache.
1741 int cert_id = 0, cert_status = 0, security_bits = -1, connection_status = 0;
1742 SSLManager::DeserializeSecurityInfo(security_info,
1743 &cert_id, &cert_status,
1744 &security_bits,
1745 &connection_status);
1746 LoadFromMemoryCacheDetails details(url, frame_origin, main_frame_origin,
1747 GetRenderProcessHost()->id(), cert_id,
1748 cert_status);
1749
1750 NotificationService::current()->Notify(
1751 NotificationType::LOAD_FROM_MEMORY_CACHE,
1752 Source<NavigationController>(&controller_),
1753 Details<LoadFromMemoryCacheDetails>(&details));
1754}
1755
1756void TabContents::OnDidDisplayInsecureContent() {
1757 displayed_insecure_content_ = true;
1758 SSLManager::NotifySSLInternalStateChanged();
1759}
1760
1761void TabContents::OnDidRunInsecureContent(
[email protected]92771112011-01-20 00:13:021762 const std::string& security_origin, const GURL& target_url) {
[email protected]724159a2010-12-30 01:11:181763 controller_.ssl_manager()->DidRunInsecureContent(security_origin);
1764}
1765
1766void TabContents::OnDocumentLoadedInFrame(int64 frame_id) {
1767 controller_.DocumentLoadedInFrame();
1768 NotificationService::current()->Notify(
1769 NotificationType::FRAME_DOM_CONTENT_LOADED,
1770 Source<NavigationController>(&controller_),
1771 Details<int64>(&frame_id));
1772}
1773
1774void TabContents::OnDidFinishLoad(int64 frame_id) {
1775 NotificationService::current()->Notify(
1776 NotificationType::FRAME_DID_FINISH_LOAD,
1777 Source<NavigationController>(&controller_),
1778 Details<int64>(&frame_id));
1779}
1780
[email protected]96d185d2009-04-24 03:28:541781// Notifies the RenderWidgetHost instance about the fact that the page is
1782// loading, or done loading and calls the base implementation.
1783void TabContents::SetIsLoading(bool is_loading,
1784 LoadNotificationDetails* details) {
1785 if (is_loading == is_loading_)
1786 return;
1787
1788 if (!is_loading) {
1789 load_state_ = net::LOAD_STATE_IDLE;
1790 load_state_host_.clear();
[email protected]094e5b22009-09-25 04:23:561791 upload_size_ = 0;
1792 upload_position_ = 0;
[email protected]96d185d2009-04-24 03:28:541793 }
1794
1795 render_manager_.SetIsLoading(is_loading);
1796
1797 is_loading_ = is_loading;
1798 waiting_for_response_ = is_loading;
1799
[email protected]6ebdc9b2010-09-27 16:55:571800 if (delegate_)
1801 delegate_->LoadingStateChanged(this);
[email protected]329581b2009-04-28 06:52:351802 NotifyNavigationStateChanged(INVALIDATE_LOAD);
[email protected]96d185d2009-04-24 03:28:541803
1804 NotificationType type = is_loading ? NotificationType::LOAD_START :
1805 NotificationType::LOAD_STOP;
[email protected]ce5c4502009-05-06 16:46:111806 NotificationDetails det = NotificationService::NoDetails();
[email protected]96d185d2009-04-24 03:28:541807 if (details)
1808 det = Details<LoadNotificationDetails>(details);
1809 NotificationService::current()->Notify(type,
1810 Source<NavigationController>(&controller_),
1811 det);
1812}
1813
[email protected]b6c874582009-05-08 19:38:311814void TabContents::AddPopup(TabContents* new_contents,
[email protected]7e7414ae2010-01-26 20:19:291815 const gfx::Rect& initial_pos) {
[email protected]57eda82a2010-05-05 21:28:061816 // A page can't spawn popups (or do anything else, either) until its load
1817 // commits, so when we reach here, the popup was spawned by the
1818 // NavigationController's last committed entry, not the active entry. For
1819 // example, if a page opens a popup in an onunload() handler, then the active
1820 // entry is the page to be loaded as we navigate away from the unloading
1821 // page. For this reason, we can't use GetURL() to get the opener URL,
1822 // because it returns the active entry.
1823 NavigationEntry* entry = controller_.GetLastCommittedEntry();
1824 GURL creator = entry ? entry->virtual_url() : GURL::EmptyGURL();
1825
1826 if (creator.is_valid() &&
[email protected]8cc8d492010-02-02 10:40:491827 profile()->GetHostContentSettingsMap()->GetContentSetting(
[email protected]ca352452010-08-06 11:14:091828 creator, CONTENT_SETTINGS_TYPE_POPUPS, "") == CONTENT_SETTING_ALLOW) {
[email protected]8cc8d492010-02-02 10:40:491829 AddNewContents(new_contents, NEW_POPUP, initial_pos, true);
1830 } else {
[email protected]c65e2f152010-10-14 15:30:401831 if (!blocked_contents_)
1832 blocked_contents_ = new BlockedContentContainer(this);
1833 blocked_contents_->AddTabContents(new_contents, NEW_POPUP, initial_pos,
1834 true);
[email protected]55126132010-08-19 14:53:281835 content_settings_delegate_->OnContentBlocked(CONTENT_SETTINGS_TYPE_POPUPS,
1836 std::string());
[email protected]8cc8d492010-02-02 10:40:491837 }
[email protected]b9681312008-11-07 00:08:261838}
[email protected]616ed5a2008-11-21 22:27:241839
[email protected]616ed5a2008-11-21 22:27:241840void TabContents::ExpireInfoBars(
1841 const NavigationController::LoadCommittedDetails& details) {
1842 // Only hide InfoBars when the user has done something that makes the main
1843 // frame load. We don't want various automatic or subframe navigations making
1844 // it disappear.
1845 if (!details.is_user_initiated_main_frame_load())
1846 return;
1847
[email protected]f86a07022008-11-25 01:06:051848 for (int i = infobar_delegate_count() - 1; i >= 0; --i) {
[email protected]616ed5a2008-11-21 22:27:241849 InfoBarDelegate* delegate = GetInfoBarDelegateAt(i);
[email protected]f86a07022008-11-25 01:06:051850 if (delegate->ShouldExpire(details))
[email protected]616ed5a2008-11-21 22:27:241851 RemoveInfoBar(delegate);
[email protected]616ed5a2008-11-21 22:27:241852 }
1853}
[email protected]fdd61c62009-04-22 19:22:571854
[email protected]96d185d2009-04-24 03:28:541855DOMUI* TabContents::GetDOMUIForCurrentState() {
1856 // When there is a pending navigation entry, we want to use the pending DOMUI
1857 // that goes along with it to control the basic flags. For example, we want to
1858 // show the pending URL in the URL bar, so we want the display_url flag to
1859 // be from the pending entry.
1860 //
1861 // The confusion comes because there are multiple possibilities for the
1862 // initial load in a tab as a side effect of the way the RenderViewHostManager
1863 // works.
1864 //
1865 // - For the very first tab the load looks "normal". The new tab DOM UI is
1866 // the pending one, and we want it to apply here.
1867 //
1868 // - For subsequent new tabs, they'll get a new SiteInstance which will then
1869 // get switched to the one previously associated with the new tab pages.
1870 // This switching will cause the manager to commit the RVH/DOMUI. So we'll
1871 // have a committed DOM UI in this case.
1872 //
1873 // This condition handles all of these cases:
1874 //
1875 // - First load in first tab: no committed nav entry + pending nav entry +
1876 // pending dom ui:
1877 // -> Use pending DOM UI if any.
1878 //
1879 // - First load in second tab: no committed nav entry + pending nav entry +
1880 // no pending DOM UI:
1881 // -> Use the committed DOM UI if any.
1882 //
1883 // - Second navigation in any tab: committed nav entry + pending nav entry:
1884 // -> Use pending DOM UI if any.
1885 //
1886 // - Normal state with no load: committed nav entry + no pending nav entry:
1887 // -> Use committed DOM UI.
1888 if (controller_.pending_entry() &&
1889 (controller_.GetLastCommittedEntry() ||
1890 render_manager_.pending_dom_ui()))
1891 return render_manager_.pending_dom_ui();
1892 return render_manager_.dom_ui();
1893}
[email protected]420ae012009-04-24 05:16:321894
1895void TabContents::DidNavigateMainFramePostCommit(
1896 const NavigationController::LoadCommittedDetails& details,
1897 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]c2e74fe82009-09-03 17:57:441898 if (opener_dom_ui_type_ != DOMUIFactory::kNoDOMUI) {
1899 // If this is a window.open navigation, use the same DOMUI as the renderer
1900 // that opened the window, as long as both renderers have the same
1901 // privileges.
[email protected]583d45c12010-08-31 02:48:121902 if (opener_dom_ui_type_ ==
1903 DOMUIFactory::GetDOMUIType(profile(), GetURL())) {
[email protected]c2e74fe82009-09-03 17:57:441904 DOMUI* dom_ui = DOMUIFactory::CreateDOMUIForURL(this, GetURL());
1905 // dom_ui might be NULL if the URL refers to a non-existent extension.
1906 if (dom_ui) {
1907 render_manager_.SetDOMUIPostCommit(dom_ui);
1908 dom_ui->RenderViewCreated(render_view_host());
1909 }
1910 }
1911 opener_dom_ui_type_ = DOMUIFactory::kNoDOMUI;
1912 }
1913
[email protected]420ae012009-04-24 05:16:321914 if (details.is_user_initiated_main_frame_load()) {
1915 // Clear the status bubble. This is a workaround for a bug where WebKit
1916 // doesn't let us know that the cursor left an element during a
1917 // transition (this is also why the mouse cursor remains as a hand after
1918 // clicking on a link); see bugs 1184641 and 980803. We don't want to
1919 // clear the bubble when a user navigates to a named anchor in the same
1920 // page.
1921 UpdateTargetURL(details.entry->page_id(), GURL());
[email protected]420ae012009-04-24 05:16:321922 }
1923
1924 // The keyword generator uses the navigation entries, so must be called after
1925 // the commit.
1926 GenerateKeywordIfNecessary(params);
1927
1928 // Allow the new page to set the title again.
1929 received_page_title_ = false;
1930
1931 // Get the favicon, either from history or request it from the net.
1932 fav_icon_helper_.FetchFavIcon(details.entry->url());
1933
[email protected]0de80162010-02-03 04:52:351934 // Clear all page actions, blocked content notifications and browser actions
1935 // for this tab, unless this is an in-page navigation.
[email protected]a6e82fc2010-02-24 22:28:141936 if (!details.is_in_page) {
[email protected]eaa7dd182010-12-14 11:09:001937 ExtensionService* service = profile()->GetExtensionService();
[email protected]92c6f9b92009-10-24 04:35:081938 if (service) {
1939 for (size_t i = 0; i < service->extensions()->size(); ++i) {
[email protected]5d246db22009-10-27 06:17:571940 ExtensionAction* browser_action =
[email protected]92c6f9b92009-10-24 04:35:081941 service->extensions()->at(i)->browser_action();
[email protected]56ce6e52009-10-27 00:10:521942 if (browser_action) {
1943 browser_action->ClearAllValuesForTab(controller().session_id().id());
1944 NotificationService::current()->Notify(
1945 NotificationType::EXTENSION_BROWSER_ACTION_UPDATED,
[email protected]5d246db22009-10-27 06:17:571946 Source<ExtensionAction>(browser_action),
[email protected]56ce6e52009-10-27 00:10:521947 NotificationService::NoDetails());
1948 }
[email protected]92c6f9b92009-10-24 04:35:081949
[email protected]5d246db22009-10-27 06:17:571950 ExtensionAction* page_action =
[email protected]56ce6e52009-10-27 00:10:521951 service->extensions()->at(i)->page_action();
1952 if (page_action) {
1953 page_action->ClearAllValuesForTab(controller().session_id().id());
1954 PageActionStateChanged();
1955 }
[email protected]92c6f9b92009-10-24 04:35:081956 }
[email protected]aece2c7f2009-08-27 20:43:171957 }
[email protected]0de80162010-02-03 04:52:351958
1959 // Close blocked popups.
[email protected]c65e2f152010-10-14 15:30:401960 if (blocked_contents_) {
[email protected]0fbd70332010-06-01 19:28:341961 AutoReset<bool> auto_reset(&dont_notify_render_view_, true);
[email protected]c65e2f152010-10-14 15:30:401962 blocked_contents_->Destroy();
1963 blocked_contents_ = NULL;
[email protected]0de80162010-02-03 04:52:351964 }
1965
1966 // Clear "blocked" flags.
[email protected]da4f8a12010-08-19 08:33:411967 content_settings_delegate_->ClearBlockedContentSettingsExceptForCookies();
[email protected]9fb83e82010-07-02 18:24:551968 content_settings_delegate_->GeolocationDidNavigate(details);
[email protected]f17a0ee2010-05-17 17:38:471969
1970 // Once the main frame is navigated, we're no longer considered to have
1971 // displayed insecure content.
1972 displayed_insecure_content_ = false;
[email protected]aece2c7f2009-08-27 20:43:171973 }
[email protected]ce5c4502009-05-06 16:46:111974
[email protected]8cc8d492010-02-02 10:40:491975 // Close constrained windows if necessary.
1976 if (!net::RegistryControlledDomainService::SameDomainOrHost(
1977 details.previous_url, details.entry->url()))
1978 CloseConstrainedWindows();
1979
[email protected]420ae012009-04-24 05:16:321980 // Update the starred state.
1981 UpdateStarredStateForCurrentURL();
[email protected]c07c9752010-03-12 05:57:531982
[email protected]3c9e1872010-11-18 16:17:491983 // Notify observers about navigation.
1984 FOR_EACH_OBSERVER(WebNavigationObserver, web_navigation_observers_,
1985 DidNavigateMainFramePostCommit(details, params));
[email protected]420ae012009-04-24 05:16:321986}
1987
1988void TabContents::DidNavigateAnyFramePostCommit(
1989 RenderViewHost* render_view_host,
1990 const NavigationController::LoadCommittedDetails& details,
1991 const ViewHostMsg_FrameNavigate_Params& params) {
1992 // If we navigate, start showing messages again. This does nothing to prevent
1993 // a malicious script from spamming messages, since the script could just
1994 // reload the page to stop blocking.
1995 suppress_javascript_messages_ = false;
1996
[email protected]3c9e1872010-11-18 16:17:491997 // Notify observers about navigation.
1998 FOR_EACH_OBSERVER(WebNavigationObserver, web_navigation_observers_,
1999 DidNavigateAnyFramePostCommit(details, params));
[email protected]3b1c1812010-02-12 21:53:552000
2001 // Let the LanguageState clear its state.
[email protected]80fc6112010-08-13 20:10:582002 language_state_.DidNavigate(details);
[email protected]420ae012009-04-24 05:16:322003}
2004
[email protected]8cc8d492010-02-02 10:40:492005void TabContents::CloseConstrainedWindows() {
2006 // Clear out any constrained windows since we are leaving this page entirely.
[email protected]420ae012009-04-24 05:16:322007 // We use indices instead of iterators in case CloseWindow does something
2008 // that may invalidate an iterator.
[email protected]61d68ef12011-01-13 14:02:562009 for (size_t i = 0; i < child_windows_.size(); ++i) {
2010 ConstrainedWindow* window = child_windows_[child_windows_.size() - 1 - i];
[email protected]fa1cf0b82010-01-15 21:49:442011 if (window) {
[email protected]420ae012009-04-24 05:16:322012 window->CloseConstrainedWindow();
[email protected]fa1cf0b82010-01-15 21:49:442013 BlockTabContent(false);
2014 }
[email protected]420ae012009-04-24 05:16:322015 }
2016}
2017
2018void TabContents::UpdateStarredStateForCurrentURL() {
2019 BookmarkModel* model = profile()->GetBookmarkModel();
2020 const bool old_state = is_starred_;
2021 is_starred_ = (model && model->IsBookmarked(GetURL()));
2022
2023 if (is_starred_ != old_state && delegate())
2024 delegate()->URLStarredChanged(this, is_starred_);
2025}
2026
2027void TabContents::UpdateAlternateErrorPageURL() {
2028 GURL url = GetAlternateErrorPageURL();
2029 render_view_host()->SetAlternateErrorPageURL(url);
2030}
2031
2032void TabContents::UpdateWebPreferences() {
2033 render_view_host()->UpdateWebPreferences(GetWebkitPrefs());
2034}
2035
[email protected]d0b8d092010-10-25 04:05:172036void TabContents::UpdateZoomLevel() {
2037 render_view_host()->SetZoomLevel(GetZoomLevel());
2038}
2039
[email protected]420ae012009-04-24 05:16:322040void TabContents::UpdateMaxPageIDIfNecessary(SiteInstance* site_instance,
2041 RenderViewHost* rvh) {
2042 // If we are creating a RVH for a restored controller, then we might
2043 // have more page IDs than the SiteInstance's current max page ID. We must
2044 // make sure that the max page ID is larger than any restored page ID.
2045 // Note that it is ok for conflicting page IDs to exist in another tab
2046 // (i.e., NavigationController), but if any page ID is larger than the max,
2047 // the back/forward list will get confused.
2048 int max_restored_page_id = controller_.max_restored_page_id();
2049 if (max_restored_page_id > 0) {
2050 int curr_max_page_id = site_instance->max_page_id();
2051 if (max_restored_page_id > curr_max_page_id) {
2052 // Need to update the site instance immediately.
2053 site_instance->UpdateMaxPageID(max_restored_page_id);
2054
2055 // Also tell the renderer to update its internal representation. We
2056 // need to reserve enough IDs to make all restored page IDs less than
2057 // the max.
2058 if (curr_max_page_id < 0)
2059 curr_max_page_id = 0;
2060 rvh->ReservePageIDRange(max_restored_page_id - curr_max_page_id);
2061 }
2062 }
2063}
2064
[email protected]ec0b6c42010-08-26 03:16:582065scoped_refptr<history::HistoryAddPageArgs>
2066TabContents::CreateHistoryAddPageArgs(
[email protected]ebe89e062009-08-13 23:16:542067 const GURL& virtual_url,
[email protected]befd8d822009-07-01 04:51:472068 const NavigationController::LoadCommittedDetails& details,
[email protected]420ae012009-04-24 05:16:322069 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]ec0b6c42010-08-26 03:16:582070 scoped_refptr<history::HistoryAddPageArgs> add_page_args(
2071 new history::HistoryAddPageArgs(
2072 params.url, base::Time::Now(), this, params.page_id, params.referrer,
2073 params.redirects, params.transition, history::SOURCE_BROWSED,
2074 details.did_replace_entry));
2075 if (PageTransition::IsMainFrame(params.transition) &&
2076 virtual_url != params.url) {
2077 // Hack on the "virtual" URL so that it will appear in history. For some
2078 // types of URLs, we will display a magic URL that is different from where
2079 // the page is actually navigated. We want the user to see in history what
2080 // they saw in the URL bar, so we add the virtual URL as a redirect. This
2081 // only applies to the main frame, as the virtual URL doesn't apply to
2082 // sub-frames.
2083 add_page_args->url = virtual_url;
2084 if (!add_page_args->redirects.empty())
2085 add_page_args->redirects.back() = virtual_url;
[email protected]420ae012009-04-24 05:16:322086 }
[email protected]ec0b6c42010-08-26 03:16:582087 return add_page_args;
[email protected]420ae012009-04-24 05:16:322088}
2089
2090bool TabContents::UpdateTitleForEntry(NavigationEntry* entry,
2091 const std::wstring& title) {
2092 // For file URLs without a title, use the pathname instead. In the case of a
2093 // synthesized title, we don't want the update to count toward the "one set
2094 // per page of the title to history."
[email protected]e53668962010-06-23 15:35:252095 string16 final_title;
[email protected]420ae012009-04-24 05:16:322096 bool explicit_set;
2097 if (entry->url().SchemeIsFile() && title.empty()) {
[email protected]e53668962010-06-23 15:35:252098 final_title = UTF8ToUTF16(entry->url().ExtractFileName());
[email protected]420ae012009-04-24 05:16:322099 explicit_set = false; // Don't count synthetic titles toward the set limit.
2100 } else {
[email protected]e53668962010-06-23 15:35:252101 TrimWhitespace(WideToUTF16Hack(title), TRIM_ALL, &final_title);
[email protected]420ae012009-04-24 05:16:322102 explicit_set = true;
2103 }
2104
[email protected]e53668962010-06-23 15:35:252105 if (final_title == entry->title())
[email protected]420ae012009-04-24 05:16:322106 return false; // Nothing changed, don't bother.
2107
[email protected]e53668962010-06-23 15:35:252108 entry->set_title(final_title);
[email protected]420ae012009-04-24 05:16:322109
[email protected]ec0b6c42010-08-26 03:16:582110 if (!received_page_title_) {
2111 UpdateHistoryPageTitle(*entry);
[email protected]420ae012009-04-24 05:16:322112 received_page_title_ = explicit_set;
2113 }
2114
2115 // Lastly, set the title for the view.
[email protected]e53668962010-06-23 15:35:252116 view_->SetPageTitle(UTF16ToWideHack(final_title));
[email protected]420ae012009-04-24 05:16:322117
[email protected]cbc0e1b2010-04-12 18:33:042118 NotificationService::current()->Notify(
2119 NotificationType::TAB_CONTENTS_TITLE_UPDATED,
2120 Source<TabContents>(this),
2121 NotificationService::NoDetails());
2122
[email protected]420ae012009-04-24 05:16:322123 return true;
2124}
2125
2126void TabContents::NotifySwapped() {
2127 // After sending out a swap notification, we need to send a disconnect
2128 // notification so that clients that pick up a pointer to |this| can NULL the
2129 // pointer. See Bug 1230284.
2130 notify_disconnection_ = true;
2131 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:342132 NotificationType::TAB_CONTENTS_SWAPPED,
[email protected]d82ed61e2009-06-16 02:46:222133 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:322134 NotificationService::NoDetails());
2135}
2136
2137void TabContents::NotifyConnected() {
2138 notify_disconnection_ = true;
2139 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:342140 NotificationType::TAB_CONTENTS_CONNECTED,
[email protected]d82ed61e2009-06-16 02:46:222141 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:322142 NotificationService::NoDetails());
2143}
2144
2145void TabContents::NotifyDisconnected() {
2146 if (!notify_disconnection_)
2147 return;
2148
2149 notify_disconnection_ = false;
2150 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:342151 NotificationType::TAB_CONTENTS_DISCONNECTED,
[email protected]d82ed61e2009-06-16 02:46:222152 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:322153 NotificationService::NoDetails());
2154}
2155
2156void TabContents::GenerateKeywordIfNecessary(
2157 const ViewHostMsg_FrameNavigate_Params& params) {
2158 if (!params.searchable_form_url.is_valid())
2159 return;
2160
2161 if (profile()->IsOffTheRecord())
2162 return;
2163
2164 int last_index = controller_.last_committed_entry_index();
2165 // When there was no previous page, the last index will be 0. This is
2166 // normally due to a form submit that opened in a new tab.
2167 // TODO(brettw) bug 916126: we should support keywords when form submits
2168 // happen in new tabs.
2169 if (last_index <= 0)
2170 return;
2171 const NavigationEntry* previous_entry =
2172 controller_.GetEntryAtIndex(last_index - 1);
2173 if (IsFormSubmit(previous_entry)) {
2174 // Only generate a keyword if the previous page wasn't itself a form
2175 // submit.
2176 return;
2177 }
2178
2179 GURL keyword_url = previous_entry->user_typed_url().is_valid() ?
2180 previous_entry->user_typed_url() : previous_entry->url();
[email protected]400b133f2011-01-19 18:32:302181 string16 keyword =
[email protected]420ae012009-04-24 05:16:322182 TemplateURLModel::GenerateKeyword(keyword_url, true); // autodetected
2183 if (keyword.empty())
2184 return;
2185
2186 TemplateURLModel* url_model = profile()->GetTemplateURLModel();
2187 if (!url_model)
2188 return;
2189
2190 if (!url_model->loaded()) {
2191 url_model->Load();
2192 return;
2193 }
2194
2195 const TemplateURL* current_url;
[email protected]ddd231e2010-06-29 20:35:192196 GURL url = params.searchable_form_url;
[email protected]420ae012009-04-24 05:16:322197 if (!url_model->CanReplaceKeyword(keyword, url, &current_url))
2198 return;
2199
2200 if (current_url) {
2201 if (current_url->originating_url().is_valid()) {
2202 // The existing keyword was generated from an OpenSearch description
2203 // document, don't regenerate.
2204 return;
2205 }
2206 url_model->Remove(current_url);
2207 }
2208 TemplateURL* new_url = new TemplateURL();
2209 new_url->set_keyword(keyword);
2210 new_url->set_short_name(keyword);
[email protected]ddd231e2010-06-29 20:35:192211 new_url->SetURL(url.spec(), 0, 0);
[email protected]420ae012009-04-24 05:16:322212 new_url->add_input_encoding(params.searchable_form_encoding);
2213 DCHECK(controller_.GetLastCommittedEntry());
2214 const GURL& favicon_url =
2215 controller_.GetLastCommittedEntry()->favicon().url();
2216 if (favicon_url.is_valid()) {
2217 new_url->SetFavIconURL(favicon_url);
2218 } else {
2219 // The favicon url isn't valid. This means there really isn't a favicon,
2220 // or the favicon url wasn't obtained before the load started. This assumes
2221 // the later.
2222 // TODO(sky): Need a way to set the favicon that doesn't involve generating
2223 // its url.
2224 new_url->SetFavIconURL(TemplateURL::GenerateFaviconURL(params.referrer));
2225 }
2226 new_url->set_safe_for_autoreplace(true);
2227 url_model->Add(new_url);
2228}
2229
[email protected]bf5c2ff392009-07-08 16:24:332230void TabContents::OnUserGesture() {
2231 // See comment in RenderViewHostDelegate::OnUserGesture as to why we do this.
[email protected]41291322010-07-15 17:09:012232 DownloadRequestLimiter* limiter =
2233 g_browser_process->download_request_limiter();
2234 if (limiter)
2235 limiter->OnUserGesture(this);
[email protected]d070ec62010-07-27 21:28:262236 ExternalProtocolHandler::PermitLaunchUrl();
[email protected]bf5c2ff392009-07-08 16:24:332237}
2238
2239void TabContents::OnFindReply(int request_id,
2240 int number_of_matches,
2241 const gfx::Rect& selection_rect,
2242 int active_match_ordinal,
2243 bool final_update) {
[email protected]3ce7ced2009-07-24 18:52:422244 // Ignore responses for requests that have been aborted.
2245 if (find_op_aborted_)
2246 return;
2247
[email protected]bf5c2ff392009-07-08 16:24:332248 // Ignore responses for requests other than the one we have most recently
2249 // issued. That way we won't act on stale results when the user has
2250 // already typed in another query.
2251 if (request_id != current_find_request_id_)
2252 return;
2253
2254 if (number_of_matches == -1)
2255 number_of_matches = last_search_result_.number_of_matches();
2256 if (active_match_ordinal == -1)
2257 active_match_ordinal = last_search_result_.active_match_ordinal();
2258
2259 gfx::Rect selection = selection_rect;
2260 if (selection.IsEmpty())
2261 selection = last_search_result_.selection_rect();
2262
2263 // Notify the UI, automation and any other observers that a find result was
2264 // found.
2265 last_search_result_ = FindNotificationDetails(request_id, number_of_matches,
2266 selection, active_match_ordinal,
2267 final_update);
2268 NotificationService::current()->Notify(
2269 NotificationType::FIND_RESULT_AVAILABLE,
2270 Source<TabContents>(this),
2271 Details<FindNotificationDetails>(&last_search_result_));
2272}
2273
2274void TabContents::GoToEntryAtOffset(int offset) {
[email protected]876bc832010-09-07 16:29:542275 if (!delegate_ || delegate_->OnGoToEntryOffset(offset)) {
2276 NavigationEntry* entry = controller_.GetEntryAtOffset(offset);
2277 if (!entry)
2278 return;
2279 // Note that we don't call NavigationController::GotToOffset() as we don't
2280 // want to create a pending navigation entry (it might end up lingering
2281 // http://crbug.com/51680).
[email protected]784688a62010-09-13 07:06:522282 entry->set_transition_type(entry->transition_type() |
2283 PageTransition::FORWARD_BACK);
[email protected]876bc832010-09-07 16:29:542284 NavigateToEntry(*entry, NavigationController::NO_RELOAD);
2285 }
[email protected]bf5c2ff392009-07-08 16:24:332286}
2287
[email protected]bf5c2ff392009-07-08 16:24:332288void TabContents::OnMissingPluginStatus(int status) {
2289#if defined(OS_WIN)
2290// TODO(PORT): pull in when plug-ins work
2291 GetPluginInstaller()->OnMissingPluginStatus(status);
2292#endif
2293}
2294
2295void TabContents::OnCrashedPlugin(const FilePath& plugin_path) {
[email protected]bf5c2ff392009-07-08 16:24:332296 DCHECK(!plugin_path.value().empty());
2297
2298 std::wstring plugin_name = plugin_path.ToWStringHack();
[email protected]191eb3f72010-12-21 06:27:502299 webkit::npapi::WebPluginInfo plugin_info;
2300 if (webkit::npapi::PluginList::Singleton()->GetPluginInfoByPath(
[email protected]45446a52010-11-04 17:41:002301 plugin_path, &plugin_info) &&
2302 !plugin_info.name.empty()) {
2303 plugin_name = UTF16ToWide(plugin_info.name);
[email protected]996fd702009-09-04 19:12:372304#if defined(OS_MACOSX)
[email protected]45446a52010-11-04 17:41:002305 // Many plugins on the Mac have .plugin in the actual name, which looks
2306 // terrible, so look for that and strip it off if present.
2307 const std::wstring plugin_extension(L".plugin");
2308 if (EndsWith(plugin_name, plugin_extension, true))
2309 plugin_name.erase(plugin_name.length() - plugin_extension.length());
[email protected]996fd702009-09-04 19:12:372310#endif // OS_MACOSX
[email protected]bf5c2ff392009-07-08 16:24:332311 }
[email protected]2ae8a922010-06-28 20:27:532312 SkBitmap* crash_icon = ResourceBundle::GetSharedInstance().GetBitmapNamed(
2313 IDR_INFOBAR_PLUGIN_CRASHED);
[email protected]f4f50ef2011-01-21 19:01:192314 AddInfoBar(new SimpleAlertInfoBarDelegate(this, crash_icon,
2315 l10n_util::GetStringFUTF16(IDS_PLUGIN_CRASHED_PROMPT,
2316 WideToUTF16Hack(plugin_name)), true));
[email protected]bf5c2ff392009-07-08 16:24:332317}
2318
2319void TabContents::OnCrashedWorker() {
[email protected]f4f50ef2011-01-21 19:01:192320 AddInfoBar(new SimpleAlertInfoBarDelegate(this, NULL,
2321 l10n_util::GetStringUTF16(IDS_WEBWORKER_CRASHED_PROMPT), true));
[email protected]bf5c2ff392009-07-08 16:24:332322}
2323
[email protected]38789d82010-11-17 06:03:442324void TabContents::OnDidGetApplicationInfo(int32 page_id,
2325 const WebApplicationInfo& info) {
[email protected]ed5431872009-11-17 08:39:512326 web_app_info_ = info;
[email protected]bf5c2ff392009-07-08 16:24:332327
[email protected]ed5431872009-11-17 08:39:512328 if (delegate())
2329 delegate()->OnDidGetApplicationInfo(this, page_id);
[email protected]bf5c2ff392009-07-08 16:24:332330}
2331
[email protected]bb461532010-11-26 21:50:232332void TabContents::OnInstallApplication(const WebApplicationInfo& info) {
2333 if (delegate())
2334 delegate()->OnInstallApplication(this, info);
2335}
2336
[email protected]3c2826852010-12-16 19:09:142337void TabContents::OnBlockedOutdatedPlugin(const string16& name,
2338 const GURL& update_url) {
2339 new OutdatedPluginInfoBar(this, name, update_url);
[email protected]851b1eb2010-08-09 13:32:292340}
2341
[email protected]a2cf65eb2010-01-18 08:20:382342void TabContents::OnPageContents(const GURL& url,
2343 int renderer_process_id,
2344 int32 page_id,
[email protected]e5106202010-06-11 21:12:362345 const string16& contents,
[email protected]1c57b7b2010-07-12 17:38:102346 const std::string& language,
2347 bool page_translatable) {
[email protected]a2cf65eb2010-01-18 08:20:382348 // Don't index any https pages. People generally don't want their bank
2349 // accounts, etc. indexed on their computer, especially since some of these
2350 // things are not marked cachable.
2351 // TODO(brettw) we may want to consider more elaborate heuristics such as
2352 // the cachability of the page. We may also want to consider subframes (this
2353 // test will still index subframes if the subframe is SSL).
[email protected]ccd40a52010-05-11 16:15:272354 // TODO(zelidrag) bug chromium-os:2808 - figure out if we want to reenable
2355 // content indexing for chromeos in some future releases.
2356#if !defined(OS_CHROMEOS)
[email protected]a2cf65eb2010-01-18 08:20:382357 if (!url.SchemeIsSecure()) {
2358 Profile* p = profile();
2359 if (p && !p->IsOffTheRecord()) {
2360 HistoryService* hs = p->GetHistoryService(Profile::IMPLICIT_ACCESS);
2361 if (hs)
2362 hs->SetPageContents(url, contents);
2363 }
2364 }
[email protected]ccd40a52010-05-11 16:15:272365#endif
[email protected]a8a81292010-01-21 00:32:452366
[email protected]1c57b7b2010-07-12 17:38:102367 language_state_.LanguageDetermined(language, page_translatable);
[email protected]a8a81292010-01-21 00:32:452368
2369 std::string lang = language;
2370 NotificationService::current()->Notify(
2371 NotificationType::TAB_LANGUAGE_DETERMINED,
[email protected]7e0831b2010-02-01 08:45:152372 Source<TabContents>(this),
[email protected]a8a81292010-01-21 00:32:452373 Details<std::string>(&lang));
[email protected]d54169e92011-01-21 09:19:522374
2375 // Generate the thumbnail here if the in-browser thumbnailing is enabled.
2376 if (CommandLine::ForCurrentProcess()->HasSwitch(
2377 switches::kEnableInBrowserThumbnailing)) {
2378 ThumbnailGenerator::UpdateThumbnailIfNecessary(this, url);
2379 }
[email protected]a2cf65eb2010-01-18 08:20:382380}
2381
[email protected]7e0831b2010-02-01 08:45:152382void TabContents::OnPageTranslated(int32 page_id,
2383 const std::string& original_lang,
[email protected]152b8ba32010-03-29 19:40:162384 const std::string& translated_lang,
2385 TranslateErrors::Type error_type) {
[email protected]3b1c1812010-02-12 21:53:552386 language_state_.set_current_language(translated_lang);
2387 language_state_.set_translation_pending(false);
[email protected]152b8ba32010-03-29 19:40:162388 PageTranslatedDetails details(original_lang, translated_lang, error_type);
[email protected]7e0831b2010-02-01 08:45:152389 NotificationService::current()->Notify(
2390 NotificationType::PAGE_TRANSLATED,
2391 Source<TabContents>(this),
[email protected]152b8ba32010-03-29 19:40:162392 Details<PageTranslatedDetails>(&details));
[email protected]7e0831b2010-02-01 08:45:152393}
2394
[email protected]ce833282010-11-04 15:48:392395void TabContents::OnSetSuggestions(
2396 int32 page_id,
2397 const std::vector<std::string>& suggestions) {
[email protected]15d9c0c2010-09-10 22:36:262398 if (delegate())
[email protected]ce833282010-11-04 15:48:392399 delegate()->OnSetSuggestions(page_id, suggestions);
2400}
2401
2402void TabContents::OnInstantSupportDetermined(int32 page_id, bool result) {
2403 if (delegate())
2404 delegate()->OnInstantSupportDetermined(page_id, result);
[email protected]15d9c0c2010-09-10 22:36:262405}
2406
[email protected]dcd5b332010-08-11 08:55:182407void TabContents::OnContentSettingsAccessed(bool content_was_blocked) {
[email protected]614c0e12010-03-25 10:12:562408 if (delegate_)
[email protected]20ad6a92010-04-21 14:07:242409 delegate_->OnContentSettingsChange(this);
[email protected]614c0e12010-03-25 10:12:562410}
2411
[email protected]8d3347f2009-07-09 22:00:212412RenderViewHostDelegate::View* TabContents::GetViewDelegate() {
[email protected]420ae012009-04-24 05:16:322413 return view_.get();
2414}
2415
[email protected]8d3347f2009-07-09 22:00:212416RenderViewHostDelegate::RendererManagement*
2417TabContents::GetRendererManagementDelegate() {
2418 return &render_manager_;
2419}
2420
[email protected]bf5c2ff392009-07-08 16:24:332421RenderViewHostDelegate::BrowserIntegration*
[email protected]8d3347f2009-07-09 22:00:212422 TabContents::GetBrowserIntegrationDelegate() {
2423 return this;
[email protected]bf5c2ff392009-07-08 16:24:332424}
2425
[email protected]46ba9082010-06-22 10:00:582426RenderViewHostDelegate::ContentSettings*
2427TabContents::GetContentSettingsDelegate() {
[email protected]9fb83e82010-07-02 18:24:552428 return content_settings_delegate_.get();
[email protected]46ba9082010-06-22 10:00:582429}
2430
[email protected]8d3347f2009-07-09 22:00:212431RenderViewHostDelegate::Save* TabContents::GetSaveDelegate() {
[email protected]420ae012009-04-24 05:16:322432 return save_package_.get(); // May be NULL, but we can return NULL.
2433}
2434
[email protected]8d3347f2009-07-09 22:00:212435RenderViewHostDelegate::Printing* TabContents::GetPrintingDelegate() {
[email protected]12802702010-07-09 19:43:092436 return printing_.get();
[email protected]8d3347f2009-07-09 22:00:212437}
2438
2439RenderViewHostDelegate::FavIcon* TabContents::GetFavIconDelegate() {
2440 return &fav_icon_helper_;
2441}
2442
[email protected]1f18184a2010-07-21 19:34:492443RenderViewHostDelegate::SSL* TabContents::GetSSLDelegate() {
2444 return GetSSLHelper();
2445}
2446
[email protected]b5977a0c2010-08-24 19:46:262447RenderViewHostDelegate::FileSelect* TabContents::GetFileSelectDelegate() {
2448 if (file_select_helper_.get() == NULL)
[email protected]ba70d082010-09-10 16:54:492449 file_select_helper_.reset(new FileSelectHelper(profile()));
[email protected]b5977a0c2010-08-24 19:46:262450 return file_select_helper_.get();
2451}
2452
[email protected]fd571bb2010-03-19 01:23:542453AutomationResourceRoutingDelegate*
2454TabContents::GetAutomationResourceRoutingDelegate() {
2455 return delegate();
2456}
2457
[email protected]ced90ae12010-02-20 02:06:162458RenderViewHostDelegate::BookmarkDrag* TabContents::GetBookmarkDragDelegate() {
2459 return bookmark_drag_;
2460}
2461
2462void TabContents::SetBookmarkDragDelegate(
2463 RenderViewHostDelegate::BookmarkDrag* bookmark_drag) {
2464 bookmark_drag_ = bookmark_drag;
2465}
2466
[email protected]9fb83e82010-07-02 18:24:552467TabSpecificContentSettings* TabContents::GetTabSpecificContentSettings() const {
2468 return content_settings_delegate_.get();
2469}
2470
[email protected]93623c5d2009-12-10 21:40:322471RendererPreferences TabContents::GetRendererPrefs(Profile* profile) const {
[email protected]840b1512009-07-21 16:53:462472 return renderer_preferences_;
[email protected]80d96fa2009-06-10 22:34:512473}
2474
[email protected]57c6a652009-05-04 07:58:342475TabContents* TabContents::GetAsTabContents() {
2476 return this;
2477}
2478
[email protected]7b291f92009-08-14 05:43:532479ViewType::Type TabContents::GetRenderViewType() const {
2480 return ViewType::TAB_CONTENTS;
2481}
2482
2483int TabContents::GetBrowserWindowID() const {
2484 return controller().window_id().id();
2485}
2486
[email protected]420ae012009-04-24 05:16:322487void TabContents::RenderViewCreated(RenderViewHost* render_view_host) {
[email protected]2e4633c2009-07-09 16:58:062488 NotificationService::current()->Notify(
2489 NotificationType::RENDER_VIEW_HOST_CREATED_FOR_TAB,
2490 Source<TabContents>(this),
2491 Details<RenderViewHost>(render_view_host));
[email protected]420ae012009-04-24 05:16:322492 NavigationEntry* entry = controller_.GetActiveEntry();
2493 if (!entry)
2494 return;
2495
2496 // When we're creating views, we're still doing initial setup, so we always
2497 // use the pending DOM UI rather than any possibly existing committed one.
2498 if (render_manager_.pending_dom_ui()) {
2499 render_manager_.pending_dom_ui()->RenderViewCreated(render_view_host);
2500 }
2501
2502 if (entry->IsViewSourceMode()) {
2503 // Put the renderer in view source mode.
2504 render_view_host->Send(
2505 new ViewMsg_EnableViewSourceMode(render_view_host->routing_id()));
2506 }
[email protected]0666aef2009-05-13 19:48:082507
2508 view()->RenderViewCreated(render_view_host);
[email protected]420ae012009-04-24 05:16:322509}
2510
2511void TabContents::RenderViewReady(RenderViewHost* rvh) {
2512 if (rvh != render_view_host()) {
2513 // Don't notify the world, since this came from a renderer in the
2514 // background.
2515 return;
2516 }
2517
2518 NotifyConnected();
[email protected]9d8a4642009-07-29 17:25:302519 bool was_crashed = is_crashed();
[email protected]443b80e2010-12-14 00:42:232520 SetIsCrashed(base::TERMINATION_STATUS_STILL_RUNNING, 0);
[email protected]9d8a4642009-07-29 17:25:302521
2522 // Restore the focus to the tab (otherwise the focus will be on the top
2523 // window).
[email protected]484ae5912010-09-29 19:16:142524 if (was_crashed && !FocusLocationBarByDefault() &&
2525 (!delegate_ || delegate_->ShouldFocusPageAfterCrash())) {
[email protected]9d8a4642009-07-29 17:25:302526 Focus();
[email protected]484ae5912010-09-29 19:16:142527 }
[email protected]420ae012009-04-24 05:16:322528}
2529
[email protected]443b80e2010-12-14 00:42:232530void TabContents::RenderViewGone(RenderViewHost* rvh,
2531 base::TerminationStatus status,
2532 int error_code) {
[email protected]420ae012009-04-24 05:16:322533 // Ask the print preview if this renderer was valuable.
[email protected]12802702010-07-09 19:43:092534 if (!printing_->OnRenderViewGone(rvh))
[email protected]420ae012009-04-24 05:16:322535 return;
2536 if (rvh != render_view_host()) {
2537 // The pending page's RenderViewHost is gone.
2538 return;
2539 }
2540
2541 SetIsLoading(false, NULL);
2542 NotifyDisconnected();
[email protected]443b80e2010-12-14 00:42:232543 SetIsCrashed(status, error_code);
[email protected]420ae012009-04-24 05:16:322544
[email protected]ff9efc62010-02-18 22:10:542545 // Remove all infobars.
[email protected]e5430c92010-02-18 22:39:212546 for (int i = infobar_delegate_count() - 1; i >=0 ; --i)
[email protected]ff9efc62010-02-18 22:10:542547 RemoveInfoBar(GetInfoBarDelegateAt(i));
2548
[email protected]8e537302009-06-10 21:19:582549 // Tell the view that we've crashed so it can prepare the sad tab page.
[email protected]cf68b092010-01-15 20:05:552550 // Only do this if we're not in browser shutdown, so that TabContents
2551 // objects that are not in a browser (e.g., HTML dialogs) and thus are
2552 // visible do not flash a sad tab page.
2553 if (browser_shutdown::GetShutdownType() == browser_shutdown::NOT_VALID)
[email protected]a8f024392011-01-13 21:50:162554 view_->OnTabCrashed(status, error_code);
[email protected]420ae012009-04-24 05:16:322555
2556 // Hide any visible hung renderer warning for this web contents' process.
[email protected]e7b8d832009-11-20 22:41:002557 hung_renderer_dialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:322558}
2559
[email protected]2e4633c2009-07-09 16:58:062560void TabContents::RenderViewDeleted(RenderViewHost* rvh) {
2561 NotificationService::current()->Notify(
2562 NotificationType::RENDER_VIEW_HOST_DELETED,
2563 Source<TabContents>(this),
2564 Details<RenderViewHost>(rvh));
[email protected]fb7b79f72009-11-06 18:00:482565 render_manager_.RenderViewDeleted(rvh);
[email protected]2e4633c2009-07-09 16:58:062566}
2567
[email protected]420ae012009-04-24 05:16:322568void TabContents::DidNavigate(RenderViewHost* rvh,
2569 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]8030f012009-09-25 18:09:372570 int extra_invalidate_flags = 0;
2571
2572 if (PageTransition::IsMainFrame(params.transition)) {
[email protected]e7d50892011-01-19 21:47:382573 if (MaybeUsePreloadedPage(params.url)) {
2574 return;
[email protected]33f74972010-12-08 16:40:362575 }
2576
[email protected]20ede072009-10-06 00:28:352577 bool was_bookmark_bar_visible = ShouldShowBookmarkBar();
[email protected]8030f012009-09-25 18:09:372578
[email protected]420ae012009-04-24 05:16:322579 render_manager_.DidNavigateMainFrame(rvh);
2580
[email protected]20ede072009-10-06 00:28:352581 if (was_bookmark_bar_visible != ShouldShowBookmarkBar())
[email protected]8030f012009-09-25 18:09:372582 extra_invalidate_flags |= INVALIDATE_BOOKMARK_BAR;
[email protected]8030f012009-09-25 18:09:372583 }
2584
[email protected]420ae012009-04-24 05:16:322585 // Update the site of the SiteInstance if it doesn't have one yet.
2586 if (!GetSiteInstance()->has_site())
2587 GetSiteInstance()->SetSite(params.url);
2588
2589 // Need to update MIME type here because it's referred to in
2590 // UpdateNavigationCommands() called by RendererDidNavigate() to
2591 // determine whether or not to enable the encoding menu.
2592 // It's updated only for the main frame. For a subframe,
2593 // RenderView::UpdateURL does not set params.contents_mime_type.
2594 // (see http://code.google.com/p/chromium/issues/detail?id=2929 )
2595 // TODO(jungshik): Add a test for the encoding menu to avoid
2596 // regressing it again.
2597 if (PageTransition::IsMainFrame(params.transition))
2598 contents_mime_type_ = params.contents_mime_type;
2599
2600 NavigationController::LoadCommittedDetails details;
[email protected]8030f012009-09-25 18:09:372601 bool did_navigate = controller_.RendererDidNavigate(
2602 params, extra_invalidate_flags, &details);
[email protected]3e90d4a2009-07-03 17:38:392603
[email protected]a9c0bfe2010-09-17 08:35:222604 // Send notification about committed provisional loads. This notification is
2605 // different from the NAV_ENTRY_COMMITTED notification which doesn't include
2606 // the actual URL navigated to and isn't sent for AUTO_SUBFRAME navigations.
2607 if (details.type != NavigationType::NAV_IGNORE) {
[email protected]dabb0d12010-10-05 12:50:072608 // For AUTO_SUBFRAME navigations, an event for the main frame is generated
2609 // that is not recorded in the navigation history. For the purpose of
2610 // tracking navigation events, we treat this event as a sub frame navigation
2611 // event.
2612 bool is_main_frame = did_navigate ? details.is_main_frame : false;
2613 ProvisionalLoadDetails load_details(
2614 is_main_frame, details.is_in_page, params.url, std::string(), false,
[email protected]dd0aafd2010-12-02 13:13:412615 false, params.frame_id);
[email protected]a9c0bfe2010-09-17 08:35:222616 load_details.set_transition_type(params.transition);
2617 // Whether or not a page transition was triggered by going backward or
2618 // forward in the history is only stored in the navigation controller's
2619 // entry list.
2620 if (did_navigate &&
2621 (controller_.GetActiveEntry()->transition_type() &
2622 PageTransition::FORWARD_BACK)) {
2623 load_details.set_transition_type(
2624 params.transition | PageTransition::FORWARD_BACK);
2625 }
2626 NotificationService::current()->Notify(
2627 NotificationType::FRAME_PROVISIONAL_LOAD_COMMITTED,
2628 Source<NavigationController>(&controller_),
2629 Details<ProvisionalLoadDetails>(&load_details));
[email protected]a9c0bfe2010-09-17 08:35:222630 }
2631
[email protected]3e90d4a2009-07-03 17:38:392632 // Update history. Note that this needs to happen after the entry is complete,
2633 // which WillNavigate[Main,Sub]Frame will do before this function is called.
[email protected]ec0b6c42010-08-26 03:16:582634 if (params.should_update_history) {
[email protected]3e90d4a2009-07-03 17:38:392635 // Most of the time, the displayURL matches the loaded URL, but for about:
[email protected]ec0b6c42010-08-26 03:16:582636 // URLs, we use a data: URL as the real value. We actually want to save the
2637 // about: URL to the history db and keep the data: URL hidden. This is what
2638 // the TabContents' URL getter does.
2639 scoped_refptr<history::HistoryAddPageArgs> add_page_args(
2640 CreateHistoryAddPageArgs(GetURL(), details, params));
2641 if (!delegate() ||
2642 delegate()->ShouldAddNavigationToHistory(*add_page_args,
2643 details.type)) {
2644 UpdateHistoryForNavigation(add_page_args);
2645 }
[email protected]3e90d4a2009-07-03 17:38:392646 }
2647
[email protected]76543b92009-08-31 17:27:452648 if (!did_navigate)
2649 return; // No navigation happened.
[email protected]420ae012009-04-24 05:16:322650
2651 // DO NOT ADD MORE STUFF TO THIS FUNCTION! Your component should either listen
2652 // for the appropriate notification (best) or you can add it to
2653 // DidNavigateMainFramePostCommit / DidNavigateAnyFramePostCommit (only if
2654 // necessary, please).
2655
2656 // Run post-commit tasks.
2657 if (details.is_main_frame)
2658 DidNavigateMainFramePostCommit(details, params);
2659 DidNavigateAnyFramePostCommit(rvh, details, params);
2660}
2661
2662void TabContents::UpdateState(RenderViewHost* rvh,
2663 int32 page_id,
2664 const std::string& state) {
2665 DCHECK(rvh == render_view_host());
2666
2667 // We must be prepared to handle state updates for any page, these occur
2668 // when the user is scrolling and entering form data, as well as when we're
2669 // leaving a page, in which case our state may have already been moved to
2670 // the next page. The navigation controller will look up the appropriate
2671 // NavigationEntry and update it when it is notified via the delegate.
2672
2673 int entry_index = controller_.GetEntryIndexWithPageID(
2674 GetSiteInstance(), page_id);
2675 if (entry_index < 0)
2676 return;
2677 NavigationEntry* entry = controller_.GetEntryAtIndex(entry_index);
2678
2679 if (state == entry->content_state())
2680 return; // Nothing to update.
2681 entry->set_content_state(state);
2682 controller_.NotifyEntryChanged(entry, entry_index);
2683}
2684
2685void TabContents::UpdateTitle(RenderViewHost* rvh,
2686 int32 page_id, const std::wstring& title) {
2687 // If we have a title, that's a pretty good indication that we've started
2688 // getting useful data.
2689 SetNotWaitingForResponse();
2690
2691 DCHECK(rvh == render_view_host());
[email protected]371cc8b2010-09-01 16:03:482692 NavigationEntry* entry = controller_.GetEntryWithPageID(rvh->site_instance(),
[email protected]ec0b6c42010-08-26 03:16:582693 page_id);
[email protected]420ae012009-04-24 05:16:322694 if (!entry || !UpdateTitleForEntry(entry, title))
2695 return;
2696
2697 // Broadcast notifications when the UI should be updated.
2698 if (entry == controller_.GetEntryAtOffset(0))
[email protected]f1cd5e82009-10-23 17:19:032699 NotifyNavigationStateChanged(INVALIDATE_TITLE);
[email protected]420ae012009-04-24 05:16:322700}
2701
[email protected]420ae012009-04-24 05:16:322702void TabContents::UpdateEncoding(RenderViewHost* render_view_host,
[email protected]41fc0322009-09-04 22:23:402703 const std::string& encoding) {
[email protected]420ae012009-04-24 05:16:322704 set_encoding(encoding);
2705}
2706
2707void TabContents::UpdateTargetURL(int32 page_id, const GURL& url) {
2708 if (delegate())
2709 delegate()->UpdateTargetURL(this, url);
2710}
2711
2712void TabContents::UpdateThumbnail(const GURL& url,
2713 const SkBitmap& bitmap,
2714 const ThumbnailScore& score) {
[email protected]e6baed562010-11-02 22:33:442715 if (profile()->IsOffTheRecord())
2716 return;
2717
[email protected]420ae012009-04-24 05:16:322718 // Tell History about this thumbnail
[email protected]54097492f2011-01-18 08:32:072719 history::TopSites* ts = profile()->GetTopSites();
2720 if (ts)
2721 ts->SetPageThumbnail(url, bitmap, score);
[email protected]420ae012009-04-24 05:16:322722}
2723
[email protected]c4e98902010-06-01 10:20:142724void TabContents::UpdateInspectorSetting(const std::string& key,
2725 const std::string& value) {
[email protected]24d27d72010-10-12 16:30:052726 RenderViewHostDelegateHelper::UpdateInspectorSetting(profile(), key, value);
[email protected]0df30122009-06-03 12:13:082727}
2728
[email protected]45aafc92010-06-07 14:18:532729void TabContents::ClearInspectorSettings() {
[email protected]24d27d72010-10-12 16:30:052730 RenderViewHostDelegateHelper::ClearInspectorSettings(profile());
[email protected]45aafc92010-06-07 14:18:532731}
2732
[email protected]420ae012009-04-24 05:16:322733void TabContents::Close(RenderViewHost* rvh) {
[email protected]07707302009-11-06 00:50:292734 // The UI may be in an event-tracking loop, such as between the
2735 // mouse-down and mouse-up in text selection or a button click.
2736 // Defer the close until after tracking is complete, so that we
2737 // don't free objects out from under the UI.
2738 // TODO(shess): This could probably be integrated with the
2739 // IsDoingDrag() test below. Punting for now because I need more
2740 // research to understand how this impacts platforms other than Mac.
2741 // TODO(shess): This could get more fine-grained. For instance,
2742 // closing a tab in another window while selecting text in the
2743 // current window's Omnibox should be just fine.
2744 if (view()->IsEventTracking()) {
2745 view()->CloseTabAfterEventTracking();
2746 return;
2747 }
2748
[email protected]24a4d1062009-07-10 23:10:422749 // If we close the tab while we're in the middle of a drag, we'll crash.
2750 // Instead, cancel the drag and close it as soon as the drag ends.
2751 if (view()->IsDoingDrag()) {
2752 view()->CancelDragAndCloseTab();
2753 return;
2754 }
2755
[email protected]420ae012009-04-24 05:16:322756 // Ignore this if it comes from a RenderViewHost that we aren't showing.
2757 if (delegate() && rvh == render_view_host())
2758 delegate()->CloseContents(this);
2759}
2760
2761void TabContents::RequestMove(const gfx::Rect& new_bounds) {
2762 if (delegate() && delegate()->IsPopup(this))
2763 delegate()->MoveContents(this, new_bounds);
2764}
2765
[email protected]7ab1e7d62009-10-14 23:32:012766void TabContents::DidStartLoading() {
[email protected]420ae012009-04-24 05:16:322767 SetIsLoading(true, NULL);
[email protected]3c9e1872010-11-18 16:17:492768
[email protected]de4761b2010-12-07 21:05:212769 if (delegate()) {
2770 bool is_print_preview_tab =
2771 printing::PrintPreviewTabController::IsPrintPreviewTab(this);
2772 if (content_restrictions_ || is_print_preview_tab) {
2773 content_restrictions_= is_print_preview_tab ?
2774 CONTENT_RESTRICTION_PRINT : 0;
2775 delegate()->ContentRestrictionsChanged(this);
2776 }
[email protected]9e823662010-10-13 23:36:002777 }
[email protected]3c9e1872010-11-18 16:17:492778
2779 // Notify observers about navigation.
2780 FOR_EACH_OBSERVER(WebNavigationObserver, web_navigation_observers_,
2781 DidStartLoading());
[email protected]420ae012009-04-24 05:16:322782}
2783
[email protected]7ab1e7d62009-10-14 23:32:012784void TabContents::DidStopLoading() {
[email protected]420ae012009-04-24 05:16:322785 scoped_ptr<LoadNotificationDetails> details;
2786
2787 NavigationEntry* entry = controller_.GetActiveEntry();
2788 // An entry may not exist for a stop when loading an initial blank page or
2789 // if an iframe injected by script into a blank page finishes loading.
2790 if (entry) {
[email protected]420ae012009-04-24 05:16:322791 base::TimeDelta elapsed = base::TimeTicks::Now() - current_load_start_;
2792
2793 details.reset(new LoadNotificationDetails(
[email protected]ebe89e062009-08-13 23:16:542794 entry->virtual_url(),
[email protected]420ae012009-04-24 05:16:322795 entry->transition_type(),
2796 elapsed,
2797 &controller_,
2798 controller_.GetCurrentEntryIndex()));
2799 }
2800
[email protected]420ae012009-04-24 05:16:322801 SetIsLoading(false, details.get());
[email protected]3c9e1872010-11-18 16:17:492802
2803 // Notify observers about navigation.
2804 FOR_EACH_OBSERVER(WebNavigationObserver, web_navigation_observers_,
2805 DidStopLoading());
[email protected]420ae012009-04-24 05:16:322806}
2807
[email protected]1a3c3cb2010-12-16 21:03:402808void TabContents::DidChangeLoadProgress(double progress) {
2809 if (delegate())
2810 delegate()->LoadProgressChanged(progress);
2811}
2812
[email protected]25497492010-09-11 15:15:082813void TabContents::DocumentOnLoadCompletedInMainFrame(
2814 RenderViewHost* render_view_host,
2815 int32 page_id) {
2816 NotificationService::current()->Notify(
2817 NotificationType::LOAD_COMPLETED_MAIN_FRAME,
2818 Source<TabContents>(this),
2819 Details<int>(&page_id));
2820}
2821
[email protected]420ae012009-04-24 05:16:322822void TabContents::RequestOpenURL(const GURL& url, const GURL& referrer,
2823 WindowOpenDisposition disposition) {
2824 if (render_manager_.dom_ui()) {
2825 // When we're a DOM UI, it will provide a page transition type for us (this
2826 // is so the new tab page can specify AUTO_BOOKMARK for automatically
2827 // generated suggestions).
2828 //
2829 // Note also that we hide the referrer for DOM UI pages. We don't really
[email protected]60e448982009-05-06 04:21:162830 // want web sites to see a referrer of "chrome://blah" (and some
2831 // chrome: URLs might have search terms or other stuff we don't want to
[email protected]420ae012009-04-24 05:16:322832 // send to the site), so we send no referrer.
2833 OpenURL(url, GURL(), disposition,
2834 render_manager_.dom_ui()->link_transition_type());
2835 } else {
2836 OpenURL(url, referrer, disposition, PageTransition::LINK);
2837 }
2838}
2839
[email protected]1ee614862009-07-06 20:29:182840void TabContents::DomOperationResponse(const std::string& json_string,
2841 int automation_id) {
2842}
2843
[email protected]8b8e7c92010-08-19 18:05:562844void TabContents::ProcessDOMUIMessage(
2845 const ViewHostMsg_DomMessage_Params& params) {
[email protected]420ae012009-04-24 05:16:322846 if (!render_manager_.dom_ui()) {
[email protected]c2e74fe82009-09-03 17:57:442847 // This can happen if someone uses window.open() to open an extension URL
2848 // from a non-extension context.
[email protected]8b8e7c92010-08-19 18:05:562849 render_view_host()->BlockExtensionRequest(params.request_id);
[email protected]420ae012009-04-24 05:16:322850 return;
2851 }
[email protected]8b8e7c92010-08-19 18:05:562852 render_manager_.dom_ui()->ProcessDOMUIMessage(params);
[email protected]420ae012009-04-24 05:16:322853}
2854
2855void TabContents::ProcessExternalHostMessage(const std::string& message,
2856 const std::string& origin,
2857 const std::string& target) {
2858 if (delegate())
2859 delegate()->ForwardMessageToExternalHost(message, origin, target);
2860}
2861
[email protected]420ae012009-04-24 05:16:322862void TabContents::RunJavaScriptMessage(
2863 const std::wstring& message,
2864 const std::wstring& default_prompt,
2865 const GURL& frame_url,
2866 const int flags,
2867 IPC::Message* reply_msg,
2868 bool* did_suppress_message) {
2869 // Suppress javascript messages when requested and when inside a constrained
2870 // popup window (because that activates them and breaks them out of the
2871 // constrained window jail).
[email protected]91ed49a2009-07-07 21:29:552872 // Also suppress messages when showing an interstitial. The interstitial is
2873 // shown over the previous page, we don't want the hidden page dialogs to
2874 // interfere with the interstitial.
[email protected]8f55e802010-12-06 18:11:502875 bool suppress_this_message =
2876 suppress_javascript_messages_ ||
2877 showing_interstitial_page() ||
2878 (delegate() && delegate()->ShouldSuppressDialogs());
[email protected]420ae012009-04-24 05:16:322879 if (delegate())
2880 suppress_this_message |=
[email protected]b6c874582009-05-08 19:38:312881 (delegate()->GetConstrainingContents(this) != this);
[email protected]420ae012009-04-24 05:16:322882
2883 *did_suppress_message = suppress_this_message;
2884
2885 if (!suppress_this_message) {
2886 base::TimeDelta time_since_last_message(
2887 base::TimeTicks::Now() - last_javascript_message_dismissal_);
2888 bool show_suppress_checkbox = false;
2889 // Show a checkbox offering to suppress further messages if this message is
2890 // being displayed within kJavascriptMessageExpectedDelay of the last one.
2891 if (time_since_last_message <
2892 base::TimeDelta::FromMilliseconds(kJavascriptMessageExpectedDelay))
2893 show_suppress_checkbox = true;
2894
[email protected]d6ad7442010-09-05 19:06:522895 RunJavascriptMessageBox(profile(), this, frame_url, flags, message,
2896 default_prompt, show_suppress_checkbox, reply_msg);
[email protected]420ae012009-04-24 05:16:322897 } else {
2898 // If we are suppressing messages, just reply as is if the user immediately
2899 // pressed "Cancel".
[email protected]beb440c2009-11-06 04:08:542900 OnMessageBoxClosed(reply_msg, false, std::wstring());
[email protected]420ae012009-04-24 05:16:322901 }
2902}
2903
2904void TabContents::RunBeforeUnloadConfirm(const std::wstring& message,
2905 IPC::Message* reply_msg) {
[email protected]5aab5e22010-12-08 22:13:292906 if (delegate())
2907 delegate()->WillRunBeforeUnloadConfirm();
[email protected]8f55e802010-12-06 18:11:502908 if (delegate() && delegate()->ShouldSuppressDialogs()) {
2909 render_view_host()->JavaScriptMessageBoxClosed(reply_msg, true,
2910 std::wstring());
2911 return;
2912 }
[email protected]7ab1e7d62009-10-14 23:32:012913 is_showing_before_unload_dialog_ = true;
[email protected]d82ed61e2009-06-16 02:46:222914 RunBeforeUnloadDialog(this, message, reply_msg);
[email protected]420ae012009-04-24 05:16:322915}
2916
2917void TabContents::ShowModalHTMLDialog(const GURL& url, int width, int height,
2918 const std::string& json_arguments,
2919 IPC::Message* reply_msg) {
2920 if (delegate()) {
2921 HtmlDialogUIDelegate* dialog_delegate =
2922 new ModalHtmlDialogDelegate(url, width, height, json_arguments,
[email protected]d82ed61e2009-06-16 02:46:222923 reply_msg, this);
[email protected]420ae012009-04-24 05:16:322924 delegate()->ShowHtmlDialog(dialog_delegate, NULL);
2925 }
2926}
2927
[email protected]420ae012009-04-24 05:16:322928// Checks to see if we should generate a keyword based on the OSDD, and if
2929// necessary uses TemplateURLFetcher to download the OSDD and create a keyword.
[email protected]2de7e002010-10-11 22:58:202930void TabContents::PageHasOSDD(
2931 RenderViewHost* render_view_host,
2932 int32 page_id,
2933 const GURL& url,
2934 const ViewHostMsg_PageHasOSDD_Type& msg_provider_type) {
[email protected]0b5b85c2010-10-11 23:00:122935 // Make sure page_id is the current page and other basic checks.
[email protected]420ae012009-04-24 05:16:322936 DCHECK(url.is_valid());
2937 if (!IsActiveEntry(page_id))
2938 return;
[email protected]420ae012009-04-24 05:16:322939 if (!profile()->GetTemplateURLFetcher())
2940 return;
[email protected]420ae012009-04-24 05:16:322941 if (profile()->IsOffTheRecord())
2942 return;
2943
[email protected]2de7e002010-10-11 22:58:202944 TemplateURLFetcher::ProviderType provider_type;
2945 switch (msg_provider_type.type) {
2946 case ViewHostMsg_PageHasOSDD_Type::AUTODETECTED_PROVIDER:
2947 provider_type = TemplateURLFetcher::AUTODETECTED_PROVIDER;
2948 break;
2949
2950 case ViewHostMsg_PageHasOSDD_Type::EXPLICIT_DEFAULT_PROVIDER:
2951 provider_type = TemplateURLFetcher::EXPLICIT_DEFAULT_PROVIDER;
2952 break;
2953
2954 case ViewHostMsg_PageHasOSDD_Type::EXPLICIT_PROVIDER:
2955 provider_type = TemplateURLFetcher::EXPLICIT_PROVIDER;
2956 break;
2957
2958 default:
2959 NOTREACHED();
2960 return;
2961 }
2962
[email protected]420ae012009-04-24 05:16:322963 const NavigationEntry* entry = controller_.GetLastCommittedEntry();
2964 DCHECK(entry);
2965
2966 const NavigationEntry* base_entry = entry;
2967 if (IsFormSubmit(base_entry)) {
2968 // If the current page is a form submit, find the last page that was not
2969 // a form submit and use its url to generate the keyword from.
2970 int index = controller_.last_committed_entry_index() - 1;
2971 while (index >= 0 && IsFormSubmit(controller_.GetEntryAtIndex(index)))
2972 index--;
2973 if (index >= 0)
2974 base_entry = controller_.GetEntryAtIndex(index);
2975 else
2976 base_entry = NULL;
2977 }
2978
2979 // We want to use the user typed URL if available since that represents what
2980 // the user typed to get here, and fall back on the regular URL if not.
2981 if (!base_entry)
2982 return;
2983 GURL keyword_url = base_entry->user_typed_url().is_valid() ?
2984 base_entry->user_typed_url() : base_entry->url();
2985 if (!keyword_url.is_valid())
2986 return;
[email protected]2de7e002010-10-11 22:58:202987
[email protected]400b133f2011-01-19 18:32:302988 string16 keyword = TemplateURLModel::GenerateKeyword(
[email protected]0b5b85c2010-10-11 23:00:122989 keyword_url,
2990 provider_type == TemplateURLFetcher::AUTODETECTED_PROVIDER);
[email protected]a88dbdd52010-01-15 19:25:532991
[email protected]0b5b85c2010-10-11 23:00:122992 // Download the OpenSearch description document. If this is successful, a
[email protected]420ae012009-04-24 05:16:322993 // new keyword will be created when done.
[email protected]420ae012009-04-24 05:16:322994 profile()->GetTemplateURLFetcher()->ScheduleDownload(
2995 keyword,
2996 url,
2997 base_entry->favicon().url(),
[email protected]0b5b85c2010-10-11 23:00:122998 new TemplateURLFetcherUICallbacks(this),
[email protected]2de7e002010-10-11 22:58:202999 provider_type);
[email protected]420ae012009-04-24 05:16:323000}
3001
[email protected]420ae012009-04-24 05:16:323002GURL TabContents::GetAlternateErrorPageURL() const {
3003 GURL url;
3004 // Disable alternate error pages when in OffTheRecord/Incognito mode.
3005 if (profile()->IsOffTheRecord())
3006 return url;
3007
3008 PrefService* prefs = profile()->GetPrefs();
3009 DCHECK(prefs);
3010 if (prefs->GetBoolean(prefs::kAlternateErrorPagesEnabled)) {
[email protected]a33457b82009-09-21 23:07:153011 url = google_util::AppendGoogleLocaleParam(
3012 GURL(google_util::kLinkDoctorBaseURL));
[email protected]420ae012009-04-24 05:16:323013 url = google_util::AppendGoogleTLDParam(url);
3014 }
3015 return url;
3016}
3017
3018WebPreferences TabContents::GetWebkitPrefs() {
[email protected]6c32ce72010-03-08 05:18:063019 Profile* profile = render_view_host()->process()->profile();
[email protected]7ccddb8c2009-08-04 17:36:553020 bool is_dom_ui = false;
[email protected]447021c2010-09-08 21:29:083021 WebPreferences web_prefs =
3022 RenderViewHostDelegateHelper::GetWebkitPrefs(profile, is_dom_ui);
3023
[email protected]9beff752010-09-22 19:35:433024 // Force accelerated compositing and 2d canvas off for chrome: and
3025 // chrome-extension: pages.
[email protected]27c790d42010-10-22 09:28:433026 if (GetURL().SchemeIs(chrome::kChromeDevToolsScheme) ||
3027 GetURL().SchemeIs(chrome::kChromeUIScheme)) {
[email protected]447021c2010-09-08 21:29:083028 web_prefs.accelerated_compositing_enabled = false;
[email protected]9beff752010-09-22 19:35:433029 web_prefs.accelerated_2d_canvas_enabled = false;
[email protected]447021c2010-09-08 21:29:083030 }
3031
[email protected]39553442010-09-09 00:01:553032#if defined(OS_MACOSX)
3033 // Disable accelerated compositing if IOSurface's are not supported,
3034 // as is the case in 10.5.
3035 if (!IOSurfaceSupport::Initialize())
3036 web_prefs.accelerated_compositing_enabled = false;
3037#endif
3038
[email protected]447021c2010-09-08 21:29:083039 return web_prefs;
[email protected]420ae012009-04-24 05:16:323040}
3041
[email protected]fa1cf0b82010-01-15 21:49:443042void TabContents::OnIgnoredUIEvent() {
3043 if (constrained_window_count()) {
3044 ConstrainedWindow* window = *constrained_window_begin();
3045 window->FocusConstrainedWindow();
3046 }
3047}
3048
[email protected]420ae012009-04-24 05:16:323049void TabContents::OnJSOutOfMemory() {
[email protected]f4f50ef2011-01-21 19:01:193050 AddInfoBar(new SimpleAlertInfoBarDelegate(this, NULL,
3051 l10n_util::GetStringUTF16(IDS_JS_OUT_OF_MEMORY_PROMPT), true));
[email protected]420ae012009-04-24 05:16:323052}
3053
[email protected]420ae012009-04-24 05:16:323054void TabContents::OnCrossSiteResponse(int new_render_process_host_id,
3055 int new_request_id) {
[email protected]57c6a652009-05-04 07:58:343056 // Allows the TabContents to react when a cross-site response is ready to be
[email protected]420ae012009-04-24 05:16:323057 // delivered to a pending RenderViewHost. We must first run the onunload
3058 // handler of the old RenderViewHost before we can allow it to proceed.
3059 render_manager_.OnCrossSiteResponse(new_render_process_host_id,
3060 new_request_id);
3061}
3062
[email protected]420ae012009-04-24 05:16:323063void TabContents::RendererUnresponsive(RenderViewHost* rvh,
3064 bool is_during_unload) {
3065 if (is_during_unload) {
3066 // Hang occurred while firing the beforeunload/unload handler.
3067 // Pretend the handler fired so tab closing continues as if it had.
[email protected]829e7612009-04-25 01:15:113068 rvh->set_sudden_termination_allowed(true);
[email protected]420ae012009-04-24 05:16:323069
3070 if (!render_manager_.ShouldCloseTabOnUnresponsiveRenderer())
3071 return;
3072
3073 // If the tab hangs in the beforeunload/unload handler there's really
3074 // nothing we can do to recover. Pretend the unload listeners have
3075 // all fired and close the tab. If the hang is in the beforeunload handler
3076 // then the user will not have the option of cancelling the close.
3077 Close(rvh);
3078 return;
3079 }
3080
3081 if (render_view_host() && render_view_host()->IsRenderViewLive())
[email protected]e7b8d832009-11-20 22:41:003082 hung_renderer_dialog::ShowForTabContents(this);
[email protected]420ae012009-04-24 05:16:323083}
3084
3085void TabContents::RendererResponsive(RenderViewHost* render_view_host) {
[email protected]e7b8d832009-11-20 22:41:003086 hung_renderer_dialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:323087}
3088
3089void TabContents::LoadStateChanged(const GURL& url,
[email protected]094e5b22009-09-25 04:23:563090 net::LoadState load_state,
3091 uint64 upload_position,
3092 uint64 upload_size) {
[email protected]420ae012009-04-24 05:16:323093 load_state_ = load_state;
[email protected]094e5b22009-09-25 04:23:563094 upload_position_ = upload_position;
3095 upload_size_ = upload_size;
[email protected]d686e812009-06-03 19:10:293096 std::wstring languages =
[email protected]ddd231e2010-06-29 20:35:193097 UTF8ToWide(profile()->GetPrefs()->GetString(prefs::kAcceptLanguages));
[email protected]d686e812009-06-03 19:10:293098 std::string host = url.host();
[email protected]42197a22010-12-28 23:29:423099 load_state_host_ = WideToUTF16Hack(
3100 net::IDNToUnicode(host.c_str(), host.size(), languages, NULL));
[email protected]420ae012009-04-24 05:16:323101 if (load_state_ == net::LOAD_STATE_READING_RESPONSE)
3102 SetNotWaitingForResponse();
3103 if (is_loading())
[email protected]c9cd2222009-05-06 05:16:503104 NotifyNavigationStateChanged(INVALIDATE_LOAD | INVALIDATE_TAB);
[email protected]420ae012009-04-24 05:16:323105}
3106
[email protected]1ee614862009-07-06 20:29:183107bool TabContents::IsExternalTabContainer() const {
3108 if (!delegate())
3109 return false;
3110
3111 return delegate()->IsExternalTabContainer();
3112}
3113
[email protected]1ee614862009-07-06 20:29:183114void TabContents::DidInsertCSS() {
3115 // This RVHDelegate function is used for extensions and not us.
[email protected]420ae012009-04-24 05:16:323116}
3117
[email protected]546ae4e02010-12-08 14:57:193118void TabContents::FocusedNodeChanged(bool is_editable_node) {
[email protected]130efb02009-09-18 18:54:353119 NotificationService::current()->Notify(
3120 NotificationType::FOCUS_CHANGED_IN_PAGE,
3121 Source<RenderViewHost>(render_view_host()),
[email protected]546ae4e02010-12-08 14:57:193122 Details<const bool>(&is_editable_node));
[email protected]130efb02009-09-18 18:54:353123}
3124
[email protected]b75b8292010-10-01 07:28:253125void TabContents::UpdateZoomLimits(int minimum_percent,
3126 int maximum_percent,
3127 bool remember) {
3128 minimum_zoom_percent_ = minimum_percent;
3129 maximum_zoom_percent_ = maximum_percent;
3130 temporary_zoom_settings_ = !remember;
[email protected]ebf40a72010-07-22 01:46:383131}
3132
[email protected]9e823662010-10-13 23:36:003133void TabContents::UpdateContentRestrictions(int restrictions) {
3134 content_restrictions_ = restrictions;
3135 delegate()->ContentRestrictionsChanged(this);
[email protected]166e3042010-10-12 22:42:323136}
3137
[email protected]420ae012009-04-24 05:16:323138void TabContents::BeforeUnloadFiredFromRenderManager(
3139 bool proceed,
3140 bool* proceed_to_fire_unload) {
3141 if (delegate())
3142 delegate()->BeforeUnloadFired(this, proceed, proceed_to_fire_unload);
3143}
3144
[email protected]3a3d47472010-07-15 21:03:543145void TabContents::DidStartLoadingFromRenderManager(
3146 RenderViewHost* render_view_host) {
3147 DidStartLoading();
3148}
3149
3150void TabContents::RenderViewGoneFromRenderManager(
3151 RenderViewHost* render_view_host) {
[email protected]443b80e2010-12-14 00:42:233152 DCHECK(crashed_status_ != base::TERMINATION_STATUS_STILL_RUNNING);
3153 RenderViewGone(render_view_host, crashed_status_, crashed_error_code_);
[email protected]3a3d47472010-07-15 21:03:543154}
3155
[email protected]420ae012009-04-24 05:16:323156void TabContents::UpdateRenderViewSizeForRenderManager() {
3157 // TODO(brettw) this is a hack. See TabContentsView::SizeContents.
[email protected]0323ee42010-02-17 22:03:263158 gfx::Size size = view_->GetContainerSize();
3159 // 0x0 isn't a valid window size (minimal window size is 1x1) but it may be
3160 // here during container initialization and normal window size will be set
3161 // later. In case of tab duplication this resizing to 0x0 prevents setting
3162 // normal size later so just ignore it.
3163 if (!size.IsEmpty())
3164 view_->SizeContents(size);
[email protected]420ae012009-04-24 05:16:323165}
3166
[email protected]3a3d47472010-07-15 21:03:543167void TabContents::NotifySwappedFromRenderManager() {
3168 NotifySwapped();
3169}
3170
3171NavigationController& TabContents::GetControllerForRenderManager() {
3172 return controller();
3173}
3174
[email protected]420ae012009-04-24 05:16:323175DOMUI* TabContents::CreateDOMUIForRenderManager(const GURL& url) {
[email protected]d82ed61e2009-06-16 02:46:223176 return DOMUIFactory::CreateDOMUIForURL(this, url);
[email protected]420ae012009-04-24 05:16:323177}
3178
3179NavigationEntry*
3180TabContents::GetLastCommittedNavigationEntryForRenderManager() {
3181 return controller_.GetLastCommittedEntry();
3182}
3183
3184bool TabContents::CreateRenderViewForRenderManager(
3185 RenderViewHost* render_view_host) {
[email protected]420ae012009-04-24 05:16:323186 RenderWidgetHostView* rwh_view = view_->CreateViewForWidget(render_view_host);
[email protected]1a98a932009-11-17 00:12:523187
[email protected]674b3822010-08-04 04:02:513188 if (!render_view_host->CreateRenderView(string16()))
[email protected]420ae012009-04-24 05:16:323189 return false;
3190
3191 // Now that the RenderView has been created, we need to tell it its size.
3192 rwh_view->SetSize(view_->GetContainerSize());
3193
3194 UpdateMaxPageIDIfNecessary(render_view_host->site_instance(),
3195 render_view_host);
3196 return true;
3197}
3198
3199void TabContents::Observe(NotificationType type,
3200 const NotificationSource& source,
3201 const NotificationDetails& details) {
3202 switch (type.value) {
3203 case NotificationType::BOOKMARK_MODEL_LOADED:
3204 // BookmarkModel finished loading, fall through to update starred state.
3205 case NotificationType::URLS_STARRED: {
3206 // Somewhere, a URL has been starred.
3207 // Ignore notifications for profiles other than our current one.
3208 Profile* source_profile = Source<Profile>(source).ptr();
3209 if (!source_profile || !source_profile->IsSameProfile(profile()))
3210 return;
3211
3212 UpdateStarredStateForCurrentURL();
3213 break;
3214 }
3215 case NotificationType::PREF_CHANGED: {
[email protected]57ecc4b2010-08-11 03:02:513216 std::string* pref_name_in = Details<std::string>(details).ptr();
[email protected]420ae012009-04-24 05:16:323217 DCHECK(Source<PrefService>(source).ptr() == profile()->GetPrefs());
3218 if (*pref_name_in == prefs::kAlternateErrorPagesEnabled) {
3219 UpdateAlternateErrorPageURL();
[email protected]88e14382010-10-14 21:45:533220 } else if ((*pref_name_in == prefs::kDefaultCharset) ||
3221 StartsWithASCII(*pref_name_in, "webkit.webprefs.", true)) {
[email protected]420ae012009-04-24 05:16:323222 UpdateWebPreferences();
[email protected]d0b8d092010-10-25 04:05:173223 } else if (*pref_name_in == prefs::kDefaultZoomLevel) {
3224 UpdateZoomLevel();
[email protected]420ae012009-04-24 05:16:323225 } else {
3226 NOTREACHED() << "unexpected pref change notification" << *pref_name_in;
3227 }
3228 break;
3229 }
3230 case NotificationType::RENDER_WIDGET_HOST_DESTROYED:
3231 view_->RenderWidgetHostDestroyed(Source<RenderWidgetHost>(source).ptr());
3232 break;
3233
3234 case NotificationType::NAV_ENTRY_COMMITTED: {
3235 DCHECK(&controller_ == Source<NavigationController>(source).ptr());
3236
3237 NavigationController::LoadCommittedDetails& committed_details =
3238 *(Details<NavigationController::LoadCommittedDetails>(details).ptr());
3239 ExpireInfoBars(committed_details);
3240 break;
3241 }
3242
[email protected]d3ba77272009-09-03 00:06:093243#if defined(OS_LINUX)
3244 case NotificationType::BROWSER_THEME_CHANGED: {
[email protected]7ff431e2010-01-07 18:18:543245 renderer_preferences_util::UpdateFromSystemSettings(
3246 &renderer_preferences_, profile());
[email protected]d3ba77272009-09-03 00:06:093247 render_view_host()->SyncRendererPrefs();
3248 break;
3249 }
3250#endif
3251
[email protected]6c32ce72010-03-08 05:18:063252 case NotificationType::USER_STYLE_SHEET_UPDATED:
3253 UpdateWebPreferences();
3254 break;
3255
[email protected]79580c62010-02-02 02:36:253256 case NotificationType::CONTENT_SETTINGS_CHANGED: {
[email protected]5f24f212010-12-02 14:47:453257 Details<const ContentSettingsDetails> settings_details(details);
[email protected]79580c62010-02-02 02:36:253258 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]da0c8e92010-02-09 23:25:493259 GURL entry_url;
[email protected]0314ae02010-04-08 09:18:293260 if (entry)
[email protected]da0c8e92010-02-09 23:25:493261 entry_url = entry->url();
[email protected]0314ae02010-04-08 09:18:293262 if (settings_details.ptr()->update_all() ||
3263 settings_details.ptr()->pattern().Matches(entry_url)) {
3264 render_view_host()->SendContentSettings(entry_url,
[email protected]b9a4ea7f2010-06-25 07:11:233265 profile()->GetHostContentSettingsMap()->
3266 GetContentSettings(entry_url));
[email protected]79580c62010-02-02 02:36:253267 }
3268 break;
3269 }
3270
[email protected]e5adff72010-06-14 18:13:063271 case NotificationType::EXTENSION_LOADED:
[email protected]a888b29e62010-04-01 13:38:573272 break;
[email protected]a888b29e62010-04-01 13:38:573273
3274 case NotificationType::EXTENSION_UNLOADED:
[email protected]a888b29e62010-04-01 13:38:573275 break;
[email protected]a888b29e62010-04-01 13:38:573276
[email protected]88152052010-10-01 04:05:183277 case NotificationType::GOOGLE_URL_UPDATED:
3278 UpdateAlternateErrorPageURL();
3279 break;
3280
[email protected]420ae012009-04-24 05:16:323281 default:
3282 NOTREACHED();
3283 }
3284}
[email protected]aacd7cc2009-09-02 14:51:103285
[email protected]9adb9692010-10-29 23:14:023286void TabContents::UpdateExtensionAppIcon(const Extension* extension) {
[email protected]98aa0b52010-05-06 17:03:083287 extension_app_icon_.reset();
[email protected]f075fe0a22010-04-26 04:06:433288
[email protected]a888b29e62010-04-01 13:38:573289 if (extension) {
[email protected]98aa0b52010-05-06 17:03:083290 extension_app_image_loader_.reset(new ImageLoadingTracker(this));
3291 extension_app_image_loader_->LoadImage(
[email protected]a888b29e62010-04-01 13:38:573292 extension,
[email protected]807871f2010-09-16 01:04:483293 extension->GetIconResource(Extension::EXTENSION_ICON_SMALLISH,
3294 ExtensionIconSet::MATCH_EXACTLY),
[email protected]a888b29e62010-04-01 13:38:573295 gfx::Size(Extension::EXTENSION_ICON_SMALLISH,
3296 Extension::EXTENSION_ICON_SMALLISH),
3297 ImageLoadingTracker::CACHE);
3298 } else {
[email protected]98aa0b52010-05-06 17:03:083299 extension_app_image_loader_.reset(NULL);
[email protected]a888b29e62010-04-01 13:38:573300 }
3301}
3302
[email protected]9adb9692010-10-29 23:14:023303const Extension* TabContents::GetExtensionContaining(const GURL& url) {
[email protected]eaa7dd182010-12-14 11:09:003304 ExtensionService* extensions_service = profile()->GetExtensionService();
[email protected]a888b29e62010-04-01 13:38:573305 if (!extensions_service)
3306 return NULL;
3307
[email protected]9adb9692010-10-29 23:14:023308 const Extension* extension = extensions_service->GetExtensionByURL(url);
[email protected]a888b29e62010-04-01 13:38:573309 return extension ?
3310 extension : extensions_service->GetExtensionByWebExtent(url);
3311}
3312
[email protected]a888b29e62010-04-01 13:38:573313void TabContents::OnImageLoaded(SkBitmap* image, ExtensionResource resource,
3314 int index) {
3315 if (image) {
[email protected]98aa0b52010-05-06 17:03:083316 extension_app_icon_ = *image;
[email protected]a888b29e62010-04-01 13:38:573317 NotifyNavigationStateChanged(INVALIDATE_TAB);
3318 }
3319}
3320
[email protected]beb440c2009-11-06 04:08:543321gfx::NativeWindow TabContents::GetMessageBoxRootWindow() {
3322 return view_->GetTopLevelNativeWindow();
3323}
3324
3325void TabContents::OnMessageBoxClosed(IPC::Message* reply_msg,
3326 bool success,
3327 const std::wstring& prompt) {
3328 last_javascript_message_dismissal_ = base::TimeTicks::Now();
3329 if (is_showing_before_unload_dialog_ && !success) {
3330 // If a beforeunload dialog is canceled, we need to stop the throbber from
3331 // spinning, since we forced it to start spinning in Navigate.
3332 DidStopLoading();
3333
3334 tab_close_start_time_ = base::TimeTicks();
3335 }
3336 is_showing_before_unload_dialog_ = false;
3337 render_view_host()->JavaScriptMessageBoxClosed(reply_msg, success, prompt);
3338}
3339
3340void TabContents::SetSuppressMessageBoxes(bool suppress_message_boxes) {
3341 set_suppress_javascript_messages(suppress_message_boxes);
3342}
3343
[email protected]3a3d47472010-07-15 21:03:543344TabContents* TabContents::AsTabContents() {
3345 return this;
3346}
3347
3348ExtensionHost* TabContents::AsExtensionHost() {
3349 return NULL;
3350}
3351
[email protected]41fc0322009-09-04 22:23:403352void TabContents::set_encoding(const std::string& encoding) {
[email protected]aacd7cc2009-09-02 14:51:103353 encoding_ = CharacterEncoding::GetCanonicalEncodingNameByAliasName(encoding);
3354}
[email protected]f45d2a72010-03-08 23:28:353355
3356void TabContents::SetAppIcon(const SkBitmap& app_icon) {
3357 app_icon_ = app_icon;
3358 NotifyNavigationStateChanged(INVALIDATE_TITLE);
3359}
[email protected]33f74972010-12-08 16:40:363360
3361void TabContents::SwapInRenderViewHost(RenderViewHost* rvh) {
3362 render_manager_.SwapInRenderViewHost(rvh);
3363}
3364
3365void TabContents::CreateViewAndSetSizeForRVH(RenderViewHost* rvh) {
3366 RenderWidgetHostView* rwh_view = view()->CreateViewForWidget(rvh);
3367 rwh_view->SetSize(view()->GetContainerSize());
3368}
[email protected]e7d50892011-01-19 21:47:383369
3370bool TabContents::MaybeUsePreloadedPage(const GURL& url) {
3371 PrerenderManager* pm = profile()->GetPrerenderManager();
3372 if (pm != NULL) {
3373 if (pm->MaybeUsePreloadedPage(this, url)) {
3374 // TODO(tburkard): If the preloaded page has not finished preloading
3375 // yet, we should not do this.
3376 DidStopLoading();
3377 return true;
3378 }
3379 }
3380 return false;
3381}