blob: 73b1b9f917afec81d38342662dc5e0e5a1b343ef [file] [log] [blame]
[email protected]7e7414ae2010-01-26 20:19:291// Copyright (c) 2010 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]f3ec7742009-01-15 00:59:165#include "chrome/browser/tab_contents/tab_contents.h"
initial.commit09911bf2008-07-26 23:55:296
[email protected]06821f42d2010-05-03 22:34:357#if defined(OS_CHROMEOS)
8// For GdkScreen
9#include <gdk/gdk.h>
10#endif // defined(OS_CHROMEOS)
11
[email protected]a92b8642009-05-05 23:38:5612#include "app/l10n_util.h"
[email protected]9929da92009-05-05 02:05:1113#include "app/resource_bundle.h"
[email protected]614519812010-03-19 08:20:5614#include "app/text_elider.h"
[email protected]8cc8d492010-02-02 10:40:4915#include "base/auto_reset.h"
[email protected]420ae012009-04-24 05:16:3216#include "base/file_version_info.h"
[email protected]7cf1b6ce2010-03-20 06:37:0117#include "base/i18n/rtl.h"
[email protected]420ae012009-04-24 05:16:3218#include "base/process_util.h"
[email protected]96d185d2009-04-24 03:28:5419#include "base/string16.h"
[email protected]996fd702009-09-04 19:12:3720#include "base/string_util.h"
[email protected]96d185d2009-04-24 03:28:5421#include "base/time.h"
[email protected]6db0def92010-03-02 19:55:4522#include "chrome/browser/autocomplete_history_manager.h"
[email protected]21674df2009-10-28 19:51:2123#include "chrome/browser/autofill/autofill_manager.h"
[email protected]66ba4932009-06-04 19:22:1324#include "chrome/browser/blocked_popup_container.h"
[email protected]420ae012009-04-24 05:16:3225#include "chrome/browser/bookmarks/bookmark_model.h"
[email protected]281918912010-05-27 22:05:1326#include "chrome/browser/browser.h"
[email protected]96d185d2009-04-24 03:28:5427#include "chrome/browser/browser_process.h"
[email protected]cf68b092010-01-15 20:05:5528#include "chrome/browser/browser_shutdown.h"
initial.commit09911bf2008-07-26 23:55:2929#include "chrome/browser/cert_store.h"
[email protected]aacd7cc2009-09-02 14:51:1030#include "chrome/browser/character_encoding.h"
[email protected]96d185d2009-04-24 03:28:5431#include "chrome/browser/debugger/devtools_manager.h"
[email protected]420ae012009-04-24 05:16:3232#include "chrome/browser/dom_operation_notification_details.h"
[email protected]96d185d2009-04-24 03:28:5433#include "chrome/browser/dom_ui/dom_ui.h"
34#include "chrome/browser/dom_ui/dom_ui_factory.h"
[email protected]bcef0dc02009-02-28 00:35:0235#include "chrome/browser/download/download_item_model.h"
[email protected]420ae012009-04-24 05:16:3236#include "chrome/browser/download/download_manager.h"
[email protected]41291322010-07-15 17:09:0137#include "chrome/browser/download/download_request_limiter.h"
[email protected]e7eaedde2009-09-25 20:09:4938#include "chrome/browser/external_protocol_handler.h"
[email protected]92c6f9b92009-10-24 04:35:0839#include "chrome/browser/extensions/extensions_service.h"
[email protected]523623c2010-05-19 16:03:4240#include "chrome/browser/history/top_sites.h"
[email protected]0189bc722009-08-28 21:56:4841#include "chrome/browser/favicon_service.h"
[email protected]e5d2f7132010-04-09 18:13:5342#include "chrome/browser/find_bar_state.h"
[email protected]420ae012009-04-24 05:16:3243#include "chrome/browser/google_util.h"
[email protected]79580c62010-02-02 02:36:2544#include "chrome/browser/host_content_settings_map.h"
[email protected]8897c382009-05-06 17:53:2645#include "chrome/browser/hung_renderer_dialog.h"
[email protected]12f74a92010-02-05 22:32:1446#include "chrome/browser/message_box_handler.h"
[email protected]420ae012009-04-24 05:16:3247#include "chrome/browser/load_from_memory_cache_details.h"
48#include "chrome/browser/load_notification_details.h"
[email protected]84abba62009-10-07 17:01:4449#include "chrome/browser/metrics/metric_event_duration_details.h"
[email protected]ea0e80892009-08-22 00:36:0550#include "chrome/browser/modal_html_dialog_delegate.h"
[email protected]c8865482009-07-23 20:40:1051#include "chrome/browser/omnibox_search_hint.h"
[email protected]fdd61c62009-04-22 19:22:5752#include "chrome/browser/password_manager/password_manager.h"
[email protected]14a000d2010-04-29 21:44:2453#include "chrome/browser/platform_util.h"
[email protected]fdd61c62009-04-22 19:22:5754#include "chrome/browser/plugin_installer.h"
[email protected]8e0bbf12010-07-20 00:03:4355#include "chrome/browser/popup_blocked_animation.h"
[email protected]052313b2010-02-19 09:43:0856#include "chrome/browser/pref_service.h"
[email protected]12802702010-07-09 19:43:0957#include "chrome/browser/printing/print_view_manager.h"
[email protected]ce560f82009-06-03 09:39:4458#include "chrome/browser/profile.h"
[email protected]1db6ff152009-10-12 15:32:0759#include "chrome/browser/renderer_host/render_process_host.h"
[email protected]8cb5d5b2010-02-09 11:36:1660#include "chrome/browser/renderer_host/render_view_host.h"
[email protected]96d185d2009-04-24 03:28:5461#include "chrome/browser/renderer_host/render_widget_host_view.h"
[email protected]fd694982009-07-03 17:32:3762#include "chrome/browser/renderer_host/resource_request_details.h"
[email protected]1db6ff152009-10-12 15:32:0763#include "chrome/browser/renderer_host/site_instance.h"
[email protected]96d185d2009-04-24 03:28:5464#include "chrome/browser/renderer_host/web_cache_manager.h"
[email protected]93623c5d2009-12-10 21:40:3265#include "chrome/browser/renderer_preferences_util.h"
[email protected]152b8ba32010-03-29 19:40:1666#include "chrome/browser/search_engines/template_url_fetcher.h"
67#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]fdd61c62009-04-22 19:22:5774#include "chrome/browser/tab_contents/tab_contents_view.h"
[email protected]58dca552009-06-17 00:35:0275#include "chrome/browser/tab_contents/thumbnail_generator.h"
[email protected]152b8ba32010-03-29 19:40:1676#include "chrome/browser/translate/page_translated_details.h"
[email protected]54ec6472010-04-09 19:39:5877#include "chrome/common/bindings_policy.h"
[email protected]35f7d212009-04-29 21:19:2778#include "chrome/common/chrome_switches.h"
[email protected]3b355642010-02-05 16:01:4979#include "chrome/common/extensions/extension.h"
[email protected]5d246db22009-10-27 06:17:5780#include "chrome/common/extensions/extension_action.h"
[email protected]942690b132010-05-11 06:42:1481#include "chrome/common/extensions/extension_resource.h"
[email protected]12802702010-07-09 19:43:0982#include "chrome/common/extensions/url_pattern.h"
83#include "chrome/common/navigation_types.h"
84#include "chrome/common/net/url_request_context_getter.h"
[email protected]bfd04a62009-02-01 18:16:5685#include "chrome/common/notification_service.h"
initial.commit09911bf2008-07-26 23:55:2986#include "chrome/common/pref_names.h"
[email protected]420ae012009-04-24 05:16:3287#include "chrome/common/render_messages.h"
[email protected]1ee614862009-07-06 20:29:1888#include "chrome/common/renderer_preferences.h"
[email protected]96d185d2009-04-24 03:28:5489#include "chrome/common/url_constants.h"
[email protected]ea68c06a2010-04-23 17:25:3790#include "grit/chromium_strings.h"
[email protected]8897c382009-05-06 17:53:2691#include "grit/generated_resources.h"
[email protected]420ae012009-04-24 05:16:3292#include "grit/locale_settings.h"
[email protected]fd626782010-01-22 05:26:0693#include "grit/platform_locale_settings.h"
[email protected]ea68c06a2010-04-23 17:25:3794#include "grit/theme_resources.h"
[email protected]420ae012009-04-24 05:16:3295#include "net/base/mime_util.h"
96#include "net/base/net_errors.h"
[email protected]d686e812009-06-03 19:10:2997#include "net/base/net_util.h"
[email protected]420ae012009-04-24 05:16:3298#include "net/base/registry_controlled_domain.h"
[email protected]1ee614862009-07-06 20:29:1899#include "webkit/glue/webpreferences.h"
[email protected]d5e311b2010-07-20 17:15:03100#include "webkit/glue/password_form.h"
[email protected]3e45ba92009-02-20 21:09:00101
[email protected]420ae012009-04-24 05:16:32102// Cross-Site Navigations
103//
104// If a TabContents is told to navigate to a different web site (as determined
105// by SiteInstance), it will replace its current RenderViewHost with a new
106// RenderViewHost dedicated to the new SiteInstance. This works as follows:
107//
108// - Navigate determines whether the destination is cross-site, and if so,
109// it creates a pending_render_view_host_ and moves into the PENDING
110// RendererState.
111// - The pending RVH is "suspended," so that no navigation messages are sent to
112// its renderer until the onbeforeunload JavaScript handler has a chance to
113// run in the current RVH.
114// - The pending RVH tells CrossSiteRequestManager (a thread-safe singleton)
115// that it has a pending cross-site request. ResourceDispatcherHost will
116// check for this when the response arrives.
117// - The current RVH runs its onbeforeunload handler. If it returns false, we
118// cancel all the pending logic and go back to NORMAL. Otherwise we allow
119// the pending RVH to send the navigation request to its renderer.
120// - ResourceDispatcherHost receives a ResourceRequest on the IO thread. It
121// checks CrossSiteRequestManager to see that the RVH responsible has a
122// pending cross-site request, and then installs a CrossSiteEventHandler.
123// - When RDH receives a response, the BufferedEventHandler determines whether
124// it is a download. If so, it sends a message to the new renderer causing
125// it to cancel the request, and the download proceeds in the download
126// thread. For now, we stay in a PENDING state (with a pending RVH) until
[email protected]57c6a652009-05-04 07:58:34127// the next DidNavigate event for this TabContents. This isn't ideal, but it
[email protected]420ae012009-04-24 05:16:32128// doesn't affect any functionality.
129// - After RDH receives a response and determines that it is safe and not a
130// download, it pauses the response to first run the old page's onunload
131// handler. It does this by asynchronously calling the OnCrossSiteResponse
[email protected]57c6a652009-05-04 07:58:34132// method of TabContents on the UI thread, which sends a ClosePage message
[email protected]420ae012009-04-24 05:16:32133// to the current RVH.
134// - Once the onunload handler is finished, a ClosePage_ACK message is sent to
135// the ResourceDispatcherHost, who unpauses the response. Data is then sent
136// to the pending RVH.
137// - The pending renderer sends a FrameNavigate message that invokes the
138// DidNavigate method. This replaces the current RVH with the
139// pending RVH and goes back to the NORMAL RendererState.
140
141namespace {
142
143// Amount of time we wait between when a key event is received and the renderer
144// is queried for its state and pushed to the NavigationEntry.
145const int kQueryStateDelay = 5000;
146
147const int kSyncWaitDelay = 40;
148
[email protected]304f8792009-08-19 21:28:09149#if defined(OS_CHROMEOS)
150// If a popup window is bigger than this fraction of the screen on chrome os,
151// turn it into a tab
152const float kMaxWidthFactor = 0.5;
153const float kMaxHeightFactor = 0.6;
154#endif
155
[email protected]420ae012009-04-24 05:16:32156// If another javascript message box is displayed within
157// kJavascriptMessageExpectedDelay of a previous javascript message box being
158// dismissed, display an option to suppress future message boxes from this
159// contents.
160const int kJavascriptMessageExpectedDelay = 1000;
161
[email protected]420ae012009-04-24 05:16:32162// The list of prefs we want to observe.
163const wchar_t* kPrefsToObserve[] = {
164 prefs::kAlternateErrorPagesEnabled,
165 prefs::kWebKitJavaEnabled,
166 prefs::kWebKitJavascriptEnabled,
167 prefs::kWebKitLoadsImagesAutomatically,
168 prefs::kWebKitPluginsEnabled,
169 prefs::kWebKitUsesUniversalDetector,
170 prefs::kWebKitSerifFontFamily,
171 prefs::kWebKitSansSerifFontFamily,
172 prefs::kWebKitFixedFontFamily,
173 prefs::kWebKitDefaultFontSize,
174 prefs::kWebKitDefaultFixedFontSize,
[email protected]1661a3c02010-05-20 16:32:36175 prefs::kWebkitTabsToLinks,
[email protected]420ae012009-04-24 05:16:32176 prefs::kDefaultCharset
177 // kWebKitStandardFontIsSerif needs to be added
178 // if we let users pick which font to use, serif or sans-serif when
179 // no font is specified or a CSS generic family (serif or sans-serif)
180 // is not specified.
181};
182
183const int kPrefsToObserveLength = arraysize(kPrefsToObserve);
184
[email protected]420ae012009-04-24 05:16:32185// Returns true if the entry's transition type is FORM_SUBMIT.
186bool IsFormSubmit(const NavigationEntry* entry) {
187 return (PageTransition::StripQualifier(entry->transition_type()) ==
188 PageTransition::FORM_SUBMIT);
189}
190
191#if defined(OS_WIN)
192
193BOOL CALLBACK InvalidateWindow(HWND hwnd, LPARAM lparam) {
194 // Note: erase is required to properly paint some widgets borders. This can
195 // be seen with textfields.
196 InvalidateRect(hwnd, NULL, TRUE);
197 return TRUE;
198}
199#endif
200
[email protected]5e369672009-11-03 23:48:30201ViewMsg_Navigate_Params::NavigationType GetNavigationType(
[email protected]1ccb3568d2010-02-19 10:51:16202 Profile* profile, const NavigationEntry& entry,
203 NavigationController::ReloadType reload_type) {
204 switch (reload_type) {
205 case NavigationController::RELOAD:
206 return ViewMsg_Navigate_Params::RELOAD;
207 case NavigationController::RELOAD_IGNORING_CACHE:
208 return ViewMsg_Navigate_Params::RELOAD_IGNORING_CACHE;
209 case NavigationController::NO_RELOAD:
210 break; // Fall through to rest of function.
211 }
[email protected]5e369672009-11-03 23:48:30212
213 if (entry.restore_type() == NavigationEntry::RESTORE_LAST_SESSION &&
214 profile->DidLastSessionExitCleanly())
215 return ViewMsg_Navigate_Params::RESTORE;
216
217 return ViewMsg_Navigate_Params::NORMAL;
218}
219
[email protected]3cc72b12010-03-18 23:03:00220void MakeNavigateParams(const NavigationController& controller,
[email protected]1ccb3568d2010-02-19 10:51:16221 NavigationController::ReloadType reload_type,
222 ViewMsg_Navigate_Params* params) {
[email protected]3cc72b12010-03-18 23:03:00223 const NavigationEntry& entry = *controller.pending_entry();
[email protected]056de2d2009-06-26 16:41:34224 params->page_id = entry.page_id();
[email protected]3cc72b12010-03-18 23:03:00225 params->pending_history_list_offset = controller.pending_entry_index();
226 params->current_history_list_offset = controller.last_committed_entry_index();
227 params->current_history_list_length = controller.entry_count();
[email protected]056de2d2009-06-26 16:41:34228 params->url = entry.url();
229 params->referrer = entry.referrer();
230 params->transition = entry.transition_type();
231 params->state = entry.content_state();
[email protected]3cc72b12010-03-18 23:03:00232 params->navigation_type =
233 GetNavigationType(controller.profile(), entry, reload_type);
[email protected]056de2d2009-06-26 16:41:34234 params->request_time = base::Time::Now();
235}
236
[email protected]420ae012009-04-24 05:16:32237} // namespace
238
239// -----------------------------------------------------------------------------
240
[email protected]fdd61c62009-04-22 19:22:57241// static
242int TabContents::find_request_id_counter_ = -1;
243
[email protected]420ae012009-04-24 05:16:32244
[email protected]420ae012009-04-24 05:16:32245TabContents::TabContents(Profile* profile,
246 SiteInstance* site_instance,
247 int routing_id,
[email protected]34ac70502009-09-25 17:07:23248 const TabContents* base_tab_contents)
[email protected]b680ad22009-04-15 23:19:42249 : delegate_(NULL),
[email protected]66ba4932009-06-04 19:22:13250 ALLOW_THIS_IN_INITIALIZER_LIST(controller_(this, profile)),
251 ALLOW_THIS_IN_INITIALIZER_LIST(view_(
[email protected]d82ed61e2009-06-16 02:46:22252 TabContentsView::Create(this))),
253 ALLOW_THIS_IN_INITIALIZER_LIST(render_manager_(this, this)),
[email protected]fdd61c62009-04-22 19:22:57254 property_bag_(),
255 registrar_(),
[email protected]12802702010-07-09 19:43:09256 ALLOW_THIS_IN_INITIALIZER_LIST(printing_(
257 new printing::PrintViewManager(*this))),
[email protected]fdd61c62009-04-22 19:22:57258 save_package_(),
[email protected]6db0def92010-03-02 19:55:45259 autocomplete_history_manager_(),
[email protected]21674df2009-10-28 19:51:21260 autofill_manager_(),
[email protected]fdd61c62009-04-22 19:22:57261 password_manager_(),
262 plugin_installer_(),
[email protected]6f821dd2010-02-23 00:54:06263 bookmark_drag_(NULL),
[email protected]d82ed61e2009-06-16 02:46:22264 ALLOW_THIS_IN_INITIALIZER_LIST(fav_icon_helper_(this)),
[email protected]fdd61c62009-04-22 19:22:57265 select_file_dialog_(),
[email protected]d5f942ba2008-09-26 19:30:34266 is_loading_(false),
initial.commit09911bf2008-07-26 23:55:29267 is_crashed_(false),
[email protected]d5f942ba2008-09-26 19:30:34268 waiting_for_response_(false),
[email protected]d5f942ba2008-09-26 19:30:34269 max_page_id_(-1),
[email protected]fdd61c62009-04-22 19:22:57270 current_load_start_(),
271 load_state_(net::LOAD_STATE_IDLE),
272 load_state_host_(),
[email protected]094e5b22009-09-25 04:23:56273 upload_size_(0),
274 upload_position_(0),
[email protected]fdd61c62009-04-22 19:22:57275 received_page_title_(false),
276 is_starred_(false),
277 contents_mime_type_(),
278 encoding_(),
[email protected]332af7732009-03-11 13:21:35279 blocked_popups_(NULL),
[email protected]8cc8d492010-02-02 10:40:49280 dont_notify_render_view_(false),
[email protected]f17a0ee2010-05-17 17:38:47281 displayed_insecure_content_(false),
[email protected]fdd61c62009-04-22 19:22:57282 infobar_delegates_(),
[email protected]fdd61c62009-04-22 19:22:57283 find_ui_active_(false),
284 find_op_aborted_(false),
285 current_find_request_id_(find_request_id_counter_++),
[email protected]e491f1c2009-05-22 20:28:12286 last_search_case_sensitive_(false),
[email protected]e491f1c2009-05-22 20:28:12287 last_search_result_(),
[email protected]98aa0b52010-05-06 17:03:08288 extension_app_(NULL),
[email protected]fdd61c62009-04-22 19:22:57289 capturing_contents_(false),
290 is_being_destroyed_(false),
291 notify_disconnection_(false),
292 history_requests_(),
293#if defined(OS_WIN)
294 message_box_active_(CreateEvent(NULL, TRUE, FALSE, NULL)),
295#endif
296 last_javascript_message_dismissal_(),
[email protected]c2e74fe82009-09-03 17:57:44297 suppress_javascript_messages_(false),
[email protected]7ab1e7d62009-10-14 23:32:01298 is_showing_before_unload_dialog_(false),
[email protected]7ff431e2010-01-07 18:18:54299 renderer_preferences_(),
[email protected]1a242c32010-02-02 21:20:54300 opener_dom_ui_type_(DOMUIFactory::kNoDOMUI),
[email protected]777a7b62010-05-11 13:28:16301 language_state_(&controller_),
[email protected]0cce15f2010-05-21 16:58:39302 closed_by_user_gesture_(false) {
[email protected]7ff431e2010-01-07 18:18:54303 renderer_preferences_util::UpdateFromSystemSettings(
304 &renderer_preferences_, profile);
305
[email protected]9fb83e82010-07-02 18:24:55306 content_settings_delegate_.reset(
307 new TabSpecificContentSettings(this, profile));
308
[email protected]16779842009-07-08 23:45:29309#if defined(OS_CHROMEOS)
[email protected]58dca552009-06-17 00:35:02310 // Make sure the thumbnailer is started before starting the render manager.
311 // The thumbnailer will want to listen for RVH creations, one of which will
312 // happen in RVHManager::Init.
313 ThumbnailGenerator* generator = g_browser_process->GetThumbnailGenerator();
314 if (generator)
315 generator->StartThumbnailing();
316#endif
317
[email protected]12636df2009-09-28 22:32:21318 render_manager_.Init(profile, site_instance, routing_id);
[email protected]420ae012009-04-24 05:16:32319
[email protected]34ac70502009-09-25 17:07:23320 // We have the initial size of the view be based on the size of the passed in
321 // tab contents (normally a tab from the same window).
322 view_->CreateView(base_tab_contents ?
323 base_tab_contents->view()->GetContainerSize() : gfx::Size());
[email protected]420ae012009-04-24 05:16:32324
325 // Register for notifications about all interested prefs change.
326 PrefService* prefs = profile->GetPrefs();
327 if (prefs) {
328 for (int i = 0; i < kPrefsToObserveLength; ++i)
329 prefs->AddPrefObserver(kPrefsToObserve[i], this);
330 }
331
332 // Register for notifications about URL starredness changing on any profile.
333 registrar_.Add(this, NotificationType::URLS_STARRED,
334 NotificationService::AllSources());
335 registrar_.Add(this, NotificationType::BOOKMARK_MODEL_LOADED,
336 NotificationService::AllSources());
337 registrar_.Add(this, NotificationType::RENDER_WIDGET_HOST_DESTROYED,
338 NotificationService::AllSources());
[email protected]d3ba77272009-09-03 00:06:09339#if defined(OS_LINUX)
340 registrar_.Add(this, NotificationType::BROWSER_THEME_CHANGED,
341 NotificationService::AllSources());
342#endif
[email protected]420ae012009-04-24 05:16:32343
[email protected]6c32ce72010-03-08 05:18:06344 registrar_.Add(this, NotificationType::USER_STYLE_SHEET_UPDATED,
345 NotificationService::AllSources());
346
[email protected]79580c62010-02-02 02:36:25347 // Register for notifications about content setting changes.
348 registrar_.Add(this, NotificationType::CONTENT_SETTINGS_CHANGED,
349 NotificationService::AllSources());
350
[email protected]a888b29e62010-04-01 13:38:57351 // Listen for extension changes so we can update extension_for_current_page_.
352 registrar_.Add(this, NotificationType::EXTENSION_LOADED,
353 NotificationService::AllSources());
354 registrar_.Add(this, NotificationType::EXTENSION_UNLOADED,
355 NotificationService::AllSources());
356 registrar_.Add(this, NotificationType::EXTENSION_UNLOADED_DISABLED,
357 NotificationService::AllSources());
358
[email protected]c8865482009-07-23 20:40:10359 // Set-up the showing of the omnibox search infobar if applicable.
360 if (OmniboxSearchHint::IsEnabled(profile))
361 omnibox_search_hint_.reset(new OmniboxSearchHint(this));
[email protected]332af7732009-03-11 13:21:35362}
initial.commit09911bf2008-07-26 23:55:29363
364TabContents::~TabContents() {
[email protected]420ae012009-04-24 05:16:32365 is_being_destroyed_ = true;
366
[email protected]bfe4c1582009-10-02 18:11:09367 // We don't want any notifications while we're running our destructor.
[email protected]420ae012009-04-24 05:16:32368 registrar_.RemoveAll();
369
370 // Unregister the notifications of all observed prefs change.
371 PrefService* prefs = profile()->GetPrefs();
372 if (prefs) {
373 for (int i = 0; i < kPrefsToObserveLength; ++i)
374 prefs->RemovePrefObserver(kPrefsToObserve[i], this);
375 }
376
[email protected]420ae012009-04-24 05:16:32377 NotifyDisconnected();
[email protected]e7b8d832009-11-20 22:41:00378 hung_renderer_dialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:32379
[email protected]420ae012009-04-24 05:16:32380 // First cleanly close all child windows.
381 // TODO(mpcomplete): handle case if MaybeCloseChildWindows() already asked
382 // some of these to close. CloseWindows is async, so it might get called
383 // twice before it runs.
[email protected]8cc8d492010-02-02 10:40:49384 CloseConstrainedWindows();
[email protected]420ae012009-04-24 05:16:32385
[email protected]8cc8d492010-02-02 10:40:49386 // Close all blocked popups.
[email protected]66ba4932009-06-04 19:22:13387 if (blocked_popups_)
388 blocked_popups_->Destroy();
389
[email protected]420ae012009-04-24 05:16:32390 // Notify any observer that have a reference on this tab contents.
391 NotificationService::current()->Notify(
392 NotificationType::TAB_CONTENTS_DESTROYED,
393 Source<TabContents>(this),
394 NotificationService::NoDetails());
395
[email protected]1e5e74f2009-05-27 20:55:12396 // Notify any lasting InfobarDelegates that have not yet been removed that
397 // whatever infobar they were handling in this TabContents has closed,
398 // because the TabContents is going away entirely.
399 // This must happen after the TAB_CONTENTS_DESTROYED notification as the
400 // notification may trigger infobars calls that access their delegate. (and
401 // some implementations of InfoBarDelegate do delete themselves on
402 // InfoBarClosed()).
403 for (int i = 0; i < infobar_delegate_count(); ++i) {
404 InfoBarDelegate* delegate = GetInfoBarDelegateAt(i);
405 delegate->InfoBarClosed();
406 }
407 infobar_delegates_.clear();
408
[email protected]420ae012009-04-24 05:16:32409 // TODO(brettw) this should be moved to the view.
410#if defined(OS_WIN)
411 // If we still have a window handle, destroy it. GetNativeView can return
412 // NULL if this contents was part of a window that closed.
413 if (GetNativeView())
414 ::DestroyWindow(GetNativeView());
415#endif
[email protected]7ab1e7d62009-10-14 23:32:01416
417 // OnCloseStarted isn't called in unit tests.
418 if (!tab_close_start_time_.is_null()) {
419 UMA_HISTOGRAM_TIMES("Tab.Close",
420 base::TimeTicks::Now() - tab_close_start_time_);
421 }
initial.commit09911bf2008-07-26 23:55:29422}
423
[email protected]d5f942ba2008-09-26 19:30:34424// static
425void TabContents::RegisterUserPrefs(PrefService* prefs) {
[email protected]420ae012009-04-24 05:16:32426 prefs->RegisterBooleanPref(prefs::kAlternateErrorPagesEnabled, true);
427
428 WebPreferences pref_defaults;
429 prefs->RegisterBooleanPref(prefs::kWebKitJavascriptEnabled,
430 pref_defaults.javascript_enabled);
431 prefs->RegisterBooleanPref(prefs::kWebKitWebSecurityEnabled,
432 pref_defaults.web_security_enabled);
433 prefs->RegisterBooleanPref(
434 prefs::kWebKitJavascriptCanOpenWindowsAutomatically, true);
435 prefs->RegisterBooleanPref(prefs::kWebKitLoadsImagesAutomatically,
436 pref_defaults.loads_images_automatically);
437 prefs->RegisterBooleanPref(prefs::kWebKitPluginsEnabled,
438 pref_defaults.plugins_enabled);
439 prefs->RegisterBooleanPref(prefs::kWebKitDomPasteEnabled,
440 pref_defaults.dom_paste_enabled);
441 prefs->RegisterBooleanPref(prefs::kWebKitShrinksStandaloneImagesToFit,
442 pref_defaults.shrinks_standalone_images_to_fit);
[email protected]c4e98902010-06-01 10:20:14443 prefs->RegisterDictionaryPref(prefs::kWebKitInspectorSettings);
[email protected]420ae012009-04-24 05:16:32444 prefs->RegisterBooleanPref(prefs::kWebKitTextAreasAreResizable,
445 pref_defaults.text_areas_are_resizable);
446 prefs->RegisterBooleanPref(prefs::kWebKitJavaEnabled,
447 pref_defaults.java_enabled);
[email protected]1661a3c02010-05-20 16:32:36448 prefs->RegisterBooleanPref(prefs::kWebkitTabsToLinks,
449 pref_defaults.tabs_to_links);
[email protected]420ae012009-04-24 05:16:32450
451 prefs->RegisterLocalizedStringPref(prefs::kAcceptLanguages,
452 IDS_ACCEPT_LANGUAGES);
453 prefs->RegisterLocalizedStringPref(prefs::kDefaultCharset,
454 IDS_DEFAULT_ENCODING);
455 prefs->RegisterLocalizedBooleanPref(prefs::kWebKitStandardFontIsSerif,
456 IDS_STANDARD_FONT_IS_SERIF);
457 prefs->RegisterLocalizedStringPref(prefs::kWebKitFixedFontFamily,
458 IDS_FIXED_FONT_FAMILY);
459 prefs->RegisterLocalizedStringPref(prefs::kWebKitSerifFontFamily,
460 IDS_SERIF_FONT_FAMILY);
461 prefs->RegisterLocalizedStringPref(prefs::kWebKitSansSerifFontFamily,
462 IDS_SANS_SERIF_FONT_FAMILY);
463 prefs->RegisterLocalizedStringPref(prefs::kWebKitCursiveFontFamily,
464 IDS_CURSIVE_FONT_FAMILY);
465 prefs->RegisterLocalizedStringPref(prefs::kWebKitFantasyFontFamily,
466 IDS_FANTASY_FONT_FAMILY);
467 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitDefaultFontSize,
468 IDS_DEFAULT_FONT_SIZE);
469 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitDefaultFixedFontSize,
470 IDS_DEFAULT_FIXED_FONT_SIZE);
471 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitMinimumFontSize,
472 IDS_MINIMUM_FONT_SIZE);
473 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitMinimumLogicalFontSize,
474 IDS_MINIMUM_LOGICAL_FONT_SIZE);
475 prefs->RegisterLocalizedBooleanPref(prefs::kWebKitUsesUniversalDetector,
476 IDS_USES_UNIVERSAL_DETECTOR);
477 prefs->RegisterLocalizedStringPref(prefs::kStaticEncodings,
478 IDS_STATIC_ENCODING_LIST);
initial.commit09911bf2008-07-26 23:55:29479}
480
[email protected]13367f72009-07-08 16:39:02481// Returns true if contains content rendered by an extension.
482bool TabContents::HostsExtension() const {
483 return GetURL().SchemeIs(chrome::kExtensionScheme);
[email protected]d5f942ba2008-09-26 19:30:34484}
485
[email protected]cf28ec52010-04-09 22:56:34486AutoFillManager* TabContents::GetAutoFillManager() {
487 if (autofill_manager_.get() == NULL)
488 autofill_manager_.reset(new AutoFillManager(this));
489 return autofill_manager_.get();
490}
491
[email protected]6e95706d2009-04-23 22:55:01492PasswordManager* TabContents::GetPasswordManager() {
493 if (password_manager_.get() == NULL)
494 password_manager_.reset(new PasswordManager(this));
495 return password_manager_.get();
496}
497
498PluginInstaller* TabContents::GetPluginInstaller() {
499 if (plugin_installer_.get() == NULL)
500 plugin_installer_.reset(new PluginInstaller(this));
501 return plugin_installer_.get();
502}
503
[email protected]8cb5d5b2010-02-09 11:36:16504RenderProcessHost* TabContents::GetRenderProcessHost() const {
505 return render_manager_.current_host()->process();
506}
507
[email protected]98aa0b52010-05-06 17:03:08508void TabContents::SetExtensionApp(Extension* extension) {
[email protected]867a73e12010-03-19 20:45:46509 DCHECK(!extension || extension->GetFullLaunchURL().is_valid());
[email protected]98aa0b52010-05-06 17:03:08510 extension_app_ = extension;
[email protected]fca656c2010-02-10 20:30:10511
[email protected]98aa0b52010-05-06 17:03:08512 UpdateExtensionAppIcon(extension_app_);
[email protected]a888b29e62010-04-01 13:38:57513
[email protected]fca656c2010-02-10 20:30:10514 NotificationService::current()->Notify(
515 NotificationType::TAB_CONTENTS_APPLICATION_EXTENSION_CHANGED,
516 Source<TabContents>(this),
517 NotificationService::NoDetails());
[email protected]3b355642010-02-05 16:01:49518}
519
[email protected]98aa0b52010-05-06 17:03:08520void TabContents::SetExtensionAppById(const std::string& extension_app_id) {
521 if (extension_app_id.empty())
[email protected]911f8152010-03-18 16:46:40522 return;
523
524 ExtensionsService* extension_service = profile()->GetExtensionsService();
525 if (extension_service && extension_service->is_ready()) {
526 Extension* extension =
[email protected]98aa0b52010-05-06 17:03:08527 extension_service->GetExtensionById(extension_app_id, false);
[email protected]911f8152010-03-18 16:46:40528 if (extension)
[email protected]98aa0b52010-05-06 17:03:08529 SetExtensionApp(extension);
[email protected]911f8152010-03-18 16:46:40530 }
531}
532
[email protected]98aa0b52010-05-06 17:03:08533SkBitmap* TabContents::GetExtensionAppIcon() {
[email protected]8eb043b2010-04-30 07:19:51534 // We don't show the big icons in tabs for TYPE_EXTENSION_APP windows because
535 // for those windows, we already have a big icon in the top-left outside any
536 // tab. Having big tab icons too looks kinda redonk.
[email protected]06821f42d2010-05-03 22:34:35537 Browser* browser = delegate_ ? delegate_->GetBrowser() : NULL;
538 if (browser && browser->type() == Browser::TYPE_EXTENSION_APP)
[email protected]8eb043b2010-04-30 07:19:51539 return NULL;
[email protected]8eb043b2010-04-30 07:19:51540
[email protected]98aa0b52010-05-06 17:03:08541 if (extension_app_icon_.empty())
[email protected]8eb043b2010-04-30 07:19:51542 return NULL;
543
[email protected]98aa0b52010-05-06 17:03:08544 return &extension_app_icon_;
[email protected]8eb043b2010-04-30 07:19:51545}
546
[email protected]d5f942ba2008-09-26 19:30:34547const GURL& TabContents::GetURL() const {
548 // We may not have a navigation entry yet
[email protected]ce3fa3c2009-04-20 19:55:57549 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]ebe89e062009-08-13 23:16:54550 return entry ? entry->virtual_url() : GURL::EmptyGURL();
[email protected]d5f942ba2008-09-26 19:30:34551}
552
[email protected]96d185d2009-04-24 03:28:54553const string16& TabContents::GetTitle() const {
[email protected]4c6092c5b2009-06-06 00:23:55554 // Transient entries take precedence. They are used for interstitial pages
555 // that are shown on top of existing pages.
556 NavigationEntry* entry = controller_.GetTransientEntry();
557 if (entry)
558 return entry->GetTitleForDisplay(&controller_);
559
[email protected]96d185d2009-04-24 03:28:54560 DOMUI* our_dom_ui = render_manager_.pending_dom_ui() ?
561 render_manager_.pending_dom_ui() : render_manager_.dom_ui();
562 if (our_dom_ui) {
563 // Don't override the title in view source mode.
[email protected]4c6092c5b2009-06-06 00:23:55564 entry = controller_.GetActiveEntry();
[email protected]96d185d2009-04-24 03:28:54565 if (!(entry && entry->IsViewSourceMode())) {
566 // Give the DOM UI the chance to override our title.
567 const string16& title = our_dom_ui->overridden_title();
568 if (!title.empty())
569 return title;
570 }
571 }
572
573 // We use the title for the last committed entry rather than a pending
574 // navigation entry. For example, when the user types in a URL, we want to
575 // keep the old page's title until the new load has committed and we get a new
576 // title.
[email protected]96d185d2009-04-24 03:28:54577 entry = controller_.GetLastCommittedEntry();
578 if (entry)
579 return entry->GetTitleForDisplay(&controller_);
[email protected]96d185d2009-04-24 03:28:54580 return EmptyString16();
581}
582
[email protected]bbbc1ef2010-02-12 18:03:41583// static
584string16 TabContents::GetDefaultTitle() {
585 return l10n_util::GetStringUTF16(IDS_DEFAULT_TAB_TITLE);
586}
587
[email protected]d5f942ba2008-09-26 19:30:34588int32 TabContents::GetMaxPageID() {
589 if (GetSiteInstance())
590 return GetSiteInstance()->max_page_id();
591 else
592 return max_page_id_;
593}
594
595void TabContents::UpdateMaxPageID(int32 page_id) {
596 // Ensure both the SiteInstance and RenderProcessHost update their max page
[email protected]57c6a652009-05-04 07:58:34597 // IDs in sync. Only TabContents will also have site instances, except during
[email protected]d5f942ba2008-09-26 19:30:34598 // testing.
599 if (GetSiteInstance())
600 GetSiteInstance()->UpdateMaxPageID(page_id);
[email protected]8cb5d5b2010-02-09 11:36:16601 GetRenderProcessHost()->UpdateMaxPageID(page_id);
[email protected]d5f942ba2008-09-26 19:30:34602}
603
[email protected]96d185d2009-04-24 03:28:54604SiteInstance* TabContents::GetSiteInstance() const {
605 return render_manager_.current_host()->site_instance();
606}
607
[email protected]96d185d2009-04-24 03:28:54608bool TabContents::ShouldDisplayURL() {
[email protected]8ab24cc2009-11-13 20:56:15609 // Don't hide the url in view source mode and with interstitials.
[email protected]96d185d2009-04-24 03:28:54610 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]8ab24cc2009-11-13 20:56:15611 if (entry && (entry->IsViewSourceMode() ||
612 entry->page_type() == NavigationEntry::INTERSTITIAL_PAGE)) {
[email protected]96d185d2009-04-24 03:28:54613 return true;
[email protected]8ab24cc2009-11-13 20:56:15614 }
[email protected]83e3895b2009-06-11 00:07:16615
[email protected]96d185d2009-04-24 03:28:54616 DOMUI* dom_ui = GetDOMUIForCurrentState();
617 if (dom_ui)
618 return !dom_ui->should_hide_url();
619 return true;
620}
621
[email protected]d5f942ba2008-09-26 19:30:34622SkBitmap TabContents::GetFavIcon() const {
623 // Like GetTitle(), we also want to use the favicon for the last committed
624 // entry rather than a pending navigation entry.
[email protected]ce3fa3c2009-04-20 19:55:57625 NavigationEntry* entry = controller_.GetTransientEntry();
[email protected]cbab76d2008-10-13 22:42:47626 if (entry)
627 return entry->favicon().bitmap();
628
[email protected]ce3fa3c2009-04-20 19:55:57629 entry = controller_.GetLastCommittedEntry();
[email protected]d5f942ba2008-09-26 19:30:34630 if (entry)
631 return entry->favicon().bitmap();
[email protected]d5f942ba2008-09-26 19:30:34632 return SkBitmap();
633}
634
[email protected]0b303cc2009-09-28 22:35:15635bool TabContents::FavIconIsValid() const {
636 NavigationEntry* entry = controller_.GetTransientEntry();
637 if (entry)
638 return entry->favicon().is_valid();
639
640 entry = controller_.GetLastCommittedEntry();
641 if (entry)
642 return entry->favicon().is_valid();
643
644 return false;
645}
646
[email protected]96d185d2009-04-24 03:28:54647bool TabContents::ShouldDisplayFavIcon() {
648 // Always display a throbber during pending loads.
649 if (controller_.GetLastCommittedEntry() && controller_.pending_entry())
650 return true;
651
652 DOMUI* dom_ui = GetDOMUIForCurrentState();
653 if (dom_ui)
654 return !dom_ui->hide_favicon();
655 return true;
656}
657
[email protected]96d185d2009-04-24 03:28:54658std::wstring TabContents::GetStatusText() const {
659 if (!is_loading() || load_state_ == net::LOAD_STATE_IDLE)
660 return std::wstring();
661
662 switch (load_state_) {
663 case net::LOAD_STATE_WAITING_FOR_CACHE:
664 return l10n_util::GetString(IDS_LOAD_STATE_WAITING_FOR_CACHE);
[email protected]8a1f3312010-05-25 19:25:04665 case net::LOAD_STATE_ESTABLISHING_PROXY_TUNNEL:
666 return l10n_util::GetString(IDS_LOAD_STATE_ESTABLISHING_PROXY_TUNNEL);
[email protected]96d185d2009-04-24 03:28:54667 case net::LOAD_STATE_RESOLVING_PROXY_FOR_URL:
668 return l10n_util::GetString(IDS_LOAD_STATE_RESOLVING_PROXY_FOR_URL);
669 case net::LOAD_STATE_RESOLVING_HOST:
670 return l10n_util::GetString(IDS_LOAD_STATE_RESOLVING_HOST);
671 case net::LOAD_STATE_CONNECTING:
672 return l10n_util::GetString(IDS_LOAD_STATE_CONNECTING);
[email protected]375e3112010-05-27 20:32:06673 case net::LOAD_STATE_SSL_HANDSHAKE:
674 return l10n_util::GetString(IDS_LOAD_STATE_SSL_HANDSHAKE);
[email protected]96d185d2009-04-24 03:28:54675 case net::LOAD_STATE_SENDING_REQUEST:
[email protected]094e5b22009-09-25 04:23:56676 if (upload_size_)
677 return l10n_util::GetStringF(
678 IDS_LOAD_STATE_SENDING_REQUEST_WITH_PROGRESS,
679 static_cast<int>((100 * upload_position_) / upload_size_));
680 else
681 return l10n_util::GetString(IDS_LOAD_STATE_SENDING_REQUEST);
[email protected]96d185d2009-04-24 03:28:54682 case net::LOAD_STATE_WAITING_FOR_RESPONSE:
683 return l10n_util::GetStringF(IDS_LOAD_STATE_WAITING_FOR_RESPONSE,
684 load_state_host_);
685 // Ignore net::LOAD_STATE_READING_RESPONSE and net::LOAD_STATE_IDLE
686 case net::LOAD_STATE_IDLE:
687 case net::LOAD_STATE_READING_RESPONSE:
688 break;
689 }
690
691 return std::wstring();
692}
693
[email protected]d5f942ba2008-09-26 19:30:34694void TabContents::SetIsCrashed(bool state) {
695 if (state == is_crashed_)
696 return;
697
698 is_crashed_ = state;
[email protected]c9cd2222009-05-06 05:16:50699 NotifyNavigationStateChanged(INVALIDATE_TAB);
[email protected]d5f942ba2008-09-26 19:30:34700}
701
[email protected]744ef172009-10-16 21:53:46702void TabContents::PageActionStateChanged() {
703 NotifyNavigationStateChanged(TabContents::INVALIDATE_PAGE_ACTIONS);
[email protected]d7eaf572009-07-01 21:57:00704}
[email protected]ce5c4502009-05-06 16:46:11705
[email protected]d5f942ba2008-09-26 19:30:34706void TabContents::NotifyNavigationStateChanged(unsigned changed_flags) {
707 if (delegate_)
708 delegate_->NavigationStateChanged(this, changed_flags);
709}
710
[email protected]96d185d2009-04-24 03:28:54711void TabContents::DidBecomeSelected() {
712 controller_.SetActive(true);
[email protected]8cb5d5b2010-02-09 11:36:16713 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
714 if (rwhv) {
715 rwhv->DidBecomeSelected();
[email protected]789e9152009-08-04 21:59:43716#if defined(OS_MACOSX)
[email protected]8cb5d5b2010-02-09 11:36:16717 rwhv->SetActive(true);
[email protected]789e9152009-08-04 21:59:43718#endif
719 }
[email protected]96d185d2009-04-24 03:28:54720
[email protected]8cb5d5b2010-02-09 11:36:16721 WebCacheManager::GetInstance()->ObserveActivity(GetRenderProcessHost()->id());
[email protected]96d185d2009-04-24 03:28:54722}
723
724void TabContents::WasHidden() {
725 if (!capturing_contents()) {
726 // |render_view_host()| can be NULL if the user middle clicks a link to open
727 // a tab in then background, then closes the tab before selecting it. This
728 // is because closing the tab calls TabContents::Destroy(), which removes
729 // the |render_view_host()|; then when we actually destroy the window,
730 // OnWindowPosChanged() notices and calls HideContents() (which calls us).
[email protected]8cb5d5b2010-02-09 11:36:16731 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
732 if (rwhv)
733 rwhv->WasHidden();
[email protected]96d185d2009-04-24 03:28:54734 }
735
736 NotificationService::current()->Notify(
737 NotificationType::TAB_CONTENTS_HIDDEN,
738 Source<TabContents>(this),
739 NotificationService::NoDetails());
740}
741
[email protected]d5f942ba2008-09-26 19:30:34742void TabContents::Activate() {
743 if (delegate_)
744 delegate_->ActivateContents(this);
745}
746
[email protected]96d185d2009-04-24 03:28:54747void TabContents::ShowContents() {
[email protected]8cb5d5b2010-02-09 11:36:16748 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
749 if (rwhv)
750 rwhv->DidBecomeSelected();
[email protected]96d185d2009-04-24 03:28:54751}
752
753void TabContents::HideContents() {
754 // TODO(pkasting): http://b/1239839 Right now we purposefully don't call
755 // our superclass HideContents(), because some callers want to be very picky
756 // about the order in which these get called. In addition to making the code
757 // here practically impossible to understand, this also means we end up
758 // calling TabContents::WasHidden() twice if callers call both versions of
[email protected]57c6a652009-05-04 07:58:34759 // HideContents() on a TabContents.
[email protected]96d185d2009-04-24 03:28:54760 WasHidden();
761}
762
[email protected]c0588052008-10-27 23:01:50763void TabContents::OpenURL(const GURL& url, const GURL& referrer,
[email protected]d5f942ba2008-09-26 19:30:34764 WindowOpenDisposition disposition,
765 PageTransition::Type transition) {
766 if (delegate_)
[email protected]c0588052008-10-27 23:01:50767 delegate_->OpenURLFromTab(this, url, referrer, disposition, transition);
[email protected]d5f942ba2008-09-26 19:30:34768}
769
[email protected]1ccb3568d2010-02-19 10:51:16770bool TabContents::NavigateToPendingEntry(
771 NavigationController::ReloadType reload_type) {
[email protected]96d185d2009-04-24 03:28:54772 const NavigationEntry& entry = *controller_.pending_entry();
773
774 RenderViewHost* dest_render_view_host = render_manager_.Navigate(entry);
775 if (!dest_render_view_host)
776 return false; // Unable to create the desired render view host.
777
[email protected]770dd8b2010-05-24 18:11:39778 if (delegate_ && delegate_->ShouldEnablePreferredSizeNotifications()) {
779 dest_render_view_host->EnablePreferredSizeChangedMode(
780 kPreferredSizeWidth | kPreferredSizeHeightThisIsSlow);
781 }
[email protected]9fb325e2010-05-06 18:23:24782
[email protected]0352e5472010-04-29 00:11:40783 // For security, we should never send non-DOM-UI URLs (other than about:blank)
784 // to a DOM UI renderer. Double check that here.
[email protected]54ec6472010-04-09 19:39:58785 int enabled_bindings = dest_render_view_host->enabled_bindings();
[email protected]0352e5472010-04-29 00:11:40786 bool is_allowed_in_dom_ui_renderer =
787 DOMUIFactory::UseDOMUIForURL(entry.url()) ||
788 entry.url() == GURL(chrome::kAboutBlankURL);
[email protected]54ec6472010-04-09 19:39:58789 CHECK(!BindingsPolicy::is_dom_ui_enabled(enabled_bindings) ||
[email protected]0352e5472010-04-29 00:11:40790 is_allowed_in_dom_ui_renderer);
[email protected]54ec6472010-04-09 19:39:58791
[email protected]96d185d2009-04-24 03:28:54792 // Tell DevTools agent that it is attached prior to the navigation.
[email protected]d9f9b792009-06-24 13:17:12793 DevToolsManager* devtools_manager = DevToolsManager::GetInstance();
[email protected]7e8e6b62009-05-08 11:28:32794 if (devtools_manager) { // NULL in unit tests.
[email protected]3cc72b12010-03-18 23:03:00795 devtools_manager->OnNavigatingToPendingEntry(render_view_host(),
796 dest_render_view_host,
797 entry.url());
[email protected]7e8e6b62009-05-08 11:28:32798 }
[email protected]96d185d2009-04-24 03:28:54799
800 // Used for page load time metrics.
801 current_load_start_ = base::TimeTicks::Now();
802
803 // Navigate in the desired RenderViewHost.
[email protected]056de2d2009-06-26 16:41:34804 ViewMsg_Navigate_Params navigate_params;
[email protected]3cc72b12010-03-18 23:03:00805 MakeNavigateParams(controller_, reload_type, &navigate_params);
[email protected]056de2d2009-06-26 16:41:34806 dest_render_view_host->Navigate(navigate_params);
[email protected]96d185d2009-04-24 03:28:54807
808 if (entry.page_id() == -1) {
809 // HACK!! This code suppresses javascript: URLs from being added to
810 // session history, which is what we want to do for javascript: URLs that
811 // do not generate content. What we really need is a message from the
812 // renderer telling us that a new page was not created. The same message
813 // could be used for mailto: URLs and the like.
814 if (entry.url().SchemeIs(chrome::kJavaScriptScheme))
815 return false;
816 }
817
818 // Clear any provisional password saves - this stops password infobars
819 // showing up on pages the user navigates to while the right page is
820 // loading.
821 GetPasswordManager()->ClearProvisionalSave();
822
[email protected]1ccb3568d2010-02-19 10:51:16823 if (reload_type != NavigationController::NO_RELOAD &&
824 !profile()->IsOffTheRecord()) {
[email protected]0189bc722009-08-28 21:56:48825 FaviconService* favicon_service =
826 profile()->GetFaviconService(Profile::IMPLICIT_ACCESS);
827 if (favicon_service)
828 favicon_service->SetFaviconOutOfDateForPage(entry.url());
[email protected]96d185d2009-04-24 03:28:54829 }
830
[email protected]d5f942ba2008-09-26 19:30:34831 return true;
832}
833
[email protected]96d185d2009-04-24 03:28:54834void TabContents::Stop() {
835 render_manager_.Stop();
[email protected]12802702010-07-09 19:43:09836 printing_->Stop();
[email protected]96d185d2009-04-24 03:28:54837}
838
[email protected]96d185d2009-04-24 03:28:54839void TabContents::DisassociateFromPopupCount() {
840 render_view_host()->DisassociateFromPopupCount();
841}
842
843TabContents* TabContents::Clone() {
844 // We create a new SiteInstance so that the new tab won't share processes
845 // with the old one. This can be changed in the future if we need it to share
846 // processes for some reason.
[email protected]420ae012009-04-24 05:16:32847 TabContents* tc = new TabContents(profile(),
[email protected]96d185d2009-04-24 03:28:54848 SiteInstance::CreateSiteInstance(profile()),
[email protected]12636df2009-09-28 22:32:21849 MSG_ROUTING_NONE, this);
[email protected]96d185d2009-04-24 03:28:54850 tc->controller().CopyStateFrom(controller_);
[email protected]98aa0b52010-05-06 17:03:08851 tc->extension_app_ = extension_app_;
[email protected]98aa0b52010-05-06 17:03:08852 tc->extension_app_icon_ = extension_app_icon_;
[email protected]96d185d2009-04-24 03:28:54853 return tc;
854}
855
[email protected]4d677202009-07-19 07:37:12856void TabContents::ShowPageInfo(const GURL& url,
857 const NavigationEntry::SSLStatus& ssl,
858 bool show_history) {
859 if (!delegate_)
860 return;
861
[email protected]bb678332009-07-21 00:15:50862 delegate_->ShowPageInfo(profile(), url, ssl, show_history);
[email protected]4d677202009-07-19 07:37:12863}
864
initial.commit09911bf2008-07-26 23:55:29865ConstrainedWindow* TabContents::CreateConstrainedDialog(
[email protected]e8382172009-06-19 22:16:28866 ConstrainedWindowDelegate* delegate) {
initial.commit09911bf2008-07-26 23:55:29867 ConstrainedWindow* window =
[email protected]e8382172009-06-19 22:16:28868 ConstrainedWindow::CreateConstrainedDialog(this, delegate);
initial.commit09911bf2008-07-26 23:55:29869 child_windows_.push_back(window);
[email protected]fa1cf0b82010-01-15 21:49:44870
871 if (child_windows_.size() == 1) {
872 window->ShowConstrainedWindow();
873 BlockTabContent(true);
874 }
875
initial.commit09911bf2008-07-26 23:55:29876 return window;
877}
878
[email protected]fa1cf0b82010-01-15 21:49:44879void TabContents::BlockTabContent(bool blocked) {
[email protected]8dccd7cb2010-02-25 05:19:55880 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
881 if (rwhv)
882 rwhv->SetVisuallyDeemphasized(blocked);
[email protected]fa1cf0b82010-01-15 21:49:44883 render_view_host()->set_ignore_input_events(blocked);
884 if (delegate_)
885 delegate_->SetTabContentBlocked(this, blocked);
886}
887
initial.commit09911bf2008-07-26 23:55:29888void TabContents::AddNewContents(TabContents* new_contents,
889 WindowOpenDisposition disposition,
890 const gfx::Rect& initial_pos,
[email protected]7e7414ae2010-01-26 20:19:29891 bool user_gesture) {
initial.commit09911bf2008-07-26 23:55:29892 if (!delegate_)
893 return;
894
[email protected]35f7d212009-04-29 21:19:27895 if ((disposition == NEW_POPUP) && !user_gesture &&
896 !CommandLine::ForCurrentProcess()->HasSwitch(
897 switches::kDisablePopupBlocking)) {
[email protected]2c4410d2009-05-06 23:46:22898 // Unrequested popups from normal pages are constrained unless they're in
[email protected]b6c874582009-05-08 19:38:31899 // the whitelist. The popup owner will handle checking this.
[email protected]7e7414ae2010-01-26 20:19:29900 delegate_->GetConstrainingContents(this)->AddPopup(
901 new_contents, initial_pos);
[email protected]b6c874582009-05-08 19:38:31902 } else {
[email protected]304f8792009-08-19 21:28:09903#if defined(OS_CHROMEOS)
904 if (disposition == NEW_POPUP) {
905 // If the popup is bigger than a given factor of the screen, then
906 // turn it into a foreground tab (on chrome os only)
[email protected]8323b082010-07-16 16:56:11907 // Also check for width or height == 0, which would otherwise indicate
908 // a tab sized popup window.
[email protected]304f8792009-08-19 21:28:09909 GdkScreen* screen = gdk_screen_get_default();
910 int max_width = gdk_screen_get_width(screen) * kMaxWidthFactor;
911 int max_height = gdk_screen_get_height(screen) * kMaxHeightFactor;
[email protected]8323b082010-07-16 16:56:11912 if (initial_pos.width() > max_width || initial_pos.width() == 0 ||
913 initial_pos.height() > max_height || initial_pos.height() == 0) {
[email protected]304f8792009-08-19 21:28:09914 disposition = NEW_FOREGROUND_TAB;
915 }
916 }
917#endif
918
[email protected]0aa55312008-10-17 21:53:08919 new_contents->DisassociateFromPopupCount();
initial.commit09911bf2008-07-26 23:55:29920 delegate_->AddNewContents(this, new_contents, disposition, initial_pos,
921 user_gesture);
[email protected]7f37dd92009-09-15 16:35:49922 NotificationService::current()->Notify(
923 NotificationType::TAB_ADDED,
924 Source<TabContentsDelegate>(delegate_),
925 Details<TabContents>(this));
initial.commit09911bf2008-07-26 23:55:29926 }
initial.commit09911bf2008-07-26 23:55:29927
[email protected]8cc8d492010-02-02 10:40:49928 // TODO(pkasting): Why is this necessary?
929 PopupNotificationVisibilityChanged(blocked_popups_ != NULL);
initial.commit09911bf2008-07-26 23:55:29930}
931
[email protected]bcd9dc42010-01-23 16:38:18932bool TabContents::ExecuteCode(int request_id, const std::string& extension_id,
[email protected]61f5a7b2009-12-22 22:21:20933 const std::vector<URLPattern>& host_permissions,
[email protected]20ad2692009-11-20 18:27:20934 bool is_js_code, const std::string& code_string,
935 bool all_frames) {
[email protected]ebd0eae72010-02-16 23:50:51936 RenderViewHost* host = render_view_host();
937 if (!host)
938 return false;
939
940 return host->Send(new ViewMsg_ExecuteCode(host->routing_id(),
941 ViewMsg_ExecuteCode_Params(request_id, extension_id, host_permissions,
942 is_js_code, code_string, all_frames)));
[email protected]912256b32009-09-18 09:47:35943}
944
[email protected]96d185d2009-04-24 03:28:54945void TabContents::PopupNotificationVisibilityChanged(bool visible) {
[email protected]8cc8d492010-02-02 10:40:49946 if (is_being_destroyed_)
947 return;
[email protected]9fb83e82010-07-02 18:24:55948 content_settings_delegate_->SetPopupsBlocked(visible);
[email protected]8cc8d492010-02-02 10:40:49949 if (!dont_notify_render_view_)
[email protected]e8345242010-05-06 03:00:40950 render_view_host()->AllowScriptToClose(!visible);
[email protected]8e0bbf12010-07-20 00:03:43951 if (visible)
952 PopupBlockedAnimation::Show(this);
[email protected]96d185d2009-04-24 03:28:54953}
954
[email protected]5c9e97a2009-09-09 23:48:30955gfx::NativeView TabContents::GetContentNativeView() const {
[email protected]96d185d2009-04-24 03:28:54956 return view_->GetContentNativeView();
957}
958
959gfx::NativeView TabContents::GetNativeView() const {
960 return view_->GetNativeView();
961}
962
963void TabContents::GetContainerBounds(gfx::Rect *out) const {
964 view_->GetContainerBounds(out);
965}
966
967void TabContents::Focus() {
968 view_->Focus();
969}
970
[email protected]90daadb42009-06-08 21:27:28971void TabContents::FocusThroughTabTraversal(bool reverse) {
[email protected]7e383692009-06-12 19:14:54972 if (showing_interstitial_page()) {
973 render_manager_.interstitial_page()->FocusThroughTabTraversal(reverse);
974 return;
975 }
[email protected]96d185d2009-04-24 03:28:54976 render_view_host()->SetInitialFocus(reverse);
977}
978
979bool TabContents::FocusLocationBarByDefault() {
980 DOMUI* dom_ui = GetDOMUIForCurrentState();
981 if (dom_ui)
982 return dom_ui->focus_location_bar_by_default();
[email protected]3e3f0eb2009-06-22 18:33:43983 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]e0d481582009-09-15 21:06:25984 if (entry && entry->url() == GURL(chrome::kAboutBlankURL))
[email protected]3e3f0eb2009-06-22 18:33:43985 return true;
[email protected]96d185d2009-04-24 03:28:54986 return false;
987}
988
[email protected]a26dc362010-04-23 01:48:58989void TabContents::SetFocusToLocationBar(bool select_all) {
[email protected]a11aaf22010-03-30 00:03:38990 if (delegate())
[email protected]a26dc362010-04-23 01:48:58991 delegate()->SetFocusToLocationBar(select_all);
[email protected]a11aaf22010-03-30 00:03:38992}
993
[email protected]616ed5a2008-11-21 22:27:24994void TabContents::AddInfoBar(InfoBarDelegate* delegate) {
[email protected]f7019302010-03-26 19:58:32995 if (delegate_ && !delegate_->infobars_enabled()) {
996 delegate->InfoBarClosed();
997 return;
998 }
999
[email protected]616ed5a2008-11-21 22:27:241000 // Look through the existing InfoBarDelegates we have for a match. If we've
1001 // already got one that matches, then we don't add the new one.
[email protected]f86a07022008-11-25 01:06:051002 for (int i = 0; i < infobar_delegate_count(); ++i) {
[email protected]6d27a7b2009-12-18 23:25:451003 if (GetInfoBarDelegateAt(i)->EqualsDelegate(delegate)) {
1004 // Tell the new infobar to close so that it can clean itself up.
1005 delegate->InfoBarClosed();
[email protected]616ed5a2008-11-21 22:27:241006 return;
[email protected]6d27a7b2009-12-18 23:25:451007 }
[email protected]616ed5a2008-11-21 22:27:241008 }
1009
1010 infobar_delegates_.push_back(delegate);
[email protected]bfd04a62009-02-01 18:16:561011 NotificationService::current()->Notify(
1012 NotificationType::TAB_CONTENTS_INFOBAR_ADDED,
1013 Source<TabContents>(this),
1014 Details<InfoBarDelegate>(delegate));
[email protected]616ed5a2008-11-21 22:27:241015
1016 // Add ourselves as an observer for navigations the first time a delegate is
1017 // added. We use this notification to expire InfoBars that need to expire on
1018 // page transitions.
1019 if (infobar_delegates_.size() == 1) {
[email protected]bfd04a62009-02-01 18:16:561020 registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED,
[email protected]ce3fa3c2009-04-20 19:55:571021 Source<NavigationController>(&controller_));
[email protected]616ed5a2008-11-21 22:27:241022 }
1023}
1024
1025void TabContents::RemoveInfoBar(InfoBarDelegate* delegate) {
[email protected]f7019302010-03-26 19:58:321026 if (delegate_ && !delegate_->infobars_enabled()) {
1027 return;
1028 }
1029
[email protected]616ed5a2008-11-21 22:27:241030 std::vector<InfoBarDelegate*>::iterator it =
1031 find(infobar_delegates_.begin(), infobar_delegates_.end(), delegate);
1032 if (it != infobar_delegates_.end()) {
1033 InfoBarDelegate* delegate = *it;
[email protected]bfd04a62009-02-01 18:16:561034 NotificationService::current()->Notify(
1035 NotificationType::TAB_CONTENTS_INFOBAR_REMOVED,
1036 Source<TabContents>(this),
1037 Details<InfoBarDelegate>(delegate));
[email protected]f86a07022008-11-25 01:06:051038 infobar_delegates_.erase(it);
[email protected]616ed5a2008-11-21 22:27:241039
[email protected]6a02963e2009-01-06 16:58:031040 // Remove ourselves as an observer if we are tracking no more InfoBars.
1041 if (infobar_delegates_.empty()) {
[email protected]bfd04a62009-02-01 18:16:561042 registrar_.Remove(this, NotificationType::NAV_ENTRY_COMMITTED,
[email protected]ce3fa3c2009-04-20 19:55:571043 Source<NavigationController>(&controller_));
[email protected]6a02963e2009-01-06 16:58:031044 }
[email protected]616ed5a2008-11-21 22:27:241045 }
1046}
[email protected]3edd9522009-03-04 22:19:411047
[email protected]f81821c2009-08-07 01:29:471048void TabContents::ReplaceInfoBar(InfoBarDelegate* old_delegate,
1049 InfoBarDelegate* new_delegate) {
[email protected]f7019302010-03-26 19:58:321050 if (delegate_ && !delegate_->infobars_enabled()) {
1051 new_delegate->InfoBarClosed();
1052 return;
1053 }
1054
[email protected]f81821c2009-08-07 01:29:471055 std::vector<InfoBarDelegate*>::iterator it =
1056 find(infobar_delegates_.begin(), infobar_delegates_.end(), old_delegate);
1057 DCHECK(it != infobar_delegates_.end());
1058
1059 // Notify the container about the change of plans.
1060 scoped_ptr<std::pair<InfoBarDelegate*, InfoBarDelegate*> > details(
1061 new std::pair<InfoBarDelegate*, InfoBarDelegate*>(
1062 old_delegate, new_delegate));
1063 NotificationService::current()->Notify(
1064 NotificationType::TAB_CONTENTS_INFOBAR_REPLACED,
1065 Source<TabContents>(this),
1066 Details<std::pair<InfoBarDelegate*, InfoBarDelegate*> >(details.get()));
1067
1068 // Remove the old one.
1069 infobar_delegates_.erase(it);
1070
1071 // Add the new one.
1072 infobar_delegates_.push_back(new_delegate);
1073}
1074
[email protected]20ede072009-10-06 00:28:351075bool TabContents::ShouldShowBookmarkBar() {
[email protected]aa9196f2009-10-22 16:20:271076 if (showing_interstitial_page())
1077 return false;
1078
[email protected]96d185d2009-04-24 03:28:541079 // See GetDOMUIForCurrentState() comment for more info. This case is very
1080 // similar, but for non-first loads, we want to use the committed entry. This
1081 // is so the bookmarks bar disappears at the same time the page does.
1082 if (controller_.GetLastCommittedEntry()) {
1083 // Not the first load, always use the committed DOM UI.
1084 if (render_manager_.dom_ui())
1085 return render_manager_.dom_ui()->force_bookmark_bar_visible();
1086 return false; // Default.
1087 }
1088
1089 // When it's the first load, we know either the pending one or the committed
1090 // one will have the DOM UI in it (see GetDOMUIForCurrentState), and only one
1091 // of them will be valid, so we can just check both.
1092 if (render_manager_.pending_dom_ui())
1093 return render_manager_.pending_dom_ui()->force_bookmark_bar_visible();
1094 if (render_manager_.dom_ui())
1095 return render_manager_.dom_ui()->force_bookmark_bar_visible();
1096 return false; // Default.
1097}
1098
[email protected]d8f16ae2009-08-30 03:47:261099bool TabContents::IsExtensionShelfAlwaysVisible() {
1100 // See GetDOMUIForCurrentState() comment for more info. This case is very
1101 // similar, but for non-first loads, we want to use the committed entry. This
1102 // is so the bookmarks bar disappears at the same time the page does.
1103 if (controller_.GetLastCommittedEntry()) {
1104 // Not the first load, always use the committed DOM UI.
1105 if (render_manager_.dom_ui())
1106 return render_manager_.dom_ui()->force_extension_shelf_visible();
1107 return false; // Default.
1108 }
1109
1110 // When it's the first load, we know either the pending one or the committed
1111 // one will have the DOM UI in it (see GetDOMUIForCurrentState), and only one
1112 // of them will be valid, so we can just check both.
1113 if (render_manager_.pending_dom_ui())
1114 return render_manager_.pending_dom_ui()->force_extension_shelf_visible();
1115 if (render_manager_.dom_ui())
1116 return render_manager_.dom_ui()->force_extension_shelf_visible();
1117 return false; // Default.
1118}
1119
[email protected]3edd9522009-03-04 22:19:411120void TabContents::ToolbarSizeChanged(bool is_animating) {
1121 TabContentsDelegate* d = delegate();
1122 if (d)
1123 d->ToolbarSizeChanged(this, is_animating);
1124}
[email protected]616ed5a2008-11-21 22:27:241125
[email protected]fc6fb7fb2009-11-07 02:35:041126bool TabContents::CanDownload(int request_id) {
1127 TabContentsDelegate* d = delegate();
1128 if (d)
1129 return d->CanDownload(request_id);
1130 return true;
1131}
1132
[email protected]d5f942ba2008-09-26 19:30:341133void TabContents::OnStartDownload(DownloadItem* download) {
1134 DCHECK(download);
[email protected]d5f942ba2008-09-26 19:30:341135
1136 // Download in a constrained popup is shown in the tab that opened it.
[email protected]b6c874582009-05-08 19:38:311137 TabContents* tab_contents = delegate()->GetConstrainingContents(this);
[email protected]d5f942ba2008-09-26 19:30:341138
[email protected]c70f9b82010-04-21 07:31:111139 if (tab_contents && tab_contents->delegate()) {
[email protected]59560e0b2009-06-04 03:30:221140 tab_contents->delegate()->OnStartDownload(download);
[email protected]c70f9b82010-04-21 07:31:111141 // If the download occurs in a new tab, close it
1142 if (controller_.IsInitialNavigation() && (tab_contents == this))
1143 delegate()->CloseContents(this);
1144 }
[email protected]3edd9522009-03-04 22:19:411145}
[email protected]3edd9522009-03-04 22:19:411146
[email protected]d5f942ba2008-09-26 19:30:341147void TabContents::WillClose(ConstrainedWindow* window) {
1148 ConstrainedWindowList::iterator it =
1149 find(child_windows_.begin(), child_windows_.end(), window);
[email protected]fa1cf0b82010-01-15 21:49:441150 bool removed_topmost_window = it == child_windows_.begin();
[email protected]d5f942ba2008-09-26 19:30:341151 if (it != child_windows_.end())
1152 child_windows_.erase(it);
[email protected]55098792010-03-24 18:20:371153 if (child_windows_.size() > 0) {
1154 if (removed_topmost_window) {
1155 child_windows_[0]->ShowConstrainedWindow();
1156 }
[email protected]fa1cf0b82010-01-15 21:49:441157 BlockTabContent(true);
1158 } else {
1159 BlockTabContent(false);
1160 }
[email protected]66ba4932009-06-04 19:22:131161}
[email protected]d5f942ba2008-09-26 19:30:341162
[email protected]66ba4932009-06-04 19:22:131163void TabContents::WillCloseBlockedPopupContainer(
1164 BlockedPopupContainer* container) {
1165 DCHECK(blocked_popups_ == container);
1166 blocked_popups_ = NULL;
[email protected]8cc8d492010-02-02 10:40:491167 PopupNotificationVisibilityChanged(false);
[email protected]d5f942ba2008-09-26 19:30:341168}
1169
[email protected]d5f942ba2008-09-26 19:30:341170void TabContents::DidMoveOrResize(ConstrainedWindow* window) {
[email protected]332af7732009-03-11 13:21:351171#if defined(OS_WIN)
[email protected]92edc472009-02-10 20:32:061172 UpdateWindow(GetNativeView());
[email protected]332af7732009-03-11 13:21:351173#endif
[email protected]d5f942ba2008-09-26 19:30:341174}
1175
[email protected]c1dc9f132009-11-20 23:13:211176void TabContents::StartFinding(string16 search_string,
[email protected]e491f1c2009-05-22 20:28:121177 bool forward_direction,
1178 bool case_sensitive) {
[email protected]c1dc9f132009-11-20 23:13:211179 // If search_string is empty, it means FindNext was pressed with a keyboard
[email protected]6e95706d2009-04-23 22:55:011180 // shortcut so unless we have something to search for we return early.
[email protected]c1dc9f132009-11-20 23:13:211181 if (search_string.empty() && find_text_.empty()) {
[email protected]e5d2f7132010-04-09 18:13:531182 string16 last_search_prepopulate_text =
1183 FindBarState::GetLastPrepopulateText(profile());
1184
[email protected]87fae512009-12-11 02:20:211185 // Try the last thing we searched for on this tab, then the last thing
1186 // searched for on any tab.
1187 if (!previous_find_text_.empty())
1188 search_string = previous_find_text_;
[email protected]e5d2f7132010-04-09 18:13:531189 else if (!last_search_prepopulate_text.empty())
1190 search_string = last_search_prepopulate_text;
[email protected]87fae512009-12-11 02:20:211191 else
[email protected]e491f1c2009-05-22 20:28:121192 return;
[email protected]e491f1c2009-05-22 20:28:121193 }
[email protected]6e95706d2009-04-23 22:55:011194
[email protected]101c90e2009-10-26 21:14:441195 // Keep track of the previous search.
1196 previous_find_text_ = find_text_;
1197
[email protected]6e95706d2009-04-23 22:55:011198 // This is a FindNext operation if we are searching for the same text again,
1199 // or if the passed in search text is empty (FindNext keyboard shortcut). The
1200 // exception to this is if the Find was aborted (then we don't want FindNext
1201 // because the highlighting has been cleared and we need it to reappear). We
1202 // therefore treat FindNext after an aborted Find operation as a full fledged
1203 // Find.
[email protected]c1dc9f132009-11-20 23:13:211204 bool find_next = (find_text_ == search_string || search_string.empty()) &&
[email protected]e491f1c2009-05-22 20:28:121205 (last_search_case_sensitive_ == case_sensitive) &&
[email protected]6e95706d2009-04-23 22:55:011206 !find_op_aborted_;
1207 if (!find_next)
1208 current_find_request_id_ = find_request_id_counter_++;
1209
[email protected]c1dc9f132009-11-20 23:13:211210 if (!search_string.empty())
1211 find_text_ = search_string;
[email protected]e491f1c2009-05-22 20:28:121212 last_search_case_sensitive_ = case_sensitive;
[email protected]6e95706d2009-04-23 22:55:011213
1214 find_op_aborted_ = false;
1215
1216 // Keep track of what the last search was across the tabs.
[email protected]e5d2f7132010-04-09 18:13:531217 FindBarState* find_bar_state = profile()->GetFindBarState();
1218 find_bar_state->set_last_prepopulate_text(find_text_);
[email protected]6e95706d2009-04-23 22:55:011219 render_view_host()->StartFinding(current_find_request_id_,
1220 find_text_,
1221 forward_direction,
[email protected]e491f1c2009-05-22 20:28:121222 case_sensitive,
[email protected]6e95706d2009-04-23 22:55:011223 find_next);
1224}
1225
[email protected]5bc8fe92010-03-11 18:19:001226void TabContents::StopFinding(
1227 FindBarController::SelectionAction selection_action) {
[email protected]d6e8d332010-06-04 22:26:011228 if (selection_action == FindBarController::kClearSelection) {
1229 // kClearSelection means the find string has been cleared by the user, but
1230 // the UI has not been dismissed. In that case we want to clear the
1231 // previously remembered search (http://crbug.com/42639).
1232 previous_find_text_ = string16();
1233 } else {
[email protected]2d2dffc2009-06-09 21:41:001234 find_ui_active_ = false;
[email protected]d6e8d332010-06-04 22:26:011235 if (!find_text_.empty())
1236 previous_find_text_ = find_text_;
1237 }
[email protected]101c90e2009-10-26 21:14:441238 find_text_.clear();
[email protected]6e95706d2009-04-23 22:55:011239 find_op_aborted_ = true;
[email protected]e491f1c2009-05-22 20:28:121240 last_search_result_ = FindNotificationDetails();
[email protected]5bc8fe92010-03-11 18:19:001241 render_view_host()->StopFinding(selection_action);
[email protected]6e95706d2009-04-23 22:55:011242}
1243
[email protected]420ae012009-04-24 05:16:321244void TabContents::OnSavePage() {
1245 // If we can not save the page, try to download it.
1246 if (!SavePackage::IsSavableContents(contents_mime_type())) {
1247 DownloadManager* dlm = profile()->GetDownloadManager();
1248 const GURL& current_page_url = GetURL();
1249 if (dlm && current_page_url.is_valid())
[email protected]d82ed61e2009-06-16 02:46:221250 dlm->DownloadUrl(current_page_url, GURL(), "", this);
[email protected]420ae012009-04-24 05:16:321251 return;
1252 }
1253
1254 Stop();
1255
1256 // Create the save package and possibly prompt the user for the name to save
1257 // the page as. The user prompt is an asynchronous operation that runs on
1258 // another thread.
[email protected]d82ed61e2009-06-16 02:46:221259 save_package_ = new SavePackage(this);
[email protected]420ae012009-04-24 05:16:321260 save_package_->GetSaveInfo();
1261}
1262
1263// Used in automated testing to bypass prompting the user for file names.
1264// Instead, the names and paths are hard coded rather than running them through
1265// file name sanitation and extension / mime checking.
[email protected]828cabe2009-09-26 22:47:111266bool TabContents::SavePage(const FilePath& main_file, const FilePath& dir_path,
[email protected]420ae012009-04-24 05:16:321267 SavePackage::SavePackageType save_type) {
1268 // Stop the page from navigating.
1269 Stop();
1270
[email protected]828cabe2009-09-26 22:47:111271 save_package_ = new SavePackage(this, save_type, main_file, dir_path);
[email protected]cdf6cc82009-08-04 21:48:591272 return save_package_->Init();
[email protected]420ae012009-04-24 05:16:321273}
1274
[email protected]89d6e6e2009-12-03 23:06:511275void TabContents::EmailPageLocation() {
1276 std::string title = EscapeQueryParamValue(UTF16ToUTF8(GetTitle()), false);
1277 std::string page_url = EscapeQueryParamValue(GetURL().spec(), false);
1278 std::string mailto = std::string("mailto:?subject=Fwd:%20") +
1279 title + "&body=%0A%0A" + page_url;
1280 platform_util::OpenExternal(GURL(mailto));
1281}
1282
[email protected]420ae012009-04-24 05:16:321283void TabContents::PrintPreview() {
1284 // We don't show the print preview yet, only the print dialog.
1285 PrintNow();
1286}
1287
1288bool TabContents::PrintNow() {
1289 // We can't print interstitial page for now.
1290 if (showing_interstitial_page())
1291 return false;
1292
1293 return render_view_host()->PrintPages();
1294}
1295
[email protected]82270452009-06-19 15:58:011296void TabContents::PrintingDone(int document_cookie, bool success) {
1297 render_view_host()->PrintingDone(document_cookie, success);
1298}
1299
[email protected]420ae012009-04-24 05:16:321300bool TabContents::IsActiveEntry(int32 page_id) {
1301 NavigationEntry* active_entry = controller_.GetActiveEntry();
1302 return (active_entry != NULL &&
1303 active_entry->site_instance() == GetSiteInstance() &&
1304 active_entry->page_id() == page_id);
1305}
1306
[email protected]b2fe07d12010-02-09 14:38:081307void TabContents::SetOverrideEncoding(const std::string& encoding) {
[email protected]8cb5d5b2010-02-09 11:36:161308 set_encoding(encoding);
1309 render_view_host()->SetPageEncoding(encoding);
1310}
1311
[email protected]b2fe07d12010-02-09 14:38:081312void TabContents::ResetOverrideEncoding() {
[email protected]8cb5d5b2010-02-09 11:36:161313 reset_encoding();
1314 render_view_host()->ResetPageEncodingToDefault();
1315}
1316
1317void TabContents::WindowMoveOrResizeStarted() {
1318 render_view_host()->WindowMoveOrResizeStarted();
1319}
1320
[email protected]3f8eb7f2009-10-02 23:58:051321void TabContents::LogNewTabTime(const std::string& event_name) {
1322 // Not all new tab pages get timed. In those cases, we don't have a
1323 // new_tab_start_time_.
1324 if (new_tab_start_time_.is_null())
1325 return;
1326
1327 base::TimeDelta duration = base::TimeTicks::Now() - new_tab_start_time_;
[email protected]84abba62009-10-07 17:01:441328 MetricEventDurationDetails details(event_name,
1329 static_cast<int>(duration.InMilliseconds()));
1330
[email protected]7ab1e7d62009-10-14 23:32:011331 if (event_name == "Tab.NewTabScriptStart") {
1332 UMA_HISTOGRAM_TIMES("Tab.NewTabScriptStart", duration);
1333 } else if (event_name == "Tab.NewTabDOMContentLoaded") {
1334 UMA_HISTOGRAM_TIMES("Tab.NewTabDOMContentLoaded", duration);
1335 } else if (event_name == "Tab.NewTabOnload") {
1336 UMA_HISTOGRAM_TIMES("Tab.NewTabOnload", duration);
[email protected]3f8eb7f2009-10-02 23:58:051337 // The new tab page has finished loading; reset it.
1338 new_tab_start_time_ = base::TimeTicks();
[email protected]84abba62009-10-07 17:01:441339 } else {
1340 NOTREACHED();
[email protected]3f8eb7f2009-10-02 23:58:051341 }
[email protected]84abba62009-10-07 17:01:441342 NotificationService::current()->Notify(
1343 NotificationType::METRIC_EVENT_DURATION,
1344 Source<TabContents>(this),
1345 Details<MetricEventDurationDetails>(&details));
[email protected]3f8eb7f2009-10-02 23:58:051346}
1347
[email protected]7ab1e7d62009-10-14 23:32:011348void TabContents::OnCloseStarted() {
1349 if (tab_close_start_time_.is_null())
1350 tab_close_start_time_ = base::TimeTicks::Now();
1351}
1352
[email protected]b5558cf22010-07-12 17:30:061353void TabContents::set_request_context(URLRequestContextGetter* context) {
1354 request_context_ = context;
1355}
1356
[email protected]46624bf2010-06-09 16:04:191357bool TabContents::ShouldAcceptDragAndDrop() const {
1358#if defined(OS_CHROMEOS)
1359 // ChromeOS panels (pop-ups) do not take drag-n-drop.
1360 // See http://crosbug.com/2413
1361 return delegate() && !delegate()->IsPopup(const_cast<TabContents*>(this));
1362#else
1363 return true;
1364#endif
1365}
1366
[email protected]c37e3b62010-01-22 00:15:171367TabContents* TabContents::CloneAndMakePhantom() {
[email protected]c37e3b62010-01-22 00:15:171368 TabNavigation tab_nav;
[email protected]df72ac72010-02-06 00:43:161369
1370 NavigationEntry* entry = controller().GetActiveEntry();
[email protected]98aa0b52010-05-06 17:03:081371 if (extension_app_)
1372 tab_nav.set_virtual_url(extension_app_->GetFullLaunchURL());
[email protected]df72ac72010-02-06 00:43:161373 else if (entry)
1374 tab_nav.SetFromNavigationEntry(*entry);
1375
[email protected]c37e3b62010-01-22 00:15:171376 std::vector<TabNavigation> navigations;
1377 navigations.push_back(tab_nav);
1378
1379 TabContents* new_contents =
1380 new TabContents(profile(), NULL, MSG_ROUTING_NONE, NULL);
[email protected]98aa0b52010-05-06 17:03:081381 new_contents->SetExtensionApp(extension_app_);
[email protected]c37e3b62010-01-22 00:15:171382 new_contents->controller().RestoreFromState(navigations, 0, false);
[email protected]c37e3b62010-01-22 00:15:171383
[email protected]98aa0b52010-05-06 17:03:081384 if (!extension_app_ && entry)
[email protected]df72ac72010-02-06 00:43:161385 new_contents->controller().GetActiveEntry()->favicon() = entry->favicon();
1386
[email protected]c37e3b62010-01-22 00:15:171387 return new_contents;
1388}
1389
[email protected]96d185d2009-04-24 03:28:541390// Notifies the RenderWidgetHost instance about the fact that the page is
1391// loading, or done loading and calls the base implementation.
1392void TabContents::SetIsLoading(bool is_loading,
1393 LoadNotificationDetails* details) {
1394 if (is_loading == is_loading_)
1395 return;
1396
1397 if (!is_loading) {
1398 load_state_ = net::LOAD_STATE_IDLE;
1399 load_state_host_.clear();
[email protected]094e5b22009-09-25 04:23:561400 upload_size_ = 0;
1401 upload_position_ = 0;
[email protected]96d185d2009-04-24 03:28:541402 }
1403
1404 render_manager_.SetIsLoading(is_loading);
1405
1406 is_loading_ = is_loading;
1407 waiting_for_response_ = is_loading;
1408
1409 if (delegate_)
1410 delegate_->LoadingStateChanged(this);
[email protected]329581b2009-04-28 06:52:351411 NotifyNavigationStateChanged(INVALIDATE_LOAD);
[email protected]96d185d2009-04-24 03:28:541412
1413 NotificationType type = is_loading ? NotificationType::LOAD_START :
1414 NotificationType::LOAD_STOP;
[email protected]ce5c4502009-05-06 16:46:111415 NotificationDetails det = NotificationService::NoDetails();
[email protected]96d185d2009-04-24 03:28:541416 if (details)
1417 det = Details<LoadNotificationDetails>(details);
1418 NotificationService::current()->Notify(type,
1419 Source<NavigationController>(&controller_),
1420 det);
1421}
1422
[email protected]b6c874582009-05-08 19:38:311423void TabContents::AddPopup(TabContents* new_contents,
[email protected]7e7414ae2010-01-26 20:19:291424 const gfx::Rect& initial_pos) {
[email protected]57eda82a2010-05-05 21:28:061425 // A page can't spawn popups (or do anything else, either) until its load
1426 // commits, so when we reach here, the popup was spawned by the
1427 // NavigationController's last committed entry, not the active entry. For
1428 // example, if a page opens a popup in an onunload() handler, then the active
1429 // entry is the page to be loaded as we navigate away from the unloading
1430 // page. For this reason, we can't use GetURL() to get the opener URL,
1431 // because it returns the active entry.
1432 NavigationEntry* entry = controller_.GetLastCommittedEntry();
1433 GURL creator = entry ? entry->virtual_url() : GURL::EmptyGURL();
1434
1435 if (creator.is_valid() &&
[email protected]8cc8d492010-02-02 10:40:491436 profile()->GetHostContentSettingsMap()->GetContentSetting(
[email protected]57eda82a2010-05-05 21:28:061437 creator, CONTENT_SETTINGS_TYPE_POPUPS) == CONTENT_SETTING_ALLOW) {
[email protected]8cc8d492010-02-02 10:40:491438 AddNewContents(new_contents, NEW_POPUP, initial_pos, true);
1439 } else {
1440 if (!blocked_popups_)
1441 blocked_popups_ = new BlockedPopupContainer(this);
1442 blocked_popups_->AddTabContents(new_contents, initial_pos);
[email protected]9fb83e82010-07-02 18:24:551443 content_settings_delegate_->OnContentBlocked(CONTENT_SETTINGS_TYPE_POPUPS);
[email protected]8cc8d492010-02-02 10:40:491444 }
[email protected]b9681312008-11-07 00:08:261445}
[email protected]616ed5a2008-11-21 22:27:241446
1447namespace {
1448bool TransitionIsReload(PageTransition::Type transition) {
1449 return PageTransition::StripQualifier(transition) == PageTransition::RELOAD;
1450}
1451}
1452
1453void TabContents::ExpireInfoBars(
1454 const NavigationController::LoadCommittedDetails& details) {
1455 // Only hide InfoBars when the user has done something that makes the main
1456 // frame load. We don't want various automatic or subframe navigations making
1457 // it disappear.
1458 if (!details.is_user_initiated_main_frame_load())
1459 return;
1460
[email protected]f86a07022008-11-25 01:06:051461 for (int i = infobar_delegate_count() - 1; i >= 0; --i) {
[email protected]616ed5a2008-11-21 22:27:241462 InfoBarDelegate* delegate = GetInfoBarDelegateAt(i);
[email protected]f86a07022008-11-25 01:06:051463 if (delegate->ShouldExpire(details))
[email protected]616ed5a2008-11-21 22:27:241464 RemoveInfoBar(delegate);
[email protected]616ed5a2008-11-21 22:27:241465 }
1466}
[email protected]fdd61c62009-04-22 19:22:571467
[email protected]96d185d2009-04-24 03:28:541468DOMUI* TabContents::GetDOMUIForCurrentState() {
1469 // When there is a pending navigation entry, we want to use the pending DOMUI
1470 // that goes along with it to control the basic flags. For example, we want to
1471 // show the pending URL in the URL bar, so we want the display_url flag to
1472 // be from the pending entry.
1473 //
1474 // The confusion comes because there are multiple possibilities for the
1475 // initial load in a tab as a side effect of the way the RenderViewHostManager
1476 // works.
1477 //
1478 // - For the very first tab the load looks "normal". The new tab DOM UI is
1479 // the pending one, and we want it to apply here.
1480 //
1481 // - For subsequent new tabs, they'll get a new SiteInstance which will then
1482 // get switched to the one previously associated with the new tab pages.
1483 // This switching will cause the manager to commit the RVH/DOMUI. So we'll
1484 // have a committed DOM UI in this case.
1485 //
1486 // This condition handles all of these cases:
1487 //
1488 // - First load in first tab: no committed nav entry + pending nav entry +
1489 // pending dom ui:
1490 // -> Use pending DOM UI if any.
1491 //
1492 // - First load in second tab: no committed nav entry + pending nav entry +
1493 // no pending DOM UI:
1494 // -> Use the committed DOM UI if any.
1495 //
1496 // - Second navigation in any tab: committed nav entry + pending nav entry:
1497 // -> Use pending DOM UI if any.
1498 //
1499 // - Normal state with no load: committed nav entry + no pending nav entry:
1500 // -> Use committed DOM UI.
1501 if (controller_.pending_entry() &&
1502 (controller_.GetLastCommittedEntry() ||
1503 render_manager_.pending_dom_ui()))
1504 return render_manager_.pending_dom_ui();
1505 return render_manager_.dom_ui();
1506}
[email protected]420ae012009-04-24 05:16:321507
1508void TabContents::DidNavigateMainFramePostCommit(
1509 const NavigationController::LoadCommittedDetails& details,
1510 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]c2e74fe82009-09-03 17:57:441511 if (opener_dom_ui_type_ != DOMUIFactory::kNoDOMUI) {
1512 // If this is a window.open navigation, use the same DOMUI as the renderer
1513 // that opened the window, as long as both renderers have the same
1514 // privileges.
1515 if (opener_dom_ui_type_ == DOMUIFactory::GetDOMUIType(GetURL())) {
1516 DOMUI* dom_ui = DOMUIFactory::CreateDOMUIForURL(this, GetURL());
1517 // dom_ui might be NULL if the URL refers to a non-existent extension.
1518 if (dom_ui) {
1519 render_manager_.SetDOMUIPostCommit(dom_ui);
1520 dom_ui->RenderViewCreated(render_view_host());
1521 }
1522 }
1523 opener_dom_ui_type_ = DOMUIFactory::kNoDOMUI;
1524 }
1525
[email protected]420ae012009-04-24 05:16:321526 if (details.is_user_initiated_main_frame_load()) {
1527 // Clear the status bubble. This is a workaround for a bug where WebKit
1528 // doesn't let us know that the cursor left an element during a
1529 // transition (this is also why the mouse cursor remains as a hand after
1530 // clicking on a link); see bugs 1184641 and 980803. We don't want to
1531 // clear the bubble when a user navigates to a named anchor in the same
1532 // page.
1533 UpdateTargetURL(details.entry->page_id(), GURL());
1534
1535 // UpdateHelpersForDidNavigate will handle the case where the password_form
1536 // origin is valid.
1537 // TODO(brettw) bug 1343111: Password manager stuff in here needs to be
1538 // cleaned up and covered by tests.
1539 if (!params.password_form.origin.is_valid())
1540 GetPasswordManager()->DidNavigate();
1541 }
1542
1543 // The keyword generator uses the navigation entries, so must be called after
1544 // the commit.
1545 GenerateKeywordIfNecessary(params);
1546
1547 // Allow the new page to set the title again.
1548 received_page_title_ = false;
1549
1550 // Get the favicon, either from history or request it from the net.
1551 fav_icon_helper_.FetchFavIcon(details.entry->url());
1552
[email protected]0de80162010-02-03 04:52:351553 // Clear all page actions, blocked content notifications and browser actions
1554 // for this tab, unless this is an in-page navigation.
[email protected]a6e82fc2010-02-24 22:28:141555 if (!details.is_in_page) {
[email protected]92c6f9b92009-10-24 04:35:081556 ExtensionsService* service = profile()->GetExtensionsService();
1557 if (service) {
1558 for (size_t i = 0; i < service->extensions()->size(); ++i) {
[email protected]5d246db22009-10-27 06:17:571559 ExtensionAction* browser_action =
[email protected]92c6f9b92009-10-24 04:35:081560 service->extensions()->at(i)->browser_action();
[email protected]56ce6e52009-10-27 00:10:521561 if (browser_action) {
1562 browser_action->ClearAllValuesForTab(controller().session_id().id());
1563 NotificationService::current()->Notify(
1564 NotificationType::EXTENSION_BROWSER_ACTION_UPDATED,
[email protected]5d246db22009-10-27 06:17:571565 Source<ExtensionAction>(browser_action),
[email protected]56ce6e52009-10-27 00:10:521566 NotificationService::NoDetails());
1567 }
[email protected]92c6f9b92009-10-24 04:35:081568
[email protected]5d246db22009-10-27 06:17:571569 ExtensionAction* page_action =
[email protected]56ce6e52009-10-27 00:10:521570 service->extensions()->at(i)->page_action();
1571 if (page_action) {
1572 page_action->ClearAllValuesForTab(controller().session_id().id());
1573 PageActionStateChanged();
1574 }
[email protected]92c6f9b92009-10-24 04:35:081575 }
[email protected]aece2c7f2009-08-27 20:43:171576 }
[email protected]0de80162010-02-03 04:52:351577
1578 // Close blocked popups.
1579 if (blocked_popups_) {
[email protected]0fbd70332010-06-01 19:28:341580 AutoReset<bool> auto_reset(&dont_notify_render_view_, true);
[email protected]0de80162010-02-03 04:52:351581 blocked_popups_->Destroy();
[email protected]da7b5262010-02-25 21:12:461582 blocked_popups_ = NULL;
[email protected]0de80162010-02-03 04:52:351583 }
1584
1585 // Clear "blocked" flags.
[email protected]9fb83e82010-07-02 18:24:551586 content_settings_delegate_->ClearBlockedContentSettings();
1587 content_settings_delegate_->GeolocationDidNavigate(details);
[email protected]f17a0ee2010-05-17 17:38:471588
1589 // Once the main frame is navigated, we're no longer considered to have
1590 // displayed insecure content.
1591 displayed_insecure_content_ = false;
[email protected]aece2c7f2009-08-27 20:43:171592 }
[email protected]ce5c4502009-05-06 16:46:111593
[email protected]8cc8d492010-02-02 10:40:491594 // Close constrained windows if necessary.
1595 if (!net::RegistryControlledDomainService::SameDomainOrHost(
1596 details.previous_url, details.entry->url()))
1597 CloseConstrainedWindows();
1598
[email protected]420ae012009-04-24 05:16:321599 // Update the starred state.
1600 UpdateStarredStateForCurrentURL();
[email protected]c07c9752010-03-12 05:57:531601
1602 // Clear the cache of forms in AutoFill.
1603 if (autofill_manager_.get())
1604 autofill_manager_->Reset();
[email protected]420ae012009-04-24 05:16:321605}
1606
1607void TabContents::DidNavigateAnyFramePostCommit(
1608 RenderViewHost* render_view_host,
1609 const NavigationController::LoadCommittedDetails& details,
1610 const ViewHostMsg_FrameNavigate_Params& params) {
1611 // If we navigate, start showing messages again. This does nothing to prevent
1612 // a malicious script from spamming messages, since the script could just
1613 // reload the page to stop blocking.
1614 suppress_javascript_messages_ = false;
1615
[email protected]420ae012009-04-24 05:16:321616 // Notify the password manager of the navigation or form submit.
1617 // TODO(brettw) bug 1343111: Password manager stuff in here needs to be
1618 // cleaned up and covered by tests.
1619 if (params.password_form.origin.is_valid())
1620 GetPasswordManager()->ProvisionallySavePassword(params.password_form);
[email protected]3b1c1812010-02-12 21:53:551621
1622 // Let the LanguageState clear its state.
[email protected]34cec8d82010-03-05 18:27:011623 bool reload = details.entry->transition_type() == PageTransition::RELOAD ||
1624 details.type == NavigationType::SAME_PAGE;
1625 language_state_.DidNavigate(reload, details.is_in_page);
[email protected]420ae012009-04-24 05:16:321626}
1627
[email protected]8cc8d492010-02-02 10:40:491628void TabContents::CloseConstrainedWindows() {
1629 // Clear out any constrained windows since we are leaving this page entirely.
[email protected]420ae012009-04-24 05:16:321630 // We use indices instead of iterators in case CloseWindow does something
1631 // that may invalidate an iterator.
1632 int size = static_cast<int>(child_windows_.size());
1633 for (int i = size - 1; i >= 0; --i) {
1634 ConstrainedWindow* window = child_windows_[i];
[email protected]fa1cf0b82010-01-15 21:49:441635 if (window) {
[email protected]420ae012009-04-24 05:16:321636 window->CloseConstrainedWindow();
[email protected]fa1cf0b82010-01-15 21:49:441637 BlockTabContent(false);
1638 }
[email protected]420ae012009-04-24 05:16:321639 }
1640}
1641
1642void TabContents::UpdateStarredStateForCurrentURL() {
1643 BookmarkModel* model = profile()->GetBookmarkModel();
1644 const bool old_state = is_starred_;
1645 is_starred_ = (model && model->IsBookmarked(GetURL()));
1646
1647 if (is_starred_ != old_state && delegate())
1648 delegate()->URLStarredChanged(this, is_starred_);
1649}
1650
1651void TabContents::UpdateAlternateErrorPageURL() {
1652 GURL url = GetAlternateErrorPageURL();
1653 render_view_host()->SetAlternateErrorPageURL(url);
1654}
1655
1656void TabContents::UpdateWebPreferences() {
1657 render_view_host()->UpdateWebPreferences(GetWebkitPrefs());
1658}
1659
1660void TabContents::UpdateMaxPageIDIfNecessary(SiteInstance* site_instance,
1661 RenderViewHost* rvh) {
1662 // If we are creating a RVH for a restored controller, then we might
1663 // have more page IDs than the SiteInstance's current max page ID. We must
1664 // make sure that the max page ID is larger than any restored page ID.
1665 // Note that it is ok for conflicting page IDs to exist in another tab
1666 // (i.e., NavigationController), but if any page ID is larger than the max,
1667 // the back/forward list will get confused.
1668 int max_restored_page_id = controller_.max_restored_page_id();
1669 if (max_restored_page_id > 0) {
1670 int curr_max_page_id = site_instance->max_page_id();
1671 if (max_restored_page_id > curr_max_page_id) {
1672 // Need to update the site instance immediately.
1673 site_instance->UpdateMaxPageID(max_restored_page_id);
1674
1675 // Also tell the renderer to update its internal representation. We
1676 // need to reserve enough IDs to make all restored page IDs less than
1677 // the max.
1678 if (curr_max_page_id < 0)
1679 curr_max_page_id = 0;
1680 rvh->ReservePageIDRange(max_restored_page_id - curr_max_page_id);
1681 }
1682 }
1683}
1684
1685void TabContents::UpdateHistoryForNavigation(
[email protected]ebe89e062009-08-13 23:16:541686 const GURL& virtual_url,
[email protected]befd8d822009-07-01 04:51:471687 const NavigationController::LoadCommittedDetails& details,
[email protected]420ae012009-04-24 05:16:321688 const ViewHostMsg_FrameNavigate_Params& params) {
1689 if (profile()->IsOffTheRecord())
1690 return;
1691
1692 // Add to history service.
1693 HistoryService* hs = profile()->GetHistoryService(Profile::IMPLICIT_ACCESS);
1694 if (hs) {
1695 if (PageTransition::IsMainFrame(params.transition) &&
[email protected]ebe89e062009-08-13 23:16:541696 virtual_url != params.url) {
1697 // Hack on the "virtual" URL so that it will appear in history. For some
[email protected]420ae012009-04-24 05:16:321698 // types of URLs, we will display a magic URL that is different from where
1699 // the page is actually navigated. We want the user to see in history
[email protected]ebe89e062009-08-13 23:16:541700 // what they saw in the URL bar, so we add the virtual URL as a redirect.
1701 // This only applies to the main frame, as the virtual URL doesn't apply
[email protected]420ae012009-04-24 05:16:321702 // to sub-frames.
1703 std::vector<GURL> redirects = params.redirects;
1704 if (!redirects.empty())
[email protected]ebe89e062009-08-13 23:16:541705 redirects.back() = virtual_url;
1706 hs->AddPage(virtual_url, this, params.page_id, params.referrer,
[email protected]befd8d822009-07-01 04:51:471707 params.transition, redirects, details.did_replace_entry);
[email protected]420ae012009-04-24 05:16:321708 } else {
1709 hs->AddPage(params.url, this, params.page_id, params.referrer,
[email protected]befd8d822009-07-01 04:51:471710 params.transition, params.redirects,
1711 details.did_replace_entry);
[email protected]420ae012009-04-24 05:16:321712 }
1713 }
1714}
1715
1716bool TabContents::UpdateTitleForEntry(NavigationEntry* entry,
1717 const std::wstring& title) {
1718 // For file URLs without a title, use the pathname instead. In the case of a
1719 // synthesized title, we don't want the update to count toward the "one set
1720 // per page of the title to history."
[email protected]e53668962010-06-23 15:35:251721 string16 final_title;
[email protected]420ae012009-04-24 05:16:321722 bool explicit_set;
1723 if (entry->url().SchemeIsFile() && title.empty()) {
[email protected]e53668962010-06-23 15:35:251724 final_title = UTF8ToUTF16(entry->url().ExtractFileName());
[email protected]420ae012009-04-24 05:16:321725 explicit_set = false; // Don't count synthetic titles toward the set limit.
1726 } else {
[email protected]e53668962010-06-23 15:35:251727 TrimWhitespace(WideToUTF16Hack(title), TRIM_ALL, &final_title);
[email protected]420ae012009-04-24 05:16:321728 explicit_set = true;
1729 }
1730
[email protected]e53668962010-06-23 15:35:251731 if (final_title == entry->title())
[email protected]420ae012009-04-24 05:16:321732 return false; // Nothing changed, don't bother.
1733
[email protected]e53668962010-06-23 15:35:251734 entry->set_title(final_title);
[email protected]420ae012009-04-24 05:16:321735
1736 // Update the history system for this page.
1737 if (!profile()->IsOffTheRecord() && !received_page_title_) {
1738 HistoryService* hs =
1739 profile()->GetHistoryService(Profile::IMPLICIT_ACCESS);
1740 if (hs)
[email protected]ebe89e062009-08-13 23:16:541741 hs->SetPageTitle(entry->virtual_url(), final_title);
[email protected]420ae012009-04-24 05:16:321742
1743 // Don't allow the title to be saved again for explicitly set ones.
1744 received_page_title_ = explicit_set;
1745 }
1746
1747 // Lastly, set the title for the view.
[email protected]e53668962010-06-23 15:35:251748 view_->SetPageTitle(UTF16ToWideHack(final_title));
[email protected]420ae012009-04-24 05:16:321749
[email protected]cbc0e1b2010-04-12 18:33:041750 NotificationService::current()->Notify(
1751 NotificationType::TAB_CONTENTS_TITLE_UPDATED,
1752 Source<TabContents>(this),
1753 NotificationService::NoDetails());
1754
[email protected]420ae012009-04-24 05:16:321755 return true;
1756}
1757
1758void TabContents::NotifySwapped() {
1759 // After sending out a swap notification, we need to send a disconnect
1760 // notification so that clients that pick up a pointer to |this| can NULL the
1761 // pointer. See Bug 1230284.
1762 notify_disconnection_ = true;
1763 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341764 NotificationType::TAB_CONTENTS_SWAPPED,
[email protected]d82ed61e2009-06-16 02:46:221765 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321766 NotificationService::NoDetails());
1767}
1768
1769void TabContents::NotifyConnected() {
1770 notify_disconnection_ = true;
1771 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341772 NotificationType::TAB_CONTENTS_CONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221773 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321774 NotificationService::NoDetails());
1775}
1776
1777void TabContents::NotifyDisconnected() {
1778 if (!notify_disconnection_)
1779 return;
1780
1781 notify_disconnection_ = false;
1782 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341783 NotificationType::TAB_CONTENTS_DISCONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221784 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321785 NotificationService::NoDetails());
1786}
1787
1788void TabContents::GenerateKeywordIfNecessary(
1789 const ViewHostMsg_FrameNavigate_Params& params) {
1790 if (!params.searchable_form_url.is_valid())
1791 return;
1792
1793 if (profile()->IsOffTheRecord())
1794 return;
1795
1796 int last_index = controller_.last_committed_entry_index();
1797 // When there was no previous page, the last index will be 0. This is
1798 // normally due to a form submit that opened in a new tab.
1799 // TODO(brettw) bug 916126: we should support keywords when form submits
1800 // happen in new tabs.
1801 if (last_index <= 0)
1802 return;
1803 const NavigationEntry* previous_entry =
1804 controller_.GetEntryAtIndex(last_index - 1);
1805 if (IsFormSubmit(previous_entry)) {
1806 // Only generate a keyword if the previous page wasn't itself a form
1807 // submit.
1808 return;
1809 }
1810
1811 GURL keyword_url = previous_entry->user_typed_url().is_valid() ?
1812 previous_entry->user_typed_url() : previous_entry->url();
1813 std::wstring keyword =
1814 TemplateURLModel::GenerateKeyword(keyword_url, true); // autodetected
1815 if (keyword.empty())
1816 return;
1817
1818 TemplateURLModel* url_model = profile()->GetTemplateURLModel();
1819 if (!url_model)
1820 return;
1821
1822 if (!url_model->loaded()) {
1823 url_model->Load();
1824 return;
1825 }
1826
1827 const TemplateURL* current_url;
[email protected]ddd231e2010-06-29 20:35:191828 GURL url = params.searchable_form_url;
[email protected]420ae012009-04-24 05:16:321829 if (!url_model->CanReplaceKeyword(keyword, url, &current_url))
1830 return;
1831
1832 if (current_url) {
1833 if (current_url->originating_url().is_valid()) {
1834 // The existing keyword was generated from an OpenSearch description
1835 // document, don't regenerate.
1836 return;
1837 }
1838 url_model->Remove(current_url);
1839 }
1840 TemplateURL* new_url = new TemplateURL();
1841 new_url->set_keyword(keyword);
1842 new_url->set_short_name(keyword);
[email protected]ddd231e2010-06-29 20:35:191843 new_url->SetURL(url.spec(), 0, 0);
[email protected]420ae012009-04-24 05:16:321844 new_url->add_input_encoding(params.searchable_form_encoding);
1845 DCHECK(controller_.GetLastCommittedEntry());
1846 const GURL& favicon_url =
1847 controller_.GetLastCommittedEntry()->favicon().url();
1848 if (favicon_url.is_valid()) {
1849 new_url->SetFavIconURL(favicon_url);
1850 } else {
1851 // The favicon url isn't valid. This means there really isn't a favicon,
1852 // or the favicon url wasn't obtained before the load started. This assumes
1853 // the later.
1854 // TODO(sky): Need a way to set the favicon that doesn't involve generating
1855 // its url.
1856 new_url->SetFavIconURL(TemplateURL::GenerateFaviconURL(params.referrer));
1857 }
1858 new_url->set_safe_for_autoreplace(true);
1859 url_model->Add(new_url);
1860}
1861
[email protected]bf5c2ff392009-07-08 16:24:331862void TabContents::OnUserGesture() {
1863 // See comment in RenderViewHostDelegate::OnUserGesture as to why we do this.
[email protected]41291322010-07-15 17:09:011864 DownloadRequestLimiter* limiter =
1865 g_browser_process->download_request_limiter();
1866 if (limiter)
1867 limiter->OnUserGesture(this);
[email protected]e7eaedde2009-09-25 20:09:491868 ExternalProtocolHandler::OnUserGesture();
[email protected]bf5c2ff392009-07-08 16:24:331869 controller_.OnUserGesture();
1870}
1871
1872void TabContents::OnFindReply(int request_id,
1873 int number_of_matches,
1874 const gfx::Rect& selection_rect,
1875 int active_match_ordinal,
1876 bool final_update) {
[email protected]3ce7ced2009-07-24 18:52:421877 // Ignore responses for requests that have been aborted.
1878 if (find_op_aborted_)
1879 return;
1880
[email protected]bf5c2ff392009-07-08 16:24:331881 // Ignore responses for requests other than the one we have most recently
1882 // issued. That way we won't act on stale results when the user has
1883 // already typed in another query.
1884 if (request_id != current_find_request_id_)
1885 return;
1886
1887 if (number_of_matches == -1)
1888 number_of_matches = last_search_result_.number_of_matches();
1889 if (active_match_ordinal == -1)
1890 active_match_ordinal = last_search_result_.active_match_ordinal();
1891
1892 gfx::Rect selection = selection_rect;
1893 if (selection.IsEmpty())
1894 selection = last_search_result_.selection_rect();
1895
1896 // Notify the UI, automation and any other observers that a find result was
1897 // found.
1898 last_search_result_ = FindNotificationDetails(request_id, number_of_matches,
1899 selection, active_match_ordinal,
1900 final_update);
1901 NotificationService::current()->Notify(
1902 NotificationType::FIND_RESULT_AVAILABLE,
1903 Source<TabContents>(this),
1904 Details<FindNotificationDetails>(&last_search_result_));
1905}
1906
1907void TabContents::GoToEntryAtOffset(int offset) {
[email protected]f9cc4c452009-10-13 14:56:381908 if (!delegate_ || delegate_->OnGoToEntryOffset(offset))
1909 controller_.GoToOffset(offset);
[email protected]bf5c2ff392009-07-08 16:24:331910}
1911
[email protected]bf5c2ff392009-07-08 16:24:331912void TabContents::OnMissingPluginStatus(int status) {
1913#if defined(OS_WIN)
1914// TODO(PORT): pull in when plug-ins work
1915 GetPluginInstaller()->OnMissingPluginStatus(status);
1916#endif
1917}
1918
1919void TabContents::OnCrashedPlugin(const FilePath& plugin_path) {
[email protected]bf5c2ff392009-07-08 16:24:331920 DCHECK(!plugin_path.value().empty());
1921
1922 std::wstring plugin_name = plugin_path.ToWStringHack();
[email protected]d32d46182009-07-23 00:09:351923#if defined(OS_WIN) || defined(OS_MACOSX)
[email protected]bf5c2ff392009-07-08 16:24:331924 scoped_ptr<FileVersionInfo> version_info(
1925 FileVersionInfo::CreateFileVersionInfo(plugin_path));
1926 if (version_info.get()) {
1927 const std::wstring& product_name = version_info->product_name();
[email protected]996fd702009-09-04 19:12:371928 if (!product_name.empty()) {
[email protected]bf5c2ff392009-07-08 16:24:331929 plugin_name = product_name;
[email protected]996fd702009-09-04 19:12:371930#if defined(OS_MACOSX)
1931 // Many plugins on the Mac have .plugin in the actual name, which looks
1932 // terrible, so look for that and strip it off if present.
1933 const std::wstring plugin_extension(L".plugin");
1934 if (EndsWith(plugin_name, plugin_extension, true))
1935 plugin_name.erase(plugin_name.length() - plugin_extension.length());
1936#endif // OS_MACOSX
1937 }
[email protected]bf5c2ff392009-07-08 16:24:331938 }
[email protected]4a560ed2009-07-13 23:35:261939#else
1940 NOTIMPLEMENTED() << " convert plugin path to plugin name";
1941#endif
[email protected]2ae8a922010-06-28 20:27:531942 SkBitmap* crash_icon = ResourceBundle::GetSharedInstance().GetBitmapNamed(
1943 IDR_INFOBAR_PLUGIN_CRASHED);
[email protected]bf5c2ff392009-07-08 16:24:331944 AddInfoBar(new SimpleAlertInfoBarDelegate(
1945 this, l10n_util::GetStringF(IDS_PLUGIN_CRASHED_PROMPT, plugin_name),
[email protected]2ae8a922010-06-28 20:27:531946 crash_icon, true));
[email protected]bf5c2ff392009-07-08 16:24:331947}
1948
1949void TabContents::OnCrashedWorker() {
1950 AddInfoBar(new SimpleAlertInfoBarDelegate(
1951 this, l10n_util::GetString(IDS_WEBWORKER_CRASHED_PROMPT),
[email protected]938e1f92010-04-01 18:09:421952 NULL, true));
[email protected]bf5c2ff392009-07-08 16:24:331953}
1954
1955void TabContents::OnDidGetApplicationInfo(
1956 int32 page_id,
1957 const webkit_glue::WebApplicationInfo& info) {
[email protected]ed5431872009-11-17 08:39:511958 web_app_info_ = info;
[email protected]bf5c2ff392009-07-08 16:24:331959
[email protected]ed5431872009-11-17 08:39:511960 if (delegate())
1961 delegate()->OnDidGetApplicationInfo(this, page_id);
[email protected]bf5c2ff392009-07-08 16:24:331962}
1963
[email protected]a2cf65eb2010-01-18 08:20:381964void TabContents::OnPageContents(const GURL& url,
1965 int renderer_process_id,
1966 int32 page_id,
[email protected]e5106202010-06-11 21:12:361967 const string16& contents,
[email protected]1c57b7b2010-07-12 17:38:101968 const std::string& language,
1969 bool page_translatable) {
[email protected]a2cf65eb2010-01-18 08:20:381970 // Don't index any https pages. People generally don't want their bank
1971 // accounts, etc. indexed on their computer, especially since some of these
1972 // things are not marked cachable.
1973 // TODO(brettw) we may want to consider more elaborate heuristics such as
1974 // the cachability of the page. We may also want to consider subframes (this
1975 // test will still index subframes if the subframe is SSL).
[email protected]ccd40a52010-05-11 16:15:271976 // TODO(zelidrag) bug chromium-os:2808 - figure out if we want to reenable
1977 // content indexing for chromeos in some future releases.
1978#if !defined(OS_CHROMEOS)
[email protected]a2cf65eb2010-01-18 08:20:381979 if (!url.SchemeIsSecure()) {
1980 Profile* p = profile();
1981 if (p && !p->IsOffTheRecord()) {
1982 HistoryService* hs = p->GetHistoryService(Profile::IMPLICIT_ACCESS);
1983 if (hs)
1984 hs->SetPageContents(url, contents);
1985 }
1986 }
[email protected]ccd40a52010-05-11 16:15:271987#endif
[email protected]a8a81292010-01-21 00:32:451988
[email protected]1c57b7b2010-07-12 17:38:101989 language_state_.LanguageDetermined(language, page_translatable);
[email protected]a8a81292010-01-21 00:32:451990
1991 std::string lang = language;
1992 NotificationService::current()->Notify(
1993 NotificationType::TAB_LANGUAGE_DETERMINED,
[email protected]7e0831b2010-02-01 08:45:151994 Source<TabContents>(this),
[email protected]a8a81292010-01-21 00:32:451995 Details<std::string>(&lang));
[email protected]a2cf65eb2010-01-18 08:20:381996}
1997
[email protected]7e0831b2010-02-01 08:45:151998void TabContents::OnPageTranslated(int32 page_id,
1999 const std::string& original_lang,
[email protected]152b8ba32010-03-29 19:40:162000 const std::string& translated_lang,
2001 TranslateErrors::Type error_type) {
[email protected]3b1c1812010-02-12 21:53:552002 language_state_.set_current_language(translated_lang);
2003 language_state_.set_translation_pending(false);
[email protected]152b8ba32010-03-29 19:40:162004 PageTranslatedDetails details(original_lang, translated_lang, error_type);
[email protected]7e0831b2010-02-01 08:45:152005 NotificationService::current()->Notify(
2006 NotificationType::PAGE_TRANSLATED,
2007 Source<TabContents>(this),
[email protected]152b8ba32010-03-29 19:40:162008 Details<PageTranslatedDetails>(&details));
[email protected]7e0831b2010-02-01 08:45:152009}
2010
[email protected]bf5c2ff392009-07-08 16:24:332011void TabContents::DidStartProvisionalLoadForFrame(
2012 RenderViewHost* render_view_host,
2013 bool is_main_frame,
2014 const GURL& url) {
2015 ProvisionalLoadDetails details(is_main_frame,
2016 controller_.IsURLInPageNavigation(url),
2017 url, std::string(), false);
2018 NotificationService::current()->Notify(
2019 NotificationType::FRAME_PROVISIONAL_LOAD_START,
2020 Source<NavigationController>(&controller_),
2021 Details<ProvisionalLoadDetails>(&details));
2022}
2023
2024void TabContents::DidStartReceivingResourceResponse(
[email protected]3dbb80b2010-02-09 22:41:202025 const ResourceRequestDetails& details) {
[email protected]bf5c2ff392009-07-08 16:24:332026 NotificationService::current()->Notify(
2027 NotificationType::RESOURCE_RESPONSE_STARTED,
2028 Source<NavigationController>(&controller()),
[email protected]3dbb80b2010-02-09 22:41:202029 Details<const ResourceRequestDetails>(&details));
[email protected]bf5c2ff392009-07-08 16:24:332030}
2031
[email protected]3dbb80b2010-02-09 22:41:202032void TabContents::DidRedirectResource(
2033 const ResourceRedirectDetails& details) {
[email protected]bf5c2ff392009-07-08 16:24:332034 NotificationService::current()->Notify(
2035 NotificationType::RESOURCE_RECEIVED_REDIRECT,
2036 Source<NavigationController>(&controller()),
[email protected]3dbb80b2010-02-09 22:41:202037 Details<const ResourceRequestDetails>(&details));
[email protected]bf5c2ff392009-07-08 16:24:332038}
2039
2040void TabContents::DidLoadResourceFromMemoryCache(
2041 const GURL& url,
2042 const std::string& frame_origin,
2043 const std::string& main_frame_origin,
2044 const std::string& security_info) {
2045 // Send out a notification that we loaded a resource from our memory cache.
[email protected]fc7de492010-07-12 14:49:042046 int cert_id = 0, cert_status = 0, security_bits = 0, connection_status = 0;
[email protected]bf5c2ff392009-07-08 16:24:332047 SSLManager::DeserializeSecurityInfo(security_info,
2048 &cert_id, &cert_status,
[email protected]fc7de492010-07-12 14:49:042049 &security_bits,
2050 &connection_status);
[email protected]bf5c2ff392009-07-08 16:24:332051 LoadFromMemoryCacheDetails details(url, frame_origin, main_frame_origin,
[email protected]8cb5d5b2010-02-09 11:36:162052 GetRenderProcessHost()->id(), cert_id,
2053 cert_status);
[email protected]bf5c2ff392009-07-08 16:24:332054
2055 NotificationService::current()->Notify(
2056 NotificationType::LOAD_FROM_MEMORY_CACHE,
2057 Source<NavigationController>(&controller_),
2058 Details<LoadFromMemoryCacheDetails>(&details));
2059}
2060
[email protected]e3d60e5d2009-09-25 21:08:292061void TabContents::DidDisplayInsecureContent() {
[email protected]f17a0ee2010-05-17 17:38:472062 displayed_insecure_content_ = true;
2063 SSLManager::NotifySSLInternalStateChanged();
[email protected]e3d60e5d2009-09-25 21:08:292064}
2065
2066void TabContents::DidRunInsecureContent(const std::string& security_origin) {
[email protected]09843b5f2009-09-26 08:30:592067 controller_.ssl_manager()->DidRunInsecureContent(security_origin);
[email protected]e3d60e5d2009-09-25 21:08:292068}
2069
[email protected]bf5c2ff392009-07-08 16:24:332070void TabContents::DidFailProvisionalLoadWithError(
2071 RenderViewHost* render_view_host,
2072 bool is_main_frame,
2073 int error_code,
2074 const GURL& url,
2075 bool showing_repost_interstitial) {
2076 if (net::ERR_ABORTED == error_code) {
2077 // EVIL HACK ALERT! Ignore failed loads when we're showing interstitials.
2078 // This means that the interstitial won't be torn down properly, which is
2079 // bad. But if we have an interstitial, go back to another tab type, and
2080 // then load the same interstitial again, we could end up getting the first
2081 // interstitial's "failed" message (as a result of the cancel) when we're on
2082 // the second one.
2083 //
2084 // We can't tell this apart, so we think we're tearing down the current page
2085 // which will cause a crash later one. There is also some code in
2086 // RenderViewHostManager::RendererAbortedProvisionalLoad that is commented
2087 // out because of this problem.
2088 //
2089 // http://code.google.com/p/chromium/issues/detail?id=2855
2090 // Because this will not tear down the interstitial properly, if "back" is
2091 // back to another tab type, the interstitial will still be somewhat alive
2092 // in the previous tab type. If you navigate somewhere that activates the
2093 // tab with the interstitial again, you'll see a flash before the new load
2094 // commits of the interstitial page.
2095 if (showing_interstitial_page()) {
2096 LOG(WARNING) << "Discarding message during interstitial.";
2097 return;
2098 }
2099
2100 // This will discard our pending entry if we cancelled the load (e.g., if we
2101 // decided to download the file instead of load it). Only discard the
2102 // pending entry if the URLs match, otherwise the user initiated a navigate
2103 // before the page loaded so that the discard would discard the wrong entry.
2104 NavigationEntry* pending_entry = controller_.pending_entry();
2105 if (pending_entry && pending_entry->url() == url) {
2106 controller_.DiscardNonCommittedEntries();
2107 // Update the URL display.
2108 NotifyNavigationStateChanged(TabContents::INVALIDATE_URL);
2109 }
2110
2111 render_manager_.RendererAbortedProvisionalLoad(render_view_host);
2112 }
2113
2114 // Send out a notification that we failed a provisional load with an error.
2115 ProvisionalLoadDetails details(is_main_frame,
2116 controller_.IsURLInPageNavigation(url),
2117 url, std::string(), false);
2118 details.set_error_code(error_code);
2119
2120 NotificationService::current()->Notify(
2121 NotificationType::FAIL_PROVISIONAL_LOAD_WITH_ERROR,
2122 Source<NavigationController>(&controller_),
2123 Details<ProvisionalLoadDetails>(&details));
2124}
2125
2126void TabContents::DocumentLoadedInFrame() {
2127 controller_.DocumentLoadedInFrame();
2128}
2129
[email protected]9fb83e82010-07-02 18:24:552130void TabContents::OnContentSettingsChange() {
[email protected]614c0e12010-03-25 10:12:562131 if (delegate_)
[email protected]20ad6a92010-04-21 14:07:242132 delegate_->OnContentSettingsChange(this);
[email protected]614c0e12010-03-25 10:12:562133}
2134
[email protected]8d3347f2009-07-09 22:00:212135RenderViewHostDelegate::View* TabContents::GetViewDelegate() {
[email protected]420ae012009-04-24 05:16:322136 return view_.get();
2137}
2138
[email protected]8d3347f2009-07-09 22:00:212139RenderViewHostDelegate::RendererManagement*
2140TabContents::GetRendererManagementDelegate() {
2141 return &render_manager_;
2142}
2143
[email protected]bf5c2ff392009-07-08 16:24:332144RenderViewHostDelegate::BrowserIntegration*
[email protected]8d3347f2009-07-09 22:00:212145 TabContents::GetBrowserIntegrationDelegate() {
2146 return this;
[email protected]bf5c2ff392009-07-08 16:24:332147}
2148
[email protected]8d3347f2009-07-09 22:00:212149RenderViewHostDelegate::Resource* TabContents::GetResourceDelegate() {
2150 return this;
[email protected]bf5c2ff392009-07-08 16:24:332151}
2152
[email protected]46ba9082010-06-22 10:00:582153RenderViewHostDelegate::ContentSettings*
2154TabContents::GetContentSettingsDelegate() {
[email protected]9fb83e82010-07-02 18:24:552155 return content_settings_delegate_.get();
[email protected]46ba9082010-06-22 10:00:582156}
2157
[email protected]8d3347f2009-07-09 22:00:212158RenderViewHostDelegate::Save* TabContents::GetSaveDelegate() {
[email protected]420ae012009-04-24 05:16:322159 return save_package_.get(); // May be NULL, but we can return NULL.
2160}
2161
[email protected]8d3347f2009-07-09 22:00:212162RenderViewHostDelegate::Printing* TabContents::GetPrintingDelegate() {
[email protected]12802702010-07-09 19:43:092163 return printing_.get();
[email protected]8d3347f2009-07-09 22:00:212164}
2165
2166RenderViewHostDelegate::FavIcon* TabContents::GetFavIconDelegate() {
2167 return &fav_icon_helper_;
2168}
2169
[email protected]e8dbd674b2010-03-11 18:34:402170RenderViewHostDelegate::Autocomplete* TabContents::GetAutocompleteDelegate() {
[email protected]6db0def92010-03-02 19:55:452171 if (autocomplete_history_manager_.get() == NULL)
2172 autocomplete_history_manager_.reset(new AutocompleteHistoryManager(this));
2173 return autocomplete_history_manager_.get();
[email protected]bf5c2ff392009-07-08 16:24:332174}
2175
[email protected]21674df2009-10-28 19:51:212176RenderViewHostDelegate::AutoFill* TabContents::GetAutoFillDelegate() {
[email protected]cf28ec52010-04-09 22:56:342177 return GetAutoFillManager();
[email protected]21674df2009-10-28 19:51:212178}
2179
[email protected]fd571bb2010-03-19 01:23:542180AutomationResourceRoutingDelegate*
2181TabContents::GetAutomationResourceRoutingDelegate() {
2182 return delegate();
2183}
2184
[email protected]ced90ae12010-02-20 02:06:162185RenderViewHostDelegate::BookmarkDrag* TabContents::GetBookmarkDragDelegate() {
2186 return bookmark_drag_;
2187}
2188
2189void TabContents::SetBookmarkDragDelegate(
2190 RenderViewHostDelegate::BookmarkDrag* bookmark_drag) {
2191 bookmark_drag_ = bookmark_drag;
2192}
2193
[email protected]9fb83e82010-07-02 18:24:552194TabSpecificContentSettings* TabContents::GetTabSpecificContentSettings() const {
2195 return content_settings_delegate_.get();
2196}
2197
[email protected]93623c5d2009-12-10 21:40:322198RendererPreferences TabContents::GetRendererPrefs(Profile* profile) const {
[email protected]840b1512009-07-21 16:53:462199 return renderer_preferences_;
[email protected]80d96fa2009-06-10 22:34:512200}
2201
[email protected]57c6a652009-05-04 07:58:342202TabContents* TabContents::GetAsTabContents() {
2203 return this;
2204}
2205
[email protected]7b291f92009-08-14 05:43:532206ViewType::Type TabContents::GetRenderViewType() const {
2207 return ViewType::TAB_CONTENTS;
2208}
2209
2210int TabContents::GetBrowserWindowID() const {
2211 return controller().window_id().id();
2212}
2213
[email protected]420ae012009-04-24 05:16:322214void TabContents::RenderViewCreated(RenderViewHost* render_view_host) {
[email protected]2e4633c2009-07-09 16:58:062215 NotificationService::current()->Notify(
2216 NotificationType::RENDER_VIEW_HOST_CREATED_FOR_TAB,
2217 Source<TabContents>(this),
2218 Details<RenderViewHost>(render_view_host));
[email protected]420ae012009-04-24 05:16:322219 NavigationEntry* entry = controller_.GetActiveEntry();
2220 if (!entry)
2221 return;
2222
2223 // When we're creating views, we're still doing initial setup, so we always
2224 // use the pending DOM UI rather than any possibly existing committed one.
2225 if (render_manager_.pending_dom_ui()) {
2226 render_manager_.pending_dom_ui()->RenderViewCreated(render_view_host);
2227 }
2228
2229 if (entry->IsViewSourceMode()) {
2230 // Put the renderer in view source mode.
2231 render_view_host->Send(
2232 new ViewMsg_EnableViewSourceMode(render_view_host->routing_id()));
2233 }
[email protected]0666aef2009-05-13 19:48:082234
2235 view()->RenderViewCreated(render_view_host);
[email protected]420ae012009-04-24 05:16:322236}
2237
2238void TabContents::RenderViewReady(RenderViewHost* rvh) {
2239 if (rvh != render_view_host()) {
2240 // Don't notify the world, since this came from a renderer in the
2241 // background.
2242 return;
2243 }
2244
2245 NotifyConnected();
[email protected]9d8a4642009-07-29 17:25:302246 bool was_crashed = is_crashed();
[email protected]420ae012009-04-24 05:16:322247 SetIsCrashed(false);
[email protected]9d8a4642009-07-29 17:25:302248
2249 // Restore the focus to the tab (otherwise the focus will be on the top
2250 // window).
2251 if (was_crashed && !FocusLocationBarByDefault())
2252 Focus();
[email protected]420ae012009-04-24 05:16:322253}
2254
2255void TabContents::RenderViewGone(RenderViewHost* rvh) {
2256 // Ask the print preview if this renderer was valuable.
[email protected]12802702010-07-09 19:43:092257 if (!printing_->OnRenderViewGone(rvh))
[email protected]420ae012009-04-24 05:16:322258 return;
2259 if (rvh != render_view_host()) {
2260 // The pending page's RenderViewHost is gone.
2261 return;
2262 }
2263
2264 SetIsLoading(false, NULL);
2265 NotifyDisconnected();
2266 SetIsCrashed(true);
2267
[email protected]ff9efc62010-02-18 22:10:542268 // Remove all infobars.
[email protected]e5430c92010-02-18 22:39:212269 for (int i = infobar_delegate_count() - 1; i >=0 ; --i)
[email protected]ff9efc62010-02-18 22:10:542270 RemoveInfoBar(GetInfoBarDelegateAt(i));
2271
[email protected]8e537302009-06-10 21:19:582272 // Tell the view that we've crashed so it can prepare the sad tab page.
[email protected]cf68b092010-01-15 20:05:552273 // Only do this if we're not in browser shutdown, so that TabContents
2274 // objects that are not in a browser (e.g., HTML dialogs) and thus are
2275 // visible do not flash a sad tab page.
2276 if (browser_shutdown::GetShutdownType() == browser_shutdown::NOT_VALID)
2277 view_->OnTabCrashed();
[email protected]420ae012009-04-24 05:16:322278
2279 // Hide any visible hung renderer warning for this web contents' process.
[email protected]e7b8d832009-11-20 22:41:002280 hung_renderer_dialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:322281}
2282
[email protected]2e4633c2009-07-09 16:58:062283void TabContents::RenderViewDeleted(RenderViewHost* rvh) {
2284 NotificationService::current()->Notify(
2285 NotificationType::RENDER_VIEW_HOST_DELETED,
2286 Source<TabContents>(this),
2287 Details<RenderViewHost>(rvh));
[email protected]fb7b79f72009-11-06 18:00:482288 render_manager_.RenderViewDeleted(rvh);
[email protected]2e4633c2009-07-09 16:58:062289}
2290
[email protected]420ae012009-04-24 05:16:322291void TabContents::DidNavigate(RenderViewHost* rvh,
2292 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]8030f012009-09-25 18:09:372293 int extra_invalidate_flags = 0;
2294
2295 if (PageTransition::IsMainFrame(params.transition)) {
[email protected]20ede072009-10-06 00:28:352296 bool was_bookmark_bar_visible = ShouldShowBookmarkBar();
[email protected]8030f012009-09-25 18:09:372297 bool was_extension_shelf_visible = IsExtensionShelfAlwaysVisible();
2298
[email protected]420ae012009-04-24 05:16:322299 render_manager_.DidNavigateMainFrame(rvh);
2300
[email protected]20ede072009-10-06 00:28:352301 if (was_bookmark_bar_visible != ShouldShowBookmarkBar())
[email protected]8030f012009-09-25 18:09:372302 extra_invalidate_flags |= INVALIDATE_BOOKMARK_BAR;
2303 if (was_extension_shelf_visible != IsExtensionShelfAlwaysVisible())
2304 extra_invalidate_flags |= INVALIDATE_EXTENSION_SHELF;
2305 }
2306
[email protected]420ae012009-04-24 05:16:322307 // Update the site of the SiteInstance if it doesn't have one yet.
2308 if (!GetSiteInstance()->has_site())
2309 GetSiteInstance()->SetSite(params.url);
2310
2311 // Need to update MIME type here because it's referred to in
2312 // UpdateNavigationCommands() called by RendererDidNavigate() to
2313 // determine whether or not to enable the encoding menu.
2314 // It's updated only for the main frame. For a subframe,
2315 // RenderView::UpdateURL does not set params.contents_mime_type.
2316 // (see http://code.google.com/p/chromium/issues/detail?id=2929 )
2317 // TODO(jungshik): Add a test for the encoding menu to avoid
2318 // regressing it again.
2319 if (PageTransition::IsMainFrame(params.transition))
2320 contents_mime_type_ = params.contents_mime_type;
2321
2322 NavigationController::LoadCommittedDetails details;
[email protected]8030f012009-09-25 18:09:372323 bool did_navigate = controller_.RendererDidNavigate(
2324 params, extra_invalidate_flags, &details);
[email protected]3e90d4a2009-07-03 17:38:392325
2326 // Update history. Note that this needs to happen after the entry is complete,
2327 // which WillNavigate[Main,Sub]Frame will do before this function is called.
[email protected]2e29d922009-10-22 22:13:352328 if (params.should_update_history &&
2329 (!delegate() || delegate()->ShouldAddNavigationsToHistory())) {
[email protected]3e90d4a2009-07-03 17:38:392330 // Most of the time, the displayURL matches the loaded URL, but for about:
2331 // URLs, we use a data: URL as the real value. We actually want to save
2332 // the about: URL to the history db and keep the data: URL hidden. This is
2333 // what the TabContents' URL getter does.
2334 UpdateHistoryForNavigation(GetURL(), details, params);
2335 }
2336
[email protected]76543b92009-08-31 17:27:452337 if (!did_navigate)
2338 return; // No navigation happened.
[email protected]420ae012009-04-24 05:16:322339
2340 // DO NOT ADD MORE STUFF TO THIS FUNCTION! Your component should either listen
2341 // for the appropriate notification (best) or you can add it to
2342 // DidNavigateMainFramePostCommit / DidNavigateAnyFramePostCommit (only if
2343 // necessary, please).
2344
2345 // Run post-commit tasks.
2346 if (details.is_main_frame)
2347 DidNavigateMainFramePostCommit(details, params);
2348 DidNavigateAnyFramePostCommit(rvh, details, params);
2349}
2350
2351void TabContents::UpdateState(RenderViewHost* rvh,
2352 int32 page_id,
2353 const std::string& state) {
2354 DCHECK(rvh == render_view_host());
2355
2356 // We must be prepared to handle state updates for any page, these occur
2357 // when the user is scrolling and entering form data, as well as when we're
2358 // leaving a page, in which case our state may have already been moved to
2359 // the next page. The navigation controller will look up the appropriate
2360 // NavigationEntry and update it when it is notified via the delegate.
2361
2362 int entry_index = controller_.GetEntryIndexWithPageID(
2363 GetSiteInstance(), page_id);
2364 if (entry_index < 0)
2365 return;
2366 NavigationEntry* entry = controller_.GetEntryAtIndex(entry_index);
2367
2368 if (state == entry->content_state())
2369 return; // Nothing to update.
2370 entry->set_content_state(state);
2371 controller_.NotifyEntryChanged(entry, entry_index);
2372}
2373
2374void TabContents::UpdateTitle(RenderViewHost* rvh,
2375 int32 page_id, const std::wstring& title) {
2376 // If we have a title, that's a pretty good indication that we've started
2377 // getting useful data.
2378 SetNotWaitingForResponse();
2379
2380 DCHECK(rvh == render_view_host());
2381 NavigationEntry* entry = controller_.GetEntryWithPageID(GetSiteInstance(),
2382 page_id);
2383 if (!entry || !UpdateTitleForEntry(entry, title))
2384 return;
2385
2386 // Broadcast notifications when the UI should be updated.
2387 if (entry == controller_.GetEntryAtOffset(0))
[email protected]f1cd5e82009-10-23 17:19:032388 NotifyNavigationStateChanged(INVALIDATE_TITLE);
[email protected]420ae012009-04-24 05:16:322389}
2390
[email protected]420ae012009-04-24 05:16:322391void TabContents::UpdateEncoding(RenderViewHost* render_view_host,
[email protected]41fc0322009-09-04 22:23:402392 const std::string& encoding) {
[email protected]420ae012009-04-24 05:16:322393 set_encoding(encoding);
2394}
2395
2396void TabContents::UpdateTargetURL(int32 page_id, const GURL& url) {
2397 if (delegate())
2398 delegate()->UpdateTargetURL(this, url);
2399}
2400
2401void TabContents::UpdateThumbnail(const GURL& url,
2402 const SkBitmap& bitmap,
2403 const ThumbnailScore& score) {
2404 // Tell History about this thumbnail
[email protected]523623c2010-05-19 16:03:422405 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kTopSites)) {
[email protected]72d0fc72009-07-09 17:30:492406 if (!profile()->IsOffTheRecord())
[email protected]523623c2010-05-19 16:03:422407 profile()->GetTopSites()->SetPageThumbnail(url, bitmap, score);
[email protected]449478302009-06-09 20:04:282408 } else {
2409 HistoryService* hs;
2410 if (!profile()->IsOffTheRecord() &&
2411 (hs = profile()->GetHistoryService(Profile::IMPLICIT_ACCESS))) {
2412 hs->SetPageThumbnail(url, bitmap, score);
2413 }
[email protected]420ae012009-04-24 05:16:322414 }
2415}
2416
[email protected]c4e98902010-06-01 10:20:142417void TabContents::UpdateInspectorSetting(const std::string& key,
2418 const std::string& value) {
2419 DictionaryValue* inspector_settings =
2420 profile()->GetPrefs()->GetMutableDictionary(
2421 prefs::kWebKitInspectorSettings);
[email protected]628cf5a52010-06-07 14:25:332422 inspector_settings->SetWithoutPathExpansion(UTF8ToWide(key),
2423 Value::CreateStringValue(value));
[email protected]0df30122009-06-03 12:13:082424}
2425
[email protected]45aafc92010-06-07 14:18:532426void TabContents::ClearInspectorSettings() {
2427 DictionaryValue* inspector_settings =
2428 profile()->GetPrefs()->GetMutableDictionary(
2429 prefs::kWebKitInspectorSettings);
2430 inspector_settings->Clear();
2431}
2432
[email protected]420ae012009-04-24 05:16:322433void TabContents::Close(RenderViewHost* rvh) {
[email protected]07707302009-11-06 00:50:292434 // The UI may be in an event-tracking loop, such as between the
2435 // mouse-down and mouse-up in text selection or a button click.
2436 // Defer the close until after tracking is complete, so that we
2437 // don't free objects out from under the UI.
2438 // TODO(shess): This could probably be integrated with the
2439 // IsDoingDrag() test below. Punting for now because I need more
2440 // research to understand how this impacts platforms other than Mac.
2441 // TODO(shess): This could get more fine-grained. For instance,
2442 // closing a tab in another window while selecting text in the
2443 // current window's Omnibox should be just fine.
2444 if (view()->IsEventTracking()) {
2445 view()->CloseTabAfterEventTracking();
2446 return;
2447 }
2448
[email protected]24a4d1062009-07-10 23:10:422449 // If we close the tab while we're in the middle of a drag, we'll crash.
2450 // Instead, cancel the drag and close it as soon as the drag ends.
2451 if (view()->IsDoingDrag()) {
2452 view()->CancelDragAndCloseTab();
2453 return;
2454 }
2455
[email protected]420ae012009-04-24 05:16:322456 // Ignore this if it comes from a RenderViewHost that we aren't showing.
2457 if (delegate() && rvh == render_view_host())
2458 delegate()->CloseContents(this);
2459}
2460
2461void TabContents::RequestMove(const gfx::Rect& new_bounds) {
2462 if (delegate() && delegate()->IsPopup(this))
2463 delegate()->MoveContents(this, new_bounds);
2464}
2465
[email protected]7ab1e7d62009-10-14 23:32:012466void TabContents::DidStartLoading() {
[email protected]420ae012009-04-24 05:16:322467 SetIsLoading(true, NULL);
2468}
2469
[email protected]7ab1e7d62009-10-14 23:32:012470void TabContents::DidStopLoading() {
[email protected]420ae012009-04-24 05:16:322471 scoped_ptr<LoadNotificationDetails> details;
2472
2473 NavigationEntry* entry = controller_.GetActiveEntry();
2474 // An entry may not exist for a stop when loading an initial blank page or
2475 // if an iframe injected by script into a blank page finishes loading.
2476 if (entry) {
[email protected]420ae012009-04-24 05:16:322477 base::TimeDelta elapsed = base::TimeTicks::Now() - current_load_start_;
2478
2479 details.reset(new LoadNotificationDetails(
[email protected]ebe89e062009-08-13 23:16:542480 entry->virtual_url(),
[email protected]420ae012009-04-24 05:16:322481 entry->transition_type(),
2482 elapsed,
2483 &controller_,
2484 controller_.GetCurrentEntryIndex()));
2485 }
2486
2487 // Tell PasswordManager we've finished a page load, which serves as a
2488 // green light to save pending passwords and reset itself.
2489 GetPasswordManager()->DidStopLoading();
2490
2491 SetIsLoading(false, details.get());
2492}
2493
[email protected]420ae012009-04-24 05:16:322494void TabContents::DidRedirectProvisionalLoad(int32 page_id,
2495 const GURL& source_url,
2496 const GURL& target_url) {
2497 NavigationEntry* entry;
2498 if (page_id == -1)
2499 entry = controller_.pending_entry();
2500 else
2501 entry = controller_.GetEntryWithPageID(GetSiteInstance(), page_id);
2502 if (!entry || entry->url() != source_url)
2503 return;
2504 entry->set_url(target_url);
2505}
2506
[email protected]420ae012009-04-24 05:16:322507void TabContents::RequestOpenURL(const GURL& url, const GURL& referrer,
2508 WindowOpenDisposition disposition) {
2509 if (render_manager_.dom_ui()) {
2510 // When we're a DOM UI, it will provide a page transition type for us (this
2511 // is so the new tab page can specify AUTO_BOOKMARK for automatically
2512 // generated suggestions).
2513 //
2514 // Note also that we hide the referrer for DOM UI pages. We don't really
[email protected]60e448982009-05-06 04:21:162515 // want web sites to see a referrer of "chrome://blah" (and some
2516 // chrome: URLs might have search terms or other stuff we don't want to
[email protected]420ae012009-04-24 05:16:322517 // send to the site), so we send no referrer.
2518 OpenURL(url, GURL(), disposition,
2519 render_manager_.dom_ui()->link_transition_type());
2520 } else {
2521 OpenURL(url, referrer, disposition, PageTransition::LINK);
2522 }
2523}
2524
[email protected]1ee614862009-07-06 20:29:182525void TabContents::DomOperationResponse(const std::string& json_string,
2526 int automation_id) {
2527}
2528
[email protected]420ae012009-04-24 05:16:322529void TabContents::ProcessDOMUIMessage(const std::string& message,
[email protected]438c97d2010-05-21 23:30:152530 const ListValue* content,
[email protected]bb64b512010-04-02 21:01:392531 const GURL& source_url,
[email protected]811bfe32009-07-01 08:46:252532 int request_id,
2533 bool has_callback) {
[email protected]420ae012009-04-24 05:16:322534 if (!render_manager_.dom_ui()) {
[email protected]c2e74fe82009-09-03 17:57:442535 // This can happen if someone uses window.open() to open an extension URL
2536 // from a non-extension context.
2537 render_view_host()->BlockExtensionRequest(request_id);
[email protected]420ae012009-04-24 05:16:322538 return;
2539 }
[email protected]bb64b512010-04-02 21:01:392540 render_manager_.dom_ui()->ProcessDOMUIMessage(message, content, source_url,
2541 request_id,
[email protected]811bfe32009-07-01 08:46:252542 has_callback);
[email protected]420ae012009-04-24 05:16:322543}
2544
2545void TabContents::ProcessExternalHostMessage(const std::string& message,
2546 const std::string& origin,
2547 const std::string& target) {
2548 if (delegate())
2549 delegate()->ForwardMessageToExternalHost(message, origin, target);
2550}
2551
[email protected]cdaf8d02010-03-30 19:52:472552void TabContents::RunFileChooser(
2553 const ViewHostMsg_RunFileChooser_Params &params) {
[email protected]420ae012009-04-24 05:16:322554 if (!select_file_dialog_.get())
2555 select_file_dialog_ = SelectFileDialog::Create(this);
[email protected]cdaf8d02010-03-30 19:52:472556
2557 SelectFileDialog::Type dialog_type;
2558 switch (params.mode) {
2559 case ViewHostMsg_RunFileChooser_Params::Open:
2560 dialog_type = SelectFileDialog::SELECT_OPEN_FILE;
2561 break;
2562 case ViewHostMsg_RunFileChooser_Params::OpenMultiple:
2563 dialog_type = SelectFileDialog::SELECT_OPEN_MULTI_FILE;
2564 break;
2565 case ViewHostMsg_RunFileChooser_Params::Save:
2566 dialog_type = SelectFileDialog::SELECT_SAVEAS_FILE;
2567 break;
2568 default:
[email protected]bed31952010-03-30 20:07:592569 dialog_type = SelectFileDialog::SELECT_OPEN_FILE; // Prevent warning.
[email protected]cdaf8d02010-03-30 19:52:472570 NOTREACHED();
2571 }
[email protected]35896a32010-06-09 08:42:512572 FilePath default_file_name = params.default_file_name;
2573 if (default_file_name.empty())
2574 default_file_name = profile()->last_selected_directory();
[email protected]cdaf8d02010-03-30 19:52:472575 select_file_dialog_->SelectFile(dialog_type, params.title,
[email protected]35896a32010-06-09 08:42:512576 default_file_name,
[email protected]420ae012009-04-24 05:16:322577 NULL, 0, FILE_PATH_LITERAL(""),
2578 view_->GetTopLevelNativeWindow(), NULL);
2579}
2580
2581void TabContents::RunJavaScriptMessage(
2582 const std::wstring& message,
2583 const std::wstring& default_prompt,
2584 const GURL& frame_url,
2585 const int flags,
2586 IPC::Message* reply_msg,
2587 bool* did_suppress_message) {
2588 // Suppress javascript messages when requested and when inside a constrained
2589 // popup window (because that activates them and breaks them out of the
2590 // constrained window jail).
[email protected]91ed49a2009-07-07 21:29:552591 // Also suppress messages when showing an interstitial. The interstitial is
2592 // shown over the previous page, we don't want the hidden page dialogs to
2593 // interfere with the interstitial.
2594 bool suppress_this_message = suppress_javascript_messages_ ||
2595 showing_interstitial_page();
[email protected]420ae012009-04-24 05:16:322596 if (delegate())
2597 suppress_this_message |=
[email protected]b6c874582009-05-08 19:38:312598 (delegate()->GetConstrainingContents(this) != this);
[email protected]420ae012009-04-24 05:16:322599
2600 *did_suppress_message = suppress_this_message;
2601
2602 if (!suppress_this_message) {
2603 base::TimeDelta time_since_last_message(
2604 base::TimeTicks::Now() - last_javascript_message_dismissal_);
2605 bool show_suppress_checkbox = false;
2606 // Show a checkbox offering to suppress further messages if this message is
2607 // being displayed within kJavascriptMessageExpectedDelay of the last one.
2608 if (time_since_last_message <
2609 base::TimeDelta::FromMilliseconds(kJavascriptMessageExpectedDelay))
2610 show_suppress_checkbox = true;
2611
[email protected]d82ed61e2009-06-16 02:46:222612 RunJavascriptMessageBox(this, frame_url, flags, message, default_prompt,
[email protected]420ae012009-04-24 05:16:322613 show_suppress_checkbox, reply_msg);
2614 } else {
2615 // If we are suppressing messages, just reply as is if the user immediately
2616 // pressed "Cancel".
[email protected]beb440c2009-11-06 04:08:542617 OnMessageBoxClosed(reply_msg, false, std::wstring());
[email protected]420ae012009-04-24 05:16:322618 }
2619}
2620
2621void TabContents::RunBeforeUnloadConfirm(const std::wstring& message,
2622 IPC::Message* reply_msg) {
[email protected]7ab1e7d62009-10-14 23:32:012623 is_showing_before_unload_dialog_ = true;
[email protected]d82ed61e2009-06-16 02:46:222624 RunBeforeUnloadDialog(this, message, reply_msg);
[email protected]420ae012009-04-24 05:16:322625}
2626
2627void TabContents::ShowModalHTMLDialog(const GURL& url, int width, int height,
2628 const std::string& json_arguments,
2629 IPC::Message* reply_msg) {
2630 if (delegate()) {
2631 HtmlDialogUIDelegate* dialog_delegate =
2632 new ModalHtmlDialogDelegate(url, width, height, json_arguments,
[email protected]d82ed61e2009-06-16 02:46:222633 reply_msg, this);
[email protected]420ae012009-04-24 05:16:322634 delegate()->ShowHtmlDialog(dialog_delegate, NULL);
2635 }
2636}
2637
[email protected]4d44a1c2010-04-28 19:20:412638void TabContents::PasswordFormsFound(
[email protected]ed3fb032009-06-16 19:50:562639 const std::vector<webkit_glue::PasswordForm>& forms) {
[email protected]4d44a1c2010-04-28 19:20:412640 GetPasswordManager()->PasswordFormsFound(forms);
2641}
2642
2643void TabContents::PasswordFormsVisible(
2644 const std::vector<webkit_glue::PasswordForm>& visible_forms) {
2645 GetPasswordManager()->PasswordFormsVisible(visible_forms);
[email protected]420ae012009-04-24 05:16:322646}
2647
[email protected]420ae012009-04-24 05:16:322648// Checks to see if we should generate a keyword based on the OSDD, and if
2649// necessary uses TemplateURLFetcher to download the OSDD and create a keyword.
2650void TabContents::PageHasOSDD(RenderViewHost* render_view_host,
2651 int32 page_id, const GURL& url,
2652 bool autodetected) {
2653 // Make sure page_id is the current page, and the TemplateURLModel is loaded.
2654 DCHECK(url.is_valid());
2655 if (!IsActiveEntry(page_id))
2656 return;
2657 TemplateURLModel* url_model = profile()->GetTemplateURLModel();
2658 if (!url_model)
2659 return;
2660 if (!url_model->loaded()) {
2661 url_model->Load();
2662 return;
2663 }
2664 if (!profile()->GetTemplateURLFetcher())
2665 return;
2666
2667 if (profile()->IsOffTheRecord())
2668 return;
2669
2670 const NavigationEntry* entry = controller_.GetLastCommittedEntry();
2671 DCHECK(entry);
2672
2673 const NavigationEntry* base_entry = entry;
2674 if (IsFormSubmit(base_entry)) {
2675 // If the current page is a form submit, find the last page that was not
2676 // a form submit and use its url to generate the keyword from.
2677 int index = controller_.last_committed_entry_index() - 1;
2678 while (index >= 0 && IsFormSubmit(controller_.GetEntryAtIndex(index)))
2679 index--;
2680 if (index >= 0)
2681 base_entry = controller_.GetEntryAtIndex(index);
2682 else
2683 base_entry = NULL;
2684 }
2685
2686 // We want to use the user typed URL if available since that represents what
2687 // the user typed to get here, and fall back on the regular URL if not.
2688 if (!base_entry)
2689 return;
2690 GURL keyword_url = base_entry->user_typed_url().is_valid() ?
2691 base_entry->user_typed_url() : base_entry->url();
2692 if (!keyword_url.is_valid())
2693 return;
2694 std::wstring keyword = TemplateURLModel::GenerateKeyword(keyword_url,
2695 autodetected);
[email protected]a88dbdd52010-01-15 19:25:532696
2697 // For JS added OSDD empty keyword is OK because we will generate keyword
2698 // later from OSDD content.
2699 if (autodetected && keyword.empty())
[email protected]420ae012009-04-24 05:16:322700 return;
2701 const TemplateURL* template_url =
2702 url_model->GetTemplateURLForKeyword(keyword);
2703 if (template_url && (!template_url->safe_for_autoreplace() ||
2704 template_url->originating_url() == url)) {
2705 // Either there is a user created TemplateURL for this keyword, or the
2706 // keyword has the same OSDD url and we've parsed it.
2707 return;
2708 }
2709
2710 // Download the OpenSearch description document. If this is successful a
2711 // new keyword will be created when done.
[email protected]420ae012009-04-24 05:16:322712 profile()->GetTemplateURLFetcher()->ScheduleDownload(
2713 keyword,
2714 url,
2715 base_entry->favicon().url(),
[email protected]d0cc9fb52009-06-23 02:26:162716 this,
[email protected]420ae012009-04-24 05:16:322717 autodetected);
2718}
2719
[email protected]155f35e2010-07-17 00:30:182720// Indicates if the two inputs have the same security origin.
2721// |requested_origin| should only be a security origin (no path, etc.).
2722// It is ok if |template_url| is NULL.
2723static bool IsSameOrigin(const GURL& requested_origin,
2724 const TemplateURL* template_url) {
2725 DCHECK(requested_origin == requested_origin.GetOrigin());
2726 return template_url && requested_origin ==
2727 TemplateURLModel::GenerateSearchURL(template_url).GetOrigin();
2728}
2729
2730ViewHostMsg_GetSearchProviderInstallState_Params
2731 TabContents::GetSearchProviderInstallState(const GURL& requested_host) {
2732 // Get the last committed entry since that is the page executing the
2733 // javascript as opposed to a page being navigated to. We don't want
2734 // to trust the page to tell us the url to avoid using potentially
2735 // compromised information.
2736 NavigationEntry* entry = controller_.GetLastCommittedEntry();
2737 GURL page_origin = entry ? entry->virtual_url().GetOrigin() : GURL();
2738 GURL requested_origin = requested_host.GetOrigin();
2739 // Do the security check before any others to avoid information leaks.
2740 if (page_origin != requested_origin)
2741 return ViewHostMsg_GetSearchProviderInstallState_Params::Denied();
2742
2743 // In incognito mode, no search information is exposed. (This check must be
2744 // done after the security check or else a web site can detect that the
2745 // user is in incognito mode just by doing a cross origin request.)
2746 if (profile()->IsOffTheRecord())
2747 return ViewHostMsg_GetSearchProviderInstallState_Params::NotInstalled();
2748
2749 TemplateURLModel* url_model = profile()->GetTemplateURLModel();
2750 if (!url_model)
2751 return ViewHostMsg_GetSearchProviderInstallState_Params::NotInstalled();
2752 if (!url_model->loaded())
2753 url_model->Load();
2754
2755 // First check to see if the url is the default search provider.
2756 if (IsSameOrigin(requested_origin, url_model->GetDefaultSearchProvider())) {
2757 return ViewHostMsg_GetSearchProviderInstallState_Params::
2758 InstalledAsDefault();
2759 }
2760
2761 // Is the url any search provider?
2762 std::vector<const TemplateURL*> urls = url_model->GetTemplateURLs();
2763 for (std::vector<const TemplateURL*>::iterator i = urls.begin();
2764 i != urls.end(); ++i) {
2765 const TemplateURL* template_url = (*i);
2766 if (IsSameOrigin(requested_origin, template_url)) {
2767 return ViewHostMsg_GetSearchProviderInstallState_Params::
2768 InstallButNotDefault();
2769 }
2770 }
2771 return ViewHostMsg_GetSearchProviderInstallState_Params::NotInstalled();
2772}
2773
[email protected]420ae012009-04-24 05:16:322774GURL TabContents::GetAlternateErrorPageURL() const {
2775 GURL url;
2776 // Disable alternate error pages when in OffTheRecord/Incognito mode.
2777 if (profile()->IsOffTheRecord())
2778 return url;
2779
2780 PrefService* prefs = profile()->GetPrefs();
2781 DCHECK(prefs);
2782 if (prefs->GetBoolean(prefs::kAlternateErrorPagesEnabled)) {
[email protected]a33457b82009-09-21 23:07:152783 url = google_util::AppendGoogleLocaleParam(
2784 GURL(google_util::kLinkDoctorBaseURL));
[email protected]420ae012009-04-24 05:16:322785 url = google_util::AppendGoogleTLDParam(url);
2786 }
2787 return url;
2788}
2789
2790WebPreferences TabContents::GetWebkitPrefs() {
[email protected]6c32ce72010-03-08 05:18:062791 Profile* profile = render_view_host()->process()->profile();
[email protected]7ccddb8c2009-08-04 17:36:552792 bool is_dom_ui = false;
[email protected]6c32ce72010-03-08 05:18:062793 return RenderViewHostDelegateHelper::GetWebkitPrefs(profile, is_dom_ui);
[email protected]420ae012009-04-24 05:16:322794}
2795
[email protected]fa1cf0b82010-01-15 21:49:442796void TabContents::OnIgnoredUIEvent() {
2797 if (constrained_window_count()) {
2798 ConstrainedWindow* window = *constrained_window_begin();
2799 window->FocusConstrainedWindow();
2800 }
2801}
2802
[email protected]420ae012009-04-24 05:16:322803void TabContents::OnJSOutOfMemory() {
2804 AddInfoBar(new SimpleAlertInfoBarDelegate(
[email protected]938e1f92010-04-01 18:09:422805 this, l10n_util::GetString(IDS_JS_OUT_OF_MEMORY_PROMPT), NULL, true));
[email protected]420ae012009-04-24 05:16:322806}
2807
[email protected]420ae012009-04-24 05:16:322808void TabContents::OnCrossSiteResponse(int new_render_process_host_id,
2809 int new_request_id) {
[email protected]57c6a652009-05-04 07:58:342810 // Allows the TabContents to react when a cross-site response is ready to be
[email protected]420ae012009-04-24 05:16:322811 // delivered to a pending RenderViewHost. We must first run the onunload
2812 // handler of the old RenderViewHost before we can allow it to proceed.
2813 render_manager_.OnCrossSiteResponse(new_render_process_host_id,
2814 new_request_id);
2815}
2816
[email protected]420ae012009-04-24 05:16:322817gfx::Rect TabContents::GetRootWindowResizerRect() const {
2818 if (delegate())
2819 return delegate()->GetRootWindowResizerRect();
2820 return gfx::Rect();
2821}
2822
2823void TabContents::RendererUnresponsive(RenderViewHost* rvh,
2824 bool is_during_unload) {
2825 if (is_during_unload) {
2826 // Hang occurred while firing the beforeunload/unload handler.
2827 // Pretend the handler fired so tab closing continues as if it had.
[email protected]829e7612009-04-25 01:15:112828 rvh->set_sudden_termination_allowed(true);
[email protected]420ae012009-04-24 05:16:322829
2830 if (!render_manager_.ShouldCloseTabOnUnresponsiveRenderer())
2831 return;
2832
2833 // If the tab hangs in the beforeunload/unload handler there's really
2834 // nothing we can do to recover. Pretend the unload listeners have
2835 // all fired and close the tab. If the hang is in the beforeunload handler
2836 // then the user will not have the option of cancelling the close.
2837 Close(rvh);
2838 return;
2839 }
2840
2841 if (render_view_host() && render_view_host()->IsRenderViewLive())
[email protected]e7b8d832009-11-20 22:41:002842 hung_renderer_dialog::ShowForTabContents(this);
[email protected]420ae012009-04-24 05:16:322843}
2844
2845void TabContents::RendererResponsive(RenderViewHost* render_view_host) {
[email protected]e7b8d832009-11-20 22:41:002846 hung_renderer_dialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:322847}
2848
2849void TabContents::LoadStateChanged(const GURL& url,
[email protected]094e5b22009-09-25 04:23:562850 net::LoadState load_state,
2851 uint64 upload_position,
2852 uint64 upload_size) {
[email protected]420ae012009-04-24 05:16:322853 load_state_ = load_state;
[email protected]094e5b22009-09-25 04:23:562854 upload_position_ = upload_position;
2855 upload_size_ = upload_size;
[email protected]d686e812009-06-03 19:10:292856 std::wstring languages =
[email protected]ddd231e2010-06-29 20:35:192857 UTF8ToWide(profile()->GetPrefs()->GetString(prefs::kAcceptLanguages));
[email protected]d686e812009-06-03 19:10:292858 std::string host = url.host();
[email protected]ce85f602009-11-07 01:34:532859 load_state_host_ =
2860 net::IDNToUnicode(host.c_str(), host.size(), languages, NULL);
[email protected]420ae012009-04-24 05:16:322861 if (load_state_ == net::LOAD_STATE_READING_RESPONSE)
2862 SetNotWaitingForResponse();
2863 if (is_loading())
[email protected]c9cd2222009-05-06 05:16:502864 NotifyNavigationStateChanged(INVALIDATE_LOAD | INVALIDATE_TAB);
[email protected]420ae012009-04-24 05:16:322865}
2866
[email protected]1ee614862009-07-06 20:29:182867bool TabContents::IsExternalTabContainer() const {
2868 if (!delegate())
2869 return false;
2870
2871 return delegate()->IsExternalTabContainer();
2872}
2873
[email protected]1ee614862009-07-06 20:29:182874void TabContents::DidInsertCSS() {
2875 // This RVHDelegate function is used for extensions and not us.
[email protected]420ae012009-04-24 05:16:322876}
2877
[email protected]130efb02009-09-18 18:54:352878void TabContents::FocusedNodeChanged() {
2879 NotificationService::current()->Notify(
2880 NotificationType::FOCUS_CHANGED_IN_PAGE,
2881 Source<RenderViewHost>(render_view_host()),
2882 NotificationService::NoDetails());
2883}
2884
[email protected]420ae012009-04-24 05:16:322885void TabContents::FileSelected(const FilePath& path,
2886 int index, void* params) {
[email protected]35896a32010-06-09 08:42:512887 profile()->set_last_selected_directory(path.DirName());
[email protected]cdaf8d02010-03-30 19:52:472888 std::vector<FilePath> files;
2889 files.push_back(path);
2890 render_view_host()->FilesSelectedInChooser(files);
[email protected]420ae012009-04-24 05:16:322891}
2892
2893void TabContents::MultiFilesSelected(const std::vector<FilePath>& files,
2894 void* params) {
[email protected]35896a32010-06-09 08:42:512895 if (!files.empty())
2896 profile()->set_last_selected_directory(files[0].DirName());
[email protected]cdaf8d02010-03-30 19:52:472897 render_view_host()->FilesSelectedInChooser(files);
[email protected]420ae012009-04-24 05:16:322898}
2899
2900void TabContents::FileSelectionCanceled(void* params) {
2901 // If the user cancels choosing a file to upload we pass back an
2902 // empty vector.
[email protected]cdaf8d02010-03-30 19:52:472903 render_view_host()->FilesSelectedInChooser(std::vector<FilePath>());
[email protected]420ae012009-04-24 05:16:322904}
2905
2906void TabContents::BeforeUnloadFiredFromRenderManager(
2907 bool proceed,
2908 bool* proceed_to_fire_unload) {
2909 if (delegate())
2910 delegate()->BeforeUnloadFired(this, proceed, proceed_to_fire_unload);
2911}
2912
[email protected]3a3d47472010-07-15 21:03:542913void TabContents::DidStartLoadingFromRenderManager(
2914 RenderViewHost* render_view_host) {
2915 DidStartLoading();
2916}
2917
2918void TabContents::RenderViewGoneFromRenderManager(
2919 RenderViewHost* render_view_host) {
2920 RenderViewGone(render_view_host);
2921}
2922
[email protected]420ae012009-04-24 05:16:322923void TabContents::UpdateRenderViewSizeForRenderManager() {
2924 // TODO(brettw) this is a hack. See TabContentsView::SizeContents.
[email protected]0323ee42010-02-17 22:03:262925 gfx::Size size = view_->GetContainerSize();
2926 // 0x0 isn't a valid window size (minimal window size is 1x1) but it may be
2927 // here during container initialization and normal window size will be set
2928 // later. In case of tab duplication this resizing to 0x0 prevents setting
2929 // normal size later so just ignore it.
2930 if (!size.IsEmpty())
2931 view_->SizeContents(size);
[email protected]420ae012009-04-24 05:16:322932}
2933
[email protected]3a3d47472010-07-15 21:03:542934void TabContents::NotifySwappedFromRenderManager() {
2935 NotifySwapped();
2936}
2937
2938NavigationController& TabContents::GetControllerForRenderManager() {
2939 return controller();
2940}
2941
[email protected]420ae012009-04-24 05:16:322942DOMUI* TabContents::CreateDOMUIForRenderManager(const GURL& url) {
[email protected]d82ed61e2009-06-16 02:46:222943 return DOMUIFactory::CreateDOMUIForURL(this, url);
[email protected]420ae012009-04-24 05:16:322944}
2945
2946NavigationEntry*
2947TabContents::GetLastCommittedNavigationEntryForRenderManager() {
2948 return controller_.GetLastCommittedEntry();
2949}
2950
2951bool TabContents::CreateRenderViewForRenderManager(
2952 RenderViewHost* render_view_host) {
[email protected]420ae012009-04-24 05:16:322953 RenderWidgetHostView* rwh_view = view_->CreateViewForWidget(render_view_host);
[email protected]1a98a932009-11-17 00:12:522954
2955 scoped_refptr<URLRequestContextGetter> request_context = request_context_;
2956 if (!request_context.get())
2957 request_context = profile()->GetRequestContext();
2958
[email protected]8ab04652010-06-12 02:47:262959 if (!render_view_host->CreateRenderView(request_context, string16()))
[email protected]420ae012009-04-24 05:16:322960 return false;
2961
2962 // Now that the RenderView has been created, we need to tell it its size.
2963 rwh_view->SetSize(view_->GetContainerSize());
2964
2965 UpdateMaxPageIDIfNecessary(render_view_host->site_instance(),
2966 render_view_host);
2967 return true;
2968}
2969
2970void TabContents::Observe(NotificationType type,
2971 const NotificationSource& source,
2972 const NotificationDetails& details) {
2973 switch (type.value) {
2974 case NotificationType::BOOKMARK_MODEL_LOADED:
2975 // BookmarkModel finished loading, fall through to update starred state.
2976 case NotificationType::URLS_STARRED: {
2977 // Somewhere, a URL has been starred.
2978 // Ignore notifications for profiles other than our current one.
2979 Profile* source_profile = Source<Profile>(source).ptr();
2980 if (!source_profile || !source_profile->IsSameProfile(profile()))
2981 return;
2982
2983 UpdateStarredStateForCurrentURL();
2984 break;
2985 }
2986 case NotificationType::PREF_CHANGED: {
2987 std::wstring* pref_name_in = Details<std::wstring>(details).ptr();
2988 DCHECK(Source<PrefService>(source).ptr() == profile()->GetPrefs());
2989 if (*pref_name_in == prefs::kAlternateErrorPagesEnabled) {
2990 UpdateAlternateErrorPageURL();
2991 } else if (*pref_name_in == prefs::kDefaultCharset ||
2992 StartsWithASCII(WideToUTF8(*pref_name_in), "webkit.webprefs.", true)
2993 ) {
2994 UpdateWebPreferences();
2995 } else {
2996 NOTREACHED() << "unexpected pref change notification" << *pref_name_in;
2997 }
2998 break;
2999 }
3000 case NotificationType::RENDER_WIDGET_HOST_DESTROYED:
3001 view_->RenderWidgetHostDestroyed(Source<RenderWidgetHost>(source).ptr());
3002 break;
3003
3004 case NotificationType::NAV_ENTRY_COMMITTED: {
3005 DCHECK(&controller_ == Source<NavigationController>(source).ptr());
3006
3007 NavigationController::LoadCommittedDetails& committed_details =
3008 *(Details<NavigationController::LoadCommittedDetails>(details).ptr());
3009 ExpireInfoBars(committed_details);
3010 break;
3011 }
3012
[email protected]d3ba77272009-09-03 00:06:093013#if defined(OS_LINUX)
3014 case NotificationType::BROWSER_THEME_CHANGED: {
[email protected]7ff431e2010-01-07 18:18:543015 renderer_preferences_util::UpdateFromSystemSettings(
3016 &renderer_preferences_, profile());
[email protected]d3ba77272009-09-03 00:06:093017 render_view_host()->SyncRendererPrefs();
3018 break;
3019 }
3020#endif
3021
[email protected]6c32ce72010-03-08 05:18:063022 case NotificationType::USER_STYLE_SHEET_UPDATED:
3023 UpdateWebPreferences();
3024 break;
3025
[email protected]79580c62010-02-02 02:36:253026 case NotificationType::CONTENT_SETTINGS_CHANGED: {
[email protected]0314ae02010-04-08 09:18:293027 Details<const HostContentSettingsMap::ContentSettingsDetails>
[email protected]79580c62010-02-02 02:36:253028 settings_details(details);
[email protected]79580c62010-02-02 02:36:253029 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]da0c8e92010-02-09 23:25:493030 GURL entry_url;
[email protected]0314ae02010-04-08 09:18:293031 if (entry)
[email protected]da0c8e92010-02-09 23:25:493032 entry_url = entry->url();
[email protected]0314ae02010-04-08 09:18:293033 if (settings_details.ptr()->update_all() ||
3034 settings_details.ptr()->pattern().Matches(entry_url)) {
3035 render_view_host()->SendContentSettings(entry_url,
[email protected]b9a4ea7f2010-06-25 07:11:233036 profile()->GetHostContentSettingsMap()->
3037 GetContentSettings(entry_url));
[email protected]79580c62010-02-02 02:36:253038 }
3039 break;
3040 }
3041
[email protected]e5adff72010-06-14 18:13:063042 case NotificationType::EXTENSION_LOADED:
[email protected]a888b29e62010-04-01 13:38:573043 break;
[email protected]a888b29e62010-04-01 13:38:573044
3045 case NotificationType::EXTENSION_UNLOADED:
[email protected]e5adff72010-06-14 18:13:063046 case NotificationType::EXTENSION_UNLOADED_DISABLED:
[email protected]a888b29e62010-04-01 13:38:573047 break;
[email protected]a888b29e62010-04-01 13:38:573048
[email protected]420ae012009-04-24 05:16:323049 default:
3050 NOTREACHED();
3051 }
3052}
[email protected]aacd7cc2009-09-02 14:51:103053
[email protected]98aa0b52010-05-06 17:03:083054void TabContents::UpdateExtensionAppIcon(Extension* extension) {
3055 extension_app_icon_.reset();
[email protected]f075fe0a22010-04-26 04:06:433056
[email protected]a888b29e62010-04-01 13:38:573057 if (extension) {
[email protected]98aa0b52010-05-06 17:03:083058 extension_app_image_loader_.reset(new ImageLoadingTracker(this));
3059 extension_app_image_loader_->LoadImage(
[email protected]a888b29e62010-04-01 13:38:573060 extension,
3061 extension->GetIconPath(Extension::EXTENSION_ICON_SMALLISH),
3062 gfx::Size(Extension::EXTENSION_ICON_SMALLISH,
3063 Extension::EXTENSION_ICON_SMALLISH),
3064 ImageLoadingTracker::CACHE);
3065 } else {
[email protected]98aa0b52010-05-06 17:03:083066 extension_app_image_loader_.reset(NULL);
[email protected]a888b29e62010-04-01 13:38:573067 }
3068}
3069
3070Extension* TabContents::GetExtensionContaining(const GURL& url) {
3071 ExtensionsService* extensions_service = profile()->GetExtensionsService();
3072 if (!extensions_service)
3073 return NULL;
3074
3075 Extension* extension = extensions_service->GetExtensionByURL(url);
3076 return extension ?
3077 extension : extensions_service->GetExtensionByWebExtent(url);
3078}
3079
[email protected]a888b29e62010-04-01 13:38:573080void TabContents::OnImageLoaded(SkBitmap* image, ExtensionResource resource,
3081 int index) {
3082 if (image) {
[email protected]98aa0b52010-05-06 17:03:083083 extension_app_icon_ = *image;
[email protected]a888b29e62010-04-01 13:38:573084 NotifyNavigationStateChanged(INVALIDATE_TAB);
3085 }
3086}
3087
[email protected]beb440c2009-11-06 04:08:543088std::wstring TabContents::GetMessageBoxTitle(const GURL& frame_url,
3089 bool is_alert) {
3090 if (!frame_url.has_host())
3091 return l10n_util::GetString(
3092 is_alert ? IDS_JAVASCRIPT_ALERT_DEFAULT_TITLE
3093 : IDS_JAVASCRIPT_MESSAGEBOX_DEFAULT_TITLE);
3094
3095 // We really only want the scheme, hostname, and port.
3096 GURL::Replacements replacements;
3097 replacements.ClearUsername();
3098 replacements.ClearPassword();
3099 replacements.ClearPath();
3100 replacements.ClearQuery();
3101 replacements.ClearRef();
3102 GURL clean_url = frame_url.ReplaceComponents(replacements);
3103
3104 // TODO(brettw) it should be easier than this to do the correct language
3105 // handling without getting the accept language from the profile.
3106 std::wstring base_address = gfx::ElideUrl(clean_url, gfx::Font(), 0,
[email protected]ddd231e2010-06-29 20:35:193107 UTF8ToWide(profile()->GetPrefs()->GetString(prefs::kAcceptLanguages)));
[email protected]beb440c2009-11-06 04:08:543108 // Force URL to have LTR directionality.
[email protected]4a4afe32010-05-05 01:39:563109 base::i18n::GetDisplayStringInLTRDirectionality(&base_address);
[email protected]beb440c2009-11-06 04:08:543110
3111 return l10n_util::GetStringF(
3112 is_alert ? IDS_JAVASCRIPT_ALERT_TITLE : IDS_JAVASCRIPT_MESSAGEBOX_TITLE,
3113 base_address);
3114}
3115
3116gfx::NativeWindow TabContents::GetMessageBoxRootWindow() {
3117 return view_->GetTopLevelNativeWindow();
3118}
3119
3120void TabContents::OnMessageBoxClosed(IPC::Message* reply_msg,
3121 bool success,
3122 const std::wstring& prompt) {
3123 last_javascript_message_dismissal_ = base::TimeTicks::Now();
3124 if (is_showing_before_unload_dialog_ && !success) {
3125 // If a beforeunload dialog is canceled, we need to stop the throbber from
3126 // spinning, since we forced it to start spinning in Navigate.
3127 DidStopLoading();
3128
3129 tab_close_start_time_ = base::TimeTicks();
3130 }
3131 is_showing_before_unload_dialog_ = false;
3132 render_view_host()->JavaScriptMessageBoxClosed(reply_msg, success, prompt);
3133}
3134
3135void TabContents::SetSuppressMessageBoxes(bool suppress_message_boxes) {
3136 set_suppress_javascript_messages(suppress_message_boxes);
3137}
3138
[email protected]3a3d47472010-07-15 21:03:543139TabContents* TabContents::AsTabContents() {
3140 return this;
3141}
3142
3143ExtensionHost* TabContents::AsExtensionHost() {
3144 return NULL;
3145}
3146
[email protected]41fc0322009-09-04 22:23:403147void TabContents::set_encoding(const std::string& encoding) {
[email protected]aacd7cc2009-09-02 14:51:103148 encoding_ = CharacterEncoding::GetCanonicalEncodingNameByAliasName(encoding);
3149}
[email protected]f45d2a72010-03-08 23:28:353150
3151void TabContents::SetAppIcon(const SkBitmap& app_icon) {
3152 app_icon_ = app_icon;
3153 NotifyNavigationStateChanged(INVALIDATE_TITLE);
3154}
[email protected]ea68c06a2010-04-23 17:25:373155
3156// After a successful *new* login attempt, we take the PasswordFormManager in
3157// provisional_save_manager_ and move it to a SavePasswordInfoBarDelegate while
3158// the user makes up their mind with the "save password" infobar. Note if the
3159// login is one we already know about, the end of the line is
3160// provisional_save_manager_ because we just update it on success and so such
3161// forms never end up in an infobar.
3162class SavePasswordInfoBarDelegate : public ConfirmInfoBarDelegate {
3163 public:
3164 SavePasswordInfoBarDelegate(TabContents* tab_contents,
[email protected]12bb6822010-06-16 02:00:463165 PasswordFormManager* form_to_save)
3166 : ConfirmInfoBarDelegate(tab_contents),
3167 form_to_save_(form_to_save),
3168 infobar_response_(kNoResponse) {
[email protected]ea68c06a2010-04-23 17:25:373169 }
3170
3171 virtual ~SavePasswordInfoBarDelegate() { }
3172
3173 // Overridden from ConfirmInfoBarDelegate:
3174 virtual void InfoBarClosed() {
[email protected]12bb6822010-06-16 02:00:463175 UMA_HISTOGRAM_ENUMERATION("PasswordManager.InfoBarResponse",
3176 infobar_response_, kNumResponseTypes);
[email protected]ea68c06a2010-04-23 17:25:373177 delete this;
3178 }
3179
3180 virtual std::wstring GetMessageText() const {
3181 return l10n_util::GetString(IDS_PASSWORD_MANAGER_SAVE_PASSWORD_PROMPT);
3182 }
3183
3184 virtual SkBitmap* GetIcon() const {
3185 return ResourceBundle::GetSharedInstance().GetBitmapNamed(
3186 IDR_INFOBAR_SAVE_PASSWORD);
3187 }
3188
3189 virtual int GetButtons() const {
3190 return BUTTON_OK | BUTTON_CANCEL;
3191 }
3192
3193 virtual std::wstring GetButtonLabel(InfoBarButton button) const {
3194 if (button == BUTTON_OK)
3195 return l10n_util::GetString(IDS_PASSWORD_MANAGER_SAVE_BUTTON);
3196 if (button == BUTTON_CANCEL)
3197 return l10n_util::GetString(IDS_PASSWORD_MANAGER_BLACKLIST_BUTTON);
3198 NOTREACHED();
3199 return std::wstring();
3200 }
3201
3202 virtual bool Accept() {
3203 DCHECK(form_to_save_.get());
3204 form_to_save_->Save();
[email protected]12bb6822010-06-16 02:00:463205 infobar_response_ = kRememberPassword;
[email protected]ea68c06a2010-04-23 17:25:373206 return true;
3207 }
3208
3209 virtual bool Cancel() {
3210 DCHECK(form_to_save_.get());
3211 form_to_save_->PermanentlyBlacklist();
[email protected]12bb6822010-06-16 02:00:463212 infobar_response_ = kDontRememberPassword;
[email protected]ea68c06a2010-04-23 17:25:373213 return true;
3214 }
3215
3216 private:
3217 // The PasswordFormManager managing the form we're asking the user about,
3218 // and should update as per her decision.
3219 scoped_ptr<PasswordFormManager> form_to_save_;
3220
[email protected]12bb6822010-06-16 02:00:463221 // Used to track the results we get from the info bar.
3222 enum ResponseType {
3223 kNoResponse = 0,
3224 kRememberPassword,
3225 kDontRememberPassword,
3226 kNumResponseTypes,
3227 };
3228 ResponseType infobar_response_;
3229
[email protected]ea68c06a2010-04-23 17:25:373230 DISALLOW_COPY_AND_ASSIGN(SavePasswordInfoBarDelegate);
3231};
3232
3233void TabContents::FillPasswordForm(
[email protected]e768baf2010-07-16 19:36:163234 const webkit_glue::PasswordFormFillData& form_data) {
[email protected]ea68c06a2010-04-23 17:25:373235 render_view_host()->FillPasswordForm(form_data);
3236}
3237
3238void TabContents::AddSavePasswordInfoBar(PasswordFormManager* form_to_save) {
3239 AddInfoBar(new SavePasswordInfoBarDelegate(this, form_to_save));
3240}
3241
3242Profile* TabContents::GetProfileForPasswordManager() {
3243 return profile();
3244}
3245
3246bool TabContents::DidLastPageLoadEncounterSSLErrors() {
3247 return controller().ssl_manager()->ProcessedSSLErrorFromRequest();
3248}