blob: d23e537e80a757692f2d27108c1d50425791b19b [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]a92b8642009-05-05 23:38:567#include "app/l10n_util.h"
[email protected]9929da92009-05-05 02:05:118#include "app/resource_bundle.h"
[email protected]614519812010-03-19 08:20:569#include "app/text_elider.h"
[email protected]8cc8d492010-02-02 10:40:4910#include "base/auto_reset.h"
[email protected]420ae012009-04-24 05:16:3211#include "base/file_version_info.h"
[email protected]7cf1b6ce2010-03-20 06:37:0112#include "base/i18n/rtl.h"
[email protected]420ae012009-04-24 05:16:3213#include "base/process_util.h"
[email protected]96d185d2009-04-24 03:28:5414#include "base/string16.h"
[email protected]996fd702009-09-04 19:12:3715#include "base/string_util.h"
[email protected]96d185d2009-04-24 03:28:5416#include "base/time.h"
[email protected]6db0def92010-03-02 19:55:4517#include "chrome/browser/autocomplete_history_manager.h"
[email protected]21674df2009-10-28 19:51:2118#include "chrome/browser/autofill/autofill_manager.h"
[email protected]66ba4932009-06-04 19:22:1319#include "chrome/browser/blocked_popup_container.h"
[email protected]420ae012009-04-24 05:16:3220#include "chrome/browser/bookmarks/bookmark_model.h"
[email protected]96d185d2009-04-24 03:28:5421#include "chrome/browser/browser_process.h"
[email protected]cf68b092010-01-15 20:05:5522#include "chrome/browser/browser_shutdown.h"
initial.commit09911bf2008-07-26 23:55:2923#include "chrome/browser/cert_store.h"
[email protected]aacd7cc2009-09-02 14:51:1024#include "chrome/browser/character_encoding.h"
[email protected]96d185d2009-04-24 03:28:5425#include "chrome/browser/debugger/devtools_manager.h"
[email protected]420ae012009-04-24 05:16:3226#include "chrome/browser/dom_operation_notification_details.h"
[email protected]96d185d2009-04-24 03:28:5427#include "chrome/browser/dom_ui/dom_ui.h"
28#include "chrome/browser/dom_ui/dom_ui_factory.h"
[email protected]bcef0dc02009-02-28 00:35:0229#include "chrome/browser/download/download_item_model.h"
[email protected]420ae012009-04-24 05:16:3230#include "chrome/browser/download/download_manager.h"
31#include "chrome/browser/download/download_request_manager.h"
[email protected]e7eaedde2009-09-25 20:09:4932#include "chrome/browser/external_protocol_handler.h"
[email protected]92c6f9b92009-10-24 04:35:0833#include "chrome/browser/extensions/extensions_service.h"
[email protected]0189bc722009-08-28 21:56:4834#include "chrome/browser/favicon_service.h"
[email protected]e5d2f7132010-04-09 18:13:5335#include "chrome/browser/find_bar_state.h"
[email protected]9d062b12010-04-14 09:36:5536#include "chrome/browser/geolocation/geolocation_content_settings_map.h"
[email protected]420ae012009-04-24 05:16:3237#include "chrome/browser/google_util.h"
[email protected]79580c62010-02-02 02:36:2538#include "chrome/browser/host_content_settings_map.h"
[email protected]8897c382009-05-06 17:53:2639#include "chrome/browser/hung_renderer_dialog.h"
[email protected]12f74a92010-02-05 22:32:1440#include "chrome/browser/message_box_handler.h"
[email protected]420ae012009-04-24 05:16:3241#include "chrome/browser/load_from_memory_cache_details.h"
42#include "chrome/browser/load_notification_details.h"
[email protected]84abba62009-10-07 17:01:4443#include "chrome/browser/metrics/metric_event_duration_details.h"
[email protected]ea0e80892009-08-22 00:36:0544#include "chrome/browser/modal_html_dialog_delegate.h"
[email protected]c8865482009-07-23 20:40:1045#include "chrome/browser/omnibox_search_hint.h"
[email protected]fdd61c62009-04-22 19:22:5746#include "chrome/browser/password_manager/password_manager.h"
47#include "chrome/browser/plugin_installer.h"
[email protected]052313b2010-02-19 09:43:0848#include "chrome/browser/pref_service.h"
[email protected]ce560f82009-06-03 09:39:4449#include "chrome/browser/profile.h"
[email protected]1db6ff152009-10-12 15:32:0750#include "chrome/browser/renderer_host/render_process_host.h"
[email protected]8cb5d5b2010-02-09 11:36:1651#include "chrome/browser/renderer_host/render_view_host.h"
[email protected]96d185d2009-04-24 03:28:5452#include "chrome/browser/renderer_host/render_widget_host_view.h"
[email protected]fd694982009-07-03 17:32:3753#include "chrome/browser/renderer_host/resource_request_details.h"
[email protected]1db6ff152009-10-12 15:32:0754#include "chrome/browser/renderer_host/site_instance.h"
[email protected]96d185d2009-04-24 03:28:5455#include "chrome/browser/renderer_host/web_cache_manager.h"
[email protected]93623c5d2009-12-10 21:40:3256#include "chrome/browser/renderer_preferences_util.h"
[email protected]152b8ba32010-03-29 19:40:1657#include "chrome/browser/search_engines/template_url_fetcher.h"
58#include "chrome/browser/search_engines/template_url_model.h"
[email protected]c37e3b62010-01-22 00:15:1759#include "chrome/browser/sessions/session_types.h"
[email protected]bd1ad682009-05-15 22:19:1760#include "chrome/browser/tab_contents/infobar_delegate.h"
[email protected]7e383692009-06-12 19:14:5461#include "chrome/browser/tab_contents/interstitial_page.h"
[email protected]f3ec7742009-01-15 00:59:1662#include "chrome/browser/tab_contents/navigation_entry.h"
[email protected]de34a962009-06-26 23:22:4363#include "chrome/browser/tab_contents/provisional_load_details.h"
[email protected]f3ec7742009-01-15 00:59:1664#include "chrome/browser/tab_contents/tab_contents_delegate.h"
[email protected]fdd61c62009-04-22 19:22:5765#include "chrome/browser/tab_contents/tab_contents_view.h"
[email protected]58dca552009-06-17 00:35:0266#include "chrome/browser/tab_contents/thumbnail_generator.h"
[email protected]449478302009-06-09 20:04:2867#include "chrome/browser/thumbnail_store.h"
[email protected]152b8ba32010-03-29 19:40:1668#include "chrome/browser/translate/page_translated_details.h"
[email protected]54ec6472010-04-09 19:39:5869#include "chrome/common/bindings_policy.h"
[email protected]35f7d212009-04-29 21:19:2770#include "chrome/common/chrome_switches.h"
[email protected]3b355642010-02-05 16:01:4971#include "chrome/common/extensions/extension.h"
[email protected]5d246db22009-10-27 06:17:5772#include "chrome/common/extensions/extension_action.h"
[email protected]bfd04a62009-02-01 18:16:5673#include "chrome/common/notification_service.h"
[email protected]89d6e6e2009-12-03 23:06:5174#include "chrome/common/platform_util.h"
initial.commit09911bf2008-07-26 23:55:2975#include "chrome/common/pref_names.h"
[email protected]420ae012009-04-24 05:16:3276#include "chrome/common/render_messages.h"
[email protected]1ee614862009-07-06 20:29:1877#include "chrome/common/renderer_preferences.h"
[email protected]96d185d2009-04-24 03:28:5478#include "chrome/common/url_constants.h"
[email protected]ea68c06a2010-04-23 17:25:3779#include "grit/chromium_strings.h"
[email protected]8897c382009-05-06 17:53:2680#include "grit/generated_resources.h"
[email protected]420ae012009-04-24 05:16:3281#include "grit/locale_settings.h"
[email protected]fd626782010-01-22 05:26:0682#include "grit/platform_locale_settings.h"
[email protected]ea68c06a2010-04-23 17:25:3783#include "grit/theme_resources.h"
[email protected]420ae012009-04-24 05:16:3284#include "net/base/mime_util.h"
85#include "net/base/net_errors.h"
[email protected]d686e812009-06-03 19:10:2986#include "net/base/net_util.h"
[email protected]420ae012009-04-24 05:16:3287#include "net/base/registry_controlled_domain.h"
[email protected]1ee614862009-07-06 20:29:1888#include "webkit/glue/password_form.h"
89#include "webkit/glue/webpreferences.h"
[email protected]3e45ba92009-02-20 21:09:0090
[email protected]304f8792009-08-19 21:28:0991#if defined(OS_CHROMEOS)
92// For GdkScreen
93#include <gdk/gdk.h>
[email protected]0ba5c5e2009-08-20 16:47:1394#endif // defined(OS_CHROMEOS)
95
[email protected]420ae012009-04-24 05:16:3296// Cross-Site Navigations
97//
98// If a TabContents is told to navigate to a different web site (as determined
99// by SiteInstance), it will replace its current RenderViewHost with a new
100// RenderViewHost dedicated to the new SiteInstance. This works as follows:
101//
102// - Navigate determines whether the destination is cross-site, and if so,
103// it creates a pending_render_view_host_ and moves into the PENDING
104// RendererState.
105// - The pending RVH is "suspended," so that no navigation messages are sent to
106// its renderer until the onbeforeunload JavaScript handler has a chance to
107// run in the current RVH.
108// - The pending RVH tells CrossSiteRequestManager (a thread-safe singleton)
109// that it has a pending cross-site request. ResourceDispatcherHost will
110// check for this when the response arrives.
111// - The current RVH runs its onbeforeunload handler. If it returns false, we
112// cancel all the pending logic and go back to NORMAL. Otherwise we allow
113// the pending RVH to send the navigation request to its renderer.
114// - ResourceDispatcherHost receives a ResourceRequest on the IO thread. It
115// checks CrossSiteRequestManager to see that the RVH responsible has a
116// pending cross-site request, and then installs a CrossSiteEventHandler.
117// - When RDH receives a response, the BufferedEventHandler determines whether
118// it is a download. If so, it sends a message to the new renderer causing
119// it to cancel the request, and the download proceeds in the download
120// thread. For now, we stay in a PENDING state (with a pending RVH) until
[email protected]57c6a652009-05-04 07:58:34121// the next DidNavigate event for this TabContents. This isn't ideal, but it
[email protected]420ae012009-04-24 05:16:32122// doesn't affect any functionality.
123// - After RDH receives a response and determines that it is safe and not a
124// download, it pauses the response to first run the old page's onunload
125// handler. It does this by asynchronously calling the OnCrossSiteResponse
[email protected]57c6a652009-05-04 07:58:34126// method of TabContents on the UI thread, which sends a ClosePage message
[email protected]420ae012009-04-24 05:16:32127// to the current RVH.
128// - Once the onunload handler is finished, a ClosePage_ACK message is sent to
129// the ResourceDispatcherHost, who unpauses the response. Data is then sent
130// to the pending RVH.
131// - The pending renderer sends a FrameNavigate message that invokes the
132// DidNavigate method. This replaces the current RVH with the
133// pending RVH and goes back to the NORMAL RendererState.
134
135namespace {
136
137// Amount of time we wait between when a key event is received and the renderer
138// is queried for its state and pushed to the NavigationEntry.
139const int kQueryStateDelay = 5000;
140
141const int kSyncWaitDelay = 40;
142
[email protected]304f8792009-08-19 21:28:09143#if defined(OS_CHROMEOS)
144// If a popup window is bigger than this fraction of the screen on chrome os,
145// turn it into a tab
146const float kMaxWidthFactor = 0.5;
147const float kMaxHeightFactor = 0.6;
148#endif
149
[email protected]420ae012009-04-24 05:16:32150// If another javascript message box is displayed within
151// kJavascriptMessageExpectedDelay of a previous javascript message box being
152// dismissed, display an option to suppress future message boxes from this
153// contents.
154const int kJavascriptMessageExpectedDelay = 1000;
155
[email protected]420ae012009-04-24 05:16:32156// The list of prefs we want to observe.
157const wchar_t* kPrefsToObserve[] = {
158 prefs::kAlternateErrorPagesEnabled,
159 prefs::kWebKitJavaEnabled,
160 prefs::kWebKitJavascriptEnabled,
161 prefs::kWebKitLoadsImagesAutomatically,
162 prefs::kWebKitPluginsEnabled,
163 prefs::kWebKitUsesUniversalDetector,
164 prefs::kWebKitSerifFontFamily,
165 prefs::kWebKitSansSerifFontFamily,
166 prefs::kWebKitFixedFontFamily,
167 prefs::kWebKitDefaultFontSize,
168 prefs::kWebKitDefaultFixedFontSize,
169 prefs::kDefaultCharset
170 // kWebKitStandardFontIsSerif needs to be added
171 // if we let users pick which font to use, serif or sans-serif when
172 // no font is specified or a CSS generic family (serif or sans-serif)
173 // is not specified.
174};
175
176const int kPrefsToObserveLength = arraysize(kPrefsToObserve);
177
[email protected]420ae012009-04-24 05:16:32178// Returns true if the entry's transition type is FORM_SUBMIT.
179bool IsFormSubmit(const NavigationEntry* entry) {
180 return (PageTransition::StripQualifier(entry->transition_type()) ==
181 PageTransition::FORM_SUBMIT);
182}
183
184#if defined(OS_WIN)
185
186BOOL CALLBACK InvalidateWindow(HWND hwnd, LPARAM lparam) {
187 // Note: erase is required to properly paint some widgets borders. This can
188 // be seen with textfields.
189 InvalidateRect(hwnd, NULL, TRUE);
190 return TRUE;
191}
192#endif
193
[email protected]5e369672009-11-03 23:48:30194ViewMsg_Navigate_Params::NavigationType GetNavigationType(
[email protected]1ccb3568d2010-02-19 10:51:16195 Profile* profile, const NavigationEntry& entry,
196 NavigationController::ReloadType reload_type) {
197 switch (reload_type) {
198 case NavigationController::RELOAD:
199 return ViewMsg_Navigate_Params::RELOAD;
200 case NavigationController::RELOAD_IGNORING_CACHE:
201 return ViewMsg_Navigate_Params::RELOAD_IGNORING_CACHE;
202 case NavigationController::NO_RELOAD:
203 break; // Fall through to rest of function.
204 }
[email protected]5e369672009-11-03 23:48:30205
206 if (entry.restore_type() == NavigationEntry::RESTORE_LAST_SESSION &&
207 profile->DidLastSessionExitCleanly())
208 return ViewMsg_Navigate_Params::RESTORE;
209
210 return ViewMsg_Navigate_Params::NORMAL;
211}
212
[email protected]3cc72b12010-03-18 23:03:00213void MakeNavigateParams(const NavigationController& controller,
[email protected]1ccb3568d2010-02-19 10:51:16214 NavigationController::ReloadType reload_type,
215 ViewMsg_Navigate_Params* params) {
[email protected]3cc72b12010-03-18 23:03:00216 const NavigationEntry& entry = *controller.pending_entry();
[email protected]056de2d2009-06-26 16:41:34217 params->page_id = entry.page_id();
[email protected]3cc72b12010-03-18 23:03:00218 params->pending_history_list_offset = controller.pending_entry_index();
219 params->current_history_list_offset = controller.last_committed_entry_index();
220 params->current_history_list_length = controller.entry_count();
[email protected]056de2d2009-06-26 16:41:34221 params->url = entry.url();
222 params->referrer = entry.referrer();
223 params->transition = entry.transition_type();
224 params->state = entry.content_state();
[email protected]3cc72b12010-03-18 23:03:00225 params->navigation_type =
226 GetNavigationType(controller.profile(), entry, reload_type);
[email protected]056de2d2009-06-26 16:41:34227 params->request_time = base::Time::Now();
228}
229
[email protected]420ae012009-04-24 05:16:32230} // namespace
231
232// -----------------------------------------------------------------------------
233
[email protected]fdd61c62009-04-22 19:22:57234// static
235int TabContents::find_request_id_counter_ = -1;
236
[email protected]420ae012009-04-24 05:16:32237
[email protected]420ae012009-04-24 05:16:32238TabContents::TabContents(Profile* profile,
239 SiteInstance* site_instance,
240 int routing_id,
[email protected]34ac70502009-09-25 17:07:23241 const TabContents* base_tab_contents)
[email protected]b680ad22009-04-15 23:19:42242 : delegate_(NULL),
[email protected]66ba4932009-06-04 19:22:13243 ALLOW_THIS_IN_INITIALIZER_LIST(controller_(this, profile)),
244 ALLOW_THIS_IN_INITIALIZER_LIST(view_(
[email protected]d82ed61e2009-06-16 02:46:22245 TabContentsView::Create(this))),
246 ALLOW_THIS_IN_INITIALIZER_LIST(render_manager_(this, this)),
[email protected]fdd61c62009-04-22 19:22:57247 property_bag_(),
248 registrar_(),
[email protected]d82ed61e2009-06-16 02:46:22249 ALLOW_THIS_IN_INITIALIZER_LIST(printing_(*this)),
[email protected]fdd61c62009-04-22 19:22:57250 save_package_(),
[email protected]6db0def92010-03-02 19:55:45251 autocomplete_history_manager_(),
[email protected]21674df2009-10-28 19:51:21252 autofill_manager_(),
[email protected]fdd61c62009-04-22 19:22:57253 password_manager_(),
254 plugin_installer_(),
[email protected]6f821dd2010-02-23 00:54:06255 bookmark_drag_(NULL),
[email protected]d82ed61e2009-06-16 02:46:22256 ALLOW_THIS_IN_INITIALIZER_LIST(fav_icon_helper_(this)),
[email protected]fdd61c62009-04-22 19:22:57257 select_file_dialog_(),
[email protected]d5f942ba2008-09-26 19:30:34258 is_loading_(false),
initial.commit09911bf2008-07-26 23:55:29259 is_crashed_(false),
[email protected]d5f942ba2008-09-26 19:30:34260 waiting_for_response_(false),
[email protected]d5f942ba2008-09-26 19:30:34261 max_page_id_(-1),
[email protected]fdd61c62009-04-22 19:22:57262 current_load_start_(),
263 load_state_(net::LOAD_STATE_IDLE),
264 load_state_host_(),
[email protected]094e5b22009-09-25 04:23:56265 upload_size_(0),
266 upload_position_(0),
[email protected]fdd61c62009-04-22 19:22:57267 received_page_title_(false),
268 is_starred_(false),
269 contents_mime_type_(),
270 encoding_(),
[email protected]332af7732009-03-11 13:21:35271 blocked_popups_(NULL),
[email protected]8cc8d492010-02-02 10:40:49272 dont_notify_render_view_(false),
[email protected]fdd61c62009-04-22 19:22:57273 infobar_delegates_(),
[email protected]fdd61c62009-04-22 19:22:57274 find_ui_active_(false),
275 find_op_aborted_(false),
276 current_find_request_id_(find_request_id_counter_++),
[email protected]e491f1c2009-05-22 20:28:12277 last_search_case_sensitive_(false),
[email protected]e491f1c2009-05-22 20:28:12278 last_search_result_(),
[email protected]a888b29e62010-04-01 13:38:57279 app_extension_(NULL),
280 app_extension_for_current_page_(NULL),
[email protected]fdd61c62009-04-22 19:22:57281 capturing_contents_(false),
282 is_being_destroyed_(false),
283 notify_disconnection_(false),
284 history_requests_(),
285#if defined(OS_WIN)
286 message_box_active_(CreateEvent(NULL, TRUE, FALSE, NULL)),
287#endif
288 last_javascript_message_dismissal_(),
[email protected]c2e74fe82009-09-03 17:57:44289 suppress_javascript_messages_(false),
[email protected]7ab1e7d62009-10-14 23:32:01290 is_showing_before_unload_dialog_(false),
[email protected]7ff431e2010-01-07 18:18:54291 renderer_preferences_(),
[email protected]1a242c32010-02-02 21:20:54292 opener_dom_ui_type_(DOMUIFactory::kNoDOMUI),
[email protected]3b1c1812010-02-12 21:53:55293 language_state_(&controller_) {
[email protected]0de80162010-02-03 04:52:35294 ClearBlockedContentSettings();
[email protected]7ff431e2010-01-07 18:18:54295 renderer_preferences_util::UpdateFromSystemSettings(
296 &renderer_preferences_, profile);
297
[email protected]16779842009-07-08 23:45:29298#if defined(OS_CHROMEOS)
[email protected]58dca552009-06-17 00:35:02299 // Make sure the thumbnailer is started before starting the render manager.
300 // The thumbnailer will want to listen for RVH creations, one of which will
301 // happen in RVHManager::Init.
302 ThumbnailGenerator* generator = g_browser_process->GetThumbnailGenerator();
303 if (generator)
304 generator->StartThumbnailing();
305#endif
306
[email protected]12636df2009-09-28 22:32:21307 render_manager_.Init(profile, site_instance, routing_id);
[email protected]420ae012009-04-24 05:16:32308
[email protected]34ac70502009-09-25 17:07:23309 // We have the initial size of the view be based on the size of the passed in
310 // tab contents (normally a tab from the same window).
311 view_->CreateView(base_tab_contents ?
312 base_tab_contents->view()->GetContainerSize() : gfx::Size());
[email protected]420ae012009-04-24 05:16:32313
314 // Register for notifications about all interested prefs change.
315 PrefService* prefs = profile->GetPrefs();
316 if (prefs) {
317 for (int i = 0; i < kPrefsToObserveLength; ++i)
318 prefs->AddPrefObserver(kPrefsToObserve[i], this);
319 }
320
321 // Register for notifications about URL starredness changing on any profile.
322 registrar_.Add(this, NotificationType::URLS_STARRED,
323 NotificationService::AllSources());
324 registrar_.Add(this, NotificationType::BOOKMARK_MODEL_LOADED,
325 NotificationService::AllSources());
326 registrar_.Add(this, NotificationType::RENDER_WIDGET_HOST_DESTROYED,
327 NotificationService::AllSources());
[email protected]d3ba77272009-09-03 00:06:09328#if defined(OS_LINUX)
329 registrar_.Add(this, NotificationType::BROWSER_THEME_CHANGED,
330 NotificationService::AllSources());
331#endif
[email protected]420ae012009-04-24 05:16:32332
[email protected]6c32ce72010-03-08 05:18:06333 registrar_.Add(this, NotificationType::USER_STYLE_SHEET_UPDATED,
334 NotificationService::AllSources());
335
[email protected]79580c62010-02-02 02:36:25336 // Register for notifications about content setting changes.
337 registrar_.Add(this, NotificationType::CONTENT_SETTINGS_CHANGED,
338 NotificationService::AllSources());
339
[email protected]a888b29e62010-04-01 13:38:57340 // Listen for extension changes so we can update extension_for_current_page_.
341 registrar_.Add(this, NotificationType::EXTENSION_LOADED,
342 NotificationService::AllSources());
343 registrar_.Add(this, NotificationType::EXTENSION_UNLOADED,
344 NotificationService::AllSources());
345 registrar_.Add(this, NotificationType::EXTENSION_UNLOADED_DISABLED,
346 NotificationService::AllSources());
347
[email protected]c8865482009-07-23 20:40:10348 // Set-up the showing of the omnibox search infobar if applicable.
349 if (OmniboxSearchHint::IsEnabled(profile))
350 omnibox_search_hint_.reset(new OmniboxSearchHint(this));
[email protected]332af7732009-03-11 13:21:35351}
initial.commit09911bf2008-07-26 23:55:29352
353TabContents::~TabContents() {
[email protected]420ae012009-04-24 05:16:32354 is_being_destroyed_ = true;
355
[email protected]bfe4c1582009-10-02 18:11:09356 // We don't want any notifications while we're running our destructor.
[email protected]420ae012009-04-24 05:16:32357 registrar_.RemoveAll();
358
359 // Unregister the notifications of all observed prefs change.
360 PrefService* prefs = profile()->GetPrefs();
361 if (prefs) {
362 for (int i = 0; i < kPrefsToObserveLength; ++i)
363 prefs->RemovePrefObserver(kPrefsToObserve[i], this);
364 }
365
[email protected]420ae012009-04-24 05:16:32366 NotifyDisconnected();
[email protected]e7b8d832009-11-20 22:41:00367 hung_renderer_dialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:32368
[email protected]420ae012009-04-24 05:16:32369 // First cleanly close all child windows.
370 // TODO(mpcomplete): handle case if MaybeCloseChildWindows() already asked
371 // some of these to close. CloseWindows is async, so it might get called
372 // twice before it runs.
[email protected]8cc8d492010-02-02 10:40:49373 CloseConstrainedWindows();
[email protected]420ae012009-04-24 05:16:32374
[email protected]8cc8d492010-02-02 10:40:49375 // Close all blocked popups.
[email protected]66ba4932009-06-04 19:22:13376 if (blocked_popups_)
377 blocked_popups_->Destroy();
378
[email protected]420ae012009-04-24 05:16:32379 // Notify any observer that have a reference on this tab contents.
380 NotificationService::current()->Notify(
381 NotificationType::TAB_CONTENTS_DESTROYED,
382 Source<TabContents>(this),
383 NotificationService::NoDetails());
384
[email protected]1e5e74f2009-05-27 20:55:12385 // Notify any lasting InfobarDelegates that have not yet been removed that
386 // whatever infobar they were handling in this TabContents has closed,
387 // because the TabContents is going away entirely.
388 // This must happen after the TAB_CONTENTS_DESTROYED notification as the
389 // notification may trigger infobars calls that access their delegate. (and
390 // some implementations of InfoBarDelegate do delete themselves on
391 // InfoBarClosed()).
392 for (int i = 0; i < infobar_delegate_count(); ++i) {
393 InfoBarDelegate* delegate = GetInfoBarDelegateAt(i);
394 delegate->InfoBarClosed();
395 }
396 infobar_delegates_.clear();
397
[email protected]420ae012009-04-24 05:16:32398 // TODO(brettw) this should be moved to the view.
399#if defined(OS_WIN)
400 // If we still have a window handle, destroy it. GetNativeView can return
401 // NULL if this contents was part of a window that closed.
402 if (GetNativeView())
403 ::DestroyWindow(GetNativeView());
404#endif
[email protected]7ab1e7d62009-10-14 23:32:01405
406 // OnCloseStarted isn't called in unit tests.
407 if (!tab_close_start_time_.is_null()) {
408 UMA_HISTOGRAM_TIMES("Tab.Close",
409 base::TimeTicks::Now() - tab_close_start_time_);
410 }
initial.commit09911bf2008-07-26 23:55:29411}
412
[email protected]d5f942ba2008-09-26 19:30:34413// static
414void TabContents::RegisterUserPrefs(PrefService* prefs) {
[email protected]420ae012009-04-24 05:16:32415 prefs->RegisterBooleanPref(prefs::kAlternateErrorPagesEnabled, true);
416
417 WebPreferences pref_defaults;
418 prefs->RegisterBooleanPref(prefs::kWebKitJavascriptEnabled,
419 pref_defaults.javascript_enabled);
420 prefs->RegisterBooleanPref(prefs::kWebKitWebSecurityEnabled,
421 pref_defaults.web_security_enabled);
422 prefs->RegisterBooleanPref(
423 prefs::kWebKitJavascriptCanOpenWindowsAutomatically, true);
424 prefs->RegisterBooleanPref(prefs::kWebKitLoadsImagesAutomatically,
425 pref_defaults.loads_images_automatically);
426 prefs->RegisterBooleanPref(prefs::kWebKitPluginsEnabled,
427 pref_defaults.plugins_enabled);
428 prefs->RegisterBooleanPref(prefs::kWebKitDomPasteEnabled,
429 pref_defaults.dom_paste_enabled);
430 prefs->RegisterBooleanPref(prefs::kWebKitShrinksStandaloneImagesToFit,
431 pref_defaults.shrinks_standalone_images_to_fit);
[email protected]0df30122009-06-03 12:13:08432 prefs->RegisterStringPref(prefs::kWebKitInspectorSettings,
[email protected]8922e1f2009-10-03 05:01:26433 UTF8ToWide(pref_defaults.inspector_settings));
[email protected]420ae012009-04-24 05:16:32434 prefs->RegisterBooleanPref(prefs::kWebKitTextAreasAreResizable,
435 pref_defaults.text_areas_are_resizable);
436 prefs->RegisterBooleanPref(prefs::kWebKitJavaEnabled,
437 pref_defaults.java_enabled);
438
439 prefs->RegisterLocalizedStringPref(prefs::kAcceptLanguages,
440 IDS_ACCEPT_LANGUAGES);
441 prefs->RegisterLocalizedStringPref(prefs::kDefaultCharset,
442 IDS_DEFAULT_ENCODING);
443 prefs->RegisterLocalizedBooleanPref(prefs::kWebKitStandardFontIsSerif,
444 IDS_STANDARD_FONT_IS_SERIF);
445 prefs->RegisterLocalizedStringPref(prefs::kWebKitFixedFontFamily,
446 IDS_FIXED_FONT_FAMILY);
447 prefs->RegisterLocalizedStringPref(prefs::kWebKitSerifFontFamily,
448 IDS_SERIF_FONT_FAMILY);
449 prefs->RegisterLocalizedStringPref(prefs::kWebKitSansSerifFontFamily,
450 IDS_SANS_SERIF_FONT_FAMILY);
451 prefs->RegisterLocalizedStringPref(prefs::kWebKitCursiveFontFamily,
452 IDS_CURSIVE_FONT_FAMILY);
453 prefs->RegisterLocalizedStringPref(prefs::kWebKitFantasyFontFamily,
454 IDS_FANTASY_FONT_FAMILY);
455 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitDefaultFontSize,
456 IDS_DEFAULT_FONT_SIZE);
457 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitDefaultFixedFontSize,
458 IDS_DEFAULT_FIXED_FONT_SIZE);
459 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitMinimumFontSize,
460 IDS_MINIMUM_FONT_SIZE);
461 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitMinimumLogicalFontSize,
462 IDS_MINIMUM_LOGICAL_FONT_SIZE);
463 prefs->RegisterLocalizedBooleanPref(prefs::kWebKitUsesUniversalDetector,
464 IDS_USES_UNIVERSAL_DETECTOR);
465 prefs->RegisterLocalizedStringPref(prefs::kStaticEncodings,
466 IDS_STATIC_ENCODING_LIST);
initial.commit09911bf2008-07-26 23:55:29467}
468
[email protected]13367f72009-07-08 16:39:02469// Returns true if contains content rendered by an extension.
470bool TabContents::HostsExtension() const {
471 return GetURL().SchemeIs(chrome::kExtensionScheme);
[email protected]d5f942ba2008-09-26 19:30:34472}
473
[email protected]cf28ec52010-04-09 22:56:34474AutoFillManager* TabContents::GetAutoFillManager() {
475 if (autofill_manager_.get() == NULL)
476 autofill_manager_.reset(new AutoFillManager(this));
477 return autofill_manager_.get();
478}
479
[email protected]6e95706d2009-04-23 22:55:01480PasswordManager* TabContents::GetPasswordManager() {
481 if (password_manager_.get() == NULL)
482 password_manager_.reset(new PasswordManager(this));
483 return password_manager_.get();
484}
485
486PluginInstaller* TabContents::GetPluginInstaller() {
487 if (plugin_installer_.get() == NULL)
488 plugin_installer_.reset(new PluginInstaller(this));
489 return plugin_installer_.get();
490}
491
[email protected]8cb5d5b2010-02-09 11:36:16492RenderProcessHost* TabContents::GetRenderProcessHost() const {
493 return render_manager_.current_host()->process();
494}
495
[email protected]3b355642010-02-05 16:01:49496void TabContents::SetAppExtension(Extension* extension) {
[email protected]867a73e12010-03-19 20:45:46497 DCHECK(!extension || extension->GetFullLaunchURL().is_valid());
[email protected]3b355642010-02-05 16:01:49498 app_extension_ = extension;
[email protected]fca656c2010-02-10 20:30:10499
[email protected]a888b29e62010-04-01 13:38:57500 UpdateAppExtensionIcon(app_extension_);
501
[email protected]fca656c2010-02-10 20:30:10502 NotificationService::current()->Notify(
503 NotificationType::TAB_CONTENTS_APPLICATION_EXTENSION_CHANGED,
504 Source<TabContents>(this),
505 NotificationService::NoDetails());
[email protected]3b355642010-02-05 16:01:49506}
507
[email protected]911f8152010-03-18 16:46:40508void TabContents::SetAppExtensionById(const std::string& app_extension_id) {
509 if (app_extension_id.empty())
510 return;
511
512 ExtensionsService* extension_service = profile()->GetExtensionsService();
513 if (extension_service && extension_service->is_ready()) {
514 Extension* extension =
515 extension_service->GetExtensionById(app_extension_id, false);
516 if (extension)
517 SetAppExtension(extension);
518 }
519}
520
[email protected]d5f942ba2008-09-26 19:30:34521const GURL& TabContents::GetURL() const {
522 // We may not have a navigation entry yet
[email protected]ce3fa3c2009-04-20 19:55:57523 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]ebe89e062009-08-13 23:16:54524 return entry ? entry->virtual_url() : GURL::EmptyGURL();
[email protected]d5f942ba2008-09-26 19:30:34525}
526
[email protected]96d185d2009-04-24 03:28:54527const string16& TabContents::GetTitle() const {
[email protected]4c6092c5b2009-06-06 00:23:55528 // Transient entries take precedence. They are used for interstitial pages
529 // that are shown on top of existing pages.
530 NavigationEntry* entry = controller_.GetTransientEntry();
531 if (entry)
532 return entry->GetTitleForDisplay(&controller_);
533
[email protected]96d185d2009-04-24 03:28:54534 DOMUI* our_dom_ui = render_manager_.pending_dom_ui() ?
535 render_manager_.pending_dom_ui() : render_manager_.dom_ui();
536 if (our_dom_ui) {
537 // Don't override the title in view source mode.
[email protected]4c6092c5b2009-06-06 00:23:55538 entry = controller_.GetActiveEntry();
[email protected]96d185d2009-04-24 03:28:54539 if (!(entry && entry->IsViewSourceMode())) {
540 // Give the DOM UI the chance to override our title.
541 const string16& title = our_dom_ui->overridden_title();
542 if (!title.empty())
543 return title;
544 }
545 }
546
547 // We use the title for the last committed entry rather than a pending
548 // navigation entry. For example, when the user types in a URL, we want to
549 // keep the old page's title until the new load has committed and we get a new
550 // title.
[email protected]96d185d2009-04-24 03:28:54551 entry = controller_.GetLastCommittedEntry();
552 if (entry)
553 return entry->GetTitleForDisplay(&controller_);
[email protected]96d185d2009-04-24 03:28:54554 return EmptyString16();
555}
556
[email protected]bbbc1ef2010-02-12 18:03:41557// static
558string16 TabContents::GetDefaultTitle() {
559 return l10n_util::GetStringUTF16(IDS_DEFAULT_TAB_TITLE);
560}
561
[email protected]d5f942ba2008-09-26 19:30:34562int32 TabContents::GetMaxPageID() {
563 if (GetSiteInstance())
564 return GetSiteInstance()->max_page_id();
565 else
566 return max_page_id_;
567}
568
569void TabContents::UpdateMaxPageID(int32 page_id) {
570 // Ensure both the SiteInstance and RenderProcessHost update their max page
[email protected]57c6a652009-05-04 07:58:34571 // IDs in sync. Only TabContents will also have site instances, except during
[email protected]d5f942ba2008-09-26 19:30:34572 // testing.
573 if (GetSiteInstance())
574 GetSiteInstance()->UpdateMaxPageID(page_id);
[email protected]8cb5d5b2010-02-09 11:36:16575 GetRenderProcessHost()->UpdateMaxPageID(page_id);
[email protected]d5f942ba2008-09-26 19:30:34576}
577
[email protected]96d185d2009-04-24 03:28:54578SiteInstance* TabContents::GetSiteInstance() const {
579 return render_manager_.current_host()->site_instance();
580}
581
[email protected]96d185d2009-04-24 03:28:54582bool TabContents::ShouldDisplayURL() {
[email protected]8ab24cc2009-11-13 20:56:15583 // Don't hide the url in view source mode and with interstitials.
[email protected]96d185d2009-04-24 03:28:54584 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]8ab24cc2009-11-13 20:56:15585 if (entry && (entry->IsViewSourceMode() ||
586 entry->page_type() == NavigationEntry::INTERSTITIAL_PAGE)) {
[email protected]96d185d2009-04-24 03:28:54587 return true;
[email protected]8ab24cc2009-11-13 20:56:15588 }
[email protected]83e3895b2009-06-11 00:07:16589
[email protected]96d185d2009-04-24 03:28:54590 DOMUI* dom_ui = GetDOMUIForCurrentState();
591 if (dom_ui)
592 return !dom_ui->should_hide_url();
593 return true;
594}
595
[email protected]d5f942ba2008-09-26 19:30:34596SkBitmap TabContents::GetFavIcon() const {
597 // Like GetTitle(), we also want to use the favicon for the last committed
598 // entry rather than a pending navigation entry.
[email protected]ce3fa3c2009-04-20 19:55:57599 NavigationEntry* entry = controller_.GetTransientEntry();
[email protected]cbab76d2008-10-13 22:42:47600 if (entry)
601 return entry->favicon().bitmap();
602
[email protected]ce3fa3c2009-04-20 19:55:57603 entry = controller_.GetLastCommittedEntry();
[email protected]d5f942ba2008-09-26 19:30:34604 if (entry)
605 return entry->favicon().bitmap();
[email protected]d5f942ba2008-09-26 19:30:34606 return SkBitmap();
607}
608
[email protected]0b303cc2009-09-28 22:35:15609bool TabContents::FavIconIsValid() const {
610 NavigationEntry* entry = controller_.GetTransientEntry();
611 if (entry)
612 return entry->favicon().is_valid();
613
614 entry = controller_.GetLastCommittedEntry();
615 if (entry)
616 return entry->favicon().is_valid();
617
618 return false;
619}
620
[email protected]96d185d2009-04-24 03:28:54621bool TabContents::ShouldDisplayFavIcon() {
622 // Always display a throbber during pending loads.
623 if (controller_.GetLastCommittedEntry() && controller_.pending_entry())
624 return true;
625
626 DOMUI* dom_ui = GetDOMUIForCurrentState();
627 if (dom_ui)
628 return !dom_ui->hide_favicon();
629 return true;
630}
631
[email protected]2d5a3592010-01-30 04:29:52632bool TabContents::IsContentBlocked(ContentSettingsType content_type) const {
[email protected]8cc8d492010-02-02 10:40:49633 if (content_type == CONTENT_SETTINGS_TYPE_POPUPS)
634 return blocked_popups_ != NULL;
635
[email protected]0de80162010-02-03 04:52:35636 if (content_type == CONTENT_SETTINGS_TYPE_IMAGES ||
637 content_type == CONTENT_SETTINGS_TYPE_JAVASCRIPT ||
[email protected]3dbb80b2010-02-09 22:41:20638 content_type == CONTENT_SETTINGS_TYPE_PLUGINS ||
639 content_type == CONTENT_SETTINGS_TYPE_COOKIES)
[email protected]0de80162010-02-03 04:52:35640 return content_blocked_[content_type];
641
[email protected]b1235062010-03-25 17:31:27642 // TODO(joth): remove once fully implemented.
643 if (content_type == CONTENT_SETTINGS_TYPE_GEOLOCATION)
644 return false;
645
[email protected]3dbb80b2010-02-09 22:41:20646 NOTREACHED();
[email protected]2d5a3592010-01-30 04:29:52647 return false;
648}
649
[email protected]96d185d2009-04-24 03:28:54650std::wstring TabContents::GetStatusText() const {
651 if (!is_loading() || load_state_ == net::LOAD_STATE_IDLE)
652 return std::wstring();
653
654 switch (load_state_) {
655 case net::LOAD_STATE_WAITING_FOR_CACHE:
656 return l10n_util::GetString(IDS_LOAD_STATE_WAITING_FOR_CACHE);
657 case net::LOAD_STATE_RESOLVING_PROXY_FOR_URL:
658 return l10n_util::GetString(IDS_LOAD_STATE_RESOLVING_PROXY_FOR_URL);
659 case net::LOAD_STATE_RESOLVING_HOST:
660 return l10n_util::GetString(IDS_LOAD_STATE_RESOLVING_HOST);
661 case net::LOAD_STATE_CONNECTING:
662 return l10n_util::GetString(IDS_LOAD_STATE_CONNECTING);
663 case net::LOAD_STATE_SENDING_REQUEST:
[email protected]094e5b22009-09-25 04:23:56664 if (upload_size_)
665 return l10n_util::GetStringF(
666 IDS_LOAD_STATE_SENDING_REQUEST_WITH_PROGRESS,
667 static_cast<int>((100 * upload_position_) / upload_size_));
668 else
669 return l10n_util::GetString(IDS_LOAD_STATE_SENDING_REQUEST);
[email protected]96d185d2009-04-24 03:28:54670 case net::LOAD_STATE_WAITING_FOR_RESPONSE:
671 return l10n_util::GetStringF(IDS_LOAD_STATE_WAITING_FOR_RESPONSE,
672 load_state_host_);
673 // Ignore net::LOAD_STATE_READING_RESPONSE and net::LOAD_STATE_IDLE
674 case net::LOAD_STATE_IDLE:
675 case net::LOAD_STATE_READING_RESPONSE:
676 break;
677 }
678
679 return std::wstring();
680}
681
[email protected]d5f942ba2008-09-26 19:30:34682void TabContents::SetIsCrashed(bool state) {
683 if (state == is_crashed_)
684 return;
685
686 is_crashed_ = state;
[email protected]c9cd2222009-05-06 05:16:50687 NotifyNavigationStateChanged(INVALIDATE_TAB);
[email protected]d5f942ba2008-09-26 19:30:34688}
689
[email protected]744ef172009-10-16 21:53:46690void TabContents::PageActionStateChanged() {
691 NotifyNavigationStateChanged(TabContents::INVALIDATE_PAGE_ACTIONS);
[email protected]d7eaf572009-07-01 21:57:00692}
[email protected]ce5c4502009-05-06 16:46:11693
[email protected]d5f942ba2008-09-26 19:30:34694void TabContents::NotifyNavigationStateChanged(unsigned changed_flags) {
695 if (delegate_)
696 delegate_->NavigationStateChanged(this, changed_flags);
697}
698
[email protected]96d185d2009-04-24 03:28:54699void TabContents::DidBecomeSelected() {
700 controller_.SetActive(true);
[email protected]8cb5d5b2010-02-09 11:36:16701 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
702 if (rwhv) {
703 rwhv->DidBecomeSelected();
[email protected]789e9152009-08-04 21:59:43704#if defined(OS_MACOSX)
[email protected]8cb5d5b2010-02-09 11:36:16705 rwhv->SetActive(true);
[email protected]789e9152009-08-04 21:59:43706#endif
707 }
[email protected]96d185d2009-04-24 03:28:54708
[email protected]8cb5d5b2010-02-09 11:36:16709 WebCacheManager::GetInstance()->ObserveActivity(GetRenderProcessHost()->id());
[email protected]96d185d2009-04-24 03:28:54710}
711
712void TabContents::WasHidden() {
713 if (!capturing_contents()) {
714 // |render_view_host()| can be NULL if the user middle clicks a link to open
715 // a tab in then background, then closes the tab before selecting it. This
716 // is because closing the tab calls TabContents::Destroy(), which removes
717 // the |render_view_host()|; then when we actually destroy the window,
718 // OnWindowPosChanged() notices and calls HideContents() (which calls us).
[email protected]8cb5d5b2010-02-09 11:36:16719 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
720 if (rwhv)
721 rwhv->WasHidden();
[email protected]96d185d2009-04-24 03:28:54722 }
723
724 NotificationService::current()->Notify(
725 NotificationType::TAB_CONTENTS_HIDDEN,
726 Source<TabContents>(this),
727 NotificationService::NoDetails());
728}
729
[email protected]d5f942ba2008-09-26 19:30:34730void TabContents::Activate() {
731 if (delegate_)
732 delegate_->ActivateContents(this);
733}
734
[email protected]96d185d2009-04-24 03:28:54735void TabContents::ShowContents() {
[email protected]8cb5d5b2010-02-09 11:36:16736 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
737 if (rwhv)
738 rwhv->DidBecomeSelected();
[email protected]96d185d2009-04-24 03:28:54739}
740
741void TabContents::HideContents() {
742 // TODO(pkasting): http://b/1239839 Right now we purposefully don't call
743 // our superclass HideContents(), because some callers want to be very picky
744 // about the order in which these get called. In addition to making the code
745 // here practically impossible to understand, this also means we end up
746 // calling TabContents::WasHidden() twice if callers call both versions of
[email protected]57c6a652009-05-04 07:58:34747 // HideContents() on a TabContents.
[email protected]96d185d2009-04-24 03:28:54748 WasHidden();
749}
750
[email protected]c0588052008-10-27 23:01:50751void TabContents::OpenURL(const GURL& url, const GURL& referrer,
[email protected]d5f942ba2008-09-26 19:30:34752 WindowOpenDisposition disposition,
753 PageTransition::Type transition) {
754 if (delegate_)
[email protected]c0588052008-10-27 23:01:50755 delegate_->OpenURLFromTab(this, url, referrer, disposition, transition);
[email protected]d5f942ba2008-09-26 19:30:34756}
757
[email protected]1ccb3568d2010-02-19 10:51:16758bool TabContents::NavigateToPendingEntry(
759 NavigationController::ReloadType reload_type) {
[email protected]96d185d2009-04-24 03:28:54760 const NavigationEntry& entry = *controller_.pending_entry();
761
762 RenderViewHost* dest_render_view_host = render_manager_.Navigate(entry);
763 if (!dest_render_view_host)
764 return false; // Unable to create the desired render view host.
765
[email protected]0352e5472010-04-29 00:11:40766 // For security, we should never send non-DOM-UI URLs (other than about:blank)
767 // to a DOM UI renderer. Double check that here.
[email protected]54ec6472010-04-09 19:39:58768 int enabled_bindings = dest_render_view_host->enabled_bindings();
[email protected]0352e5472010-04-29 00:11:40769 bool is_allowed_in_dom_ui_renderer =
770 DOMUIFactory::UseDOMUIForURL(entry.url()) ||
771 entry.url() == GURL(chrome::kAboutBlankURL);
[email protected]54ec6472010-04-09 19:39:58772 CHECK(!BindingsPolicy::is_dom_ui_enabled(enabled_bindings) ||
[email protected]0352e5472010-04-29 00:11:40773 is_allowed_in_dom_ui_renderer);
[email protected]54ec6472010-04-09 19:39:58774
[email protected]96d185d2009-04-24 03:28:54775 // Tell DevTools agent that it is attached prior to the navigation.
[email protected]d9f9b792009-06-24 13:17:12776 DevToolsManager* devtools_manager = DevToolsManager::GetInstance();
[email protected]7e8e6b62009-05-08 11:28:32777 if (devtools_manager) { // NULL in unit tests.
[email protected]3cc72b12010-03-18 23:03:00778 devtools_manager->OnNavigatingToPendingEntry(render_view_host(),
779 dest_render_view_host,
780 entry.url());
[email protected]7e8e6b62009-05-08 11:28:32781 }
[email protected]96d185d2009-04-24 03:28:54782
783 // Used for page load time metrics.
784 current_load_start_ = base::TimeTicks::Now();
785
786 // Navigate in the desired RenderViewHost.
[email protected]056de2d2009-06-26 16:41:34787 ViewMsg_Navigate_Params navigate_params;
[email protected]3cc72b12010-03-18 23:03:00788 MakeNavigateParams(controller_, reload_type, &navigate_params);
[email protected]056de2d2009-06-26 16:41:34789 dest_render_view_host->Navigate(navigate_params);
[email protected]96d185d2009-04-24 03:28:54790
791 if (entry.page_id() == -1) {
792 // HACK!! This code suppresses javascript: URLs from being added to
793 // session history, which is what we want to do for javascript: URLs that
794 // do not generate content. What we really need is a message from the
795 // renderer telling us that a new page was not created. The same message
796 // could be used for mailto: URLs and the like.
797 if (entry.url().SchemeIs(chrome::kJavaScriptScheme))
798 return false;
799 }
800
801 // Clear any provisional password saves - this stops password infobars
802 // showing up on pages the user navigates to while the right page is
803 // loading.
804 GetPasswordManager()->ClearProvisionalSave();
805
[email protected]1ccb3568d2010-02-19 10:51:16806 if (reload_type != NavigationController::NO_RELOAD &&
807 !profile()->IsOffTheRecord()) {
[email protected]0189bc722009-08-28 21:56:48808 FaviconService* favicon_service =
809 profile()->GetFaviconService(Profile::IMPLICIT_ACCESS);
810 if (favicon_service)
811 favicon_service->SetFaviconOutOfDateForPage(entry.url());
[email protected]96d185d2009-04-24 03:28:54812 }
813
[email protected]a888b29e62010-04-01 13:38:57814 // The url likely changed, see if there is an extension whose extent contains
815 // the current page.
816 UpdateAppExtensionForCurrentPage();
817
[email protected]d5f942ba2008-09-26 19:30:34818 return true;
819}
820
[email protected]96d185d2009-04-24 03:28:54821void TabContents::Stop() {
822 render_manager_.Stop();
823 printing_.Stop();
824}
825
[email protected]96d185d2009-04-24 03:28:54826void TabContents::DisassociateFromPopupCount() {
827 render_view_host()->DisassociateFromPopupCount();
828}
829
830TabContents* TabContents::Clone() {
831 // We create a new SiteInstance so that the new tab won't share processes
832 // with the old one. This can be changed in the future if we need it to share
833 // processes for some reason.
[email protected]420ae012009-04-24 05:16:32834 TabContents* tc = new TabContents(profile(),
[email protected]96d185d2009-04-24 03:28:54835 SiteInstance::CreateSiteInstance(profile()),
[email protected]12636df2009-09-28 22:32:21836 MSG_ROUTING_NONE, this);
[email protected]96d185d2009-04-24 03:28:54837 tc->controller().CopyStateFrom(controller_);
838 return tc;
839}
840
[email protected]4d677202009-07-19 07:37:12841void TabContents::ShowPageInfo(const GURL& url,
842 const NavigationEntry::SSLStatus& ssl,
843 bool show_history) {
844 if (!delegate_)
845 return;
846
[email protected]bb678332009-07-21 00:15:50847 delegate_->ShowPageInfo(profile(), url, ssl, show_history);
[email protected]4d677202009-07-19 07:37:12848}
849
initial.commit09911bf2008-07-26 23:55:29850ConstrainedWindow* TabContents::CreateConstrainedDialog(
[email protected]e8382172009-06-19 22:16:28851 ConstrainedWindowDelegate* delegate) {
initial.commit09911bf2008-07-26 23:55:29852 ConstrainedWindow* window =
[email protected]e8382172009-06-19 22:16:28853 ConstrainedWindow::CreateConstrainedDialog(this, delegate);
initial.commit09911bf2008-07-26 23:55:29854 child_windows_.push_back(window);
[email protected]fa1cf0b82010-01-15 21:49:44855
856 if (child_windows_.size() == 1) {
857 window->ShowConstrainedWindow();
858 BlockTabContent(true);
859 }
860
initial.commit09911bf2008-07-26 23:55:29861 return window;
862}
863
[email protected]fa1cf0b82010-01-15 21:49:44864void TabContents::BlockTabContent(bool blocked) {
[email protected]8dccd7cb2010-02-25 05:19:55865 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
866 if (rwhv)
867 rwhv->SetVisuallyDeemphasized(blocked);
[email protected]fa1cf0b82010-01-15 21:49:44868 render_view_host()->set_ignore_input_events(blocked);
869 if (delegate_)
870 delegate_->SetTabContentBlocked(this, blocked);
871}
872
initial.commit09911bf2008-07-26 23:55:29873void TabContents::AddNewContents(TabContents* new_contents,
874 WindowOpenDisposition disposition,
875 const gfx::Rect& initial_pos,
[email protected]7e7414ae2010-01-26 20:19:29876 bool user_gesture) {
initial.commit09911bf2008-07-26 23:55:29877 if (!delegate_)
878 return;
879
[email protected]35f7d212009-04-29 21:19:27880 if ((disposition == NEW_POPUP) && !user_gesture &&
881 !CommandLine::ForCurrentProcess()->HasSwitch(
882 switches::kDisablePopupBlocking)) {
[email protected]2c4410d2009-05-06 23:46:22883 // Unrequested popups from normal pages are constrained unless they're in
[email protected]b6c874582009-05-08 19:38:31884 // the whitelist. The popup owner will handle checking this.
[email protected]7e7414ae2010-01-26 20:19:29885 delegate_->GetConstrainingContents(this)->AddPopup(
886 new_contents, initial_pos);
[email protected]b6c874582009-05-08 19:38:31887 } else {
[email protected]304f8792009-08-19 21:28:09888#if defined(OS_CHROMEOS)
889 if (disposition == NEW_POPUP) {
890 // If the popup is bigger than a given factor of the screen, then
891 // turn it into a foreground tab (on chrome os only)
892 GdkScreen* screen = gdk_screen_get_default();
893 int max_width = gdk_screen_get_width(screen) * kMaxWidthFactor;
894 int max_height = gdk_screen_get_height(screen) * kMaxHeightFactor;
895 if (initial_pos.width() > max_width ||
896 initial_pos.height() > max_height) {
897 disposition = NEW_FOREGROUND_TAB;
898 }
899 }
900#endif
901
[email protected]0aa55312008-10-17 21:53:08902 new_contents->DisassociateFromPopupCount();
initial.commit09911bf2008-07-26 23:55:29903 delegate_->AddNewContents(this, new_contents, disposition, initial_pos,
904 user_gesture);
[email protected]7f37dd92009-09-15 16:35:49905 NotificationService::current()->Notify(
906 NotificationType::TAB_ADDED,
907 Source<TabContentsDelegate>(delegate_),
908 Details<TabContents>(this));
initial.commit09911bf2008-07-26 23:55:29909 }
initial.commit09911bf2008-07-26 23:55:29910
[email protected]8cc8d492010-02-02 10:40:49911 // TODO(pkasting): Why is this necessary?
912 PopupNotificationVisibilityChanged(blocked_popups_ != NULL);
initial.commit09911bf2008-07-26 23:55:29913}
914
[email protected]bcd9dc42010-01-23 16:38:18915bool TabContents::ExecuteCode(int request_id, const std::string& extension_id,
[email protected]61f5a7b2009-12-22 22:21:20916 const std::vector<URLPattern>& host_permissions,
[email protected]20ad2692009-11-20 18:27:20917 bool is_js_code, const std::string& code_string,
918 bool all_frames) {
[email protected]ebd0eae72010-02-16 23:50:51919 RenderViewHost* host = render_view_host();
920 if (!host)
921 return false;
922
923 return host->Send(new ViewMsg_ExecuteCode(host->routing_id(),
924 ViewMsg_ExecuteCode_Params(request_id, extension_id, host_permissions,
925 is_js_code, code_string, all_frames)));
[email protected]912256b32009-09-18 09:47:35926}
927
[email protected]96d185d2009-04-24 03:28:54928void TabContents::PopupNotificationVisibilityChanged(bool visible) {
[email protected]8cc8d492010-02-02 10:40:49929 if (is_being_destroyed_)
930 return;
931 if (!dont_notify_render_view_)
932 render_view_host()->PopupNotificationVisibilityChanged(visible);
[email protected]0de80162010-02-03 04:52:35933 if (delegate_)
[email protected]20ad6a92010-04-21 14:07:24934 delegate_->OnContentSettingsChange(this);
[email protected]96d185d2009-04-24 03:28:54935}
936
[email protected]5c9e97a2009-09-09 23:48:30937gfx::NativeView TabContents::GetContentNativeView() const {
[email protected]96d185d2009-04-24 03:28:54938 return view_->GetContentNativeView();
939}
940
941gfx::NativeView TabContents::GetNativeView() const {
942 return view_->GetNativeView();
943}
944
945void TabContents::GetContainerBounds(gfx::Rect *out) const {
946 view_->GetContainerBounds(out);
947}
948
949void TabContents::Focus() {
950 view_->Focus();
951}
952
[email protected]90daadb42009-06-08 21:27:28953void TabContents::FocusThroughTabTraversal(bool reverse) {
[email protected]7e383692009-06-12 19:14:54954 if (showing_interstitial_page()) {
955 render_manager_.interstitial_page()->FocusThroughTabTraversal(reverse);
956 return;
957 }
[email protected]96d185d2009-04-24 03:28:54958 render_view_host()->SetInitialFocus(reverse);
959}
960
961bool TabContents::FocusLocationBarByDefault() {
962 DOMUI* dom_ui = GetDOMUIForCurrentState();
963 if (dom_ui)
964 return dom_ui->focus_location_bar_by_default();
[email protected]3e3f0eb2009-06-22 18:33:43965 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]e0d481582009-09-15 21:06:25966 if (entry && entry->url() == GURL(chrome::kAboutBlankURL))
[email protected]3e3f0eb2009-06-22 18:33:43967 return true;
[email protected]96d185d2009-04-24 03:28:54968 return false;
969}
970
[email protected]a26dc362010-04-23 01:48:58971void TabContents::SetFocusToLocationBar(bool select_all) {
[email protected]a11aaf22010-03-30 00:03:38972 if (delegate())
[email protected]a26dc362010-04-23 01:48:58973 delegate()->SetFocusToLocationBar(select_all);
[email protected]a11aaf22010-03-30 00:03:38974}
975
[email protected]616ed5a2008-11-21 22:27:24976void TabContents::AddInfoBar(InfoBarDelegate* delegate) {
[email protected]f7019302010-03-26 19:58:32977 if (delegate_ && !delegate_->infobars_enabled()) {
978 delegate->InfoBarClosed();
979 return;
980 }
981
[email protected]616ed5a2008-11-21 22:27:24982 // Look through the existing InfoBarDelegates we have for a match. If we've
983 // already got one that matches, then we don't add the new one.
[email protected]f86a07022008-11-25 01:06:05984 for (int i = 0; i < infobar_delegate_count(); ++i) {
[email protected]6d27a7b2009-12-18 23:25:45985 if (GetInfoBarDelegateAt(i)->EqualsDelegate(delegate)) {
986 // Tell the new infobar to close so that it can clean itself up.
987 delegate->InfoBarClosed();
[email protected]616ed5a2008-11-21 22:27:24988 return;
[email protected]6d27a7b2009-12-18 23:25:45989 }
[email protected]616ed5a2008-11-21 22:27:24990 }
991
992 infobar_delegates_.push_back(delegate);
[email protected]bfd04a62009-02-01 18:16:56993 NotificationService::current()->Notify(
994 NotificationType::TAB_CONTENTS_INFOBAR_ADDED,
995 Source<TabContents>(this),
996 Details<InfoBarDelegate>(delegate));
[email protected]616ed5a2008-11-21 22:27:24997
998 // Add ourselves as an observer for navigations the first time a delegate is
999 // added. We use this notification to expire InfoBars that need to expire on
1000 // page transitions.
1001 if (infobar_delegates_.size() == 1) {
[email protected]bfd04a62009-02-01 18:16:561002 registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED,
[email protected]ce3fa3c2009-04-20 19:55:571003 Source<NavigationController>(&controller_));
[email protected]616ed5a2008-11-21 22:27:241004 }
1005}
1006
1007void TabContents::RemoveInfoBar(InfoBarDelegate* delegate) {
[email protected]f7019302010-03-26 19:58:321008 if (delegate_ && !delegate_->infobars_enabled()) {
1009 return;
1010 }
1011
[email protected]616ed5a2008-11-21 22:27:241012 std::vector<InfoBarDelegate*>::iterator it =
1013 find(infobar_delegates_.begin(), infobar_delegates_.end(), delegate);
1014 if (it != infobar_delegates_.end()) {
1015 InfoBarDelegate* delegate = *it;
[email protected]bfd04a62009-02-01 18:16:561016 NotificationService::current()->Notify(
1017 NotificationType::TAB_CONTENTS_INFOBAR_REMOVED,
1018 Source<TabContents>(this),
1019 Details<InfoBarDelegate>(delegate));
[email protected]f86a07022008-11-25 01:06:051020 infobar_delegates_.erase(it);
[email protected]616ed5a2008-11-21 22:27:241021
[email protected]6a02963e2009-01-06 16:58:031022 // Remove ourselves as an observer if we are tracking no more InfoBars.
1023 if (infobar_delegates_.empty()) {
[email protected]bfd04a62009-02-01 18:16:561024 registrar_.Remove(this, NotificationType::NAV_ENTRY_COMMITTED,
[email protected]ce3fa3c2009-04-20 19:55:571025 Source<NavigationController>(&controller_));
[email protected]6a02963e2009-01-06 16:58:031026 }
[email protected]616ed5a2008-11-21 22:27:241027 }
1028}
[email protected]3edd9522009-03-04 22:19:411029
[email protected]f81821c2009-08-07 01:29:471030void TabContents::ReplaceInfoBar(InfoBarDelegate* old_delegate,
1031 InfoBarDelegate* new_delegate) {
[email protected]f7019302010-03-26 19:58:321032 if (delegate_ && !delegate_->infobars_enabled()) {
1033 new_delegate->InfoBarClosed();
1034 return;
1035 }
1036
[email protected]f81821c2009-08-07 01:29:471037 std::vector<InfoBarDelegate*>::iterator it =
1038 find(infobar_delegates_.begin(), infobar_delegates_.end(), old_delegate);
1039 DCHECK(it != infobar_delegates_.end());
1040
1041 // Notify the container about the change of plans.
1042 scoped_ptr<std::pair<InfoBarDelegate*, InfoBarDelegate*> > details(
1043 new std::pair<InfoBarDelegate*, InfoBarDelegate*>(
1044 old_delegate, new_delegate));
1045 NotificationService::current()->Notify(
1046 NotificationType::TAB_CONTENTS_INFOBAR_REPLACED,
1047 Source<TabContents>(this),
1048 Details<std::pair<InfoBarDelegate*, InfoBarDelegate*> >(details.get()));
1049
1050 // Remove the old one.
1051 infobar_delegates_.erase(it);
1052
1053 // Add the new one.
1054 infobar_delegates_.push_back(new_delegate);
1055}
1056
[email protected]20ede072009-10-06 00:28:351057bool TabContents::ShouldShowBookmarkBar() {
[email protected]aa9196f2009-10-22 16:20:271058 if (showing_interstitial_page())
1059 return false;
1060
[email protected]96d185d2009-04-24 03:28:541061 // See GetDOMUIForCurrentState() comment for more info. This case is very
1062 // similar, but for non-first loads, we want to use the committed entry. This
1063 // is so the bookmarks bar disappears at the same time the page does.
1064 if (controller_.GetLastCommittedEntry()) {
1065 // Not the first load, always use the committed DOM UI.
1066 if (render_manager_.dom_ui())
1067 return render_manager_.dom_ui()->force_bookmark_bar_visible();
1068 return false; // Default.
1069 }
1070
1071 // When it's the first load, we know either the pending one or the committed
1072 // one will have the DOM UI in it (see GetDOMUIForCurrentState), and only one
1073 // of them will be valid, so we can just check both.
1074 if (render_manager_.pending_dom_ui())
1075 return render_manager_.pending_dom_ui()->force_bookmark_bar_visible();
1076 if (render_manager_.dom_ui())
1077 return render_manager_.dom_ui()->force_bookmark_bar_visible();
1078 return false; // Default.
1079}
1080
[email protected]d8f16ae2009-08-30 03:47:261081bool TabContents::IsExtensionShelfAlwaysVisible() {
1082 // See GetDOMUIForCurrentState() comment for more info. This case is very
1083 // similar, but for non-first loads, we want to use the committed entry. This
1084 // is so the bookmarks bar disappears at the same time the page does.
1085 if (controller_.GetLastCommittedEntry()) {
1086 // Not the first load, always use the committed DOM UI.
1087 if (render_manager_.dom_ui())
1088 return render_manager_.dom_ui()->force_extension_shelf_visible();
1089 return false; // Default.
1090 }
1091
1092 // When it's the first load, we know either the pending one or the committed
1093 // one will have the DOM UI in it (see GetDOMUIForCurrentState), and only one
1094 // of them will be valid, so we can just check both.
1095 if (render_manager_.pending_dom_ui())
1096 return render_manager_.pending_dom_ui()->force_extension_shelf_visible();
1097 if (render_manager_.dom_ui())
1098 return render_manager_.dom_ui()->force_extension_shelf_visible();
1099 return false; // Default.
1100}
1101
[email protected]3edd9522009-03-04 22:19:411102void TabContents::ToolbarSizeChanged(bool is_animating) {
1103 TabContentsDelegate* d = delegate();
1104 if (d)
1105 d->ToolbarSizeChanged(this, is_animating);
1106}
[email protected]616ed5a2008-11-21 22:27:241107
[email protected]fc6fb7fb2009-11-07 02:35:041108bool TabContents::CanDownload(int request_id) {
1109 TabContentsDelegate* d = delegate();
1110 if (d)
1111 return d->CanDownload(request_id);
1112 return true;
1113}
1114
[email protected]d5f942ba2008-09-26 19:30:341115void TabContents::OnStartDownload(DownloadItem* download) {
1116 DCHECK(download);
[email protected]d5f942ba2008-09-26 19:30:341117
1118 // Download in a constrained popup is shown in the tab that opened it.
[email protected]b6c874582009-05-08 19:38:311119 TabContents* tab_contents = delegate()->GetConstrainingContents(this);
[email protected]d5f942ba2008-09-26 19:30:341120
[email protected]c70f9b82010-04-21 07:31:111121 if (tab_contents && tab_contents->delegate()) {
[email protected]59560e0b2009-06-04 03:30:221122 tab_contents->delegate()->OnStartDownload(download);
[email protected]c70f9b82010-04-21 07:31:111123 // If the download occurs in a new tab, close it
1124 if (controller_.IsInitialNavigation() && (tab_contents == this))
1125 delegate()->CloseContents(this);
1126 }
[email protected]3edd9522009-03-04 22:19:411127}
[email protected]3edd9522009-03-04 22:19:411128
[email protected]d5f942ba2008-09-26 19:30:341129void TabContents::WillClose(ConstrainedWindow* window) {
1130 ConstrainedWindowList::iterator it =
1131 find(child_windows_.begin(), child_windows_.end(), window);
[email protected]fa1cf0b82010-01-15 21:49:441132 bool removed_topmost_window = it == child_windows_.begin();
[email protected]d5f942ba2008-09-26 19:30:341133 if (it != child_windows_.end())
1134 child_windows_.erase(it);
[email protected]55098792010-03-24 18:20:371135 if (child_windows_.size() > 0) {
1136 if (removed_topmost_window) {
1137 child_windows_[0]->ShowConstrainedWindow();
1138 }
[email protected]fa1cf0b82010-01-15 21:49:441139 BlockTabContent(true);
1140 } else {
1141 BlockTabContent(false);
1142 }
[email protected]66ba4932009-06-04 19:22:131143}
[email protected]d5f942ba2008-09-26 19:30:341144
[email protected]66ba4932009-06-04 19:22:131145void TabContents::WillCloseBlockedPopupContainer(
1146 BlockedPopupContainer* container) {
1147 DCHECK(blocked_popups_ == container);
1148 blocked_popups_ = NULL;
[email protected]8cc8d492010-02-02 10:40:491149 PopupNotificationVisibilityChanged(false);
[email protected]d5f942ba2008-09-26 19:30:341150}
1151
[email protected]d5f942ba2008-09-26 19:30:341152void TabContents::DidMoveOrResize(ConstrainedWindow* window) {
[email protected]332af7732009-03-11 13:21:351153#if defined(OS_WIN)
[email protected]92edc472009-02-10 20:32:061154 UpdateWindow(GetNativeView());
[email protected]332af7732009-03-11 13:21:351155#endif
[email protected]d5f942ba2008-09-26 19:30:341156}
1157
[email protected]c1dc9f132009-11-20 23:13:211158void TabContents::StartFinding(string16 search_string,
[email protected]e491f1c2009-05-22 20:28:121159 bool forward_direction,
1160 bool case_sensitive) {
[email protected]c1dc9f132009-11-20 23:13:211161 // If search_string is empty, it means FindNext was pressed with a keyboard
[email protected]6e95706d2009-04-23 22:55:011162 // shortcut so unless we have something to search for we return early.
[email protected]c1dc9f132009-11-20 23:13:211163 if (search_string.empty() && find_text_.empty()) {
[email protected]e5d2f7132010-04-09 18:13:531164 string16 last_search_prepopulate_text =
1165 FindBarState::GetLastPrepopulateText(profile());
1166
[email protected]87fae512009-12-11 02:20:211167 // Try the last thing we searched for on this tab, then the last thing
1168 // searched for on any tab.
1169 if (!previous_find_text_.empty())
1170 search_string = previous_find_text_;
[email protected]e5d2f7132010-04-09 18:13:531171 else if (!last_search_prepopulate_text.empty())
1172 search_string = last_search_prepopulate_text;
[email protected]87fae512009-12-11 02:20:211173 else
[email protected]e491f1c2009-05-22 20:28:121174 return;
[email protected]e491f1c2009-05-22 20:28:121175 }
[email protected]6e95706d2009-04-23 22:55:011176
[email protected]101c90e2009-10-26 21:14:441177 // Keep track of the previous search.
1178 previous_find_text_ = find_text_;
1179
[email protected]6e95706d2009-04-23 22:55:011180 // This is a FindNext operation if we are searching for the same text again,
1181 // or if the passed in search text is empty (FindNext keyboard shortcut). The
1182 // exception to this is if the Find was aborted (then we don't want FindNext
1183 // because the highlighting has been cleared and we need it to reappear). We
1184 // therefore treat FindNext after an aborted Find operation as a full fledged
1185 // Find.
[email protected]c1dc9f132009-11-20 23:13:211186 bool find_next = (find_text_ == search_string || search_string.empty()) &&
[email protected]e491f1c2009-05-22 20:28:121187 (last_search_case_sensitive_ == case_sensitive) &&
[email protected]6e95706d2009-04-23 22:55:011188 !find_op_aborted_;
1189 if (!find_next)
1190 current_find_request_id_ = find_request_id_counter_++;
1191
[email protected]c1dc9f132009-11-20 23:13:211192 if (!search_string.empty())
1193 find_text_ = search_string;
[email protected]e491f1c2009-05-22 20:28:121194 last_search_case_sensitive_ = case_sensitive;
[email protected]6e95706d2009-04-23 22:55:011195
1196 find_op_aborted_ = false;
1197
1198 // Keep track of what the last search was across the tabs.
[email protected]e5d2f7132010-04-09 18:13:531199 FindBarState* find_bar_state = profile()->GetFindBarState();
1200 find_bar_state->set_last_prepopulate_text(find_text_);
[email protected]6e95706d2009-04-23 22:55:011201 render_view_host()->StartFinding(current_find_request_id_,
1202 find_text_,
1203 forward_direction,
[email protected]e491f1c2009-05-22 20:28:121204 case_sensitive,
[email protected]6e95706d2009-04-23 22:55:011205 find_next);
1206}
1207
[email protected]5bc8fe92010-03-11 18:19:001208void TabContents::StopFinding(
1209 FindBarController::SelectionAction selection_action) {
1210 // When the action is kClearAction, it means the find string has been cleared
[email protected]2d2dffc2009-06-09 21:41:001211 // by the user, but the UI has not been dismissed.
[email protected]5bc8fe92010-03-11 18:19:001212 if (selection_action != FindBarController::kClearSelection)
[email protected]2d2dffc2009-06-09 21:41:001213 find_ui_active_ = false;
[email protected]726bdbd72010-04-05 17:08:071214 if (!find_text_.empty())
1215 previous_find_text_ = find_text_;
[email protected]101c90e2009-10-26 21:14:441216 find_text_.clear();
[email protected]6e95706d2009-04-23 22:55:011217 find_op_aborted_ = true;
[email protected]e491f1c2009-05-22 20:28:121218 last_search_result_ = FindNotificationDetails();
[email protected]5bc8fe92010-03-11 18:19:001219 render_view_host()->StopFinding(selection_action);
[email protected]6e95706d2009-04-23 22:55:011220}
1221
[email protected]420ae012009-04-24 05:16:321222void TabContents::OnSavePage() {
1223 // If we can not save the page, try to download it.
1224 if (!SavePackage::IsSavableContents(contents_mime_type())) {
1225 DownloadManager* dlm = profile()->GetDownloadManager();
1226 const GURL& current_page_url = GetURL();
1227 if (dlm && current_page_url.is_valid())
[email protected]d82ed61e2009-06-16 02:46:221228 dlm->DownloadUrl(current_page_url, GURL(), "", this);
[email protected]420ae012009-04-24 05:16:321229 return;
1230 }
1231
1232 Stop();
1233
1234 // Create the save package and possibly prompt the user for the name to save
1235 // the page as. The user prompt is an asynchronous operation that runs on
1236 // another thread.
[email protected]d82ed61e2009-06-16 02:46:221237 save_package_ = new SavePackage(this);
[email protected]420ae012009-04-24 05:16:321238 save_package_->GetSaveInfo();
1239}
1240
1241// Used in automated testing to bypass prompting the user for file names.
1242// Instead, the names and paths are hard coded rather than running them through
1243// file name sanitation and extension / mime checking.
[email protected]828cabe2009-09-26 22:47:111244bool TabContents::SavePage(const FilePath& main_file, const FilePath& dir_path,
[email protected]420ae012009-04-24 05:16:321245 SavePackage::SavePackageType save_type) {
1246 // Stop the page from navigating.
1247 Stop();
1248
[email protected]828cabe2009-09-26 22:47:111249 save_package_ = new SavePackage(this, save_type, main_file, dir_path);
[email protected]cdf6cc82009-08-04 21:48:591250 return save_package_->Init();
[email protected]420ae012009-04-24 05:16:321251}
1252
[email protected]89d6e6e2009-12-03 23:06:511253void TabContents::EmailPageLocation() {
1254 std::string title = EscapeQueryParamValue(UTF16ToUTF8(GetTitle()), false);
1255 std::string page_url = EscapeQueryParamValue(GetURL().spec(), false);
1256 std::string mailto = std::string("mailto:?subject=Fwd:%20") +
1257 title + "&body=%0A%0A" + page_url;
1258 platform_util::OpenExternal(GURL(mailto));
1259}
1260
[email protected]420ae012009-04-24 05:16:321261void TabContents::PrintPreview() {
1262 // We don't show the print preview yet, only the print dialog.
1263 PrintNow();
1264}
1265
1266bool TabContents::PrintNow() {
1267 // We can't print interstitial page for now.
1268 if (showing_interstitial_page())
1269 return false;
1270
1271 return render_view_host()->PrintPages();
1272}
1273
[email protected]82270452009-06-19 15:58:011274void TabContents::PrintingDone(int document_cookie, bool success) {
1275 render_view_host()->PrintingDone(document_cookie, success);
1276}
1277
[email protected]420ae012009-04-24 05:16:321278bool TabContents::IsActiveEntry(int32 page_id) {
1279 NavigationEntry* active_entry = controller_.GetActiveEntry();
1280 return (active_entry != NULL &&
1281 active_entry->site_instance() == GetSiteInstance() &&
1282 active_entry->page_id() == page_id);
1283}
1284
[email protected]b2fe07d12010-02-09 14:38:081285void TabContents::SetOverrideEncoding(const std::string& encoding) {
[email protected]8cb5d5b2010-02-09 11:36:161286 set_encoding(encoding);
1287 render_view_host()->SetPageEncoding(encoding);
1288}
1289
[email protected]b2fe07d12010-02-09 14:38:081290void TabContents::ResetOverrideEncoding() {
[email protected]8cb5d5b2010-02-09 11:36:161291 reset_encoding();
1292 render_view_host()->ResetPageEncodingToDefault();
1293}
1294
1295void TabContents::WindowMoveOrResizeStarted() {
1296 render_view_host()->WindowMoveOrResizeStarted();
1297}
1298
[email protected]3f8eb7f2009-10-02 23:58:051299void TabContents::LogNewTabTime(const std::string& event_name) {
1300 // Not all new tab pages get timed. In those cases, we don't have a
1301 // new_tab_start_time_.
1302 if (new_tab_start_time_.is_null())
1303 return;
1304
1305 base::TimeDelta duration = base::TimeTicks::Now() - new_tab_start_time_;
[email protected]84abba62009-10-07 17:01:441306 MetricEventDurationDetails details(event_name,
1307 static_cast<int>(duration.InMilliseconds()));
1308
[email protected]7ab1e7d62009-10-14 23:32:011309 if (event_name == "Tab.NewTabScriptStart") {
1310 UMA_HISTOGRAM_TIMES("Tab.NewTabScriptStart", duration);
1311 } else if (event_name == "Tab.NewTabDOMContentLoaded") {
1312 UMA_HISTOGRAM_TIMES("Tab.NewTabDOMContentLoaded", duration);
1313 } else if (event_name == "Tab.NewTabOnload") {
1314 UMA_HISTOGRAM_TIMES("Tab.NewTabOnload", duration);
[email protected]3f8eb7f2009-10-02 23:58:051315 // The new tab page has finished loading; reset it.
1316 new_tab_start_time_ = base::TimeTicks();
[email protected]84abba62009-10-07 17:01:441317 } else {
1318 NOTREACHED();
[email protected]3f8eb7f2009-10-02 23:58:051319 }
[email protected]84abba62009-10-07 17:01:441320 NotificationService::current()->Notify(
1321 NotificationType::METRIC_EVENT_DURATION,
1322 Source<TabContents>(this),
1323 Details<MetricEventDurationDetails>(&details));
[email protected]3f8eb7f2009-10-02 23:58:051324}
1325
[email protected]7ab1e7d62009-10-14 23:32:011326void TabContents::OnCloseStarted() {
1327 if (tab_close_start_time_.is_null())
1328 tab_close_start_time_ = base::TimeTicks::Now();
1329}
1330
[email protected]c37e3b62010-01-22 00:15:171331TabContents* TabContents::CloneAndMakePhantom() {
[email protected]c37e3b62010-01-22 00:15:171332 TabNavigation tab_nav;
[email protected]df72ac72010-02-06 00:43:161333
1334 NavigationEntry* entry = controller().GetActiveEntry();
1335 if (app_extension_)
[email protected]867a73e12010-03-19 20:45:461336 tab_nav.set_url(app_extension_->GetFullLaunchURL());
[email protected]df72ac72010-02-06 00:43:161337 else if (entry)
1338 tab_nav.SetFromNavigationEntry(*entry);
1339
[email protected]c37e3b62010-01-22 00:15:171340 std::vector<TabNavigation> navigations;
1341 navigations.push_back(tab_nav);
1342
1343 TabContents* new_contents =
1344 new TabContents(profile(), NULL, MSG_ROUTING_NONE, NULL);
[email protected]3b355642010-02-05 16:01:491345 new_contents->SetAppExtension(app_extension_);
[email protected]c37e3b62010-01-22 00:15:171346 new_contents->controller().RestoreFromState(navigations, 0, false);
[email protected]c37e3b62010-01-22 00:15:171347
[email protected]df72ac72010-02-06 00:43:161348 if (!app_extension_ && entry)
1349 new_contents->controller().GetActiveEntry()->favicon() = entry->favicon();
1350
[email protected]c37e3b62010-01-22 00:15:171351 return new_contents;
1352}
1353
[email protected]0de80162010-02-03 04:52:351354// Resets the |content_blocked_| array.
1355void TabContents::ClearBlockedContentSettings() {
[email protected]15cf526b2010-02-12 06:33:491356 for (size_t i = 0; i < arraysize(content_blocked_); ++i)
[email protected]0de80162010-02-03 04:52:351357 content_blocked_[i] = false;
1358}
1359
[email protected]614c0e12010-03-25 10:12:561360// Resets the |geolocation_settings_| map.
[email protected]9d062b12010-04-14 09:36:551361void TabContents::ClearGeolocationContentSettings(
1362 const NavigationController::LoadCommittedDetails& details) {
1363 if (!geolocation_content_settings_.empty()) {
1364 // Clear the geolocation settings only if we're going to a new origin,
1365 // or if we're staying at the same origin and its setting is ASK.
1366 // This is to prevent clearing the icon by just changing some URL param.
1367 // TODO(bulach): refactor this logic into its own class and remove the
1368 // duplication from ContentSettingDomainListBubbleModel.
1369 if (!details.entry ||
1370 details.previous_url.GetOrigin() != details.entry->url().GetOrigin() ||
1371 (details.entry->url().is_valid() &&
1372 profile()->GetGeolocationContentSettingsMap()->GetContentSetting(
1373 details.entry->url(), details.entry->url()) ==
1374 CONTENT_SETTING_ASK)) {
1375 geolocation_content_settings_.clear();
1376 }
1377 }
[email protected]614c0e12010-03-25 10:12:561378}
1379
[email protected]96d185d2009-04-24 03:28:541380// Notifies the RenderWidgetHost instance about the fact that the page is
1381// loading, or done loading and calls the base implementation.
1382void TabContents::SetIsLoading(bool is_loading,
1383 LoadNotificationDetails* details) {
1384 if (is_loading == is_loading_)
1385 return;
1386
1387 if (!is_loading) {
1388 load_state_ = net::LOAD_STATE_IDLE;
1389 load_state_host_.clear();
[email protected]094e5b22009-09-25 04:23:561390 upload_size_ = 0;
1391 upload_position_ = 0;
[email protected]96d185d2009-04-24 03:28:541392 }
1393
1394 render_manager_.SetIsLoading(is_loading);
1395
1396 is_loading_ = is_loading;
1397 waiting_for_response_ = is_loading;
1398
1399 if (delegate_)
1400 delegate_->LoadingStateChanged(this);
[email protected]329581b2009-04-28 06:52:351401 NotifyNavigationStateChanged(INVALIDATE_LOAD);
[email protected]96d185d2009-04-24 03:28:541402
1403 NotificationType type = is_loading ? NotificationType::LOAD_START :
1404 NotificationType::LOAD_STOP;
[email protected]ce5c4502009-05-06 16:46:111405 NotificationDetails det = NotificationService::NoDetails();
[email protected]96d185d2009-04-24 03:28:541406 if (details)
1407 det = Details<LoadNotificationDetails>(details);
1408 NotificationService::current()->Notify(type,
1409 Source<NavigationController>(&controller_),
1410 det);
1411}
1412
[email protected]b6c874582009-05-08 19:38:311413void TabContents::AddPopup(TabContents* new_contents,
[email protected]7e7414ae2010-01-26 20:19:291414 const gfx::Rect& initial_pos) {
[email protected]8cc8d492010-02-02 10:40:491415 GURL url(GetURL());
1416 if (url.is_valid() &&
1417 profile()->GetHostContentSettingsMap()->GetContentSetting(
[email protected]da0c8e92010-02-09 23:25:491418 url, CONTENT_SETTINGS_TYPE_POPUPS) == CONTENT_SETTING_ALLOW) {
[email protected]8cc8d492010-02-02 10:40:491419 AddNewContents(new_contents, NEW_POPUP, initial_pos, true);
1420 } else {
1421 if (!blocked_popups_)
1422 blocked_popups_ = new BlockedPopupContainer(this);
1423 blocked_popups_->AddTabContents(new_contents, initial_pos);
1424 }
[email protected]b9681312008-11-07 00:08:261425}
[email protected]616ed5a2008-11-21 22:27:241426
1427namespace {
1428bool TransitionIsReload(PageTransition::Type transition) {
1429 return PageTransition::StripQualifier(transition) == PageTransition::RELOAD;
1430}
1431}
1432
1433void TabContents::ExpireInfoBars(
1434 const NavigationController::LoadCommittedDetails& details) {
1435 // Only hide InfoBars when the user has done something that makes the main
1436 // frame load. We don't want various automatic or subframe navigations making
1437 // it disappear.
1438 if (!details.is_user_initiated_main_frame_load())
1439 return;
1440
[email protected]f86a07022008-11-25 01:06:051441 for (int i = infobar_delegate_count() - 1; i >= 0; --i) {
[email protected]616ed5a2008-11-21 22:27:241442 InfoBarDelegate* delegate = GetInfoBarDelegateAt(i);
[email protected]f86a07022008-11-25 01:06:051443 if (delegate->ShouldExpire(details))
[email protected]616ed5a2008-11-21 22:27:241444 RemoveInfoBar(delegate);
[email protected]616ed5a2008-11-21 22:27:241445 }
1446}
[email protected]fdd61c62009-04-22 19:22:571447
[email protected]96d185d2009-04-24 03:28:541448DOMUI* TabContents::GetDOMUIForCurrentState() {
1449 // When there is a pending navigation entry, we want to use the pending DOMUI
1450 // that goes along with it to control the basic flags. For example, we want to
1451 // show the pending URL in the URL bar, so we want the display_url flag to
1452 // be from the pending entry.
1453 //
1454 // The confusion comes because there are multiple possibilities for the
1455 // initial load in a tab as a side effect of the way the RenderViewHostManager
1456 // works.
1457 //
1458 // - For the very first tab the load looks "normal". The new tab DOM UI is
1459 // the pending one, and we want it to apply here.
1460 //
1461 // - For subsequent new tabs, they'll get a new SiteInstance which will then
1462 // get switched to the one previously associated with the new tab pages.
1463 // This switching will cause the manager to commit the RVH/DOMUI. So we'll
1464 // have a committed DOM UI in this case.
1465 //
1466 // This condition handles all of these cases:
1467 //
1468 // - First load in first tab: no committed nav entry + pending nav entry +
1469 // pending dom ui:
1470 // -> Use pending DOM UI if any.
1471 //
1472 // - First load in second tab: no committed nav entry + pending nav entry +
1473 // no pending DOM UI:
1474 // -> Use the committed DOM UI if any.
1475 //
1476 // - Second navigation in any tab: committed nav entry + pending nav entry:
1477 // -> Use pending DOM UI if any.
1478 //
1479 // - Normal state with no load: committed nav entry + no pending nav entry:
1480 // -> Use committed DOM UI.
1481 if (controller_.pending_entry() &&
1482 (controller_.GetLastCommittedEntry() ||
1483 render_manager_.pending_dom_ui()))
1484 return render_manager_.pending_dom_ui();
1485 return render_manager_.dom_ui();
1486}
[email protected]420ae012009-04-24 05:16:321487
1488void TabContents::DidNavigateMainFramePostCommit(
1489 const NavigationController::LoadCommittedDetails& details,
1490 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]c2e74fe82009-09-03 17:57:441491 if (opener_dom_ui_type_ != DOMUIFactory::kNoDOMUI) {
1492 // If this is a window.open navigation, use the same DOMUI as the renderer
1493 // that opened the window, as long as both renderers have the same
1494 // privileges.
1495 if (opener_dom_ui_type_ == DOMUIFactory::GetDOMUIType(GetURL())) {
1496 DOMUI* dom_ui = DOMUIFactory::CreateDOMUIForURL(this, GetURL());
1497 // dom_ui might be NULL if the URL refers to a non-existent extension.
1498 if (dom_ui) {
1499 render_manager_.SetDOMUIPostCommit(dom_ui);
1500 dom_ui->RenderViewCreated(render_view_host());
1501 }
1502 }
1503 opener_dom_ui_type_ = DOMUIFactory::kNoDOMUI;
1504 }
1505
[email protected]420ae012009-04-24 05:16:321506 if (details.is_user_initiated_main_frame_load()) {
1507 // Clear the status bubble. This is a workaround for a bug where WebKit
1508 // doesn't let us know that the cursor left an element during a
1509 // transition (this is also why the mouse cursor remains as a hand after
1510 // clicking on a link); see bugs 1184641 and 980803. We don't want to
1511 // clear the bubble when a user navigates to a named anchor in the same
1512 // page.
1513 UpdateTargetURL(details.entry->page_id(), GURL());
1514
1515 // UpdateHelpersForDidNavigate will handle the case where the password_form
1516 // origin is valid.
1517 // TODO(brettw) bug 1343111: Password manager stuff in here needs to be
1518 // cleaned up and covered by tests.
1519 if (!params.password_form.origin.is_valid())
1520 GetPasswordManager()->DidNavigate();
1521 }
1522
1523 // The keyword generator uses the navigation entries, so must be called after
1524 // the commit.
1525 GenerateKeywordIfNecessary(params);
1526
1527 // Allow the new page to set the title again.
1528 received_page_title_ = false;
1529
1530 // Get the favicon, either from history or request it from the net.
1531 fav_icon_helper_.FetchFavIcon(details.entry->url());
1532
[email protected]0de80162010-02-03 04:52:351533 // Clear all page actions, blocked content notifications and browser actions
1534 // for this tab, unless this is an in-page navigation.
[email protected]a6e82fc2010-02-24 22:28:141535 if (!details.is_in_page) {
[email protected]92c6f9b92009-10-24 04:35:081536 ExtensionsService* service = profile()->GetExtensionsService();
1537 if (service) {
1538 for (size_t i = 0; i < service->extensions()->size(); ++i) {
[email protected]5d246db22009-10-27 06:17:571539 ExtensionAction* browser_action =
[email protected]92c6f9b92009-10-24 04:35:081540 service->extensions()->at(i)->browser_action();
[email protected]56ce6e52009-10-27 00:10:521541 if (browser_action) {
1542 browser_action->ClearAllValuesForTab(controller().session_id().id());
1543 NotificationService::current()->Notify(
1544 NotificationType::EXTENSION_BROWSER_ACTION_UPDATED,
[email protected]5d246db22009-10-27 06:17:571545 Source<ExtensionAction>(browser_action),
[email protected]56ce6e52009-10-27 00:10:521546 NotificationService::NoDetails());
1547 }
[email protected]92c6f9b92009-10-24 04:35:081548
[email protected]5d246db22009-10-27 06:17:571549 ExtensionAction* page_action =
[email protected]56ce6e52009-10-27 00:10:521550 service->extensions()->at(i)->page_action();
1551 if (page_action) {
1552 page_action->ClearAllValuesForTab(controller().session_id().id());
1553 PageActionStateChanged();
1554 }
[email protected]92c6f9b92009-10-24 04:35:081555 }
[email protected]aece2c7f2009-08-27 20:43:171556 }
[email protected]0de80162010-02-03 04:52:351557
1558 // Close blocked popups.
1559 if (blocked_popups_) {
1560 AutoReset auto_reset(&dont_notify_render_view_, true);
1561 blocked_popups_->Destroy();
[email protected]da7b5262010-02-25 21:12:461562 blocked_popups_ = NULL;
[email protected]0de80162010-02-03 04:52:351563 }
1564
1565 // Clear "blocked" flags.
1566 ClearBlockedContentSettings();
[email protected]9d062b12010-04-14 09:36:551567 ClearGeolocationContentSettings(details);
[email protected]0de80162010-02-03 04:52:351568 if (delegate_)
[email protected]20ad6a92010-04-21 14:07:241569 delegate_->OnContentSettingsChange(this);
[email protected]aece2c7f2009-08-27 20:43:171570 }
[email protected]ce5c4502009-05-06 16:46:111571
[email protected]8cc8d492010-02-02 10:40:491572 // Close constrained windows if necessary.
1573 if (!net::RegistryControlledDomainService::SameDomainOrHost(
1574 details.previous_url, details.entry->url()))
1575 CloseConstrainedWindows();
1576
[email protected]420ae012009-04-24 05:16:321577 // Update the starred state.
1578 UpdateStarredStateForCurrentURL();
[email protected]c07c9752010-03-12 05:57:531579
1580 // Clear the cache of forms in AutoFill.
1581 if (autofill_manager_.get())
1582 autofill_manager_->Reset();
[email protected]a888b29e62010-04-01 13:38:571583
1584 // The url likely changed, see if there is an extension whose extent contains
1585 // the current page.
1586 UpdateAppExtensionForCurrentPage();
[email protected]420ae012009-04-24 05:16:321587}
1588
1589void TabContents::DidNavigateAnyFramePostCommit(
1590 RenderViewHost* render_view_host,
1591 const NavigationController::LoadCommittedDetails& details,
1592 const ViewHostMsg_FrameNavigate_Params& params) {
1593 // If we navigate, start showing messages again. This does nothing to prevent
1594 // a malicious script from spamming messages, since the script could just
1595 // reload the page to stop blocking.
1596 suppress_javascript_messages_ = false;
1597
[email protected]420ae012009-04-24 05:16:321598 // Notify the password manager of the navigation or form submit.
1599 // TODO(brettw) bug 1343111: Password manager stuff in here needs to be
1600 // cleaned up and covered by tests.
1601 if (params.password_form.origin.is_valid())
1602 GetPasswordManager()->ProvisionallySavePassword(params.password_form);
[email protected]3b1c1812010-02-12 21:53:551603
1604 // Let the LanguageState clear its state.
[email protected]34cec8d82010-03-05 18:27:011605 bool reload = details.entry->transition_type() == PageTransition::RELOAD ||
1606 details.type == NavigationType::SAME_PAGE;
1607 language_state_.DidNavigate(reload, details.is_in_page);
[email protected]420ae012009-04-24 05:16:321608}
1609
[email protected]8cc8d492010-02-02 10:40:491610void TabContents::CloseConstrainedWindows() {
1611 // Clear out any constrained windows since we are leaving this page entirely.
[email protected]420ae012009-04-24 05:16:321612 // We use indices instead of iterators in case CloseWindow does something
1613 // that may invalidate an iterator.
1614 int size = static_cast<int>(child_windows_.size());
1615 for (int i = size - 1; i >= 0; --i) {
1616 ConstrainedWindow* window = child_windows_[i];
[email protected]fa1cf0b82010-01-15 21:49:441617 if (window) {
[email protected]420ae012009-04-24 05:16:321618 window->CloseConstrainedWindow();
[email protected]fa1cf0b82010-01-15 21:49:441619 BlockTabContent(false);
1620 }
[email protected]420ae012009-04-24 05:16:321621 }
1622}
1623
1624void TabContents::UpdateStarredStateForCurrentURL() {
1625 BookmarkModel* model = profile()->GetBookmarkModel();
1626 const bool old_state = is_starred_;
1627 is_starred_ = (model && model->IsBookmarked(GetURL()));
1628
1629 if (is_starred_ != old_state && delegate())
1630 delegate()->URLStarredChanged(this, is_starred_);
1631}
1632
1633void TabContents::UpdateAlternateErrorPageURL() {
1634 GURL url = GetAlternateErrorPageURL();
1635 render_view_host()->SetAlternateErrorPageURL(url);
1636}
1637
1638void TabContents::UpdateWebPreferences() {
1639 render_view_host()->UpdateWebPreferences(GetWebkitPrefs());
1640}
1641
1642void TabContents::UpdateMaxPageIDIfNecessary(SiteInstance* site_instance,
1643 RenderViewHost* rvh) {
1644 // If we are creating a RVH for a restored controller, then we might
1645 // have more page IDs than the SiteInstance's current max page ID. We must
1646 // make sure that the max page ID is larger than any restored page ID.
1647 // Note that it is ok for conflicting page IDs to exist in another tab
1648 // (i.e., NavigationController), but if any page ID is larger than the max,
1649 // the back/forward list will get confused.
1650 int max_restored_page_id = controller_.max_restored_page_id();
1651 if (max_restored_page_id > 0) {
1652 int curr_max_page_id = site_instance->max_page_id();
1653 if (max_restored_page_id > curr_max_page_id) {
1654 // Need to update the site instance immediately.
1655 site_instance->UpdateMaxPageID(max_restored_page_id);
1656
1657 // Also tell the renderer to update its internal representation. We
1658 // need to reserve enough IDs to make all restored page IDs less than
1659 // the max.
1660 if (curr_max_page_id < 0)
1661 curr_max_page_id = 0;
1662 rvh->ReservePageIDRange(max_restored_page_id - curr_max_page_id);
1663 }
1664 }
1665}
1666
1667void TabContents::UpdateHistoryForNavigation(
[email protected]ebe89e062009-08-13 23:16:541668 const GURL& virtual_url,
[email protected]befd8d822009-07-01 04:51:471669 const NavigationController::LoadCommittedDetails& details,
[email protected]420ae012009-04-24 05:16:321670 const ViewHostMsg_FrameNavigate_Params& params) {
1671 if (profile()->IsOffTheRecord())
1672 return;
1673
1674 // Add to history service.
1675 HistoryService* hs = profile()->GetHistoryService(Profile::IMPLICIT_ACCESS);
1676 if (hs) {
1677 if (PageTransition::IsMainFrame(params.transition) &&
[email protected]ebe89e062009-08-13 23:16:541678 virtual_url != params.url) {
1679 // Hack on the "virtual" URL so that it will appear in history. For some
[email protected]420ae012009-04-24 05:16:321680 // types of URLs, we will display a magic URL that is different from where
1681 // the page is actually navigated. We want the user to see in history
[email protected]ebe89e062009-08-13 23:16:541682 // what they saw in the URL bar, so we add the virtual URL as a redirect.
1683 // This only applies to the main frame, as the virtual URL doesn't apply
[email protected]420ae012009-04-24 05:16:321684 // to sub-frames.
1685 std::vector<GURL> redirects = params.redirects;
1686 if (!redirects.empty())
[email protected]ebe89e062009-08-13 23:16:541687 redirects.back() = virtual_url;
1688 hs->AddPage(virtual_url, this, params.page_id, params.referrer,
[email protected]befd8d822009-07-01 04:51:471689 params.transition, redirects, details.did_replace_entry);
[email protected]420ae012009-04-24 05:16:321690 } else {
1691 hs->AddPage(params.url, this, params.page_id, params.referrer,
[email protected]befd8d822009-07-01 04:51:471692 params.transition, params.redirects,
1693 details.did_replace_entry);
[email protected]420ae012009-04-24 05:16:321694 }
1695 }
1696}
1697
1698bool TabContents::UpdateTitleForEntry(NavigationEntry* entry,
1699 const std::wstring& title) {
1700 // For file URLs without a title, use the pathname instead. In the case of a
1701 // synthesized title, we don't want the update to count toward the "one set
1702 // per page of the title to history."
1703 std::wstring final_title;
1704 bool explicit_set;
1705 if (entry->url().SchemeIsFile() && title.empty()) {
1706 final_title = UTF8ToWide(entry->url().ExtractFileName());
1707 explicit_set = false; // Don't count synthetic titles toward the set limit.
1708 } else {
1709 TrimWhitespace(title, TRIM_ALL, &final_title);
1710 explicit_set = true;
1711 }
1712
1713 if (final_title == UTF16ToWideHack(entry->title()))
1714 return false; // Nothing changed, don't bother.
1715
1716 entry->set_title(WideToUTF16Hack(final_title));
1717
1718 // Update the history system for this page.
1719 if (!profile()->IsOffTheRecord() && !received_page_title_) {
1720 HistoryService* hs =
1721 profile()->GetHistoryService(Profile::IMPLICIT_ACCESS);
1722 if (hs)
[email protected]ebe89e062009-08-13 23:16:541723 hs->SetPageTitle(entry->virtual_url(), final_title);
[email protected]420ae012009-04-24 05:16:321724
1725 // Don't allow the title to be saved again for explicitly set ones.
1726 received_page_title_ = explicit_set;
1727 }
1728
1729 // Lastly, set the title for the view.
1730 view_->SetPageTitle(final_title);
1731
[email protected]cbc0e1b2010-04-12 18:33:041732 NotificationService::current()->Notify(
1733 NotificationType::TAB_CONTENTS_TITLE_UPDATED,
1734 Source<TabContents>(this),
1735 NotificationService::NoDetails());
1736
[email protected]420ae012009-04-24 05:16:321737 return true;
1738}
1739
1740void TabContents::NotifySwapped() {
1741 // After sending out a swap notification, we need to send a disconnect
1742 // notification so that clients that pick up a pointer to |this| can NULL the
1743 // pointer. See Bug 1230284.
1744 notify_disconnection_ = true;
1745 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341746 NotificationType::TAB_CONTENTS_SWAPPED,
[email protected]d82ed61e2009-06-16 02:46:221747 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321748 NotificationService::NoDetails());
1749}
1750
1751void TabContents::NotifyConnected() {
1752 notify_disconnection_ = true;
1753 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341754 NotificationType::TAB_CONTENTS_CONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221755 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321756 NotificationService::NoDetails());
1757}
1758
1759void TabContents::NotifyDisconnected() {
1760 if (!notify_disconnection_)
1761 return;
1762
1763 notify_disconnection_ = false;
1764 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341765 NotificationType::TAB_CONTENTS_DISCONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221766 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321767 NotificationService::NoDetails());
1768}
1769
1770void TabContents::GenerateKeywordIfNecessary(
1771 const ViewHostMsg_FrameNavigate_Params& params) {
1772 if (!params.searchable_form_url.is_valid())
1773 return;
1774
1775 if (profile()->IsOffTheRecord())
1776 return;
1777
1778 int last_index = controller_.last_committed_entry_index();
1779 // When there was no previous page, the last index will be 0. This is
1780 // normally due to a form submit that opened in a new tab.
1781 // TODO(brettw) bug 916126: we should support keywords when form submits
1782 // happen in new tabs.
1783 if (last_index <= 0)
1784 return;
1785 const NavigationEntry* previous_entry =
1786 controller_.GetEntryAtIndex(last_index - 1);
1787 if (IsFormSubmit(previous_entry)) {
1788 // Only generate a keyword if the previous page wasn't itself a form
1789 // submit.
1790 return;
1791 }
1792
1793 GURL keyword_url = previous_entry->user_typed_url().is_valid() ?
1794 previous_entry->user_typed_url() : previous_entry->url();
1795 std::wstring keyword =
1796 TemplateURLModel::GenerateKeyword(keyword_url, true); // autodetected
1797 if (keyword.empty())
1798 return;
1799
1800 TemplateURLModel* url_model = profile()->GetTemplateURLModel();
1801 if (!url_model)
1802 return;
1803
1804 if (!url_model->loaded()) {
1805 url_model->Load();
1806 return;
1807 }
1808
1809 const TemplateURL* current_url;
1810 std::wstring url = UTF8ToWide(params.searchable_form_url.spec());
1811 if (!url_model->CanReplaceKeyword(keyword, url, &current_url))
1812 return;
1813
1814 if (current_url) {
1815 if (current_url->originating_url().is_valid()) {
1816 // The existing keyword was generated from an OpenSearch description
1817 // document, don't regenerate.
1818 return;
1819 }
1820 url_model->Remove(current_url);
1821 }
1822 TemplateURL* new_url = new TemplateURL();
1823 new_url->set_keyword(keyword);
1824 new_url->set_short_name(keyword);
1825 new_url->SetURL(url, 0, 0);
1826 new_url->add_input_encoding(params.searchable_form_encoding);
1827 DCHECK(controller_.GetLastCommittedEntry());
1828 const GURL& favicon_url =
1829 controller_.GetLastCommittedEntry()->favicon().url();
1830 if (favicon_url.is_valid()) {
1831 new_url->SetFavIconURL(favicon_url);
1832 } else {
1833 // The favicon url isn't valid. This means there really isn't a favicon,
1834 // or the favicon url wasn't obtained before the load started. This assumes
1835 // the later.
1836 // TODO(sky): Need a way to set the favicon that doesn't involve generating
1837 // its url.
1838 new_url->SetFavIconURL(TemplateURL::GenerateFaviconURL(params.referrer));
1839 }
1840 new_url->set_safe_for_autoreplace(true);
1841 url_model->Add(new_url);
1842}
1843
[email protected]bf5c2ff392009-07-08 16:24:331844void TabContents::OnUserGesture() {
1845 // See comment in RenderViewHostDelegate::OnUserGesture as to why we do this.
1846 DownloadRequestManager* drm = g_browser_process->download_request_manager();
1847 if (drm)
1848 drm->OnUserGesture(this);
[email protected]e7eaedde2009-09-25 20:09:491849 ExternalProtocolHandler::OnUserGesture();
[email protected]bf5c2ff392009-07-08 16:24:331850 controller_.OnUserGesture();
1851}
1852
1853void TabContents::OnFindReply(int request_id,
1854 int number_of_matches,
1855 const gfx::Rect& selection_rect,
1856 int active_match_ordinal,
1857 bool final_update) {
[email protected]3ce7ced2009-07-24 18:52:421858 // Ignore responses for requests that have been aborted.
1859 if (find_op_aborted_)
1860 return;
1861
[email protected]bf5c2ff392009-07-08 16:24:331862 // Ignore responses for requests other than the one we have most recently
1863 // issued. That way we won't act on stale results when the user has
1864 // already typed in another query.
1865 if (request_id != current_find_request_id_)
1866 return;
1867
1868 if (number_of_matches == -1)
1869 number_of_matches = last_search_result_.number_of_matches();
1870 if (active_match_ordinal == -1)
1871 active_match_ordinal = last_search_result_.active_match_ordinal();
1872
1873 gfx::Rect selection = selection_rect;
1874 if (selection.IsEmpty())
1875 selection = last_search_result_.selection_rect();
1876
1877 // Notify the UI, automation and any other observers that a find result was
1878 // found.
1879 last_search_result_ = FindNotificationDetails(request_id, number_of_matches,
1880 selection, active_match_ordinal,
1881 final_update);
1882 NotificationService::current()->Notify(
1883 NotificationType::FIND_RESULT_AVAILABLE,
1884 Source<TabContents>(this),
1885 Details<FindNotificationDetails>(&last_search_result_));
1886}
1887
1888void TabContents::GoToEntryAtOffset(int offset) {
[email protected]f9cc4c452009-10-13 14:56:381889 if (!delegate_ || delegate_->OnGoToEntryOffset(offset))
1890 controller_.GoToOffset(offset);
[email protected]bf5c2ff392009-07-08 16:24:331891}
1892
[email protected]bf5c2ff392009-07-08 16:24:331893void TabContents::OnMissingPluginStatus(int status) {
1894#if defined(OS_WIN)
1895// TODO(PORT): pull in when plug-ins work
1896 GetPluginInstaller()->OnMissingPluginStatus(status);
1897#endif
1898}
1899
1900void TabContents::OnCrashedPlugin(const FilePath& plugin_path) {
[email protected]bf5c2ff392009-07-08 16:24:331901 DCHECK(!plugin_path.value().empty());
1902
1903 std::wstring plugin_name = plugin_path.ToWStringHack();
[email protected]d32d46182009-07-23 00:09:351904#if defined(OS_WIN) || defined(OS_MACOSX)
[email protected]bf5c2ff392009-07-08 16:24:331905 scoped_ptr<FileVersionInfo> version_info(
1906 FileVersionInfo::CreateFileVersionInfo(plugin_path));
1907 if (version_info.get()) {
1908 const std::wstring& product_name = version_info->product_name();
[email protected]996fd702009-09-04 19:12:371909 if (!product_name.empty()) {
[email protected]bf5c2ff392009-07-08 16:24:331910 plugin_name = product_name;
[email protected]996fd702009-09-04 19:12:371911#if defined(OS_MACOSX)
1912 // Many plugins on the Mac have .plugin in the actual name, which looks
1913 // terrible, so look for that and strip it off if present.
1914 const std::wstring plugin_extension(L".plugin");
1915 if (EndsWith(plugin_name, plugin_extension, true))
1916 plugin_name.erase(plugin_name.length() - plugin_extension.length());
1917#endif // OS_MACOSX
1918 }
[email protected]bf5c2ff392009-07-08 16:24:331919 }
[email protected]4a560ed2009-07-13 23:35:261920#else
1921 NOTIMPLEMENTED() << " convert plugin path to plugin name";
1922#endif
[email protected]bf5c2ff392009-07-08 16:24:331923 AddInfoBar(new SimpleAlertInfoBarDelegate(
1924 this, l10n_util::GetStringF(IDS_PLUGIN_CRASHED_PROMPT, plugin_name),
[email protected]938e1f92010-04-01 18:09:421925 NULL, true));
[email protected]bf5c2ff392009-07-08 16:24:331926}
1927
1928void TabContents::OnCrashedWorker() {
1929 AddInfoBar(new SimpleAlertInfoBarDelegate(
1930 this, l10n_util::GetString(IDS_WEBWORKER_CRASHED_PROMPT),
[email protected]938e1f92010-04-01 18:09:421931 NULL, true));
[email protected]bf5c2ff392009-07-08 16:24:331932}
1933
1934void TabContents::OnDidGetApplicationInfo(
1935 int32 page_id,
1936 const webkit_glue::WebApplicationInfo& info) {
[email protected]ed5431872009-11-17 08:39:511937 web_app_info_ = info;
[email protected]bf5c2ff392009-07-08 16:24:331938
[email protected]ed5431872009-11-17 08:39:511939 if (delegate())
1940 delegate()->OnDidGetApplicationInfo(this, page_id);
[email protected]bf5c2ff392009-07-08 16:24:331941}
1942
[email protected]a2cf65eb2010-01-18 08:20:381943void TabContents::OnPageContents(const GURL& url,
1944 int renderer_process_id,
1945 int32 page_id,
[email protected]a8a81292010-01-21 00:32:451946 const std::wstring& contents,
1947 const std::string& language) {
[email protected]a2cf65eb2010-01-18 08:20:381948 // Don't index any https pages. People generally don't want their bank
1949 // accounts, etc. indexed on their computer, especially since some of these
1950 // things are not marked cachable.
1951 // TODO(brettw) we may want to consider more elaborate heuristics such as
1952 // the cachability of the page. We may also want to consider subframes (this
1953 // test will still index subframes if the subframe is SSL).
1954 if (!url.SchemeIsSecure()) {
1955 Profile* p = profile();
1956 if (p && !p->IsOffTheRecord()) {
1957 HistoryService* hs = p->GetHistoryService(Profile::IMPLICIT_ACCESS);
1958 if (hs)
1959 hs->SetPageContents(url, contents);
1960 }
1961 }
[email protected]a8a81292010-01-21 00:32:451962
[email protected]3b1c1812010-02-12 21:53:551963 language_state_.LanguageDetermined(language);
[email protected]a8a81292010-01-21 00:32:451964
1965 std::string lang = language;
1966 NotificationService::current()->Notify(
1967 NotificationType::TAB_LANGUAGE_DETERMINED,
[email protected]7e0831b2010-02-01 08:45:151968 Source<TabContents>(this),
[email protected]a8a81292010-01-21 00:32:451969 Details<std::string>(&lang));
[email protected]a2cf65eb2010-01-18 08:20:381970}
1971
[email protected]7e0831b2010-02-01 08:45:151972void TabContents::OnPageTranslated(int32 page_id,
1973 const std::string& original_lang,
[email protected]152b8ba32010-03-29 19:40:161974 const std::string& translated_lang,
1975 TranslateErrors::Type error_type) {
[email protected]3b1c1812010-02-12 21:53:551976 language_state_.set_current_language(translated_lang);
1977 language_state_.set_translation_pending(false);
[email protected]152b8ba32010-03-29 19:40:161978 PageTranslatedDetails details(original_lang, translated_lang, error_type);
[email protected]7e0831b2010-02-01 08:45:151979 NotificationService::current()->Notify(
1980 NotificationType::PAGE_TRANSLATED,
1981 Source<TabContents>(this),
[email protected]152b8ba32010-03-29 19:40:161982 Details<PageTranslatedDetails>(&details));
[email protected]7e0831b2010-02-01 08:45:151983}
1984
[email protected]bf5c2ff392009-07-08 16:24:331985void TabContents::DidStartProvisionalLoadForFrame(
1986 RenderViewHost* render_view_host,
1987 bool is_main_frame,
1988 const GURL& url) {
1989 ProvisionalLoadDetails details(is_main_frame,
1990 controller_.IsURLInPageNavigation(url),
1991 url, std::string(), false);
1992 NotificationService::current()->Notify(
1993 NotificationType::FRAME_PROVISIONAL_LOAD_START,
1994 Source<NavigationController>(&controller_),
1995 Details<ProvisionalLoadDetails>(&details));
1996}
1997
1998void TabContents::DidStartReceivingResourceResponse(
[email protected]3dbb80b2010-02-09 22:41:201999 const ResourceRequestDetails& details) {
[email protected]bf5c2ff392009-07-08 16:24:332000 NotificationService::current()->Notify(
2001 NotificationType::RESOURCE_RESPONSE_STARTED,
2002 Source<NavigationController>(&controller()),
[email protected]3dbb80b2010-02-09 22:41:202003 Details<const ResourceRequestDetails>(&details));
[email protected]bf5c2ff392009-07-08 16:24:332004}
2005
[email protected]3dbb80b2010-02-09 22:41:202006void TabContents::DidRedirectResource(
2007 const ResourceRedirectDetails& details) {
[email protected]bf5c2ff392009-07-08 16:24:332008 NotificationService::current()->Notify(
2009 NotificationType::RESOURCE_RECEIVED_REDIRECT,
2010 Source<NavigationController>(&controller()),
[email protected]3dbb80b2010-02-09 22:41:202011 Details<const ResourceRequestDetails>(&details));
[email protected]bf5c2ff392009-07-08 16:24:332012}
2013
2014void TabContents::DidLoadResourceFromMemoryCache(
2015 const GURL& url,
2016 const std::string& frame_origin,
2017 const std::string& main_frame_origin,
2018 const std::string& security_info) {
2019 // Send out a notification that we loaded a resource from our memory cache.
2020 int cert_id = 0, cert_status = 0, security_bits = 0;
2021 SSLManager::DeserializeSecurityInfo(security_info,
2022 &cert_id, &cert_status,
2023 &security_bits);
2024 LoadFromMemoryCacheDetails details(url, frame_origin, main_frame_origin,
[email protected]8cb5d5b2010-02-09 11:36:162025 GetRenderProcessHost()->id(), cert_id,
2026 cert_status);
[email protected]bf5c2ff392009-07-08 16:24:332027
2028 NotificationService::current()->Notify(
2029 NotificationType::LOAD_FROM_MEMORY_CACHE,
2030 Source<NavigationController>(&controller_),
2031 Details<LoadFromMemoryCacheDetails>(&details));
2032}
2033
[email protected]e3d60e5d2009-09-25 21:08:292034void TabContents::DidDisplayInsecureContent() {
[email protected]09843b5f2009-09-26 08:30:592035 controller_.ssl_manager()->DidDisplayInsecureContent();
[email protected]e3d60e5d2009-09-25 21:08:292036}
2037
2038void TabContents::DidRunInsecureContent(const std::string& security_origin) {
[email protected]09843b5f2009-09-26 08:30:592039 controller_.ssl_manager()->DidRunInsecureContent(security_origin);
[email protected]e3d60e5d2009-09-25 21:08:292040}
2041
[email protected]bf5c2ff392009-07-08 16:24:332042void TabContents::DidFailProvisionalLoadWithError(
2043 RenderViewHost* render_view_host,
2044 bool is_main_frame,
2045 int error_code,
2046 const GURL& url,
2047 bool showing_repost_interstitial) {
2048 if (net::ERR_ABORTED == error_code) {
2049 // EVIL HACK ALERT! Ignore failed loads when we're showing interstitials.
2050 // This means that the interstitial won't be torn down properly, which is
2051 // bad. But if we have an interstitial, go back to another tab type, and
2052 // then load the same interstitial again, we could end up getting the first
2053 // interstitial's "failed" message (as a result of the cancel) when we're on
2054 // the second one.
2055 //
2056 // We can't tell this apart, so we think we're tearing down the current page
2057 // which will cause a crash later one. There is also some code in
2058 // RenderViewHostManager::RendererAbortedProvisionalLoad that is commented
2059 // out because of this problem.
2060 //
2061 // http://code.google.com/p/chromium/issues/detail?id=2855
2062 // Because this will not tear down the interstitial properly, if "back" is
2063 // back to another tab type, the interstitial will still be somewhat alive
2064 // in the previous tab type. If you navigate somewhere that activates the
2065 // tab with the interstitial again, you'll see a flash before the new load
2066 // commits of the interstitial page.
2067 if (showing_interstitial_page()) {
2068 LOG(WARNING) << "Discarding message during interstitial.";
2069 return;
2070 }
2071
2072 // This will discard our pending entry if we cancelled the load (e.g., if we
2073 // decided to download the file instead of load it). Only discard the
2074 // pending entry if the URLs match, otherwise the user initiated a navigate
2075 // before the page loaded so that the discard would discard the wrong entry.
2076 NavigationEntry* pending_entry = controller_.pending_entry();
2077 if (pending_entry && pending_entry->url() == url) {
2078 controller_.DiscardNonCommittedEntries();
2079 // Update the URL display.
2080 NotifyNavigationStateChanged(TabContents::INVALIDATE_URL);
2081 }
2082
2083 render_manager_.RendererAbortedProvisionalLoad(render_view_host);
2084 }
2085
2086 // Send out a notification that we failed a provisional load with an error.
2087 ProvisionalLoadDetails details(is_main_frame,
2088 controller_.IsURLInPageNavigation(url),
2089 url, std::string(), false);
2090 details.set_error_code(error_code);
2091
2092 NotificationService::current()->Notify(
2093 NotificationType::FAIL_PROVISIONAL_LOAD_WITH_ERROR,
2094 Source<NavigationController>(&controller_),
2095 Details<ProvisionalLoadDetails>(&details));
2096}
2097
2098void TabContents::DocumentLoadedInFrame() {
2099 controller_.DocumentLoadedInFrame();
2100}
2101
[email protected]0de80162010-02-03 04:52:352102void TabContents::OnContentBlocked(ContentSettingsType type) {
[email protected]b1235062010-03-25 17:31:272103 // TODO(joth): remove once fully implemented.
2104 DCHECK(type != CONTENT_SETTINGS_TYPE_GEOLOCATION);
[email protected]0de80162010-02-03 04:52:352105 content_blocked_[type] = true;
2106 if (delegate_)
[email protected]20ad6a92010-04-21 14:07:242107 delegate_->OnContentSettingsChange(this);
[email protected]0de80162010-02-03 04:52:352108}
2109
[email protected]614c0e12010-03-25 10:12:562110void TabContents::OnGeolocationPermissionSet(const GURL& requesting_origin,
2111 bool allowed) {
2112 geolocation_content_settings_[requesting_origin] =
2113 allowed ? CONTENT_SETTING_ALLOW : CONTENT_SETTING_BLOCK;
[email protected]614c0e12010-03-25 10:12:562114 if (delegate_)
[email protected]20ad6a92010-04-21 14:07:242115 delegate_->OnContentSettingsChange(this);
[email protected]614c0e12010-03-25 10:12:562116}
2117
[email protected]8d3347f2009-07-09 22:00:212118RenderViewHostDelegate::View* TabContents::GetViewDelegate() {
[email protected]420ae012009-04-24 05:16:322119 return view_.get();
2120}
2121
[email protected]8d3347f2009-07-09 22:00:212122RenderViewHostDelegate::RendererManagement*
2123TabContents::GetRendererManagementDelegate() {
2124 return &render_manager_;
2125}
2126
[email protected]bf5c2ff392009-07-08 16:24:332127RenderViewHostDelegate::BrowserIntegration*
[email protected]8d3347f2009-07-09 22:00:212128 TabContents::GetBrowserIntegrationDelegate() {
2129 return this;
[email protected]bf5c2ff392009-07-08 16:24:332130}
2131
[email protected]8d3347f2009-07-09 22:00:212132RenderViewHostDelegate::Resource* TabContents::GetResourceDelegate() {
2133 return this;
[email protected]bf5c2ff392009-07-08 16:24:332134}
2135
[email protected]8d3347f2009-07-09 22:00:212136RenderViewHostDelegate::Save* TabContents::GetSaveDelegate() {
[email protected]420ae012009-04-24 05:16:322137 return save_package_.get(); // May be NULL, but we can return NULL.
2138}
2139
[email protected]8d3347f2009-07-09 22:00:212140RenderViewHostDelegate::Printing* TabContents::GetPrintingDelegate() {
2141 return &printing_;
2142}
2143
2144RenderViewHostDelegate::FavIcon* TabContents::GetFavIconDelegate() {
2145 return &fav_icon_helper_;
2146}
2147
[email protected]e8dbd674b2010-03-11 18:34:402148RenderViewHostDelegate::Autocomplete* TabContents::GetAutocompleteDelegate() {
[email protected]6db0def92010-03-02 19:55:452149 if (autocomplete_history_manager_.get() == NULL)
2150 autocomplete_history_manager_.reset(new AutocompleteHistoryManager(this));
2151 return autocomplete_history_manager_.get();
[email protected]bf5c2ff392009-07-08 16:24:332152}
2153
[email protected]21674df2009-10-28 19:51:212154RenderViewHostDelegate::AutoFill* TabContents::GetAutoFillDelegate() {
[email protected]cf28ec52010-04-09 22:56:342155 return GetAutoFillManager();
[email protected]21674df2009-10-28 19:51:212156}
2157
[email protected]fd571bb2010-03-19 01:23:542158AutomationResourceRoutingDelegate*
2159TabContents::GetAutomationResourceRoutingDelegate() {
2160 return delegate();
2161}
2162
[email protected]ced90ae12010-02-20 02:06:162163RenderViewHostDelegate::BookmarkDrag* TabContents::GetBookmarkDragDelegate() {
2164 return bookmark_drag_;
2165}
2166
2167void TabContents::SetBookmarkDragDelegate(
2168 RenderViewHostDelegate::BookmarkDrag* bookmark_drag) {
2169 bookmark_drag_ = bookmark_drag;
2170}
2171
[email protected]93623c5d2009-12-10 21:40:322172RendererPreferences TabContents::GetRendererPrefs(Profile* profile) const {
[email protected]840b1512009-07-21 16:53:462173 return renderer_preferences_;
[email protected]80d96fa2009-06-10 22:34:512174}
2175
[email protected]57c6a652009-05-04 07:58:342176TabContents* TabContents::GetAsTabContents() {
2177 return this;
2178}
2179
[email protected]7b291f92009-08-14 05:43:532180ViewType::Type TabContents::GetRenderViewType() const {
2181 return ViewType::TAB_CONTENTS;
2182}
2183
2184int TabContents::GetBrowserWindowID() const {
2185 return controller().window_id().id();
2186}
2187
[email protected]420ae012009-04-24 05:16:322188void TabContents::RenderViewCreated(RenderViewHost* render_view_host) {
[email protected]2e4633c2009-07-09 16:58:062189 NotificationService::current()->Notify(
2190 NotificationType::RENDER_VIEW_HOST_CREATED_FOR_TAB,
2191 Source<TabContents>(this),
2192 Details<RenderViewHost>(render_view_host));
[email protected]420ae012009-04-24 05:16:322193 NavigationEntry* entry = controller_.GetActiveEntry();
2194 if (!entry)
2195 return;
2196
2197 // When we're creating views, we're still doing initial setup, so we always
2198 // use the pending DOM UI rather than any possibly existing committed one.
2199 if (render_manager_.pending_dom_ui()) {
2200 render_manager_.pending_dom_ui()->RenderViewCreated(render_view_host);
2201 }
2202
2203 if (entry->IsViewSourceMode()) {
2204 // Put the renderer in view source mode.
2205 render_view_host->Send(
2206 new ViewMsg_EnableViewSourceMode(render_view_host->routing_id()));
2207 }
[email protected]0666aef2009-05-13 19:48:082208
2209 view()->RenderViewCreated(render_view_host);
[email protected]420ae012009-04-24 05:16:322210}
2211
2212void TabContents::RenderViewReady(RenderViewHost* rvh) {
2213 if (rvh != render_view_host()) {
2214 // Don't notify the world, since this came from a renderer in the
2215 // background.
2216 return;
2217 }
2218
2219 NotifyConnected();
[email protected]9d8a4642009-07-29 17:25:302220 bool was_crashed = is_crashed();
[email protected]420ae012009-04-24 05:16:322221 SetIsCrashed(false);
[email protected]9d8a4642009-07-29 17:25:302222
2223 // Restore the focus to the tab (otherwise the focus will be on the top
2224 // window).
2225 if (was_crashed && !FocusLocationBarByDefault())
2226 Focus();
[email protected]420ae012009-04-24 05:16:322227}
2228
2229void TabContents::RenderViewGone(RenderViewHost* rvh) {
2230 // Ask the print preview if this renderer was valuable.
2231 if (!printing_.OnRenderViewGone(rvh))
2232 return;
2233 if (rvh != render_view_host()) {
2234 // The pending page's RenderViewHost is gone.
2235 return;
2236 }
2237
2238 SetIsLoading(false, NULL);
2239 NotifyDisconnected();
2240 SetIsCrashed(true);
2241
[email protected]ff9efc62010-02-18 22:10:542242 // Remove all infobars.
[email protected]e5430c92010-02-18 22:39:212243 for (int i = infobar_delegate_count() - 1; i >=0 ; --i)
[email protected]ff9efc62010-02-18 22:10:542244 RemoveInfoBar(GetInfoBarDelegateAt(i));
2245
[email protected]8e537302009-06-10 21:19:582246 // Tell the view that we've crashed so it can prepare the sad tab page.
[email protected]cf68b092010-01-15 20:05:552247 // Only do this if we're not in browser shutdown, so that TabContents
2248 // objects that are not in a browser (e.g., HTML dialogs) and thus are
2249 // visible do not flash a sad tab page.
2250 if (browser_shutdown::GetShutdownType() == browser_shutdown::NOT_VALID)
2251 view_->OnTabCrashed();
[email protected]420ae012009-04-24 05:16:322252
2253 // Hide any visible hung renderer warning for this web contents' process.
[email protected]e7b8d832009-11-20 22:41:002254 hung_renderer_dialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:322255}
2256
[email protected]2e4633c2009-07-09 16:58:062257void TabContents::RenderViewDeleted(RenderViewHost* rvh) {
2258 NotificationService::current()->Notify(
2259 NotificationType::RENDER_VIEW_HOST_DELETED,
2260 Source<TabContents>(this),
2261 Details<RenderViewHost>(rvh));
[email protected]fb7b79f72009-11-06 18:00:482262 render_manager_.RenderViewDeleted(rvh);
[email protected]2e4633c2009-07-09 16:58:062263}
2264
[email protected]420ae012009-04-24 05:16:322265void TabContents::DidNavigate(RenderViewHost* rvh,
2266 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]8030f012009-09-25 18:09:372267 int extra_invalidate_flags = 0;
2268
2269 if (PageTransition::IsMainFrame(params.transition)) {
[email protected]20ede072009-10-06 00:28:352270 bool was_bookmark_bar_visible = ShouldShowBookmarkBar();
[email protected]8030f012009-09-25 18:09:372271 bool was_extension_shelf_visible = IsExtensionShelfAlwaysVisible();
2272
[email protected]420ae012009-04-24 05:16:322273 render_manager_.DidNavigateMainFrame(rvh);
2274
[email protected]20ede072009-10-06 00:28:352275 if (was_bookmark_bar_visible != ShouldShowBookmarkBar())
[email protected]8030f012009-09-25 18:09:372276 extra_invalidate_flags |= INVALIDATE_BOOKMARK_BAR;
2277 if (was_extension_shelf_visible != IsExtensionShelfAlwaysVisible())
2278 extra_invalidate_flags |= INVALIDATE_EXTENSION_SHELF;
2279 }
2280
[email protected]420ae012009-04-24 05:16:322281 // Update the site of the SiteInstance if it doesn't have one yet.
2282 if (!GetSiteInstance()->has_site())
2283 GetSiteInstance()->SetSite(params.url);
2284
2285 // Need to update MIME type here because it's referred to in
2286 // UpdateNavigationCommands() called by RendererDidNavigate() to
2287 // determine whether or not to enable the encoding menu.
2288 // It's updated only for the main frame. For a subframe,
2289 // RenderView::UpdateURL does not set params.contents_mime_type.
2290 // (see http://code.google.com/p/chromium/issues/detail?id=2929 )
2291 // TODO(jungshik): Add a test for the encoding menu to avoid
2292 // regressing it again.
2293 if (PageTransition::IsMainFrame(params.transition))
2294 contents_mime_type_ = params.contents_mime_type;
2295
2296 NavigationController::LoadCommittedDetails details;
[email protected]8030f012009-09-25 18:09:372297 bool did_navigate = controller_.RendererDidNavigate(
2298 params, extra_invalidate_flags, &details);
[email protected]3e90d4a2009-07-03 17:38:392299
2300 // Update history. Note that this needs to happen after the entry is complete,
2301 // which WillNavigate[Main,Sub]Frame will do before this function is called.
[email protected]2e29d922009-10-22 22:13:352302 if (params.should_update_history &&
2303 (!delegate() || delegate()->ShouldAddNavigationsToHistory())) {
[email protected]3e90d4a2009-07-03 17:38:392304 // Most of the time, the displayURL matches the loaded URL, but for about:
2305 // URLs, we use a data: URL as the real value. We actually want to save
2306 // the about: URL to the history db and keep the data: URL hidden. This is
2307 // what the TabContents' URL getter does.
2308 UpdateHistoryForNavigation(GetURL(), details, params);
2309 }
2310
[email protected]76543b92009-08-31 17:27:452311 if (!did_navigate)
2312 return; // No navigation happened.
[email protected]420ae012009-04-24 05:16:322313
2314 // DO NOT ADD MORE STUFF TO THIS FUNCTION! Your component should either listen
2315 // for the appropriate notification (best) or you can add it to
2316 // DidNavigateMainFramePostCommit / DidNavigateAnyFramePostCommit (only if
2317 // necessary, please).
2318
2319 // Run post-commit tasks.
2320 if (details.is_main_frame)
2321 DidNavigateMainFramePostCommit(details, params);
2322 DidNavigateAnyFramePostCommit(rvh, details, params);
2323}
2324
2325void TabContents::UpdateState(RenderViewHost* rvh,
2326 int32 page_id,
2327 const std::string& state) {
2328 DCHECK(rvh == render_view_host());
2329
2330 // We must be prepared to handle state updates for any page, these occur
2331 // when the user is scrolling and entering form data, as well as when we're
2332 // leaving a page, in which case our state may have already been moved to
2333 // the next page. The navigation controller will look up the appropriate
2334 // NavigationEntry and update it when it is notified via the delegate.
2335
2336 int entry_index = controller_.GetEntryIndexWithPageID(
2337 GetSiteInstance(), page_id);
2338 if (entry_index < 0)
2339 return;
2340 NavigationEntry* entry = controller_.GetEntryAtIndex(entry_index);
2341
2342 if (state == entry->content_state())
2343 return; // Nothing to update.
2344 entry->set_content_state(state);
2345 controller_.NotifyEntryChanged(entry, entry_index);
2346}
2347
2348void TabContents::UpdateTitle(RenderViewHost* rvh,
2349 int32 page_id, const std::wstring& title) {
2350 // If we have a title, that's a pretty good indication that we've started
2351 // getting useful data.
2352 SetNotWaitingForResponse();
2353
2354 DCHECK(rvh == render_view_host());
2355 NavigationEntry* entry = controller_.GetEntryWithPageID(GetSiteInstance(),
2356 page_id);
2357 if (!entry || !UpdateTitleForEntry(entry, title))
2358 return;
2359
2360 // Broadcast notifications when the UI should be updated.
2361 if (entry == controller_.GetEntryAtOffset(0))
[email protected]f1cd5e82009-10-23 17:19:032362 NotifyNavigationStateChanged(INVALIDATE_TITLE);
[email protected]420ae012009-04-24 05:16:322363}
2364
[email protected]420ae012009-04-24 05:16:322365void TabContents::UpdateEncoding(RenderViewHost* render_view_host,
[email protected]41fc0322009-09-04 22:23:402366 const std::string& encoding) {
[email protected]420ae012009-04-24 05:16:322367 set_encoding(encoding);
2368}
2369
2370void TabContents::UpdateTargetURL(int32 page_id, const GURL& url) {
2371 if (delegate())
2372 delegate()->UpdateTargetURL(this, url);
2373}
2374
2375void TabContents::UpdateThumbnail(const GURL& url,
2376 const SkBitmap& bitmap,
2377 const ThumbnailScore& score) {
2378 // Tell History about this thumbnail
[email protected]449478302009-06-09 20:04:282379 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kThumbnailStore)) {
[email protected]72d0fc72009-07-09 17:30:492380 if (!profile()->IsOffTheRecord())
[email protected]42339402009-07-17 17:27:192381 profile()->GetThumbnailStore()->SetPageThumbnail(url, bitmap,
2382 score, true);
[email protected]449478302009-06-09 20:04:282383 } else {
2384 HistoryService* hs;
2385 if (!profile()->IsOffTheRecord() &&
2386 (hs = profile()->GetHistoryService(Profile::IMPLICIT_ACCESS))) {
2387 hs->SetPageThumbnail(url, bitmap, score);
2388 }
[email protected]420ae012009-04-24 05:16:322389 }
2390}
2391
[email protected]8922e1f2009-10-03 05:01:262392void TabContents::UpdateInspectorSettings(const std::string& raw_settings) {
[email protected]0df30122009-06-03 12:13:082393 profile()->GetPrefs()->SetString(prefs::kWebKitInspectorSettings,
[email protected]8922e1f2009-10-03 05:01:262394 UTF8ToWide(raw_settings));
[email protected]0df30122009-06-03 12:13:082395}
2396
[email protected]420ae012009-04-24 05:16:322397void TabContents::Close(RenderViewHost* rvh) {
[email protected]07707302009-11-06 00:50:292398 // The UI may be in an event-tracking loop, such as between the
2399 // mouse-down and mouse-up in text selection or a button click.
2400 // Defer the close until after tracking is complete, so that we
2401 // don't free objects out from under the UI.
2402 // TODO(shess): This could probably be integrated with the
2403 // IsDoingDrag() test below. Punting for now because I need more
2404 // research to understand how this impacts platforms other than Mac.
2405 // TODO(shess): This could get more fine-grained. For instance,
2406 // closing a tab in another window while selecting text in the
2407 // current window's Omnibox should be just fine.
2408 if (view()->IsEventTracking()) {
2409 view()->CloseTabAfterEventTracking();
2410 return;
2411 }
2412
[email protected]24a4d1062009-07-10 23:10:422413 // If we close the tab while we're in the middle of a drag, we'll crash.
2414 // Instead, cancel the drag and close it as soon as the drag ends.
2415 if (view()->IsDoingDrag()) {
2416 view()->CancelDragAndCloseTab();
2417 return;
2418 }
2419
[email protected]420ae012009-04-24 05:16:322420 // Ignore this if it comes from a RenderViewHost that we aren't showing.
2421 if (delegate() && rvh == render_view_host())
2422 delegate()->CloseContents(this);
2423}
2424
2425void TabContents::RequestMove(const gfx::Rect& new_bounds) {
2426 if (delegate() && delegate()->IsPopup(this))
2427 delegate()->MoveContents(this, new_bounds);
2428}
2429
[email protected]7ab1e7d62009-10-14 23:32:012430void TabContents::DidStartLoading() {
[email protected]420ae012009-04-24 05:16:322431 SetIsLoading(true, NULL);
2432}
2433
[email protected]7ab1e7d62009-10-14 23:32:012434void TabContents::DidStopLoading() {
[email protected]420ae012009-04-24 05:16:322435 scoped_ptr<LoadNotificationDetails> details;
2436
2437 NavigationEntry* entry = controller_.GetActiveEntry();
2438 // An entry may not exist for a stop when loading an initial blank page or
2439 // if an iframe injected by script into a blank page finishes loading.
2440 if (entry) {
[email protected]420ae012009-04-24 05:16:322441 base::TimeDelta elapsed = base::TimeTicks::Now() - current_load_start_;
2442
2443 details.reset(new LoadNotificationDetails(
[email protected]ebe89e062009-08-13 23:16:542444 entry->virtual_url(),
[email protected]420ae012009-04-24 05:16:322445 entry->transition_type(),
2446 elapsed,
2447 &controller_,
2448 controller_.GetCurrentEntryIndex()));
2449 }
2450
2451 // Tell PasswordManager we've finished a page load, which serves as a
2452 // green light to save pending passwords and reset itself.
2453 GetPasswordManager()->DidStopLoading();
2454
2455 SetIsLoading(false, details.get());
2456}
2457
[email protected]420ae012009-04-24 05:16:322458void TabContents::DidRedirectProvisionalLoad(int32 page_id,
2459 const GURL& source_url,
2460 const GURL& target_url) {
2461 NavigationEntry* entry;
2462 if (page_id == -1)
2463 entry = controller_.pending_entry();
2464 else
2465 entry = controller_.GetEntryWithPageID(GetSiteInstance(), page_id);
2466 if (!entry || entry->url() != source_url)
2467 return;
2468 entry->set_url(target_url);
2469}
2470
[email protected]420ae012009-04-24 05:16:322471void TabContents::RequestOpenURL(const GURL& url, const GURL& referrer,
2472 WindowOpenDisposition disposition) {
2473 if (render_manager_.dom_ui()) {
2474 // When we're a DOM UI, it will provide a page transition type for us (this
2475 // is so the new tab page can specify AUTO_BOOKMARK for automatically
2476 // generated suggestions).
2477 //
2478 // Note also that we hide the referrer for DOM UI pages. We don't really
[email protected]60e448982009-05-06 04:21:162479 // want web sites to see a referrer of "chrome://blah" (and some
2480 // chrome: URLs might have search terms or other stuff we don't want to
[email protected]420ae012009-04-24 05:16:322481 // send to the site), so we send no referrer.
2482 OpenURL(url, GURL(), disposition,
2483 render_manager_.dom_ui()->link_transition_type());
2484 } else {
2485 OpenURL(url, referrer, disposition, PageTransition::LINK);
2486 }
2487}
2488
[email protected]1ee614862009-07-06 20:29:182489void TabContents::DomOperationResponse(const std::string& json_string,
2490 int automation_id) {
2491}
2492
[email protected]420ae012009-04-24 05:16:322493void TabContents::ProcessDOMUIMessage(const std::string& message,
[email protected]e4dad9fb2009-10-06 18:15:582494 const Value* content,
[email protected]bb64b512010-04-02 21:01:392495 const GURL& source_url,
[email protected]811bfe32009-07-01 08:46:252496 int request_id,
2497 bool has_callback) {
[email protected]420ae012009-04-24 05:16:322498 if (!render_manager_.dom_ui()) {
[email protected]c2e74fe82009-09-03 17:57:442499 // This can happen if someone uses window.open() to open an extension URL
2500 // from a non-extension context.
2501 render_view_host()->BlockExtensionRequest(request_id);
[email protected]420ae012009-04-24 05:16:322502 return;
2503 }
[email protected]bb64b512010-04-02 21:01:392504 render_manager_.dom_ui()->ProcessDOMUIMessage(message, content, source_url,
2505 request_id,
[email protected]811bfe32009-07-01 08:46:252506 has_callback);
[email protected]420ae012009-04-24 05:16:322507}
2508
2509void TabContents::ProcessExternalHostMessage(const std::string& message,
2510 const std::string& origin,
2511 const std::string& target) {
2512 if (delegate())
2513 delegate()->ForwardMessageToExternalHost(message, origin, target);
2514}
2515
[email protected]cdaf8d02010-03-30 19:52:472516void TabContents::RunFileChooser(
2517 const ViewHostMsg_RunFileChooser_Params &params) {
[email protected]420ae012009-04-24 05:16:322518 if (!select_file_dialog_.get())
2519 select_file_dialog_ = SelectFileDialog::Create(this);
[email protected]cdaf8d02010-03-30 19:52:472520
2521 SelectFileDialog::Type dialog_type;
2522 switch (params.mode) {
2523 case ViewHostMsg_RunFileChooser_Params::Open:
2524 dialog_type = SelectFileDialog::SELECT_OPEN_FILE;
2525 break;
2526 case ViewHostMsg_RunFileChooser_Params::OpenMultiple:
2527 dialog_type = SelectFileDialog::SELECT_OPEN_MULTI_FILE;
2528 break;
2529 case ViewHostMsg_RunFileChooser_Params::Save:
2530 dialog_type = SelectFileDialog::SELECT_SAVEAS_FILE;
2531 break;
2532 default:
[email protected]bed31952010-03-30 20:07:592533 dialog_type = SelectFileDialog::SELECT_OPEN_FILE; // Prevent warning.
[email protected]cdaf8d02010-03-30 19:52:472534 NOTREACHED();
2535 }
2536 select_file_dialog_->SelectFile(dialog_type, params.title,
2537 params.default_file_name,
[email protected]420ae012009-04-24 05:16:322538 NULL, 0, FILE_PATH_LITERAL(""),
2539 view_->GetTopLevelNativeWindow(), NULL);
2540}
2541
2542void TabContents::RunJavaScriptMessage(
2543 const std::wstring& message,
2544 const std::wstring& default_prompt,
2545 const GURL& frame_url,
2546 const int flags,
2547 IPC::Message* reply_msg,
2548 bool* did_suppress_message) {
2549 // Suppress javascript messages when requested and when inside a constrained
2550 // popup window (because that activates them and breaks them out of the
2551 // constrained window jail).
[email protected]91ed49a2009-07-07 21:29:552552 // Also suppress messages when showing an interstitial. The interstitial is
2553 // shown over the previous page, we don't want the hidden page dialogs to
2554 // interfere with the interstitial.
2555 bool suppress_this_message = suppress_javascript_messages_ ||
2556 showing_interstitial_page();
[email protected]420ae012009-04-24 05:16:322557 if (delegate())
2558 suppress_this_message |=
[email protected]b6c874582009-05-08 19:38:312559 (delegate()->GetConstrainingContents(this) != this);
[email protected]420ae012009-04-24 05:16:322560
2561 *did_suppress_message = suppress_this_message;
2562
2563 if (!suppress_this_message) {
2564 base::TimeDelta time_since_last_message(
2565 base::TimeTicks::Now() - last_javascript_message_dismissal_);
2566 bool show_suppress_checkbox = false;
2567 // Show a checkbox offering to suppress further messages if this message is
2568 // being displayed within kJavascriptMessageExpectedDelay of the last one.
2569 if (time_since_last_message <
2570 base::TimeDelta::FromMilliseconds(kJavascriptMessageExpectedDelay))
2571 show_suppress_checkbox = true;
2572
[email protected]d82ed61e2009-06-16 02:46:222573 RunJavascriptMessageBox(this, frame_url, flags, message, default_prompt,
[email protected]420ae012009-04-24 05:16:322574 show_suppress_checkbox, reply_msg);
2575 } else {
2576 // If we are suppressing messages, just reply as is if the user immediately
2577 // pressed "Cancel".
[email protected]beb440c2009-11-06 04:08:542578 OnMessageBoxClosed(reply_msg, false, std::wstring());
[email protected]420ae012009-04-24 05:16:322579 }
2580}
2581
2582void TabContents::RunBeforeUnloadConfirm(const std::wstring& message,
2583 IPC::Message* reply_msg) {
[email protected]7ab1e7d62009-10-14 23:32:012584 is_showing_before_unload_dialog_ = true;
[email protected]d82ed61e2009-06-16 02:46:222585 RunBeforeUnloadDialog(this, message, reply_msg);
[email protected]420ae012009-04-24 05:16:322586}
2587
2588void TabContents::ShowModalHTMLDialog(const GURL& url, int width, int height,
2589 const std::string& json_arguments,
2590 IPC::Message* reply_msg) {
2591 if (delegate()) {
2592 HtmlDialogUIDelegate* dialog_delegate =
2593 new ModalHtmlDialogDelegate(url, width, height, json_arguments,
[email protected]d82ed61e2009-06-16 02:46:222594 reply_msg, this);
[email protected]420ae012009-04-24 05:16:322595 delegate()->ShowHtmlDialog(dialog_delegate, NULL);
2596 }
2597}
2598
[email protected]4d44a1c2010-04-28 19:20:412599void TabContents::PasswordFormsFound(
[email protected]ed3fb032009-06-16 19:50:562600 const std::vector<webkit_glue::PasswordForm>& forms) {
[email protected]4d44a1c2010-04-28 19:20:412601 GetPasswordManager()->PasswordFormsFound(forms);
2602}
2603
2604void TabContents::PasswordFormsVisible(
2605 const std::vector<webkit_glue::PasswordForm>& visible_forms) {
2606 GetPasswordManager()->PasswordFormsVisible(visible_forms);
[email protected]420ae012009-04-24 05:16:322607}
2608
[email protected]420ae012009-04-24 05:16:322609// Checks to see if we should generate a keyword based on the OSDD, and if
2610// necessary uses TemplateURLFetcher to download the OSDD and create a keyword.
2611void TabContents::PageHasOSDD(RenderViewHost* render_view_host,
2612 int32 page_id, const GURL& url,
2613 bool autodetected) {
2614 // Make sure page_id is the current page, and the TemplateURLModel is loaded.
2615 DCHECK(url.is_valid());
2616 if (!IsActiveEntry(page_id))
2617 return;
2618 TemplateURLModel* url_model = profile()->GetTemplateURLModel();
2619 if (!url_model)
2620 return;
2621 if (!url_model->loaded()) {
2622 url_model->Load();
2623 return;
2624 }
2625 if (!profile()->GetTemplateURLFetcher())
2626 return;
2627
2628 if (profile()->IsOffTheRecord())
2629 return;
2630
2631 const NavigationEntry* entry = controller_.GetLastCommittedEntry();
2632 DCHECK(entry);
2633
2634 const NavigationEntry* base_entry = entry;
2635 if (IsFormSubmit(base_entry)) {
2636 // If the current page is a form submit, find the last page that was not
2637 // a form submit and use its url to generate the keyword from.
2638 int index = controller_.last_committed_entry_index() - 1;
2639 while (index >= 0 && IsFormSubmit(controller_.GetEntryAtIndex(index)))
2640 index--;
2641 if (index >= 0)
2642 base_entry = controller_.GetEntryAtIndex(index);
2643 else
2644 base_entry = NULL;
2645 }
2646
2647 // We want to use the user typed URL if available since that represents what
2648 // the user typed to get here, and fall back on the regular URL if not.
2649 if (!base_entry)
2650 return;
2651 GURL keyword_url = base_entry->user_typed_url().is_valid() ?
2652 base_entry->user_typed_url() : base_entry->url();
2653 if (!keyword_url.is_valid())
2654 return;
2655 std::wstring keyword = TemplateURLModel::GenerateKeyword(keyword_url,
2656 autodetected);
[email protected]a88dbdd52010-01-15 19:25:532657
2658 // For JS added OSDD empty keyword is OK because we will generate keyword
2659 // later from OSDD content.
2660 if (autodetected && keyword.empty())
[email protected]420ae012009-04-24 05:16:322661 return;
2662 const TemplateURL* template_url =
2663 url_model->GetTemplateURLForKeyword(keyword);
2664 if (template_url && (!template_url->safe_for_autoreplace() ||
2665 template_url->originating_url() == url)) {
2666 // Either there is a user created TemplateURL for this keyword, or the
2667 // keyword has the same OSDD url and we've parsed it.
2668 return;
2669 }
2670
2671 // Download the OpenSearch description document. If this is successful a
2672 // new keyword will be created when done.
[email protected]420ae012009-04-24 05:16:322673 profile()->GetTemplateURLFetcher()->ScheduleDownload(
2674 keyword,
2675 url,
2676 base_entry->favicon().url(),
[email protected]d0cc9fb52009-06-23 02:26:162677 this,
[email protected]420ae012009-04-24 05:16:322678 autodetected);
2679}
2680
[email protected]420ae012009-04-24 05:16:322681GURL TabContents::GetAlternateErrorPageURL() const {
2682 GURL url;
2683 // Disable alternate error pages when in OffTheRecord/Incognito mode.
2684 if (profile()->IsOffTheRecord())
2685 return url;
2686
2687 PrefService* prefs = profile()->GetPrefs();
2688 DCHECK(prefs);
2689 if (prefs->GetBoolean(prefs::kAlternateErrorPagesEnabled)) {
[email protected]a33457b82009-09-21 23:07:152690 url = google_util::AppendGoogleLocaleParam(
2691 GURL(google_util::kLinkDoctorBaseURL));
[email protected]420ae012009-04-24 05:16:322692 url = google_util::AppendGoogleTLDParam(url);
2693 }
2694 return url;
2695}
2696
2697WebPreferences TabContents::GetWebkitPrefs() {
[email protected]6c32ce72010-03-08 05:18:062698 Profile* profile = render_view_host()->process()->profile();
[email protected]7ccddb8c2009-08-04 17:36:552699 bool is_dom_ui = false;
[email protected]6c32ce72010-03-08 05:18:062700 return RenderViewHostDelegateHelper::GetWebkitPrefs(profile, is_dom_ui);
[email protected]420ae012009-04-24 05:16:322701}
2702
[email protected]fa1cf0b82010-01-15 21:49:442703void TabContents::OnIgnoredUIEvent() {
2704 if (constrained_window_count()) {
2705 ConstrainedWindow* window = *constrained_window_begin();
2706 window->FocusConstrainedWindow();
2707 }
2708}
2709
[email protected]420ae012009-04-24 05:16:322710void TabContents::OnJSOutOfMemory() {
2711 AddInfoBar(new SimpleAlertInfoBarDelegate(
[email protected]938e1f92010-04-01 18:09:422712 this, l10n_util::GetString(IDS_JS_OUT_OF_MEMORY_PROMPT), NULL, true));
[email protected]420ae012009-04-24 05:16:322713}
2714
[email protected]420ae012009-04-24 05:16:322715void TabContents::OnCrossSiteResponse(int new_render_process_host_id,
2716 int new_request_id) {
[email protected]57c6a652009-05-04 07:58:342717 // Allows the TabContents to react when a cross-site response is ready to be
[email protected]420ae012009-04-24 05:16:322718 // delivered to a pending RenderViewHost. We must first run the onunload
2719 // handler of the old RenderViewHost before we can allow it to proceed.
2720 render_manager_.OnCrossSiteResponse(new_render_process_host_id,
2721 new_request_id);
2722}
2723
2724bool TabContents::CanBlur() const {
2725 return delegate() ? delegate()->CanBlur() : true;
2726}
2727
2728gfx::Rect TabContents::GetRootWindowResizerRect() const {
2729 if (delegate())
2730 return delegate()->GetRootWindowResizerRect();
2731 return gfx::Rect();
2732}
2733
2734void TabContents::RendererUnresponsive(RenderViewHost* rvh,
2735 bool is_during_unload) {
2736 if (is_during_unload) {
2737 // Hang occurred while firing the beforeunload/unload handler.
2738 // Pretend the handler fired so tab closing continues as if it had.
[email protected]829e7612009-04-25 01:15:112739 rvh->set_sudden_termination_allowed(true);
[email protected]420ae012009-04-24 05:16:322740
2741 if (!render_manager_.ShouldCloseTabOnUnresponsiveRenderer())
2742 return;
2743
2744 // If the tab hangs in the beforeunload/unload handler there's really
2745 // nothing we can do to recover. Pretend the unload listeners have
2746 // all fired and close the tab. If the hang is in the beforeunload handler
2747 // then the user will not have the option of cancelling the close.
2748 Close(rvh);
2749 return;
2750 }
2751
2752 if (render_view_host() && render_view_host()->IsRenderViewLive())
[email protected]e7b8d832009-11-20 22:41:002753 hung_renderer_dialog::ShowForTabContents(this);
[email protected]420ae012009-04-24 05:16:322754}
2755
2756void TabContents::RendererResponsive(RenderViewHost* render_view_host) {
[email protected]e7b8d832009-11-20 22:41:002757 hung_renderer_dialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:322758}
2759
2760void TabContents::LoadStateChanged(const GURL& url,
[email protected]094e5b22009-09-25 04:23:562761 net::LoadState load_state,
2762 uint64 upload_position,
2763 uint64 upload_size) {
[email protected]420ae012009-04-24 05:16:322764 load_state_ = load_state;
[email protected]094e5b22009-09-25 04:23:562765 upload_position_ = upload_position;
2766 upload_size_ = upload_size;
[email protected]d686e812009-06-03 19:10:292767 std::wstring languages =
2768 profile()->GetPrefs()->GetString(prefs::kAcceptLanguages);
[email protected]d686e812009-06-03 19:10:292769 std::string host = url.host();
[email protected]ce85f602009-11-07 01:34:532770 load_state_host_ =
2771 net::IDNToUnicode(host.c_str(), host.size(), languages, NULL);
[email protected]420ae012009-04-24 05:16:322772 if (load_state_ == net::LOAD_STATE_READING_RESPONSE)
2773 SetNotWaitingForResponse();
2774 if (is_loading())
[email protected]c9cd2222009-05-06 05:16:502775 NotifyNavigationStateChanged(INVALIDATE_LOAD | INVALIDATE_TAB);
[email protected]420ae012009-04-24 05:16:322776}
2777
[email protected]1ee614862009-07-06 20:29:182778bool TabContents::IsExternalTabContainer() const {
2779 if (!delegate())
2780 return false;
2781
2782 return delegate()->IsExternalTabContainer();
2783}
2784
[email protected]1ee614862009-07-06 20:29:182785void TabContents::DidInsertCSS() {
2786 // This RVHDelegate function is used for extensions and not us.
[email protected]420ae012009-04-24 05:16:322787}
2788
[email protected]130efb02009-09-18 18:54:352789void TabContents::FocusedNodeChanged() {
2790 NotificationService::current()->Notify(
2791 NotificationType::FOCUS_CHANGED_IN_PAGE,
2792 Source<RenderViewHost>(render_view_host()),
2793 NotificationService::NoDetails());
2794}
2795
[email protected]420ae012009-04-24 05:16:322796void TabContents::FileSelected(const FilePath& path,
2797 int index, void* params) {
[email protected]cdaf8d02010-03-30 19:52:472798 std::vector<FilePath> files;
2799 files.push_back(path);
2800 render_view_host()->FilesSelectedInChooser(files);
[email protected]420ae012009-04-24 05:16:322801}
2802
2803void TabContents::MultiFilesSelected(const std::vector<FilePath>& files,
2804 void* params) {
[email protected]cdaf8d02010-03-30 19:52:472805 render_view_host()->FilesSelectedInChooser(files);
[email protected]420ae012009-04-24 05:16:322806}
2807
2808void TabContents::FileSelectionCanceled(void* params) {
2809 // If the user cancels choosing a file to upload we pass back an
2810 // empty vector.
[email protected]cdaf8d02010-03-30 19:52:472811 render_view_host()->FilesSelectedInChooser(std::vector<FilePath>());
[email protected]420ae012009-04-24 05:16:322812}
2813
2814void TabContents::BeforeUnloadFiredFromRenderManager(
2815 bool proceed,
2816 bool* proceed_to_fire_unload) {
2817 if (delegate())
2818 delegate()->BeforeUnloadFired(this, proceed, proceed_to_fire_unload);
2819}
2820
2821void TabContents::UpdateRenderViewSizeForRenderManager() {
2822 // TODO(brettw) this is a hack. See TabContentsView::SizeContents.
[email protected]0323ee42010-02-17 22:03:262823 gfx::Size size = view_->GetContainerSize();
2824 // 0x0 isn't a valid window size (minimal window size is 1x1) but it may be
2825 // here during container initialization and normal window size will be set
2826 // later. In case of tab duplication this resizing to 0x0 prevents setting
2827 // normal size later so just ignore it.
2828 if (!size.IsEmpty())
2829 view_->SizeContents(size);
[email protected]420ae012009-04-24 05:16:322830}
2831
2832DOMUI* TabContents::CreateDOMUIForRenderManager(const GURL& url) {
[email protected]d82ed61e2009-06-16 02:46:222833 return DOMUIFactory::CreateDOMUIForURL(this, url);
[email protected]420ae012009-04-24 05:16:322834}
2835
2836NavigationEntry*
2837TabContents::GetLastCommittedNavigationEntryForRenderManager() {
2838 return controller_.GetLastCommittedEntry();
2839}
2840
2841bool TabContents::CreateRenderViewForRenderManager(
2842 RenderViewHost* render_view_host) {
[email protected]420ae012009-04-24 05:16:322843 RenderWidgetHostView* rwh_view = view_->CreateViewForWidget(render_view_host);
[email protected]1a98a932009-11-17 00:12:522844
2845 scoped_refptr<URLRequestContextGetter> request_context = request_context_;
2846 if (!request_context.get())
2847 request_context = profile()->GetRequestContext();
2848
2849 if (!render_view_host->CreateRenderView(request_context))
[email protected]420ae012009-04-24 05:16:322850 return false;
2851
2852 // Now that the RenderView has been created, we need to tell it its size.
2853 rwh_view->SetSize(view_->GetContainerSize());
2854
2855 UpdateMaxPageIDIfNecessary(render_view_host->site_instance(),
2856 render_view_host);
2857 return true;
2858}
2859
2860void TabContents::Observe(NotificationType type,
2861 const NotificationSource& source,
2862 const NotificationDetails& details) {
2863 switch (type.value) {
2864 case NotificationType::BOOKMARK_MODEL_LOADED:
2865 // BookmarkModel finished loading, fall through to update starred state.
2866 case NotificationType::URLS_STARRED: {
2867 // Somewhere, a URL has been starred.
2868 // Ignore notifications for profiles other than our current one.
2869 Profile* source_profile = Source<Profile>(source).ptr();
2870 if (!source_profile || !source_profile->IsSameProfile(profile()))
2871 return;
2872
2873 UpdateStarredStateForCurrentURL();
2874 break;
2875 }
2876 case NotificationType::PREF_CHANGED: {
2877 std::wstring* pref_name_in = Details<std::wstring>(details).ptr();
2878 DCHECK(Source<PrefService>(source).ptr() == profile()->GetPrefs());
2879 if (*pref_name_in == prefs::kAlternateErrorPagesEnabled) {
2880 UpdateAlternateErrorPageURL();
2881 } else if (*pref_name_in == prefs::kDefaultCharset ||
2882 StartsWithASCII(WideToUTF8(*pref_name_in), "webkit.webprefs.", true)
2883 ) {
2884 UpdateWebPreferences();
2885 } else {
2886 NOTREACHED() << "unexpected pref change notification" << *pref_name_in;
2887 }
2888 break;
2889 }
2890 case NotificationType::RENDER_WIDGET_HOST_DESTROYED:
2891 view_->RenderWidgetHostDestroyed(Source<RenderWidgetHost>(source).ptr());
2892 break;
2893
2894 case NotificationType::NAV_ENTRY_COMMITTED: {
2895 DCHECK(&controller_ == Source<NavigationController>(source).ptr());
2896
2897 NavigationController::LoadCommittedDetails& committed_details =
2898 *(Details<NavigationController::LoadCommittedDetails>(details).ptr());
2899 ExpireInfoBars(committed_details);
2900 break;
2901 }
2902
[email protected]d3ba77272009-09-03 00:06:092903#if defined(OS_LINUX)
2904 case NotificationType::BROWSER_THEME_CHANGED: {
[email protected]7ff431e2010-01-07 18:18:542905 renderer_preferences_util::UpdateFromSystemSettings(
2906 &renderer_preferences_, profile());
[email protected]d3ba77272009-09-03 00:06:092907 render_view_host()->SyncRendererPrefs();
2908 break;
2909 }
2910#endif
2911
[email protected]6c32ce72010-03-08 05:18:062912 case NotificationType::USER_STYLE_SHEET_UPDATED:
2913 UpdateWebPreferences();
2914 break;
2915
[email protected]79580c62010-02-02 02:36:252916 case NotificationType::CONTENT_SETTINGS_CHANGED: {
[email protected]0314ae02010-04-08 09:18:292917 Details<const HostContentSettingsMap::ContentSettingsDetails>
[email protected]79580c62010-02-02 02:36:252918 settings_details(details);
[email protected]79580c62010-02-02 02:36:252919 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]da0c8e92010-02-09 23:25:492920 GURL entry_url;
[email protected]0314ae02010-04-08 09:18:292921 if (entry)
[email protected]da0c8e92010-02-09 23:25:492922 entry_url = entry->url();
[email protected]79580c62010-02-02 02:36:252923 Source<HostContentSettingsMap> content_settings(source);
[email protected]0314ae02010-04-08 09:18:292924 if (settings_details.ptr()->update_all() ||
2925 settings_details.ptr()->pattern().Matches(entry_url)) {
2926 render_view_host()->SendContentSettings(entry_url,
[email protected]da0c8e92010-02-09 23:25:492927 content_settings.ptr()->GetContentSettings(entry_url));
[email protected]79580c62010-02-02 02:36:252928 }
2929 break;
2930 }
2931
[email protected]a888b29e62010-04-01 13:38:572932 case NotificationType::EXTENSION_LOADED: {
2933 if (!app_extension_ && !app_extension_for_current_page_ &&
2934 Source<Profile>(source).ptr() == profile()) {
2935 UpdateAppExtensionForCurrentPage();
2936 if (app_extension_for_current_page_)
2937 NotifyNavigationStateChanged(INVALIDATE_TAB);
2938 }
2939 break;
2940 }
2941
2942 case NotificationType::EXTENSION_UNLOADED:
2943 case NotificationType::EXTENSION_UNLOADED_DISABLED: {
2944 if (app_extension_for_current_page_ ==
2945 Details<Extension>(details).ptr()) {
2946 app_extension_for_current_page_ = NULL;
2947 UpdateAppExtensionForCurrentPage();
2948 NotifyNavigationStateChanged(INVALIDATE_TAB);
2949 }
2950 break;
2951 }
2952
[email protected]420ae012009-04-24 05:16:322953 default:
2954 NOTREACHED();
2955 }
2956}
[email protected]aacd7cc2009-09-02 14:51:102957
[email protected]a888b29e62010-04-01 13:38:572958void TabContents::UpdateAppExtensionIcon(Extension* extension) {
2959 app_extension_icon_.reset();
[email protected]f075fe0a22010-04-26 04:06:432960
2961 // We don't show the big icons in tabs for TYPE_EXTENSION_APP windows because
2962 // for those windows, we already have a big icon in the top-left outside any
2963 // tab. Having big tab icons too looks kinda redonk.
2964 if (!delegate_ ||
2965 !delegate_->GetBrowser() ||
2966 delegate_->GetBrowser()->type() == Browser::TYPE_EXTENSION_APP) {
2967 return;
2968 }
2969
[email protected]a888b29e62010-04-01 13:38:572970 if (extension) {
2971 app_extension_image_loader_.reset(new ImageLoadingTracker(this));
2972 app_extension_image_loader_->LoadImage(
2973 extension,
2974 extension->GetIconPath(Extension::EXTENSION_ICON_SMALLISH),
2975 gfx::Size(Extension::EXTENSION_ICON_SMALLISH,
2976 Extension::EXTENSION_ICON_SMALLISH),
2977 ImageLoadingTracker::CACHE);
2978 } else {
2979 app_extension_image_loader_.reset(NULL);
2980 }
2981}
2982
2983Extension* TabContents::GetExtensionContaining(const GURL& url) {
2984 ExtensionsService* extensions_service = profile()->GetExtensionsService();
2985 if (!extensions_service)
2986 return NULL;
2987
2988 Extension* extension = extensions_service->GetExtensionByURL(url);
2989 return extension ?
2990 extension : extensions_service->GetExtensionByWebExtent(url);
2991}
2992
2993void TabContents::UpdateAppExtensionForCurrentPage() {
2994 if (app_extension_) {
2995 // Tab has an explicit app extension; nothing to do.
2996 return;
2997 }
2998
2999 // Check the current extension before iterating through all extensions.
3000 if (app_extension_for_current_page_ &&
3001 app_extension_for_current_page_->web_extent().ContainsURL(GetURL())) {
3002 return;
3003 }
3004
3005 app_extension_for_current_page_ = GetExtensionContaining(GetURL());
3006 UpdateAppExtensionIcon(app_extension_for_current_page_);
3007}
3008
3009void TabContents::OnImageLoaded(SkBitmap* image, ExtensionResource resource,
3010 int index) {
3011 if (image) {
3012 app_extension_icon_ = *image;
3013 NotifyNavigationStateChanged(INVALIDATE_TAB);
3014 }
3015}
3016
[email protected]beb440c2009-11-06 04:08:543017std::wstring TabContents::GetMessageBoxTitle(const GURL& frame_url,
3018 bool is_alert) {
3019 if (!frame_url.has_host())
3020 return l10n_util::GetString(
3021 is_alert ? IDS_JAVASCRIPT_ALERT_DEFAULT_TITLE
3022 : IDS_JAVASCRIPT_MESSAGEBOX_DEFAULT_TITLE);
3023
3024 // We really only want the scheme, hostname, and port.
3025 GURL::Replacements replacements;
3026 replacements.ClearUsername();
3027 replacements.ClearPassword();
3028 replacements.ClearPath();
3029 replacements.ClearQuery();
3030 replacements.ClearRef();
3031 GURL clean_url = frame_url.ReplaceComponents(replacements);
3032
3033 // TODO(brettw) it should be easier than this to do the correct language
3034 // handling without getting the accept language from the profile.
3035 std::wstring base_address = gfx::ElideUrl(clean_url, gfx::Font(), 0,
3036 profile()->GetPrefs()->GetString(prefs::kAcceptLanguages));
3037 // Force URL to have LTR directionality.
[email protected]7cf1b6ce2010-03-20 06:37:013038 if (base::i18n::IsRTL())
3039 base::i18n::WrapStringWithLTRFormatting(&base_address);
[email protected]beb440c2009-11-06 04:08:543040
3041 return l10n_util::GetStringF(
3042 is_alert ? IDS_JAVASCRIPT_ALERT_TITLE : IDS_JAVASCRIPT_MESSAGEBOX_TITLE,
3043 base_address);
3044}
3045
3046gfx::NativeWindow TabContents::GetMessageBoxRootWindow() {
3047 return view_->GetTopLevelNativeWindow();
3048}
3049
3050void TabContents::OnMessageBoxClosed(IPC::Message* reply_msg,
3051 bool success,
3052 const std::wstring& prompt) {
3053 last_javascript_message_dismissal_ = base::TimeTicks::Now();
3054 if (is_showing_before_unload_dialog_ && !success) {
3055 // If a beforeunload dialog is canceled, we need to stop the throbber from
3056 // spinning, since we forced it to start spinning in Navigate.
3057 DidStopLoading();
3058
3059 tab_close_start_time_ = base::TimeTicks();
3060 }
3061 is_showing_before_unload_dialog_ = false;
3062 render_view_host()->JavaScriptMessageBoxClosed(reply_msg, success, prompt);
3063}
3064
3065void TabContents::SetSuppressMessageBoxes(bool suppress_message_boxes) {
3066 set_suppress_javascript_messages(suppress_message_boxes);
3067}
3068
[email protected]41fc0322009-09-04 22:23:403069void TabContents::set_encoding(const std::string& encoding) {
[email protected]aacd7cc2009-09-02 14:51:103070 encoding_ = CharacterEncoding::GetCanonicalEncodingNameByAliasName(encoding);
3071}
[email protected]f45d2a72010-03-08 23:28:353072
3073void TabContents::SetAppIcon(const SkBitmap& app_icon) {
3074 app_icon_ = app_icon;
3075 NotifyNavigationStateChanged(INVALIDATE_TITLE);
3076}
[email protected]ea68c06a2010-04-23 17:25:373077
3078// After a successful *new* login attempt, we take the PasswordFormManager in
3079// provisional_save_manager_ and move it to a SavePasswordInfoBarDelegate while
3080// the user makes up their mind with the "save password" infobar. Note if the
3081// login is one we already know about, the end of the line is
3082// provisional_save_manager_ because we just update it on success and so such
3083// forms never end up in an infobar.
3084class SavePasswordInfoBarDelegate : public ConfirmInfoBarDelegate {
3085 public:
3086 SavePasswordInfoBarDelegate(TabContents* tab_contents,
3087 PasswordFormManager* form_to_save) :
3088 ConfirmInfoBarDelegate(tab_contents),
3089 form_to_save_(form_to_save) {
3090 }
3091
3092 virtual ~SavePasswordInfoBarDelegate() { }
3093
3094 // Overridden from ConfirmInfoBarDelegate:
3095 virtual void InfoBarClosed() {
3096 delete this;
3097 }
3098
3099 virtual std::wstring GetMessageText() const {
3100 return l10n_util::GetString(IDS_PASSWORD_MANAGER_SAVE_PASSWORD_PROMPT);
3101 }
3102
3103 virtual SkBitmap* GetIcon() const {
3104 return ResourceBundle::GetSharedInstance().GetBitmapNamed(
3105 IDR_INFOBAR_SAVE_PASSWORD);
3106 }
3107
3108 virtual int GetButtons() const {
3109 return BUTTON_OK | BUTTON_CANCEL;
3110 }
3111
3112 virtual std::wstring GetButtonLabel(InfoBarButton button) const {
3113 if (button == BUTTON_OK)
3114 return l10n_util::GetString(IDS_PASSWORD_MANAGER_SAVE_BUTTON);
3115 if (button == BUTTON_CANCEL)
3116 return l10n_util::GetString(IDS_PASSWORD_MANAGER_BLACKLIST_BUTTON);
3117 NOTREACHED();
3118 return std::wstring();
3119 }
3120
3121 virtual bool Accept() {
3122 DCHECK(form_to_save_.get());
3123 form_to_save_->Save();
3124 return true;
3125 }
3126
3127 virtual bool Cancel() {
3128 DCHECK(form_to_save_.get());
3129 form_to_save_->PermanentlyBlacklist();
3130 return true;
3131 }
3132
3133 private:
3134 // The PasswordFormManager managing the form we're asking the user about,
3135 // and should update as per her decision.
3136 scoped_ptr<PasswordFormManager> form_to_save_;
3137
3138 DISALLOW_COPY_AND_ASSIGN(SavePasswordInfoBarDelegate);
3139};
3140
3141void TabContents::FillPasswordForm(
3142 const webkit_glue::PasswordFormDomManager::FillData& form_data) {
3143 render_view_host()->FillPasswordForm(form_data);
3144}
3145
3146void TabContents::AddSavePasswordInfoBar(PasswordFormManager* form_to_save) {
3147 AddInfoBar(new SavePasswordInfoBarDelegate(this, form_to_save));
3148}
3149
3150Profile* TabContents::GetProfileForPasswordManager() {
3151 return profile();
3152}
3153
3154bool TabContents::DidLastPageLoadEncounterSSLErrors() {
3155 return controller().ssl_manager()->ProcessedSSLErrorFromRequest();
3156}
3157