blob: 384cdd11f622c42c65529924685024360b73209f [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"
12#include "base/process_util.h"
[email protected]96d185d2009-04-24 03:28:5413#include "base/string16.h"
[email protected]996fd702009-09-04 19:12:3714#include "base/string_util.h"
[email protected]96d185d2009-04-24 03:28:5415#include "base/time.h"
[email protected]6db0def92010-03-02 19:55:4516#include "chrome/browser/autocomplete_history_manager.h"
[email protected]21674df2009-10-28 19:51:2117#include "chrome/browser/autofill/autofill_manager.h"
[email protected]66ba4932009-06-04 19:22:1318#include "chrome/browser/blocked_popup_container.h"
[email protected]420ae012009-04-24 05:16:3219#include "chrome/browser/bookmarks/bookmark_model.h"
[email protected]96d185d2009-04-24 03:28:5420#include "chrome/browser/browser_process.h"
[email protected]cf68b092010-01-15 20:05:5521#include "chrome/browser/browser_shutdown.h"
initial.commit09911bf2008-07-26 23:55:2922#include "chrome/browser/cert_store.h"
[email protected]aacd7cc2009-09-02 14:51:1023#include "chrome/browser/character_encoding.h"
[email protected]96d185d2009-04-24 03:28:5424#include "chrome/browser/debugger/devtools_manager.h"
[email protected]420ae012009-04-24 05:16:3225#include "chrome/browser/dom_operation_notification_details.h"
[email protected]96d185d2009-04-24 03:28:5426#include "chrome/browser/dom_ui/dom_ui.h"
27#include "chrome/browser/dom_ui/dom_ui_factory.h"
[email protected]bcef0dc02009-02-28 00:35:0228#include "chrome/browser/download/download_item_model.h"
[email protected]420ae012009-04-24 05:16:3229#include "chrome/browser/download/download_manager.h"
30#include "chrome/browser/download/download_request_manager.h"
[email protected]e7eaedde2009-09-25 20:09:4931#include "chrome/browser/external_protocol_handler.h"
[email protected]92c6f9b92009-10-24 04:35:0832#include "chrome/browser/extensions/extensions_service.h"
[email protected]0189bc722009-08-28 21:56:4833#include "chrome/browser/favicon_service.h"
[email protected]420ae012009-04-24 05:16:3234#include "chrome/browser/google_util.h"
[email protected]79580c62010-02-02 02:36:2535#include "chrome/browser/host_content_settings_map.h"
[email protected]8897c382009-05-06 17:53:2636#include "chrome/browser/hung_renderer_dialog.h"
[email protected]12f74a92010-02-05 22:32:1437#include "chrome/browser/message_box_handler.h"
[email protected]420ae012009-04-24 05:16:3238#include "chrome/browser/load_from_memory_cache_details.h"
39#include "chrome/browser/load_notification_details.h"
[email protected]84abba62009-10-07 17:01:4440#include "chrome/browser/metrics/metric_event_duration_details.h"
[email protected]ea0e80892009-08-22 00:36:0541#include "chrome/browser/modal_html_dialog_delegate.h"
[email protected]c8865482009-07-23 20:40:1042#include "chrome/browser/omnibox_search_hint.h"
[email protected]fdd61c62009-04-22 19:22:5743#include "chrome/browser/password_manager/password_manager.h"
44#include "chrome/browser/plugin_installer.h"
[email protected]052313b2010-02-19 09:43:0845#include "chrome/browser/pref_service.h"
[email protected]ce560f82009-06-03 09:39:4446#include "chrome/browser/profile.h"
[email protected]1db6ff152009-10-12 15:32:0747#include "chrome/browser/renderer_host/render_process_host.h"
[email protected]8cb5d5b2010-02-09 11:36:1648#include "chrome/browser/renderer_host/render_view_host.h"
[email protected]96d185d2009-04-24 03:28:5449#include "chrome/browser/renderer_host/render_widget_host_view.h"
[email protected]fd694982009-07-03 17:32:3750#include "chrome/browser/renderer_host/resource_request_details.h"
[email protected]1db6ff152009-10-12 15:32:0751#include "chrome/browser/renderer_host/site_instance.h"
[email protected]d4a00a72010-01-29 01:44:4252#include "chrome/browser/renderer_host/translation_service.h"
[email protected]96d185d2009-04-24 03:28:5453#include "chrome/browser/renderer_host/web_cache_manager.h"
[email protected]93623c5d2009-12-10 21:40:3254#include "chrome/browser/renderer_preferences_util.h"
[email protected]c37e3b62010-01-22 00:15:1755#include "chrome/browser/sessions/session_types.h"
[email protected]bd1ad682009-05-15 22:19:1756#include "chrome/browser/tab_contents/infobar_delegate.h"
[email protected]7e383692009-06-12 19:14:5457#include "chrome/browser/tab_contents/interstitial_page.h"
[email protected]f3ec7742009-01-15 00:59:1658#include "chrome/browser/tab_contents/navigation_entry.h"
[email protected]de34a962009-06-26 23:22:4359#include "chrome/browser/tab_contents/provisional_load_details.h"
[email protected]f3ec7742009-01-15 00:59:1660#include "chrome/browser/tab_contents/tab_contents_delegate.h"
[email protected]fdd61c62009-04-22 19:22:5761#include "chrome/browser/tab_contents/tab_contents_view.h"
[email protected]58dca552009-06-17 00:35:0262#include "chrome/browser/tab_contents/thumbnail_generator.h"
[email protected]449478302009-06-09 20:04:2863#include "chrome/browser/thumbnail_store.h"
[email protected]420ae012009-04-24 05:16:3264#include "chrome/browser/search_engines/template_url_fetcher.h"
65#include "chrome/browser/search_engines/template_url_model.h"
[email protected]35f7d212009-04-29 21:19:2766#include "chrome/common/chrome_switches.h"
[email protected]3b355642010-02-05 16:01:4967#include "chrome/common/extensions/extension.h"
[email protected]5d246db22009-10-27 06:17:5768#include "chrome/common/extensions/extension_action.h"
[email protected]bfd04a62009-02-01 18:16:5669#include "chrome/common/notification_service.h"
[email protected]89d6e6e2009-12-03 23:06:5170#include "chrome/common/platform_util.h"
initial.commit09911bf2008-07-26 23:55:2971#include "chrome/common/pref_names.h"
[email protected]420ae012009-04-24 05:16:3272#include "chrome/common/render_messages.h"
[email protected]1ee614862009-07-06 20:29:1873#include "chrome/common/renderer_preferences.h"
[email protected]96d185d2009-04-24 03:28:5474#include "chrome/common/url_constants.h"
[email protected]8897c382009-05-06 17:53:2675#include "grit/generated_resources.h"
[email protected]420ae012009-04-24 05:16:3276#include "grit/locale_settings.h"
[email protected]fd626782010-01-22 05:26:0677#include "grit/platform_locale_settings.h"
[email protected]420ae012009-04-24 05:16:3278#include "net/base/mime_util.h"
79#include "net/base/net_errors.h"
[email protected]d686e812009-06-03 19:10:2980#include "net/base/net_util.h"
[email protected]420ae012009-04-24 05:16:3281#include "net/base/registry_controlled_domain.h"
[email protected]1ee614862009-07-06 20:29:1882#include "webkit/glue/password_form.h"
83#include "webkit/glue/webpreferences.h"
[email protected]3e45ba92009-02-20 21:09:0084
[email protected]304f8792009-08-19 21:28:0985#if defined(OS_CHROMEOS)
86// For GdkScreen
87#include <gdk/gdk.h>
[email protected]0ba5c5e2009-08-20 16:47:1388#endif // defined(OS_CHROMEOS)
89
[email protected]420ae012009-04-24 05:16:3290// Cross-Site Navigations
91//
92// If a TabContents is told to navigate to a different web site (as determined
93// by SiteInstance), it will replace its current RenderViewHost with a new
94// RenderViewHost dedicated to the new SiteInstance. This works as follows:
95//
96// - Navigate determines whether the destination is cross-site, and if so,
97// it creates a pending_render_view_host_ and moves into the PENDING
98// RendererState.
99// - The pending RVH is "suspended," so that no navigation messages are sent to
100// its renderer until the onbeforeunload JavaScript handler has a chance to
101// run in the current RVH.
102// - The pending RVH tells CrossSiteRequestManager (a thread-safe singleton)
103// that it has a pending cross-site request. ResourceDispatcherHost will
104// check for this when the response arrives.
105// - The current RVH runs its onbeforeunload handler. If it returns false, we
106// cancel all the pending logic and go back to NORMAL. Otherwise we allow
107// the pending RVH to send the navigation request to its renderer.
108// - ResourceDispatcherHost receives a ResourceRequest on the IO thread. It
109// checks CrossSiteRequestManager to see that the RVH responsible has a
110// pending cross-site request, and then installs a CrossSiteEventHandler.
111// - When RDH receives a response, the BufferedEventHandler determines whether
112// it is a download. If so, it sends a message to the new renderer causing
113// it to cancel the request, and the download proceeds in the download
114// thread. For now, we stay in a PENDING state (with a pending RVH) until
[email protected]57c6a652009-05-04 07:58:34115// the next DidNavigate event for this TabContents. This isn't ideal, but it
[email protected]420ae012009-04-24 05:16:32116// doesn't affect any functionality.
117// - After RDH receives a response and determines that it is safe and not a
118// download, it pauses the response to first run the old page's onunload
119// handler. It does this by asynchronously calling the OnCrossSiteResponse
[email protected]57c6a652009-05-04 07:58:34120// method of TabContents on the UI thread, which sends a ClosePage message
[email protected]420ae012009-04-24 05:16:32121// to the current RVH.
122// - Once the onunload handler is finished, a ClosePage_ACK message is sent to
123// the ResourceDispatcherHost, who unpauses the response. Data is then sent
124// to the pending RVH.
125// - The pending renderer sends a FrameNavigate message that invokes the
126// DidNavigate method. This replaces the current RVH with the
127// pending RVH and goes back to the NORMAL RendererState.
128
129namespace {
130
131// Amount of time we wait between when a key event is received and the renderer
132// is queried for its state and pushed to the NavigationEntry.
133const int kQueryStateDelay = 5000;
134
135const int kSyncWaitDelay = 40;
136
[email protected]304f8792009-08-19 21:28:09137#if defined(OS_CHROMEOS)
138// If a popup window is bigger than this fraction of the screen on chrome os,
139// turn it into a tab
140const float kMaxWidthFactor = 0.5;
141const float kMaxHeightFactor = 0.6;
142#endif
143
[email protected]420ae012009-04-24 05:16:32144// If another javascript message box is displayed within
145// kJavascriptMessageExpectedDelay of a previous javascript message box being
146// dismissed, display an option to suppress future message boxes from this
147// contents.
148const int kJavascriptMessageExpectedDelay = 1000;
149
[email protected]420ae012009-04-24 05:16:32150// The list of prefs we want to observe.
151const wchar_t* kPrefsToObserve[] = {
152 prefs::kAlternateErrorPagesEnabled,
153 prefs::kWebKitJavaEnabled,
154 prefs::kWebKitJavascriptEnabled,
155 prefs::kWebKitLoadsImagesAutomatically,
156 prefs::kWebKitPluginsEnabled,
157 prefs::kWebKitUsesUniversalDetector,
158 prefs::kWebKitSerifFontFamily,
159 prefs::kWebKitSansSerifFontFamily,
160 prefs::kWebKitFixedFontFamily,
161 prefs::kWebKitDefaultFontSize,
162 prefs::kWebKitDefaultFixedFontSize,
163 prefs::kDefaultCharset
164 // kWebKitStandardFontIsSerif needs to be added
165 // if we let users pick which font to use, serif or sans-serif when
166 // no font is specified or a CSS generic family (serif or sans-serif)
167 // is not specified.
168};
169
170const int kPrefsToObserveLength = arraysize(kPrefsToObserve);
171
[email protected]420ae012009-04-24 05:16:32172// Returns true if the entry's transition type is FORM_SUBMIT.
173bool IsFormSubmit(const NavigationEntry* entry) {
174 return (PageTransition::StripQualifier(entry->transition_type()) ==
175 PageTransition::FORM_SUBMIT);
176}
177
178#if defined(OS_WIN)
179
180BOOL CALLBACK InvalidateWindow(HWND hwnd, LPARAM lparam) {
181 // Note: erase is required to properly paint some widgets borders. This can
182 // be seen with textfields.
183 InvalidateRect(hwnd, NULL, TRUE);
184 return TRUE;
185}
186#endif
187
[email protected]5e369672009-11-03 23:48:30188ViewMsg_Navigate_Params::NavigationType GetNavigationType(
[email protected]1ccb3568d2010-02-19 10:51:16189 Profile* profile, const NavigationEntry& entry,
190 NavigationController::ReloadType reload_type) {
191 switch (reload_type) {
192 case NavigationController::RELOAD:
193 return ViewMsg_Navigate_Params::RELOAD;
194 case NavigationController::RELOAD_IGNORING_CACHE:
195 return ViewMsg_Navigate_Params::RELOAD_IGNORING_CACHE;
196 case NavigationController::NO_RELOAD:
197 break; // Fall through to rest of function.
198 }
[email protected]5e369672009-11-03 23:48:30199
200 if (entry.restore_type() == NavigationEntry::RESTORE_LAST_SESSION &&
201 profile->DidLastSessionExitCleanly())
202 return ViewMsg_Navigate_Params::RESTORE;
203
204 return ViewMsg_Navigate_Params::NORMAL;
205}
206
[email protected]3cc72b12010-03-18 23:03:00207void MakeNavigateParams(const NavigationController& controller,
[email protected]1ccb3568d2010-02-19 10:51:16208 NavigationController::ReloadType reload_type,
209 ViewMsg_Navigate_Params* params) {
[email protected]3cc72b12010-03-18 23:03:00210 const NavigationEntry& entry = *controller.pending_entry();
[email protected]056de2d2009-06-26 16:41:34211 params->page_id = entry.page_id();
[email protected]3cc72b12010-03-18 23:03:00212 params->pending_history_list_offset = controller.pending_entry_index();
213 params->current_history_list_offset = controller.last_committed_entry_index();
214 params->current_history_list_length = controller.entry_count();
[email protected]056de2d2009-06-26 16:41:34215 params->url = entry.url();
216 params->referrer = entry.referrer();
217 params->transition = entry.transition_type();
218 params->state = entry.content_state();
[email protected]3cc72b12010-03-18 23:03:00219 params->navigation_type =
220 GetNavigationType(controller.profile(), entry, reload_type);
[email protected]056de2d2009-06-26 16:41:34221 params->request_time = base::Time::Now();
222}
223
[email protected]420ae012009-04-24 05:16:32224} // namespace
225
226// -----------------------------------------------------------------------------
227
[email protected]fdd61c62009-04-22 19:22:57228// static
229int TabContents::find_request_id_counter_ = -1;
230
[email protected]420ae012009-04-24 05:16:32231
[email protected]420ae012009-04-24 05:16:32232TabContents::TabContents(Profile* profile,
233 SiteInstance* site_instance,
234 int routing_id,
[email protected]34ac70502009-09-25 17:07:23235 const TabContents* base_tab_contents)
[email protected]b680ad22009-04-15 23:19:42236 : delegate_(NULL),
[email protected]66ba4932009-06-04 19:22:13237 ALLOW_THIS_IN_INITIALIZER_LIST(controller_(this, profile)),
238 ALLOW_THIS_IN_INITIALIZER_LIST(view_(
[email protected]d82ed61e2009-06-16 02:46:22239 TabContentsView::Create(this))),
240 ALLOW_THIS_IN_INITIALIZER_LIST(render_manager_(this, this)),
[email protected]fdd61c62009-04-22 19:22:57241 property_bag_(),
242 registrar_(),
[email protected]d82ed61e2009-06-16 02:46:22243 ALLOW_THIS_IN_INITIALIZER_LIST(printing_(*this)),
[email protected]fdd61c62009-04-22 19:22:57244 save_package_(),
[email protected]6db0def92010-03-02 19:55:45245 autocomplete_history_manager_(),
[email protected]21674df2009-10-28 19:51:21246 autofill_manager_(),
[email protected]fdd61c62009-04-22 19:22:57247 password_manager_(),
248 plugin_installer_(),
[email protected]6f821dd2010-02-23 00:54:06249 bookmark_drag_(NULL),
[email protected]d82ed61e2009-06-16 02:46:22250 ALLOW_THIS_IN_INITIALIZER_LIST(fav_icon_helper_(this)),
[email protected]fdd61c62009-04-22 19:22:57251 select_file_dialog_(),
[email protected]d5f942ba2008-09-26 19:30:34252 is_loading_(false),
initial.commit09911bf2008-07-26 23:55:29253 is_crashed_(false),
[email protected]d5f942ba2008-09-26 19:30:34254 waiting_for_response_(false),
[email protected]d5f942ba2008-09-26 19:30:34255 max_page_id_(-1),
[email protected]fdd61c62009-04-22 19:22:57256 current_load_start_(),
257 load_state_(net::LOAD_STATE_IDLE),
258 load_state_host_(),
[email protected]094e5b22009-09-25 04:23:56259 upload_size_(0),
260 upload_position_(0),
[email protected]fdd61c62009-04-22 19:22:57261 received_page_title_(false),
262 is_starred_(false),
263 contents_mime_type_(),
264 encoding_(),
[email protected]332af7732009-03-11 13:21:35265 blocked_popups_(NULL),
[email protected]8cc8d492010-02-02 10:40:49266 dont_notify_render_view_(false),
[email protected]fdd61c62009-04-22 19:22:57267 infobar_delegates_(),
[email protected]fdd61c62009-04-22 19:22:57268 find_ui_active_(false),
269 find_op_aborted_(false),
270 current_find_request_id_(find_request_id_counter_++),
[email protected]e491f1c2009-05-22 20:28:12271 last_search_case_sensitive_(false),
272 last_search_prepopulate_text_(NULL),
273 last_search_result_(),
[email protected]fdd61c62009-04-22 19:22:57274 capturing_contents_(false),
275 is_being_destroyed_(false),
276 notify_disconnection_(false),
277 history_requests_(),
278#if defined(OS_WIN)
279 message_box_active_(CreateEvent(NULL, TRUE, FALSE, NULL)),
280#endif
281 last_javascript_message_dismissal_(),
[email protected]c2e74fe82009-09-03 17:57:44282 suppress_javascript_messages_(false),
[email protected]7ab1e7d62009-10-14 23:32:01283 is_showing_before_unload_dialog_(false),
[email protected]7ff431e2010-01-07 18:18:54284 renderer_preferences_(),
[email protected]1a242c32010-02-02 21:20:54285 opener_dom_ui_type_(DOMUIFactory::kNoDOMUI),
[email protected]3b1c1812010-02-12 21:53:55286 app_extension_(NULL),
287 language_state_(&controller_) {
[email protected]0de80162010-02-03 04:52:35288 ClearBlockedContentSettings();
[email protected]7ff431e2010-01-07 18:18:54289 renderer_preferences_util::UpdateFromSystemSettings(
290 &renderer_preferences_, profile);
291
[email protected]16779842009-07-08 23:45:29292#if defined(OS_CHROMEOS)
[email protected]58dca552009-06-17 00:35:02293 // Make sure the thumbnailer is started before starting the render manager.
294 // The thumbnailer will want to listen for RVH creations, one of which will
295 // happen in RVHManager::Init.
296 ThumbnailGenerator* generator = g_browser_process->GetThumbnailGenerator();
297 if (generator)
298 generator->StartThumbnailing();
299#endif
300
[email protected]12636df2009-09-28 22:32:21301 render_manager_.Init(profile, site_instance, routing_id);
[email protected]420ae012009-04-24 05:16:32302
[email protected]34ac70502009-09-25 17:07:23303 // We have the initial size of the view be based on the size of the passed in
304 // tab contents (normally a tab from the same window).
305 view_->CreateView(base_tab_contents ?
306 base_tab_contents->view()->GetContainerSize() : gfx::Size());
[email protected]420ae012009-04-24 05:16:32307
308 // Register for notifications about all interested prefs change.
309 PrefService* prefs = profile->GetPrefs();
310 if (prefs) {
311 for (int i = 0; i < kPrefsToObserveLength; ++i)
312 prefs->AddPrefObserver(kPrefsToObserve[i], this);
313 }
314
315 // Register for notifications about URL starredness changing on any profile.
316 registrar_.Add(this, NotificationType::URLS_STARRED,
317 NotificationService::AllSources());
318 registrar_.Add(this, NotificationType::BOOKMARK_MODEL_LOADED,
319 NotificationService::AllSources());
320 registrar_.Add(this, NotificationType::RENDER_WIDGET_HOST_DESTROYED,
321 NotificationService::AllSources());
[email protected]d3ba77272009-09-03 00:06:09322#if defined(OS_LINUX)
323 registrar_.Add(this, NotificationType::BROWSER_THEME_CHANGED,
324 NotificationService::AllSources());
325#endif
[email protected]420ae012009-04-24 05:16:32326
[email protected]6c32ce72010-03-08 05:18:06327 registrar_.Add(this, NotificationType::USER_STYLE_SHEET_UPDATED,
328 NotificationService::AllSources());
329
[email protected]79580c62010-02-02 02:36:25330 // Register for notifications about content setting changes.
331 registrar_.Add(this, NotificationType::CONTENT_SETTINGS_CHANGED,
332 NotificationService::AllSources());
333
[email protected]420ae012009-04-24 05:16:32334 // Keep a global copy of the previous search string (if any).
335 static string16 global_last_search = string16();
[email protected]e491f1c2009-05-22 20:28:12336 last_search_prepopulate_text_ = &global_last_search;
[email protected]840b1512009-07-21 16:53:46337
[email protected]c8865482009-07-23 20:40:10338 // Set-up the showing of the omnibox search infobar if applicable.
339 if (OmniboxSearchHint::IsEnabled(profile))
340 omnibox_search_hint_.reset(new OmniboxSearchHint(this));
[email protected]332af7732009-03-11 13:21:35341}
initial.commit09911bf2008-07-26 23:55:29342
343TabContents::~TabContents() {
[email protected]420ae012009-04-24 05:16:32344 is_being_destroyed_ = true;
345
[email protected]bfe4c1582009-10-02 18:11:09346 // We don't want any notifications while we're running our destructor.
[email protected]420ae012009-04-24 05:16:32347 registrar_.RemoveAll();
348
349 // Unregister the notifications of all observed prefs change.
350 PrefService* prefs = profile()->GetPrefs();
351 if (prefs) {
352 for (int i = 0; i < kPrefsToObserveLength; ++i)
353 prefs->RemovePrefObserver(kPrefsToObserve[i], this);
354 }
355
[email protected]420ae012009-04-24 05:16:32356 NotifyDisconnected();
[email protected]e7b8d832009-11-20 22:41:00357 hung_renderer_dialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:32358
[email protected]420ae012009-04-24 05:16:32359 // First cleanly close all child windows.
360 // TODO(mpcomplete): handle case if MaybeCloseChildWindows() already asked
361 // some of these to close. CloseWindows is async, so it might get called
362 // twice before it runs.
[email protected]8cc8d492010-02-02 10:40:49363 CloseConstrainedWindows();
[email protected]420ae012009-04-24 05:16:32364
[email protected]8cc8d492010-02-02 10:40:49365 // Close all blocked popups.
[email protected]66ba4932009-06-04 19:22:13366 if (blocked_popups_)
367 blocked_popups_->Destroy();
368
[email protected]420ae012009-04-24 05:16:32369 // Notify any observer that have a reference on this tab contents.
370 NotificationService::current()->Notify(
371 NotificationType::TAB_CONTENTS_DESTROYED,
372 Source<TabContents>(this),
373 NotificationService::NoDetails());
374
[email protected]1e5e74f2009-05-27 20:55:12375 // Notify any lasting InfobarDelegates that have not yet been removed that
376 // whatever infobar they were handling in this TabContents has closed,
377 // because the TabContents is going away entirely.
378 // This must happen after the TAB_CONTENTS_DESTROYED notification as the
379 // notification may trigger infobars calls that access their delegate. (and
380 // some implementations of InfoBarDelegate do delete themselves on
381 // InfoBarClosed()).
382 for (int i = 0; i < infobar_delegate_count(); ++i) {
383 InfoBarDelegate* delegate = GetInfoBarDelegateAt(i);
384 delegate->InfoBarClosed();
385 }
386 infobar_delegates_.clear();
387
[email protected]420ae012009-04-24 05:16:32388 // TODO(brettw) this should be moved to the view.
389#if defined(OS_WIN)
390 // If we still have a window handle, destroy it. GetNativeView can return
391 // NULL if this contents was part of a window that closed.
392 if (GetNativeView())
393 ::DestroyWindow(GetNativeView());
394#endif
[email protected]7ab1e7d62009-10-14 23:32:01395
396 // OnCloseStarted isn't called in unit tests.
397 if (!tab_close_start_time_.is_null()) {
398 UMA_HISTOGRAM_TIMES("Tab.Close",
399 base::TimeTicks::Now() - tab_close_start_time_);
400 }
initial.commit09911bf2008-07-26 23:55:29401}
402
[email protected]d5f942ba2008-09-26 19:30:34403// static
404void TabContents::RegisterUserPrefs(PrefService* prefs) {
[email protected]420ae012009-04-24 05:16:32405 prefs->RegisterBooleanPref(prefs::kAlternateErrorPagesEnabled, true);
406
407 WebPreferences pref_defaults;
408 prefs->RegisterBooleanPref(prefs::kWebKitJavascriptEnabled,
409 pref_defaults.javascript_enabled);
410 prefs->RegisterBooleanPref(prefs::kWebKitWebSecurityEnabled,
411 pref_defaults.web_security_enabled);
412 prefs->RegisterBooleanPref(
413 prefs::kWebKitJavascriptCanOpenWindowsAutomatically, true);
414 prefs->RegisterBooleanPref(prefs::kWebKitLoadsImagesAutomatically,
415 pref_defaults.loads_images_automatically);
416 prefs->RegisterBooleanPref(prefs::kWebKitPluginsEnabled,
417 pref_defaults.plugins_enabled);
418 prefs->RegisterBooleanPref(prefs::kWebKitDomPasteEnabled,
419 pref_defaults.dom_paste_enabled);
420 prefs->RegisterBooleanPref(prefs::kWebKitShrinksStandaloneImagesToFit,
421 pref_defaults.shrinks_standalone_images_to_fit);
[email protected]0df30122009-06-03 12:13:08422 prefs->RegisterStringPref(prefs::kWebKitInspectorSettings,
[email protected]8922e1f2009-10-03 05:01:26423 UTF8ToWide(pref_defaults.inspector_settings));
[email protected]420ae012009-04-24 05:16:32424 prefs->RegisterBooleanPref(prefs::kWebKitTextAreasAreResizable,
425 pref_defaults.text_areas_are_resizable);
426 prefs->RegisterBooleanPref(prefs::kWebKitJavaEnabled,
427 pref_defaults.java_enabled);
428
429 prefs->RegisterLocalizedStringPref(prefs::kAcceptLanguages,
430 IDS_ACCEPT_LANGUAGES);
431 prefs->RegisterLocalizedStringPref(prefs::kDefaultCharset,
432 IDS_DEFAULT_ENCODING);
433 prefs->RegisterLocalizedBooleanPref(prefs::kWebKitStandardFontIsSerif,
434 IDS_STANDARD_FONT_IS_SERIF);
435 prefs->RegisterLocalizedStringPref(prefs::kWebKitFixedFontFamily,
436 IDS_FIXED_FONT_FAMILY);
437 prefs->RegisterLocalizedStringPref(prefs::kWebKitSerifFontFamily,
438 IDS_SERIF_FONT_FAMILY);
439 prefs->RegisterLocalizedStringPref(prefs::kWebKitSansSerifFontFamily,
440 IDS_SANS_SERIF_FONT_FAMILY);
441 prefs->RegisterLocalizedStringPref(prefs::kWebKitCursiveFontFamily,
442 IDS_CURSIVE_FONT_FAMILY);
443 prefs->RegisterLocalizedStringPref(prefs::kWebKitFantasyFontFamily,
444 IDS_FANTASY_FONT_FAMILY);
445 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitDefaultFontSize,
446 IDS_DEFAULT_FONT_SIZE);
447 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitDefaultFixedFontSize,
448 IDS_DEFAULT_FIXED_FONT_SIZE);
449 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitMinimumFontSize,
450 IDS_MINIMUM_FONT_SIZE);
451 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitMinimumLogicalFontSize,
452 IDS_MINIMUM_LOGICAL_FONT_SIZE);
453 prefs->RegisterLocalizedBooleanPref(prefs::kWebKitUsesUniversalDetector,
454 IDS_USES_UNIVERSAL_DETECTOR);
455 prefs->RegisterLocalizedStringPref(prefs::kStaticEncodings,
456 IDS_STATIC_ENCODING_LIST);
initial.commit09911bf2008-07-26 23:55:29457}
458
[email protected]13367f72009-07-08 16:39:02459// Returns true if contains content rendered by an extension.
460bool TabContents::HostsExtension() const {
461 return GetURL().SchemeIs(chrome::kExtensionScheme);
[email protected]d5f942ba2008-09-26 19:30:34462}
463
[email protected]6e95706d2009-04-23 22:55:01464PasswordManager* TabContents::GetPasswordManager() {
465 if (password_manager_.get() == NULL)
466 password_manager_.reset(new PasswordManager(this));
467 return password_manager_.get();
468}
469
470PluginInstaller* TabContents::GetPluginInstaller() {
471 if (plugin_installer_.get() == NULL)
472 plugin_installer_.reset(new PluginInstaller(this));
473 return plugin_installer_.get();
474}
475
[email protected]8cb5d5b2010-02-09 11:36:16476RenderProcessHost* TabContents::GetRenderProcessHost() const {
477 return render_manager_.current_host()->process();
478}
479
[email protected]3b355642010-02-05 16:01:49480void TabContents::SetAppExtension(Extension* extension) {
[email protected]867a73e12010-03-19 20:45:46481 DCHECK(!extension || extension->GetFullLaunchURL().is_valid());
[email protected]3b355642010-02-05 16:01:49482 app_extension_ = extension;
[email protected]fca656c2010-02-10 20:30:10483
484 NotificationService::current()->Notify(
485 NotificationType::TAB_CONTENTS_APPLICATION_EXTENSION_CHANGED,
486 Source<TabContents>(this),
487 NotificationService::NoDetails());
[email protected]3b355642010-02-05 16:01:49488}
489
[email protected]911f8152010-03-18 16:46:40490void TabContents::SetAppExtensionById(const std::string& app_extension_id) {
491 if (app_extension_id.empty())
492 return;
493
494 ExtensionsService* extension_service = profile()->GetExtensionsService();
495 if (extension_service && extension_service->is_ready()) {
496 Extension* extension =
497 extension_service->GetExtensionById(app_extension_id, false);
498 if (extension)
499 SetAppExtension(extension);
500 }
501}
502
[email protected]d5f942ba2008-09-26 19:30:34503const GURL& TabContents::GetURL() const {
504 // We may not have a navigation entry yet
[email protected]ce3fa3c2009-04-20 19:55:57505 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]ebe89e062009-08-13 23:16:54506 return entry ? entry->virtual_url() : GURL::EmptyGURL();
[email protected]d5f942ba2008-09-26 19:30:34507}
508
[email protected]96d185d2009-04-24 03:28:54509const string16& TabContents::GetTitle() const {
[email protected]4c6092c5b2009-06-06 00:23:55510 // Transient entries take precedence. They are used for interstitial pages
511 // that are shown on top of existing pages.
512 NavigationEntry* entry = controller_.GetTransientEntry();
513 if (entry)
514 return entry->GetTitleForDisplay(&controller_);
515
[email protected]96d185d2009-04-24 03:28:54516 DOMUI* our_dom_ui = render_manager_.pending_dom_ui() ?
517 render_manager_.pending_dom_ui() : render_manager_.dom_ui();
518 if (our_dom_ui) {
519 // Don't override the title in view source mode.
[email protected]4c6092c5b2009-06-06 00:23:55520 entry = controller_.GetActiveEntry();
[email protected]96d185d2009-04-24 03:28:54521 if (!(entry && entry->IsViewSourceMode())) {
522 // Give the DOM UI the chance to override our title.
523 const string16& title = our_dom_ui->overridden_title();
524 if (!title.empty())
525 return title;
526 }
527 }
528
529 // We use the title for the last committed entry rather than a pending
530 // navigation entry. For example, when the user types in a URL, we want to
531 // keep the old page's title until the new load has committed and we get a new
532 // title.
[email protected]96d185d2009-04-24 03:28:54533 entry = controller_.GetLastCommittedEntry();
534 if (entry)
535 return entry->GetTitleForDisplay(&controller_);
[email protected]96d185d2009-04-24 03:28:54536 return EmptyString16();
537}
538
[email protected]bbbc1ef2010-02-12 18:03:41539// static
540string16 TabContents::GetDefaultTitle() {
541 return l10n_util::GetStringUTF16(IDS_DEFAULT_TAB_TITLE);
542}
543
[email protected]d5f942ba2008-09-26 19:30:34544int32 TabContents::GetMaxPageID() {
545 if (GetSiteInstance())
546 return GetSiteInstance()->max_page_id();
547 else
548 return max_page_id_;
549}
550
551void TabContents::UpdateMaxPageID(int32 page_id) {
552 // Ensure both the SiteInstance and RenderProcessHost update their max page
[email protected]57c6a652009-05-04 07:58:34553 // IDs in sync. Only TabContents will also have site instances, except during
[email protected]d5f942ba2008-09-26 19:30:34554 // testing.
555 if (GetSiteInstance())
556 GetSiteInstance()->UpdateMaxPageID(page_id);
[email protected]8cb5d5b2010-02-09 11:36:16557 GetRenderProcessHost()->UpdateMaxPageID(page_id);
[email protected]d5f942ba2008-09-26 19:30:34558}
559
[email protected]96d185d2009-04-24 03:28:54560SiteInstance* TabContents::GetSiteInstance() const {
561 return render_manager_.current_host()->site_instance();
562}
563
[email protected]96d185d2009-04-24 03:28:54564bool TabContents::ShouldDisplayURL() {
[email protected]8ab24cc2009-11-13 20:56:15565 // Don't hide the url in view source mode and with interstitials.
[email protected]96d185d2009-04-24 03:28:54566 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]8ab24cc2009-11-13 20:56:15567 if (entry && (entry->IsViewSourceMode() ||
568 entry->page_type() == NavigationEntry::INTERSTITIAL_PAGE)) {
[email protected]96d185d2009-04-24 03:28:54569 return true;
[email protected]8ab24cc2009-11-13 20:56:15570 }
[email protected]83e3895b2009-06-11 00:07:16571
[email protected]96d185d2009-04-24 03:28:54572 DOMUI* dom_ui = GetDOMUIForCurrentState();
573 if (dom_ui)
574 return !dom_ui->should_hide_url();
575 return true;
576}
577
[email protected]d5f942ba2008-09-26 19:30:34578SkBitmap TabContents::GetFavIcon() const {
579 // Like GetTitle(), we also want to use the favicon for the last committed
580 // entry rather than a pending navigation entry.
[email protected]ce3fa3c2009-04-20 19:55:57581 NavigationEntry* entry = controller_.GetTransientEntry();
[email protected]cbab76d2008-10-13 22:42:47582 if (entry)
583 return entry->favicon().bitmap();
584
[email protected]ce3fa3c2009-04-20 19:55:57585 entry = controller_.GetLastCommittedEntry();
[email protected]d5f942ba2008-09-26 19:30:34586 if (entry)
587 return entry->favicon().bitmap();
[email protected]d5f942ba2008-09-26 19:30:34588 return SkBitmap();
589}
590
[email protected]0b303cc2009-09-28 22:35:15591bool TabContents::FavIconIsValid() const {
592 NavigationEntry* entry = controller_.GetTransientEntry();
593 if (entry)
594 return entry->favicon().is_valid();
595
596 entry = controller_.GetLastCommittedEntry();
597 if (entry)
598 return entry->favicon().is_valid();
599
600 return false;
601}
602
[email protected]96d185d2009-04-24 03:28:54603bool TabContents::ShouldDisplayFavIcon() {
604 // Always display a throbber during pending loads.
605 if (controller_.GetLastCommittedEntry() && controller_.pending_entry())
606 return true;
607
608 DOMUI* dom_ui = GetDOMUIForCurrentState();
609 if (dom_ui)
610 return !dom_ui->hide_favicon();
611 return true;
612}
613
[email protected]2d5a3592010-01-30 04:29:52614bool TabContents::IsContentBlocked(ContentSettingsType content_type) const {
[email protected]8cc8d492010-02-02 10:40:49615 if (content_type == CONTENT_SETTINGS_TYPE_POPUPS)
616 return blocked_popups_ != NULL;
617
[email protected]0de80162010-02-03 04:52:35618 if (content_type == CONTENT_SETTINGS_TYPE_IMAGES ||
619 content_type == CONTENT_SETTINGS_TYPE_JAVASCRIPT ||
[email protected]3dbb80b2010-02-09 22:41:20620 content_type == CONTENT_SETTINGS_TYPE_PLUGINS ||
621 content_type == CONTENT_SETTINGS_TYPE_COOKIES)
[email protected]0de80162010-02-03 04:52:35622 return content_blocked_[content_type];
623
[email protected]3dbb80b2010-02-09 22:41:20624 NOTREACHED();
[email protected]2d5a3592010-01-30 04:29:52625 return false;
626}
627
[email protected]96d185d2009-04-24 03:28:54628std::wstring TabContents::GetStatusText() const {
629 if (!is_loading() || load_state_ == net::LOAD_STATE_IDLE)
630 return std::wstring();
631
632 switch (load_state_) {
633 case net::LOAD_STATE_WAITING_FOR_CACHE:
634 return l10n_util::GetString(IDS_LOAD_STATE_WAITING_FOR_CACHE);
635 case net::LOAD_STATE_RESOLVING_PROXY_FOR_URL:
636 return l10n_util::GetString(IDS_LOAD_STATE_RESOLVING_PROXY_FOR_URL);
637 case net::LOAD_STATE_RESOLVING_HOST:
638 return l10n_util::GetString(IDS_LOAD_STATE_RESOLVING_HOST);
639 case net::LOAD_STATE_CONNECTING:
640 return l10n_util::GetString(IDS_LOAD_STATE_CONNECTING);
641 case net::LOAD_STATE_SENDING_REQUEST:
[email protected]094e5b22009-09-25 04:23:56642 if (upload_size_)
643 return l10n_util::GetStringF(
644 IDS_LOAD_STATE_SENDING_REQUEST_WITH_PROGRESS,
645 static_cast<int>((100 * upload_position_) / upload_size_));
646 else
647 return l10n_util::GetString(IDS_LOAD_STATE_SENDING_REQUEST);
[email protected]96d185d2009-04-24 03:28:54648 case net::LOAD_STATE_WAITING_FOR_RESPONSE:
649 return l10n_util::GetStringF(IDS_LOAD_STATE_WAITING_FOR_RESPONSE,
650 load_state_host_);
651 // Ignore net::LOAD_STATE_READING_RESPONSE and net::LOAD_STATE_IDLE
652 case net::LOAD_STATE_IDLE:
653 case net::LOAD_STATE_READING_RESPONSE:
654 break;
655 }
656
657 return std::wstring();
658}
659
[email protected]d5f942ba2008-09-26 19:30:34660void TabContents::SetIsCrashed(bool state) {
661 if (state == is_crashed_)
662 return;
663
664 is_crashed_ = state;
[email protected]c9cd2222009-05-06 05:16:50665 NotifyNavigationStateChanged(INVALIDATE_TAB);
[email protected]d5f942ba2008-09-26 19:30:34666}
667
[email protected]744ef172009-10-16 21:53:46668void TabContents::PageActionStateChanged() {
669 NotifyNavigationStateChanged(TabContents::INVALIDATE_PAGE_ACTIONS);
[email protected]d7eaf572009-07-01 21:57:00670}
[email protected]ce5c4502009-05-06 16:46:11671
[email protected]d5f942ba2008-09-26 19:30:34672void TabContents::NotifyNavigationStateChanged(unsigned changed_flags) {
673 if (delegate_)
674 delegate_->NavigationStateChanged(this, changed_flags);
675}
676
[email protected]96d185d2009-04-24 03:28:54677void TabContents::DidBecomeSelected() {
678 controller_.SetActive(true);
[email protected]8cb5d5b2010-02-09 11:36:16679 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
680 if (rwhv) {
681 rwhv->DidBecomeSelected();
[email protected]789e9152009-08-04 21:59:43682#if defined(OS_MACOSX)
[email protected]8cb5d5b2010-02-09 11:36:16683 rwhv->SetActive(true);
[email protected]789e9152009-08-04 21:59:43684#endif
685 }
[email protected]96d185d2009-04-24 03:28:54686
[email protected]8cb5d5b2010-02-09 11:36:16687 WebCacheManager::GetInstance()->ObserveActivity(GetRenderProcessHost()->id());
[email protected]96d185d2009-04-24 03:28:54688}
689
690void TabContents::WasHidden() {
691 if (!capturing_contents()) {
692 // |render_view_host()| can be NULL if the user middle clicks a link to open
693 // a tab in then background, then closes the tab before selecting it. This
694 // is because closing the tab calls TabContents::Destroy(), which removes
695 // the |render_view_host()|; then when we actually destroy the window,
696 // OnWindowPosChanged() notices and calls HideContents() (which calls us).
[email protected]8cb5d5b2010-02-09 11:36:16697 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
698 if (rwhv)
699 rwhv->WasHidden();
[email protected]96d185d2009-04-24 03:28:54700 }
701
702 NotificationService::current()->Notify(
703 NotificationType::TAB_CONTENTS_HIDDEN,
704 Source<TabContents>(this),
705 NotificationService::NoDetails());
706}
707
[email protected]d5f942ba2008-09-26 19:30:34708void TabContents::Activate() {
709 if (delegate_)
710 delegate_->ActivateContents(this);
711}
712
[email protected]96d185d2009-04-24 03:28:54713void TabContents::ShowContents() {
[email protected]8cb5d5b2010-02-09 11:36:16714 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
715 if (rwhv)
716 rwhv->DidBecomeSelected();
[email protected]96d185d2009-04-24 03:28:54717}
718
719void TabContents::HideContents() {
720 // TODO(pkasting): http://b/1239839 Right now we purposefully don't call
721 // our superclass HideContents(), because some callers want to be very picky
722 // about the order in which these get called. In addition to making the code
723 // here practically impossible to understand, this also means we end up
724 // calling TabContents::WasHidden() twice if callers call both versions of
[email protected]57c6a652009-05-04 07:58:34725 // HideContents() on a TabContents.
[email protected]96d185d2009-04-24 03:28:54726 WasHidden();
727}
728
[email protected]c0588052008-10-27 23:01:50729void TabContents::OpenURL(const GURL& url, const GURL& referrer,
[email protected]d5f942ba2008-09-26 19:30:34730 WindowOpenDisposition disposition,
731 PageTransition::Type transition) {
732 if (delegate_)
[email protected]c0588052008-10-27 23:01:50733 delegate_->OpenURLFromTab(this, url, referrer, disposition, transition);
[email protected]d5f942ba2008-09-26 19:30:34734}
735
[email protected]1ccb3568d2010-02-19 10:51:16736bool TabContents::NavigateToPendingEntry(
737 NavigationController::ReloadType reload_type) {
[email protected]96d185d2009-04-24 03:28:54738 const NavigationEntry& entry = *controller_.pending_entry();
739
740 RenderViewHost* dest_render_view_host = render_manager_.Navigate(entry);
741 if (!dest_render_view_host)
742 return false; // Unable to create the desired render view host.
743
744 // Tell DevTools agent that it is attached prior to the navigation.
[email protected]d9f9b792009-06-24 13:17:12745 DevToolsManager* devtools_manager = DevToolsManager::GetInstance();
[email protected]7e8e6b62009-05-08 11:28:32746 if (devtools_manager) { // NULL in unit tests.
[email protected]3cc72b12010-03-18 23:03:00747 devtools_manager->OnNavigatingToPendingEntry(render_view_host(),
748 dest_render_view_host,
749 entry.url());
[email protected]7e8e6b62009-05-08 11:28:32750 }
[email protected]96d185d2009-04-24 03:28:54751
752 // Used for page load time metrics.
753 current_load_start_ = base::TimeTicks::Now();
754
755 // Navigate in the desired RenderViewHost.
[email protected]056de2d2009-06-26 16:41:34756 ViewMsg_Navigate_Params navigate_params;
[email protected]3cc72b12010-03-18 23:03:00757 MakeNavigateParams(controller_, reload_type, &navigate_params);
[email protected]056de2d2009-06-26 16:41:34758 dest_render_view_host->Navigate(navigate_params);
[email protected]96d185d2009-04-24 03:28:54759
760 if (entry.page_id() == -1) {
761 // HACK!! This code suppresses javascript: URLs from being added to
762 // session history, which is what we want to do for javascript: URLs that
763 // do not generate content. What we really need is a message from the
764 // renderer telling us that a new page was not created. The same message
765 // could be used for mailto: URLs and the like.
766 if (entry.url().SchemeIs(chrome::kJavaScriptScheme))
767 return false;
768 }
769
770 // Clear any provisional password saves - this stops password infobars
771 // showing up on pages the user navigates to while the right page is
772 // loading.
773 GetPasswordManager()->ClearProvisionalSave();
774
[email protected]1ccb3568d2010-02-19 10:51:16775 if (reload_type != NavigationController::NO_RELOAD &&
776 !profile()->IsOffTheRecord()) {
[email protected]0189bc722009-08-28 21:56:48777 FaviconService* favicon_service =
778 profile()->GetFaviconService(Profile::IMPLICIT_ACCESS);
779 if (favicon_service)
780 favicon_service->SetFaviconOutOfDateForPage(entry.url());
[email protected]96d185d2009-04-24 03:28:54781 }
782
[email protected]d5f942ba2008-09-26 19:30:34783 return true;
784}
785
[email protected]96d185d2009-04-24 03:28:54786void TabContents::Stop() {
787 render_manager_.Stop();
788 printing_.Stop();
789}
790
[email protected]96d185d2009-04-24 03:28:54791void TabContents::DisassociateFromPopupCount() {
792 render_view_host()->DisassociateFromPopupCount();
793}
794
795TabContents* TabContents::Clone() {
796 // We create a new SiteInstance so that the new tab won't share processes
797 // with the old one. This can be changed in the future if we need it to share
798 // processes for some reason.
[email protected]420ae012009-04-24 05:16:32799 TabContents* tc = new TabContents(profile(),
[email protected]96d185d2009-04-24 03:28:54800 SiteInstance::CreateSiteInstance(profile()),
[email protected]12636df2009-09-28 22:32:21801 MSG_ROUTING_NONE, this);
[email protected]96d185d2009-04-24 03:28:54802 tc->controller().CopyStateFrom(controller_);
803 return tc;
804}
805
[email protected]4d677202009-07-19 07:37:12806void TabContents::ShowPageInfo(const GURL& url,
807 const NavigationEntry::SSLStatus& ssl,
808 bool show_history) {
809 if (!delegate_)
810 return;
811
[email protected]bb678332009-07-21 00:15:50812 delegate_->ShowPageInfo(profile(), url, ssl, show_history);
[email protected]4d677202009-07-19 07:37:12813}
814
[email protected]7e0831b2010-02-01 08:45:15815void TabContents::TranslatePage(const std::string& source_lang,
816 const std::string& target_lang) {
817 NavigationEntry* entry = controller_.GetActiveEntry();
818 if (!entry) {
819 NOTREACHED();
820 return;
821 }
[email protected]3b1c1812010-02-12 21:53:55822 language_state_.set_translation_pending(true);
[email protected]7e0831b2010-02-01 08:45:15823 render_view_host()->TranslatePage(entry->page_id(), source_lang, target_lang);
824}
825
[email protected]d7f380f2010-03-15 21:48:50826void TabContents::RevertTranslatedPage() {
827 // TODO(jcampan): revert translated page to original and remove translate
828 // infobar.
829}
830
initial.commit09911bf2008-07-26 23:55:29831ConstrainedWindow* TabContents::CreateConstrainedDialog(
[email protected]e8382172009-06-19 22:16:28832 ConstrainedWindowDelegate* delegate) {
initial.commit09911bf2008-07-26 23:55:29833 ConstrainedWindow* window =
[email protected]e8382172009-06-19 22:16:28834 ConstrainedWindow::CreateConstrainedDialog(this, delegate);
initial.commit09911bf2008-07-26 23:55:29835 child_windows_.push_back(window);
[email protected]fa1cf0b82010-01-15 21:49:44836
837 if (child_windows_.size() == 1) {
838 window->ShowConstrainedWindow();
839 BlockTabContent(true);
840 }
841
initial.commit09911bf2008-07-26 23:55:29842 return window;
843}
844
[email protected]fa1cf0b82010-01-15 21:49:44845void TabContents::BlockTabContent(bool blocked) {
[email protected]8dccd7cb2010-02-25 05:19:55846 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
847 if (rwhv)
848 rwhv->SetVisuallyDeemphasized(blocked);
[email protected]fa1cf0b82010-01-15 21:49:44849 render_view_host()->set_ignore_input_events(blocked);
850 if (delegate_)
851 delegate_->SetTabContentBlocked(this, blocked);
852}
853
initial.commit09911bf2008-07-26 23:55:29854void TabContents::AddNewContents(TabContents* new_contents,
855 WindowOpenDisposition disposition,
856 const gfx::Rect& initial_pos,
[email protected]7e7414ae2010-01-26 20:19:29857 bool user_gesture) {
initial.commit09911bf2008-07-26 23:55:29858 if (!delegate_)
859 return;
860
[email protected]35f7d212009-04-29 21:19:27861 if ((disposition == NEW_POPUP) && !user_gesture &&
862 !CommandLine::ForCurrentProcess()->HasSwitch(
863 switches::kDisablePopupBlocking)) {
[email protected]2c4410d2009-05-06 23:46:22864 // Unrequested popups from normal pages are constrained unless they're in
[email protected]b6c874582009-05-08 19:38:31865 // the whitelist. The popup owner will handle checking this.
[email protected]7e7414ae2010-01-26 20:19:29866 delegate_->GetConstrainingContents(this)->AddPopup(
867 new_contents, initial_pos);
[email protected]b6c874582009-05-08 19:38:31868 } else {
[email protected]304f8792009-08-19 21:28:09869#if defined(OS_CHROMEOS)
870 if (disposition == NEW_POPUP) {
871 // If the popup is bigger than a given factor of the screen, then
872 // turn it into a foreground tab (on chrome os only)
873 GdkScreen* screen = gdk_screen_get_default();
874 int max_width = gdk_screen_get_width(screen) * kMaxWidthFactor;
875 int max_height = gdk_screen_get_height(screen) * kMaxHeightFactor;
876 if (initial_pos.width() > max_width ||
877 initial_pos.height() > max_height) {
878 disposition = NEW_FOREGROUND_TAB;
879 }
880 }
881#endif
882
[email protected]0aa55312008-10-17 21:53:08883 new_contents->DisassociateFromPopupCount();
initial.commit09911bf2008-07-26 23:55:29884 delegate_->AddNewContents(this, new_contents, disposition, initial_pos,
885 user_gesture);
[email protected]7f37dd92009-09-15 16:35:49886 NotificationService::current()->Notify(
887 NotificationType::TAB_ADDED,
888 Source<TabContentsDelegate>(delegate_),
889 Details<TabContents>(this));
initial.commit09911bf2008-07-26 23:55:29890 }
initial.commit09911bf2008-07-26 23:55:29891
[email protected]8cc8d492010-02-02 10:40:49892 // TODO(pkasting): Why is this necessary?
893 PopupNotificationVisibilityChanged(blocked_popups_ != NULL);
initial.commit09911bf2008-07-26 23:55:29894}
895
[email protected]bcd9dc42010-01-23 16:38:18896bool TabContents::ExecuteCode(int request_id, const std::string& extension_id,
[email protected]61f5a7b2009-12-22 22:21:20897 const std::vector<URLPattern>& host_permissions,
[email protected]20ad2692009-11-20 18:27:20898 bool is_js_code, const std::string& code_string,
899 bool all_frames) {
[email protected]ebd0eae72010-02-16 23:50:51900 RenderViewHost* host = render_view_host();
901 if (!host)
902 return false;
903
904 return host->Send(new ViewMsg_ExecuteCode(host->routing_id(),
905 ViewMsg_ExecuteCode_Params(request_id, extension_id, host_permissions,
906 is_js_code, code_string, all_frames)));
[email protected]912256b32009-09-18 09:47:35907}
908
[email protected]96d185d2009-04-24 03:28:54909void TabContents::PopupNotificationVisibilityChanged(bool visible) {
[email protected]8cc8d492010-02-02 10:40:49910 if (is_being_destroyed_)
911 return;
912 if (!dont_notify_render_view_)
913 render_view_host()->PopupNotificationVisibilityChanged(visible);
[email protected]0de80162010-02-03 04:52:35914 if (delegate_)
915 delegate_->OnBlockedContentChange(this);
[email protected]96d185d2009-04-24 03:28:54916}
917
[email protected]5c9e97a2009-09-09 23:48:30918gfx::NativeView TabContents::GetContentNativeView() const {
[email protected]96d185d2009-04-24 03:28:54919 return view_->GetContentNativeView();
920}
921
922gfx::NativeView TabContents::GetNativeView() const {
923 return view_->GetNativeView();
924}
925
926void TabContents::GetContainerBounds(gfx::Rect *out) const {
927 view_->GetContainerBounds(out);
928}
929
930void TabContents::Focus() {
931 view_->Focus();
932}
933
[email protected]90daadb42009-06-08 21:27:28934void TabContents::FocusThroughTabTraversal(bool reverse) {
[email protected]7e383692009-06-12 19:14:54935 if (showing_interstitial_page()) {
936 render_manager_.interstitial_page()->FocusThroughTabTraversal(reverse);
937 return;
938 }
[email protected]96d185d2009-04-24 03:28:54939 render_view_host()->SetInitialFocus(reverse);
940}
941
942bool TabContents::FocusLocationBarByDefault() {
943 DOMUI* dom_ui = GetDOMUIForCurrentState();
944 if (dom_ui)
945 return dom_ui->focus_location_bar_by_default();
[email protected]3e3f0eb2009-06-22 18:33:43946 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]e0d481582009-09-15 21:06:25947 if (entry && entry->url() == GURL(chrome::kAboutBlankURL))
[email protected]3e3f0eb2009-06-22 18:33:43948 return true;
[email protected]96d185d2009-04-24 03:28:54949 return false;
950}
951
[email protected]616ed5a2008-11-21 22:27:24952void TabContents::AddInfoBar(InfoBarDelegate* delegate) {
953 // Look through the existing InfoBarDelegates we have for a match. If we've
954 // already got one that matches, then we don't add the new one.
[email protected]f86a07022008-11-25 01:06:05955 for (int i = 0; i < infobar_delegate_count(); ++i) {
[email protected]6d27a7b2009-12-18 23:25:45956 if (GetInfoBarDelegateAt(i)->EqualsDelegate(delegate)) {
957 // Tell the new infobar to close so that it can clean itself up.
958 delegate->InfoBarClosed();
[email protected]616ed5a2008-11-21 22:27:24959 return;
[email protected]6d27a7b2009-12-18 23:25:45960 }
[email protected]616ed5a2008-11-21 22:27:24961 }
962
963 infobar_delegates_.push_back(delegate);
[email protected]bfd04a62009-02-01 18:16:56964 NotificationService::current()->Notify(
965 NotificationType::TAB_CONTENTS_INFOBAR_ADDED,
966 Source<TabContents>(this),
967 Details<InfoBarDelegate>(delegate));
[email protected]616ed5a2008-11-21 22:27:24968
969 // Add ourselves as an observer for navigations the first time a delegate is
970 // added. We use this notification to expire InfoBars that need to expire on
971 // page transitions.
972 if (infobar_delegates_.size() == 1) {
[email protected]bfd04a62009-02-01 18:16:56973 registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED,
[email protected]ce3fa3c2009-04-20 19:55:57974 Source<NavigationController>(&controller_));
[email protected]616ed5a2008-11-21 22:27:24975 }
976}
977
978void TabContents::RemoveInfoBar(InfoBarDelegate* delegate) {
979 std::vector<InfoBarDelegate*>::iterator it =
980 find(infobar_delegates_.begin(), infobar_delegates_.end(), delegate);
981 if (it != infobar_delegates_.end()) {
982 InfoBarDelegate* delegate = *it;
[email protected]bfd04a62009-02-01 18:16:56983 NotificationService::current()->Notify(
984 NotificationType::TAB_CONTENTS_INFOBAR_REMOVED,
985 Source<TabContents>(this),
986 Details<InfoBarDelegate>(delegate));
[email protected]f86a07022008-11-25 01:06:05987 infobar_delegates_.erase(it);
[email protected]616ed5a2008-11-21 22:27:24988
[email protected]6a02963e2009-01-06 16:58:03989 // Remove ourselves as an observer if we are tracking no more InfoBars.
990 if (infobar_delegates_.empty()) {
[email protected]bfd04a62009-02-01 18:16:56991 registrar_.Remove(this, NotificationType::NAV_ENTRY_COMMITTED,
[email protected]ce3fa3c2009-04-20 19:55:57992 Source<NavigationController>(&controller_));
[email protected]6a02963e2009-01-06 16:58:03993 }
[email protected]616ed5a2008-11-21 22:27:24994 }
995}
[email protected]3edd9522009-03-04 22:19:41996
[email protected]f81821c2009-08-07 01:29:47997void TabContents::ReplaceInfoBar(InfoBarDelegate* old_delegate,
998 InfoBarDelegate* new_delegate) {
999 std::vector<InfoBarDelegate*>::iterator it =
1000 find(infobar_delegates_.begin(), infobar_delegates_.end(), old_delegate);
1001 DCHECK(it != infobar_delegates_.end());
1002
1003 // Notify the container about the change of plans.
1004 scoped_ptr<std::pair<InfoBarDelegate*, InfoBarDelegate*> > details(
1005 new std::pair<InfoBarDelegate*, InfoBarDelegate*>(
1006 old_delegate, new_delegate));
1007 NotificationService::current()->Notify(
1008 NotificationType::TAB_CONTENTS_INFOBAR_REPLACED,
1009 Source<TabContents>(this),
1010 Details<std::pair<InfoBarDelegate*, InfoBarDelegate*> >(details.get()));
1011
1012 // Remove the old one.
1013 infobar_delegates_.erase(it);
1014
1015 // Add the new one.
1016 infobar_delegates_.push_back(new_delegate);
1017}
1018
[email protected]20ede072009-10-06 00:28:351019bool TabContents::ShouldShowBookmarkBar() {
[email protected]aa9196f2009-10-22 16:20:271020 if (showing_interstitial_page())
1021 return false;
1022
[email protected]96d185d2009-04-24 03:28:541023 // See GetDOMUIForCurrentState() comment for more info. This case is very
1024 // similar, but for non-first loads, we want to use the committed entry. This
1025 // is so the bookmarks bar disappears at the same time the page does.
1026 if (controller_.GetLastCommittedEntry()) {
1027 // Not the first load, always use the committed DOM UI.
1028 if (render_manager_.dom_ui())
1029 return render_manager_.dom_ui()->force_bookmark_bar_visible();
1030 return false; // Default.
1031 }
1032
1033 // When it's the first load, we know either the pending one or the committed
1034 // one will have the DOM UI in it (see GetDOMUIForCurrentState), and only one
1035 // of them will be valid, so we can just check both.
1036 if (render_manager_.pending_dom_ui())
1037 return render_manager_.pending_dom_ui()->force_bookmark_bar_visible();
1038 if (render_manager_.dom_ui())
1039 return render_manager_.dom_ui()->force_bookmark_bar_visible();
1040 return false; // Default.
1041}
1042
[email protected]d8f16ae2009-08-30 03:47:261043bool TabContents::IsExtensionShelfAlwaysVisible() {
1044 // See GetDOMUIForCurrentState() comment for more info. This case is very
1045 // similar, but for non-first loads, we want to use the committed entry. This
1046 // is so the bookmarks bar disappears at the same time the page does.
1047 if (controller_.GetLastCommittedEntry()) {
1048 // Not the first load, always use the committed DOM UI.
1049 if (render_manager_.dom_ui())
1050 return render_manager_.dom_ui()->force_extension_shelf_visible();
1051 return false; // Default.
1052 }
1053
1054 // When it's the first load, we know either the pending one or the committed
1055 // one will have the DOM UI in it (see GetDOMUIForCurrentState), and only one
1056 // of them will be valid, so we can just check both.
1057 if (render_manager_.pending_dom_ui())
1058 return render_manager_.pending_dom_ui()->force_extension_shelf_visible();
1059 if (render_manager_.dom_ui())
1060 return render_manager_.dom_ui()->force_extension_shelf_visible();
1061 return false; // Default.
1062}
1063
[email protected]3edd9522009-03-04 22:19:411064void TabContents::ToolbarSizeChanged(bool is_animating) {
1065 TabContentsDelegate* d = delegate();
1066 if (d)
1067 d->ToolbarSizeChanged(this, is_animating);
1068}
[email protected]616ed5a2008-11-21 22:27:241069
[email protected]fc6fb7fb2009-11-07 02:35:041070bool TabContents::CanDownload(int request_id) {
1071 TabContentsDelegate* d = delegate();
1072 if (d)
1073 return d->CanDownload(request_id);
1074 return true;
1075}
1076
[email protected]d5f942ba2008-09-26 19:30:341077void TabContents::OnStartDownload(DownloadItem* download) {
1078 DCHECK(download);
[email protected]d5f942ba2008-09-26 19:30:341079
1080 // Download in a constrained popup is shown in the tab that opened it.
[email protected]b6c874582009-05-08 19:38:311081 TabContents* tab_contents = delegate()->GetConstrainingContents(this);
[email protected]d5f942ba2008-09-26 19:30:341082
[email protected]59560e0b2009-06-04 03:30:221083 if (tab_contents && tab_contents->delegate())
1084 tab_contents->delegate()->OnStartDownload(download);
[email protected]3edd9522009-03-04 22:19:411085}
[email protected]3edd9522009-03-04 22:19:411086
[email protected]d5f942ba2008-09-26 19:30:341087void TabContents::WillClose(ConstrainedWindow* window) {
1088 ConstrainedWindowList::iterator it =
1089 find(child_windows_.begin(), child_windows_.end(), window);
[email protected]fa1cf0b82010-01-15 21:49:441090 bool removed_topmost_window = it == child_windows_.begin();
[email protected]d5f942ba2008-09-26 19:30:341091 if (it != child_windows_.end())
1092 child_windows_.erase(it);
[email protected]fa1cf0b82010-01-15 21:49:441093 if (removed_topmost_window && child_windows_.size() > 0) {
1094 child_windows_[0]->ShowConstrainedWindow();
1095 BlockTabContent(true);
1096 } else {
1097 BlockTabContent(false);
1098 }
[email protected]66ba4932009-06-04 19:22:131099}
[email protected]d5f942ba2008-09-26 19:30:341100
[email protected]66ba4932009-06-04 19:22:131101void TabContents::WillCloseBlockedPopupContainer(
1102 BlockedPopupContainer* container) {
1103 DCHECK(blocked_popups_ == container);
1104 blocked_popups_ = NULL;
[email protected]8cc8d492010-02-02 10:40:491105 PopupNotificationVisibilityChanged(false);
[email protected]d5f942ba2008-09-26 19:30:341106}
1107
[email protected]d5f942ba2008-09-26 19:30:341108void TabContents::DidMoveOrResize(ConstrainedWindow* window) {
[email protected]332af7732009-03-11 13:21:351109#if defined(OS_WIN)
[email protected]92edc472009-02-10 20:32:061110 UpdateWindow(GetNativeView());
[email protected]332af7732009-03-11 13:21:351111#endif
[email protected]d5f942ba2008-09-26 19:30:341112}
1113
[email protected]c1dc9f132009-11-20 23:13:211114void TabContents::StartFinding(string16 search_string,
[email protected]e491f1c2009-05-22 20:28:121115 bool forward_direction,
1116 bool case_sensitive) {
[email protected]c1dc9f132009-11-20 23:13:211117 // If search_string is empty, it means FindNext was pressed with a keyboard
[email protected]6e95706d2009-04-23 22:55:011118 // shortcut so unless we have something to search for we return early.
[email protected]c1dc9f132009-11-20 23:13:211119 if (search_string.empty() && find_text_.empty()) {
[email protected]87fae512009-12-11 02:20:211120 // Try the last thing we searched for on this tab, then the last thing
1121 // searched for on any tab.
1122 if (!previous_find_text_.empty())
1123 search_string = previous_find_text_;
1124 else if (!last_search_prepopulate_text_->empty())
1125 search_string = *last_search_prepopulate_text_;
1126 else
[email protected]e491f1c2009-05-22 20:28:121127 return;
[email protected]e491f1c2009-05-22 20:28:121128 }
[email protected]6e95706d2009-04-23 22:55:011129
[email protected]101c90e2009-10-26 21:14:441130 // Keep track of the previous search.
1131 previous_find_text_ = find_text_;
1132
[email protected]6e95706d2009-04-23 22:55:011133 // This is a FindNext operation if we are searching for the same text again,
1134 // or if the passed in search text is empty (FindNext keyboard shortcut). The
1135 // exception to this is if the Find was aborted (then we don't want FindNext
1136 // because the highlighting has been cleared and we need it to reappear). We
1137 // therefore treat FindNext after an aborted Find operation as a full fledged
1138 // Find.
[email protected]c1dc9f132009-11-20 23:13:211139 bool find_next = (find_text_ == search_string || search_string.empty()) &&
[email protected]e491f1c2009-05-22 20:28:121140 (last_search_case_sensitive_ == case_sensitive) &&
[email protected]6e95706d2009-04-23 22:55:011141 !find_op_aborted_;
1142 if (!find_next)
1143 current_find_request_id_ = find_request_id_counter_++;
1144
[email protected]c1dc9f132009-11-20 23:13:211145 if (!search_string.empty())
1146 find_text_ = search_string;
[email protected]e491f1c2009-05-22 20:28:121147 last_search_case_sensitive_ = case_sensitive;
[email protected]6e95706d2009-04-23 22:55:011148
1149 find_op_aborted_ = false;
1150
1151 // Keep track of what the last search was across the tabs.
[email protected]c1dc9f132009-11-20 23:13:211152 *last_search_prepopulate_text_ = find_text_;
[email protected]6e95706d2009-04-23 22:55:011153 render_view_host()->StartFinding(current_find_request_id_,
1154 find_text_,
1155 forward_direction,
[email protected]e491f1c2009-05-22 20:28:121156 case_sensitive,
[email protected]6e95706d2009-04-23 22:55:011157 find_next);
1158}
1159
[email protected]5bc8fe92010-03-11 18:19:001160void TabContents::StopFinding(
1161 FindBarController::SelectionAction selection_action) {
1162 // When the action is kClearAction, it means the find string has been cleared
[email protected]2d2dffc2009-06-09 21:41:001163 // by the user, but the UI has not been dismissed.
[email protected]5bc8fe92010-03-11 18:19:001164 if (selection_action != FindBarController::kClearSelection)
[email protected]2d2dffc2009-06-09 21:41:001165 find_ui_active_ = false;
[email protected]87fae512009-12-11 02:20:211166 previous_find_text_ = find_text_;
[email protected]101c90e2009-10-26 21:14:441167 find_text_.clear();
[email protected]6e95706d2009-04-23 22:55:011168 find_op_aborted_ = true;
[email protected]e491f1c2009-05-22 20:28:121169 last_search_result_ = FindNotificationDetails();
[email protected]5bc8fe92010-03-11 18:19:001170 render_view_host()->StopFinding(selection_action);
[email protected]6e95706d2009-04-23 22:55:011171}
1172
[email protected]420ae012009-04-24 05:16:321173void TabContents::OnSavePage() {
1174 // If we can not save the page, try to download it.
1175 if (!SavePackage::IsSavableContents(contents_mime_type())) {
1176 DownloadManager* dlm = profile()->GetDownloadManager();
1177 const GURL& current_page_url = GetURL();
1178 if (dlm && current_page_url.is_valid())
[email protected]d82ed61e2009-06-16 02:46:221179 dlm->DownloadUrl(current_page_url, GURL(), "", this);
[email protected]420ae012009-04-24 05:16:321180 return;
1181 }
1182
1183 Stop();
1184
1185 // Create the save package and possibly prompt the user for the name to save
1186 // the page as. The user prompt is an asynchronous operation that runs on
1187 // another thread.
[email protected]d82ed61e2009-06-16 02:46:221188 save_package_ = new SavePackage(this);
[email protected]420ae012009-04-24 05:16:321189 save_package_->GetSaveInfo();
1190}
1191
1192// Used in automated testing to bypass prompting the user for file names.
1193// Instead, the names and paths are hard coded rather than running them through
1194// file name sanitation and extension / mime checking.
[email protected]828cabe2009-09-26 22:47:111195bool TabContents::SavePage(const FilePath& main_file, const FilePath& dir_path,
[email protected]420ae012009-04-24 05:16:321196 SavePackage::SavePackageType save_type) {
1197 // Stop the page from navigating.
1198 Stop();
1199
[email protected]828cabe2009-09-26 22:47:111200 save_package_ = new SavePackage(this, save_type, main_file, dir_path);
[email protected]cdf6cc82009-08-04 21:48:591201 return save_package_->Init();
[email protected]420ae012009-04-24 05:16:321202}
1203
[email protected]89d6e6e2009-12-03 23:06:511204void TabContents::EmailPageLocation() {
1205 std::string title = EscapeQueryParamValue(UTF16ToUTF8(GetTitle()), false);
1206 std::string page_url = EscapeQueryParamValue(GetURL().spec(), false);
1207 std::string mailto = std::string("mailto:?subject=Fwd:%20") +
1208 title + "&body=%0A%0A" + page_url;
1209 platform_util::OpenExternal(GURL(mailto));
1210}
1211
[email protected]420ae012009-04-24 05:16:321212void TabContents::PrintPreview() {
1213 // We don't show the print preview yet, only the print dialog.
1214 PrintNow();
1215}
1216
1217bool TabContents::PrintNow() {
1218 // We can't print interstitial page for now.
1219 if (showing_interstitial_page())
1220 return false;
1221
1222 return render_view_host()->PrintPages();
1223}
1224
[email protected]82270452009-06-19 15:58:011225void TabContents::PrintingDone(int document_cookie, bool success) {
1226 render_view_host()->PrintingDone(document_cookie, success);
1227}
1228
[email protected]420ae012009-04-24 05:16:321229bool TabContents::IsActiveEntry(int32 page_id) {
1230 NavigationEntry* active_entry = controller_.GetActiveEntry();
1231 return (active_entry != NULL &&
1232 active_entry->site_instance() == GetSiteInstance() &&
1233 active_entry->page_id() == page_id);
1234}
1235
[email protected]b2fe07d12010-02-09 14:38:081236void TabContents::SetOverrideEncoding(const std::string& encoding) {
[email protected]8cb5d5b2010-02-09 11:36:161237 set_encoding(encoding);
1238 render_view_host()->SetPageEncoding(encoding);
1239}
1240
[email protected]b2fe07d12010-02-09 14:38:081241void TabContents::ResetOverrideEncoding() {
[email protected]8cb5d5b2010-02-09 11:36:161242 reset_encoding();
1243 render_view_host()->ResetPageEncodingToDefault();
1244}
1245
1246void TabContents::WindowMoveOrResizeStarted() {
1247 render_view_host()->WindowMoveOrResizeStarted();
1248}
1249
[email protected]3f8eb7f2009-10-02 23:58:051250void TabContents::LogNewTabTime(const std::string& event_name) {
1251 // Not all new tab pages get timed. In those cases, we don't have a
1252 // new_tab_start_time_.
1253 if (new_tab_start_time_.is_null())
1254 return;
1255
1256 base::TimeDelta duration = base::TimeTicks::Now() - new_tab_start_time_;
[email protected]84abba62009-10-07 17:01:441257 MetricEventDurationDetails details(event_name,
1258 static_cast<int>(duration.InMilliseconds()));
1259
[email protected]7ab1e7d62009-10-14 23:32:011260 if (event_name == "Tab.NewTabScriptStart") {
1261 UMA_HISTOGRAM_TIMES("Tab.NewTabScriptStart", duration);
1262 } else if (event_name == "Tab.NewTabDOMContentLoaded") {
1263 UMA_HISTOGRAM_TIMES("Tab.NewTabDOMContentLoaded", duration);
1264 } else if (event_name == "Tab.NewTabOnload") {
1265 UMA_HISTOGRAM_TIMES("Tab.NewTabOnload", duration);
[email protected]3f8eb7f2009-10-02 23:58:051266 // The new tab page has finished loading; reset it.
1267 new_tab_start_time_ = base::TimeTicks();
[email protected]84abba62009-10-07 17:01:441268 } else {
1269 NOTREACHED();
[email protected]3f8eb7f2009-10-02 23:58:051270 }
[email protected]84abba62009-10-07 17:01:441271 NotificationService::current()->Notify(
1272 NotificationType::METRIC_EVENT_DURATION,
1273 Source<TabContents>(this),
1274 Details<MetricEventDurationDetails>(&details));
[email protected]3f8eb7f2009-10-02 23:58:051275}
1276
[email protected]7ab1e7d62009-10-14 23:32:011277void TabContents::OnCloseStarted() {
1278 if (tab_close_start_time_.is_null())
1279 tab_close_start_time_ = base::TimeTicks::Now();
1280}
1281
[email protected]c37e3b62010-01-22 00:15:171282TabContents* TabContents::CloneAndMakePhantom() {
[email protected]c37e3b62010-01-22 00:15:171283 TabNavigation tab_nav;
[email protected]df72ac72010-02-06 00:43:161284
1285 NavigationEntry* entry = controller().GetActiveEntry();
1286 if (app_extension_)
[email protected]867a73e12010-03-19 20:45:461287 tab_nav.set_url(app_extension_->GetFullLaunchURL());
[email protected]df72ac72010-02-06 00:43:161288 else if (entry)
1289 tab_nav.SetFromNavigationEntry(*entry);
1290
[email protected]c37e3b62010-01-22 00:15:171291 std::vector<TabNavigation> navigations;
1292 navigations.push_back(tab_nav);
1293
1294 TabContents* new_contents =
1295 new TabContents(profile(), NULL, MSG_ROUTING_NONE, NULL);
[email protected]3b355642010-02-05 16:01:491296 new_contents->SetAppExtension(app_extension_);
[email protected]c37e3b62010-01-22 00:15:171297 new_contents->controller().RestoreFromState(navigations, 0, false);
[email protected]c37e3b62010-01-22 00:15:171298
[email protected]df72ac72010-02-06 00:43:161299 if (!app_extension_ && entry)
1300 new_contents->controller().GetActiveEntry()->favicon() = entry->favicon();
1301
[email protected]c37e3b62010-01-22 00:15:171302 return new_contents;
1303}
1304
[email protected]0de80162010-02-03 04:52:351305// Resets the |content_blocked_| array.
1306void TabContents::ClearBlockedContentSettings() {
[email protected]15cf526b2010-02-12 06:33:491307 for (size_t i = 0; i < arraysize(content_blocked_); ++i)
[email protected]0de80162010-02-03 04:52:351308 content_blocked_[i] = false;
1309}
1310
[email protected]96d185d2009-04-24 03:28:541311// Notifies the RenderWidgetHost instance about the fact that the page is
1312// loading, or done loading and calls the base implementation.
1313void TabContents::SetIsLoading(bool is_loading,
1314 LoadNotificationDetails* details) {
1315 if (is_loading == is_loading_)
1316 return;
1317
1318 if (!is_loading) {
1319 load_state_ = net::LOAD_STATE_IDLE;
1320 load_state_host_.clear();
[email protected]094e5b22009-09-25 04:23:561321 upload_size_ = 0;
1322 upload_position_ = 0;
[email protected]96d185d2009-04-24 03:28:541323 }
1324
1325 render_manager_.SetIsLoading(is_loading);
1326
1327 is_loading_ = is_loading;
1328 waiting_for_response_ = is_loading;
1329
1330 if (delegate_)
1331 delegate_->LoadingStateChanged(this);
[email protected]329581b2009-04-28 06:52:351332 NotifyNavigationStateChanged(INVALIDATE_LOAD);
[email protected]96d185d2009-04-24 03:28:541333
1334 NotificationType type = is_loading ? NotificationType::LOAD_START :
1335 NotificationType::LOAD_STOP;
[email protected]ce5c4502009-05-06 16:46:111336 NotificationDetails det = NotificationService::NoDetails();
[email protected]96d185d2009-04-24 03:28:541337 if (details)
1338 det = Details<LoadNotificationDetails>(details);
1339 NotificationService::current()->Notify(type,
1340 Source<NavigationController>(&controller_),
1341 det);
1342}
1343
[email protected]b6c874582009-05-08 19:38:311344void TabContents::AddPopup(TabContents* new_contents,
[email protected]7e7414ae2010-01-26 20:19:291345 const gfx::Rect& initial_pos) {
[email protected]8cc8d492010-02-02 10:40:491346 GURL url(GetURL());
1347 if (url.is_valid() &&
1348 profile()->GetHostContentSettingsMap()->GetContentSetting(
[email protected]da0c8e92010-02-09 23:25:491349 url, CONTENT_SETTINGS_TYPE_POPUPS) == CONTENT_SETTING_ALLOW) {
[email protected]8cc8d492010-02-02 10:40:491350 AddNewContents(new_contents, NEW_POPUP, initial_pos, true);
1351 } else {
1352 if (!blocked_popups_)
1353 blocked_popups_ = new BlockedPopupContainer(this);
1354 blocked_popups_->AddTabContents(new_contents, initial_pos);
1355 }
[email protected]b9681312008-11-07 00:08:261356}
[email protected]616ed5a2008-11-21 22:27:241357
1358namespace {
1359bool TransitionIsReload(PageTransition::Type transition) {
1360 return PageTransition::StripQualifier(transition) == PageTransition::RELOAD;
1361}
1362}
1363
1364void TabContents::ExpireInfoBars(
1365 const NavigationController::LoadCommittedDetails& details) {
1366 // Only hide InfoBars when the user has done something that makes the main
1367 // frame load. We don't want various automatic or subframe navigations making
1368 // it disappear.
1369 if (!details.is_user_initiated_main_frame_load())
1370 return;
1371
[email protected]f86a07022008-11-25 01:06:051372 for (int i = infobar_delegate_count() - 1; i >= 0; --i) {
[email protected]616ed5a2008-11-21 22:27:241373 InfoBarDelegate* delegate = GetInfoBarDelegateAt(i);
[email protected]f86a07022008-11-25 01:06:051374 if (delegate->ShouldExpire(details))
[email protected]616ed5a2008-11-21 22:27:241375 RemoveInfoBar(delegate);
[email protected]616ed5a2008-11-21 22:27:241376 }
1377}
[email protected]fdd61c62009-04-22 19:22:571378
[email protected]96d185d2009-04-24 03:28:541379DOMUI* TabContents::GetDOMUIForCurrentState() {
1380 // When there is a pending navigation entry, we want to use the pending DOMUI
1381 // that goes along with it to control the basic flags. For example, we want to
1382 // show the pending URL in the URL bar, so we want the display_url flag to
1383 // be from the pending entry.
1384 //
1385 // The confusion comes because there are multiple possibilities for the
1386 // initial load in a tab as a side effect of the way the RenderViewHostManager
1387 // works.
1388 //
1389 // - For the very first tab the load looks "normal". The new tab DOM UI is
1390 // the pending one, and we want it to apply here.
1391 //
1392 // - For subsequent new tabs, they'll get a new SiteInstance which will then
1393 // get switched to the one previously associated with the new tab pages.
1394 // This switching will cause the manager to commit the RVH/DOMUI. So we'll
1395 // have a committed DOM UI in this case.
1396 //
1397 // This condition handles all of these cases:
1398 //
1399 // - First load in first tab: no committed nav entry + pending nav entry +
1400 // pending dom ui:
1401 // -> Use pending DOM UI if any.
1402 //
1403 // - First load in second tab: no committed nav entry + pending nav entry +
1404 // no pending DOM UI:
1405 // -> Use the committed DOM UI if any.
1406 //
1407 // - Second navigation in any tab: committed nav entry + pending nav entry:
1408 // -> Use pending DOM UI if any.
1409 //
1410 // - Normal state with no load: committed nav entry + no pending nav entry:
1411 // -> Use committed DOM UI.
1412 if (controller_.pending_entry() &&
1413 (controller_.GetLastCommittedEntry() ||
1414 render_manager_.pending_dom_ui()))
1415 return render_manager_.pending_dom_ui();
1416 return render_manager_.dom_ui();
1417}
[email protected]420ae012009-04-24 05:16:321418
1419void TabContents::DidNavigateMainFramePostCommit(
1420 const NavigationController::LoadCommittedDetails& details,
1421 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]c2e74fe82009-09-03 17:57:441422 if (opener_dom_ui_type_ != DOMUIFactory::kNoDOMUI) {
1423 // If this is a window.open navigation, use the same DOMUI as the renderer
1424 // that opened the window, as long as both renderers have the same
1425 // privileges.
1426 if (opener_dom_ui_type_ == DOMUIFactory::GetDOMUIType(GetURL())) {
1427 DOMUI* dom_ui = DOMUIFactory::CreateDOMUIForURL(this, GetURL());
1428 // dom_ui might be NULL if the URL refers to a non-existent extension.
1429 if (dom_ui) {
1430 render_manager_.SetDOMUIPostCommit(dom_ui);
1431 dom_ui->RenderViewCreated(render_view_host());
1432 }
1433 }
1434 opener_dom_ui_type_ = DOMUIFactory::kNoDOMUI;
1435 }
1436
[email protected]420ae012009-04-24 05:16:321437 if (details.is_user_initiated_main_frame_load()) {
1438 // Clear the status bubble. This is a workaround for a bug where WebKit
1439 // doesn't let us know that the cursor left an element during a
1440 // transition (this is also why the mouse cursor remains as a hand after
1441 // clicking on a link); see bugs 1184641 and 980803. We don't want to
1442 // clear the bubble when a user navigates to a named anchor in the same
1443 // page.
1444 UpdateTargetURL(details.entry->page_id(), GURL());
1445
1446 // UpdateHelpersForDidNavigate will handle the case where the password_form
1447 // origin is valid.
1448 // TODO(brettw) bug 1343111: Password manager stuff in here needs to be
1449 // cleaned up and covered by tests.
1450 if (!params.password_form.origin.is_valid())
1451 GetPasswordManager()->DidNavigate();
1452 }
1453
1454 // The keyword generator uses the navigation entries, so must be called after
1455 // the commit.
1456 GenerateKeywordIfNecessary(params);
1457
1458 // Allow the new page to set the title again.
1459 received_page_title_ = false;
1460
1461 // Get the favicon, either from history or request it from the net.
1462 fav_icon_helper_.FetchFavIcon(details.entry->url());
1463
[email protected]0de80162010-02-03 04:52:351464 // Clear all page actions, blocked content notifications and browser actions
1465 // for this tab, unless this is an in-page navigation.
[email protected]a6e82fc2010-02-24 22:28:141466 if (!details.is_in_page) {
[email protected]92c6f9b92009-10-24 04:35:081467 ExtensionsService* service = profile()->GetExtensionsService();
1468 if (service) {
1469 for (size_t i = 0; i < service->extensions()->size(); ++i) {
[email protected]5d246db22009-10-27 06:17:571470 ExtensionAction* browser_action =
[email protected]92c6f9b92009-10-24 04:35:081471 service->extensions()->at(i)->browser_action();
[email protected]56ce6e52009-10-27 00:10:521472 if (browser_action) {
1473 browser_action->ClearAllValuesForTab(controller().session_id().id());
1474 NotificationService::current()->Notify(
1475 NotificationType::EXTENSION_BROWSER_ACTION_UPDATED,
[email protected]5d246db22009-10-27 06:17:571476 Source<ExtensionAction>(browser_action),
[email protected]56ce6e52009-10-27 00:10:521477 NotificationService::NoDetails());
1478 }
[email protected]92c6f9b92009-10-24 04:35:081479
[email protected]5d246db22009-10-27 06:17:571480 ExtensionAction* page_action =
[email protected]56ce6e52009-10-27 00:10:521481 service->extensions()->at(i)->page_action();
1482 if (page_action) {
1483 page_action->ClearAllValuesForTab(controller().session_id().id());
1484 PageActionStateChanged();
1485 }
[email protected]92c6f9b92009-10-24 04:35:081486 }
[email protected]aece2c7f2009-08-27 20:43:171487 }
[email protected]0de80162010-02-03 04:52:351488
1489 // Close blocked popups.
1490 if (blocked_popups_) {
1491 AutoReset auto_reset(&dont_notify_render_view_, true);
1492 blocked_popups_->Destroy();
[email protected]da7b5262010-02-25 21:12:461493 blocked_popups_ = NULL;
[email protected]0de80162010-02-03 04:52:351494 }
1495
1496 // Clear "blocked" flags.
1497 ClearBlockedContentSettings();
1498 if (delegate_)
1499 delegate_->OnBlockedContentChange(this);
[email protected]aece2c7f2009-08-27 20:43:171500 }
[email protected]ce5c4502009-05-06 16:46:111501
[email protected]8cc8d492010-02-02 10:40:491502 // Close constrained windows if necessary.
1503 if (!net::RegistryControlledDomainService::SameDomainOrHost(
1504 details.previous_url, details.entry->url()))
1505 CloseConstrainedWindows();
1506
[email protected]420ae012009-04-24 05:16:321507 // Update the starred state.
1508 UpdateStarredStateForCurrentURL();
[email protected]c07c9752010-03-12 05:57:531509
1510 // Clear the cache of forms in AutoFill.
1511 if (autofill_manager_.get())
1512 autofill_manager_->Reset();
[email protected]420ae012009-04-24 05:16:321513}
1514
1515void TabContents::DidNavigateAnyFramePostCommit(
1516 RenderViewHost* render_view_host,
1517 const NavigationController::LoadCommittedDetails& details,
1518 const ViewHostMsg_FrameNavigate_Params& params) {
1519 // If we navigate, start showing messages again. This does nothing to prevent
1520 // a malicious script from spamming messages, since the script could just
1521 // reload the page to stop blocking.
1522 suppress_javascript_messages_ = false;
1523
[email protected]420ae012009-04-24 05:16:321524 // Notify the password manager of the navigation or form submit.
1525 // TODO(brettw) bug 1343111: Password manager stuff in here needs to be
1526 // cleaned up and covered by tests.
1527 if (params.password_form.origin.is_valid())
1528 GetPasswordManager()->ProvisionallySavePassword(params.password_form);
[email protected]3b1c1812010-02-12 21:53:551529
1530 // Let the LanguageState clear its state.
[email protected]34cec8d82010-03-05 18:27:011531 bool reload = details.entry->transition_type() == PageTransition::RELOAD ||
1532 details.type == NavigationType::SAME_PAGE;
1533 language_state_.DidNavigate(reload, details.is_in_page);
[email protected]420ae012009-04-24 05:16:321534}
1535
[email protected]8cc8d492010-02-02 10:40:491536void TabContents::CloseConstrainedWindows() {
1537 // Clear out any constrained windows since we are leaving this page entirely.
[email protected]420ae012009-04-24 05:16:321538 // We use indices instead of iterators in case CloseWindow does something
1539 // that may invalidate an iterator.
1540 int size = static_cast<int>(child_windows_.size());
1541 for (int i = size - 1; i >= 0; --i) {
1542 ConstrainedWindow* window = child_windows_[i];
[email protected]fa1cf0b82010-01-15 21:49:441543 if (window) {
[email protected]420ae012009-04-24 05:16:321544 window->CloseConstrainedWindow();
[email protected]fa1cf0b82010-01-15 21:49:441545 BlockTabContent(false);
1546 }
[email protected]420ae012009-04-24 05:16:321547 }
1548}
1549
1550void TabContents::UpdateStarredStateForCurrentURL() {
1551 BookmarkModel* model = profile()->GetBookmarkModel();
1552 const bool old_state = is_starred_;
1553 is_starred_ = (model && model->IsBookmarked(GetURL()));
1554
1555 if (is_starred_ != old_state && delegate())
1556 delegate()->URLStarredChanged(this, is_starred_);
1557}
1558
1559void TabContents::UpdateAlternateErrorPageURL() {
1560 GURL url = GetAlternateErrorPageURL();
1561 render_view_host()->SetAlternateErrorPageURL(url);
1562}
1563
1564void TabContents::UpdateWebPreferences() {
1565 render_view_host()->UpdateWebPreferences(GetWebkitPrefs());
1566}
1567
1568void TabContents::UpdateMaxPageIDIfNecessary(SiteInstance* site_instance,
1569 RenderViewHost* rvh) {
1570 // If we are creating a RVH for a restored controller, then we might
1571 // have more page IDs than the SiteInstance's current max page ID. We must
1572 // make sure that the max page ID is larger than any restored page ID.
1573 // Note that it is ok for conflicting page IDs to exist in another tab
1574 // (i.e., NavigationController), but if any page ID is larger than the max,
1575 // the back/forward list will get confused.
1576 int max_restored_page_id = controller_.max_restored_page_id();
1577 if (max_restored_page_id > 0) {
1578 int curr_max_page_id = site_instance->max_page_id();
1579 if (max_restored_page_id > curr_max_page_id) {
1580 // Need to update the site instance immediately.
1581 site_instance->UpdateMaxPageID(max_restored_page_id);
1582
1583 // Also tell the renderer to update its internal representation. We
1584 // need to reserve enough IDs to make all restored page IDs less than
1585 // the max.
1586 if (curr_max_page_id < 0)
1587 curr_max_page_id = 0;
1588 rvh->ReservePageIDRange(max_restored_page_id - curr_max_page_id);
1589 }
1590 }
1591}
1592
1593void TabContents::UpdateHistoryForNavigation(
[email protected]ebe89e062009-08-13 23:16:541594 const GURL& virtual_url,
[email protected]befd8d822009-07-01 04:51:471595 const NavigationController::LoadCommittedDetails& details,
[email protected]420ae012009-04-24 05:16:321596 const ViewHostMsg_FrameNavigate_Params& params) {
1597 if (profile()->IsOffTheRecord())
1598 return;
1599
1600 // Add to history service.
1601 HistoryService* hs = profile()->GetHistoryService(Profile::IMPLICIT_ACCESS);
1602 if (hs) {
1603 if (PageTransition::IsMainFrame(params.transition) &&
[email protected]ebe89e062009-08-13 23:16:541604 virtual_url != params.url) {
1605 // Hack on the "virtual" URL so that it will appear in history. For some
[email protected]420ae012009-04-24 05:16:321606 // types of URLs, we will display a magic URL that is different from where
1607 // the page is actually navigated. We want the user to see in history
[email protected]ebe89e062009-08-13 23:16:541608 // what they saw in the URL bar, so we add the virtual URL as a redirect.
1609 // This only applies to the main frame, as the virtual URL doesn't apply
[email protected]420ae012009-04-24 05:16:321610 // to sub-frames.
1611 std::vector<GURL> redirects = params.redirects;
1612 if (!redirects.empty())
[email protected]ebe89e062009-08-13 23:16:541613 redirects.back() = virtual_url;
1614 hs->AddPage(virtual_url, this, params.page_id, params.referrer,
[email protected]befd8d822009-07-01 04:51:471615 params.transition, redirects, details.did_replace_entry);
[email protected]420ae012009-04-24 05:16:321616 } else {
1617 hs->AddPage(params.url, this, params.page_id, params.referrer,
[email protected]befd8d822009-07-01 04:51:471618 params.transition, params.redirects,
1619 details.did_replace_entry);
[email protected]420ae012009-04-24 05:16:321620 }
1621 }
1622}
1623
1624bool TabContents::UpdateTitleForEntry(NavigationEntry* entry,
1625 const std::wstring& title) {
1626 // For file URLs without a title, use the pathname instead. In the case of a
1627 // synthesized title, we don't want the update to count toward the "one set
1628 // per page of the title to history."
1629 std::wstring final_title;
1630 bool explicit_set;
1631 if (entry->url().SchemeIsFile() && title.empty()) {
1632 final_title = UTF8ToWide(entry->url().ExtractFileName());
1633 explicit_set = false; // Don't count synthetic titles toward the set limit.
1634 } else {
1635 TrimWhitespace(title, TRIM_ALL, &final_title);
1636 explicit_set = true;
1637 }
1638
1639 if (final_title == UTF16ToWideHack(entry->title()))
1640 return false; // Nothing changed, don't bother.
1641
1642 entry->set_title(WideToUTF16Hack(final_title));
1643
1644 // Update the history system for this page.
1645 if (!profile()->IsOffTheRecord() && !received_page_title_) {
1646 HistoryService* hs =
1647 profile()->GetHistoryService(Profile::IMPLICIT_ACCESS);
1648 if (hs)
[email protected]ebe89e062009-08-13 23:16:541649 hs->SetPageTitle(entry->virtual_url(), final_title);
[email protected]420ae012009-04-24 05:16:321650
1651 // Don't allow the title to be saved again for explicitly set ones.
1652 received_page_title_ = explicit_set;
1653 }
1654
1655 // Lastly, set the title for the view.
1656 view_->SetPageTitle(final_title);
1657
1658 return true;
1659}
1660
1661void TabContents::NotifySwapped() {
1662 // After sending out a swap notification, we need to send a disconnect
1663 // notification so that clients that pick up a pointer to |this| can NULL the
1664 // pointer. See Bug 1230284.
1665 notify_disconnection_ = true;
1666 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341667 NotificationType::TAB_CONTENTS_SWAPPED,
[email protected]d82ed61e2009-06-16 02:46:221668 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321669 NotificationService::NoDetails());
1670}
1671
1672void TabContents::NotifyConnected() {
1673 notify_disconnection_ = true;
1674 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341675 NotificationType::TAB_CONTENTS_CONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221676 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321677 NotificationService::NoDetails());
1678}
1679
1680void TabContents::NotifyDisconnected() {
1681 if (!notify_disconnection_)
1682 return;
1683
1684 notify_disconnection_ = false;
1685 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341686 NotificationType::TAB_CONTENTS_DISCONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221687 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321688 NotificationService::NoDetails());
1689}
1690
1691void TabContents::GenerateKeywordIfNecessary(
1692 const ViewHostMsg_FrameNavigate_Params& params) {
1693 if (!params.searchable_form_url.is_valid())
1694 return;
1695
1696 if (profile()->IsOffTheRecord())
1697 return;
1698
1699 int last_index = controller_.last_committed_entry_index();
1700 // When there was no previous page, the last index will be 0. This is
1701 // normally due to a form submit that opened in a new tab.
1702 // TODO(brettw) bug 916126: we should support keywords when form submits
1703 // happen in new tabs.
1704 if (last_index <= 0)
1705 return;
1706 const NavigationEntry* previous_entry =
1707 controller_.GetEntryAtIndex(last_index - 1);
1708 if (IsFormSubmit(previous_entry)) {
1709 // Only generate a keyword if the previous page wasn't itself a form
1710 // submit.
1711 return;
1712 }
1713
1714 GURL keyword_url = previous_entry->user_typed_url().is_valid() ?
1715 previous_entry->user_typed_url() : previous_entry->url();
1716 std::wstring keyword =
1717 TemplateURLModel::GenerateKeyword(keyword_url, true); // autodetected
1718 if (keyword.empty())
1719 return;
1720
1721 TemplateURLModel* url_model = profile()->GetTemplateURLModel();
1722 if (!url_model)
1723 return;
1724
1725 if (!url_model->loaded()) {
1726 url_model->Load();
1727 return;
1728 }
1729
1730 const TemplateURL* current_url;
1731 std::wstring url = UTF8ToWide(params.searchable_form_url.spec());
1732 if (!url_model->CanReplaceKeyword(keyword, url, &current_url))
1733 return;
1734
1735 if (current_url) {
1736 if (current_url->originating_url().is_valid()) {
1737 // The existing keyword was generated from an OpenSearch description
1738 // document, don't regenerate.
1739 return;
1740 }
1741 url_model->Remove(current_url);
1742 }
1743 TemplateURL* new_url = new TemplateURL();
1744 new_url->set_keyword(keyword);
1745 new_url->set_short_name(keyword);
1746 new_url->SetURL(url, 0, 0);
1747 new_url->add_input_encoding(params.searchable_form_encoding);
1748 DCHECK(controller_.GetLastCommittedEntry());
1749 const GURL& favicon_url =
1750 controller_.GetLastCommittedEntry()->favicon().url();
1751 if (favicon_url.is_valid()) {
1752 new_url->SetFavIconURL(favicon_url);
1753 } else {
1754 // The favicon url isn't valid. This means there really isn't a favicon,
1755 // or the favicon url wasn't obtained before the load started. This assumes
1756 // the later.
1757 // TODO(sky): Need a way to set the favicon that doesn't involve generating
1758 // its url.
1759 new_url->SetFavIconURL(TemplateURL::GenerateFaviconURL(params.referrer));
1760 }
1761 new_url->set_safe_for_autoreplace(true);
1762 url_model->Add(new_url);
1763}
1764
[email protected]bf5c2ff392009-07-08 16:24:331765void TabContents::OnUserGesture() {
1766 // See comment in RenderViewHostDelegate::OnUserGesture as to why we do this.
1767 DownloadRequestManager* drm = g_browser_process->download_request_manager();
1768 if (drm)
1769 drm->OnUserGesture(this);
[email protected]e7eaedde2009-09-25 20:09:491770 ExternalProtocolHandler::OnUserGesture();
[email protected]bf5c2ff392009-07-08 16:24:331771 controller_.OnUserGesture();
1772}
1773
1774void TabContents::OnFindReply(int request_id,
1775 int number_of_matches,
1776 const gfx::Rect& selection_rect,
1777 int active_match_ordinal,
1778 bool final_update) {
[email protected]3ce7ced2009-07-24 18:52:421779 // Ignore responses for requests that have been aborted.
1780 if (find_op_aborted_)
1781 return;
1782
[email protected]bf5c2ff392009-07-08 16:24:331783 // Ignore responses for requests other than the one we have most recently
1784 // issued. That way we won't act on stale results when the user has
1785 // already typed in another query.
1786 if (request_id != current_find_request_id_)
1787 return;
1788
1789 if (number_of_matches == -1)
1790 number_of_matches = last_search_result_.number_of_matches();
1791 if (active_match_ordinal == -1)
1792 active_match_ordinal = last_search_result_.active_match_ordinal();
1793
1794 gfx::Rect selection = selection_rect;
1795 if (selection.IsEmpty())
1796 selection = last_search_result_.selection_rect();
1797
1798 // Notify the UI, automation and any other observers that a find result was
1799 // found.
1800 last_search_result_ = FindNotificationDetails(request_id, number_of_matches,
1801 selection, active_match_ordinal,
1802 final_update);
1803 NotificationService::current()->Notify(
1804 NotificationType::FIND_RESULT_AVAILABLE,
1805 Source<TabContents>(this),
1806 Details<FindNotificationDetails>(&last_search_result_));
1807}
1808
1809void TabContents::GoToEntryAtOffset(int offset) {
[email protected]f9cc4c452009-10-13 14:56:381810 if (!delegate_ || delegate_->OnGoToEntryOffset(offset))
1811 controller_.GoToOffset(offset);
[email protected]bf5c2ff392009-07-08 16:24:331812}
1813
[email protected]bf5c2ff392009-07-08 16:24:331814void TabContents::OnMissingPluginStatus(int status) {
1815#if defined(OS_WIN)
1816// TODO(PORT): pull in when plug-ins work
1817 GetPluginInstaller()->OnMissingPluginStatus(status);
1818#endif
1819}
1820
1821void TabContents::OnCrashedPlugin(const FilePath& plugin_path) {
[email protected]bf5c2ff392009-07-08 16:24:331822 DCHECK(!plugin_path.value().empty());
1823
1824 std::wstring plugin_name = plugin_path.ToWStringHack();
[email protected]d32d46182009-07-23 00:09:351825#if defined(OS_WIN) || defined(OS_MACOSX)
[email protected]bf5c2ff392009-07-08 16:24:331826 scoped_ptr<FileVersionInfo> version_info(
1827 FileVersionInfo::CreateFileVersionInfo(plugin_path));
1828 if (version_info.get()) {
1829 const std::wstring& product_name = version_info->product_name();
[email protected]996fd702009-09-04 19:12:371830 if (!product_name.empty()) {
[email protected]bf5c2ff392009-07-08 16:24:331831 plugin_name = product_name;
[email protected]996fd702009-09-04 19:12:371832#if defined(OS_MACOSX)
1833 // Many plugins on the Mac have .plugin in the actual name, which looks
1834 // terrible, so look for that and strip it off if present.
1835 const std::wstring plugin_extension(L".plugin");
1836 if (EndsWith(plugin_name, plugin_extension, true))
1837 plugin_name.erase(plugin_name.length() - plugin_extension.length());
1838#endif // OS_MACOSX
1839 }
[email protected]bf5c2ff392009-07-08 16:24:331840 }
[email protected]4a560ed2009-07-13 23:35:261841#else
1842 NOTIMPLEMENTED() << " convert plugin path to plugin name";
1843#endif
[email protected]bf5c2ff392009-07-08 16:24:331844 AddInfoBar(new SimpleAlertInfoBarDelegate(
1845 this, l10n_util::GetStringF(IDS_PLUGIN_CRASHED_PROMPT, plugin_name),
1846 NULL));
[email protected]bf5c2ff392009-07-08 16:24:331847}
1848
1849void TabContents::OnCrashedWorker() {
1850 AddInfoBar(new SimpleAlertInfoBarDelegate(
1851 this, l10n_util::GetString(IDS_WEBWORKER_CRASHED_PROMPT),
1852 NULL));
1853}
1854
1855void TabContents::OnDidGetApplicationInfo(
1856 int32 page_id,
1857 const webkit_glue::WebApplicationInfo& info) {
[email protected]ed5431872009-11-17 08:39:511858 web_app_info_ = info;
[email protected]bf5c2ff392009-07-08 16:24:331859
[email protected]ed5431872009-11-17 08:39:511860 if (delegate())
1861 delegate()->OnDidGetApplicationInfo(this, page_id);
[email protected]bf5c2ff392009-07-08 16:24:331862}
1863
[email protected]a2cf65eb2010-01-18 08:20:381864void TabContents::OnPageContents(const GURL& url,
1865 int renderer_process_id,
1866 int32 page_id,
[email protected]a8a81292010-01-21 00:32:451867 const std::wstring& contents,
1868 const std::string& language) {
[email protected]a2cf65eb2010-01-18 08:20:381869 // Don't index any https pages. People generally don't want their bank
1870 // accounts, etc. indexed on their computer, especially since some of these
1871 // things are not marked cachable.
1872 // TODO(brettw) we may want to consider more elaborate heuristics such as
1873 // the cachability of the page. We may also want to consider subframes (this
1874 // test will still index subframes if the subframe is SSL).
1875 if (!url.SchemeIsSecure()) {
1876 Profile* p = profile();
1877 if (p && !p->IsOffTheRecord()) {
1878 HistoryService* hs = p->GetHistoryService(Profile::IMPLICIT_ACCESS);
1879 if (hs)
1880 hs->SetPageContents(url, contents);
1881 }
1882 }
[email protected]a8a81292010-01-21 00:32:451883
[email protected]3b1c1812010-02-12 21:53:551884 language_state_.LanguageDetermined(language);
[email protected]a8a81292010-01-21 00:32:451885
1886 std::string lang = language;
1887 NotificationService::current()->Notify(
1888 NotificationType::TAB_LANGUAGE_DETERMINED,
[email protected]7e0831b2010-02-01 08:45:151889 Source<TabContents>(this),
[email protected]a8a81292010-01-21 00:32:451890 Details<std::string>(&lang));
[email protected]a2cf65eb2010-01-18 08:20:381891}
1892
[email protected]7e0831b2010-02-01 08:45:151893void TabContents::OnPageTranslated(int32 page_id,
1894 const std::string& original_lang,
1895 const std::string& translated_lang) {
[email protected]3b1c1812010-02-12 21:53:551896 language_state_.set_current_language(translated_lang);
1897 language_state_.set_translation_pending(false);
[email protected]7e0831b2010-02-01 08:45:151898 std::pair<std::string, std::string> lang_pair =
1899 std::make_pair(original_lang, translated_lang);
1900 NotificationService::current()->Notify(
1901 NotificationType::PAGE_TRANSLATED,
1902 Source<TabContents>(this),
1903 Details<std::pair<std::string, std::string> >(&lang_pair));
1904}
1905
[email protected]bf5c2ff392009-07-08 16:24:331906void TabContents::DidStartProvisionalLoadForFrame(
1907 RenderViewHost* render_view_host,
1908 bool is_main_frame,
1909 const GURL& url) {
1910 ProvisionalLoadDetails details(is_main_frame,
1911 controller_.IsURLInPageNavigation(url),
1912 url, std::string(), false);
1913 NotificationService::current()->Notify(
1914 NotificationType::FRAME_PROVISIONAL_LOAD_START,
1915 Source<NavigationController>(&controller_),
1916 Details<ProvisionalLoadDetails>(&details));
1917}
1918
1919void TabContents::DidStartReceivingResourceResponse(
[email protected]3dbb80b2010-02-09 22:41:201920 const ResourceRequestDetails& details) {
[email protected]bf5c2ff392009-07-08 16:24:331921 NotificationService::current()->Notify(
1922 NotificationType::RESOURCE_RESPONSE_STARTED,
1923 Source<NavigationController>(&controller()),
[email protected]3dbb80b2010-02-09 22:41:201924 Details<const ResourceRequestDetails>(&details));
[email protected]bf5c2ff392009-07-08 16:24:331925}
1926
[email protected]3dbb80b2010-02-09 22:41:201927void TabContents::DidRedirectResource(
1928 const ResourceRedirectDetails& details) {
[email protected]bf5c2ff392009-07-08 16:24:331929 NotificationService::current()->Notify(
1930 NotificationType::RESOURCE_RECEIVED_REDIRECT,
1931 Source<NavigationController>(&controller()),
[email protected]3dbb80b2010-02-09 22:41:201932 Details<const ResourceRequestDetails>(&details));
[email protected]bf5c2ff392009-07-08 16:24:331933}
1934
1935void TabContents::DidLoadResourceFromMemoryCache(
1936 const GURL& url,
1937 const std::string& frame_origin,
1938 const std::string& main_frame_origin,
1939 const std::string& security_info) {
1940 // Send out a notification that we loaded a resource from our memory cache.
1941 int cert_id = 0, cert_status = 0, security_bits = 0;
1942 SSLManager::DeserializeSecurityInfo(security_info,
1943 &cert_id, &cert_status,
1944 &security_bits);
1945 LoadFromMemoryCacheDetails details(url, frame_origin, main_frame_origin,
[email protected]8cb5d5b2010-02-09 11:36:161946 GetRenderProcessHost()->id(), cert_id,
1947 cert_status);
[email protected]bf5c2ff392009-07-08 16:24:331948
1949 NotificationService::current()->Notify(
1950 NotificationType::LOAD_FROM_MEMORY_CACHE,
1951 Source<NavigationController>(&controller_),
1952 Details<LoadFromMemoryCacheDetails>(&details));
1953}
1954
[email protected]e3d60e5d2009-09-25 21:08:291955void TabContents::DidDisplayInsecureContent() {
[email protected]09843b5f2009-09-26 08:30:591956 controller_.ssl_manager()->DidDisplayInsecureContent();
[email protected]e3d60e5d2009-09-25 21:08:291957}
1958
1959void TabContents::DidRunInsecureContent(const std::string& security_origin) {
[email protected]09843b5f2009-09-26 08:30:591960 controller_.ssl_manager()->DidRunInsecureContent(security_origin);
[email protected]e3d60e5d2009-09-25 21:08:291961}
1962
[email protected]bf5c2ff392009-07-08 16:24:331963void TabContents::DidFailProvisionalLoadWithError(
1964 RenderViewHost* render_view_host,
1965 bool is_main_frame,
1966 int error_code,
1967 const GURL& url,
1968 bool showing_repost_interstitial) {
1969 if (net::ERR_ABORTED == error_code) {
1970 // EVIL HACK ALERT! Ignore failed loads when we're showing interstitials.
1971 // This means that the interstitial won't be torn down properly, which is
1972 // bad. But if we have an interstitial, go back to another tab type, and
1973 // then load the same interstitial again, we could end up getting the first
1974 // interstitial's "failed" message (as a result of the cancel) when we're on
1975 // the second one.
1976 //
1977 // We can't tell this apart, so we think we're tearing down the current page
1978 // which will cause a crash later one. There is also some code in
1979 // RenderViewHostManager::RendererAbortedProvisionalLoad that is commented
1980 // out because of this problem.
1981 //
1982 // http://code.google.com/p/chromium/issues/detail?id=2855
1983 // Because this will not tear down the interstitial properly, if "back" is
1984 // back to another tab type, the interstitial will still be somewhat alive
1985 // in the previous tab type. If you navigate somewhere that activates the
1986 // tab with the interstitial again, you'll see a flash before the new load
1987 // commits of the interstitial page.
1988 if (showing_interstitial_page()) {
1989 LOG(WARNING) << "Discarding message during interstitial.";
1990 return;
1991 }
1992
1993 // This will discard our pending entry if we cancelled the load (e.g., if we
1994 // decided to download the file instead of load it). Only discard the
1995 // pending entry if the URLs match, otherwise the user initiated a navigate
1996 // before the page loaded so that the discard would discard the wrong entry.
1997 NavigationEntry* pending_entry = controller_.pending_entry();
1998 if (pending_entry && pending_entry->url() == url) {
1999 controller_.DiscardNonCommittedEntries();
2000 // Update the URL display.
2001 NotifyNavigationStateChanged(TabContents::INVALIDATE_URL);
2002 }
2003
2004 render_manager_.RendererAbortedProvisionalLoad(render_view_host);
2005 }
2006
2007 // Send out a notification that we failed a provisional load with an error.
2008 ProvisionalLoadDetails details(is_main_frame,
2009 controller_.IsURLInPageNavigation(url),
2010 url, std::string(), false);
2011 details.set_error_code(error_code);
2012
2013 NotificationService::current()->Notify(
2014 NotificationType::FAIL_PROVISIONAL_LOAD_WITH_ERROR,
2015 Source<NavigationController>(&controller_),
2016 Details<ProvisionalLoadDetails>(&details));
2017}
2018
2019void TabContents::DocumentLoadedInFrame() {
2020 controller_.DocumentLoadedInFrame();
2021}
2022
[email protected]0de80162010-02-03 04:52:352023void TabContents::OnContentBlocked(ContentSettingsType type) {
2024 content_blocked_[type] = true;
2025 if (delegate_)
2026 delegate_->OnBlockedContentChange(this);
2027}
2028
[email protected]8d3347f2009-07-09 22:00:212029RenderViewHostDelegate::View* TabContents::GetViewDelegate() {
[email protected]420ae012009-04-24 05:16:322030 return view_.get();
2031}
2032
[email protected]8d3347f2009-07-09 22:00:212033RenderViewHostDelegate::RendererManagement*
2034TabContents::GetRendererManagementDelegate() {
2035 return &render_manager_;
2036}
2037
[email protected]bf5c2ff392009-07-08 16:24:332038RenderViewHostDelegate::BrowserIntegration*
[email protected]8d3347f2009-07-09 22:00:212039 TabContents::GetBrowserIntegrationDelegate() {
2040 return this;
[email protected]bf5c2ff392009-07-08 16:24:332041}
2042
[email protected]8d3347f2009-07-09 22:00:212043RenderViewHostDelegate::Resource* TabContents::GetResourceDelegate() {
2044 return this;
[email protected]bf5c2ff392009-07-08 16:24:332045}
2046
[email protected]8d3347f2009-07-09 22:00:212047RenderViewHostDelegate::Save* TabContents::GetSaveDelegate() {
[email protected]420ae012009-04-24 05:16:322048 return save_package_.get(); // May be NULL, but we can return NULL.
2049}
2050
[email protected]8d3347f2009-07-09 22:00:212051RenderViewHostDelegate::Printing* TabContents::GetPrintingDelegate() {
2052 return &printing_;
2053}
2054
2055RenderViewHostDelegate::FavIcon* TabContents::GetFavIconDelegate() {
2056 return &fav_icon_helper_;
2057}
2058
[email protected]e8dbd674b2010-03-11 18:34:402059RenderViewHostDelegate::Autocomplete* TabContents::GetAutocompleteDelegate() {
[email protected]6db0def92010-03-02 19:55:452060 if (autocomplete_history_manager_.get() == NULL)
2061 autocomplete_history_manager_.reset(new AutocompleteHistoryManager(this));
2062 return autocomplete_history_manager_.get();
[email protected]bf5c2ff392009-07-08 16:24:332063}
2064
[email protected]21674df2009-10-28 19:51:212065RenderViewHostDelegate::AutoFill* TabContents::GetAutoFillDelegate() {
2066 if (autofill_manager_.get() == NULL)
2067 autofill_manager_.reset(new AutoFillManager(this));
2068 return autofill_manager_.get();
2069}
2070
[email protected]fd571bb2010-03-19 01:23:542071AutomationResourceRoutingDelegate*
2072TabContents::GetAutomationResourceRoutingDelegate() {
2073 return delegate();
2074}
2075
[email protected]ced90ae12010-02-20 02:06:162076RenderViewHostDelegate::BookmarkDrag* TabContents::GetBookmarkDragDelegate() {
2077 return bookmark_drag_;
2078}
2079
2080void TabContents::SetBookmarkDragDelegate(
2081 RenderViewHostDelegate::BookmarkDrag* bookmark_drag) {
2082 bookmark_drag_ = bookmark_drag;
2083}
2084
[email protected]93623c5d2009-12-10 21:40:322085RendererPreferences TabContents::GetRendererPrefs(Profile* profile) const {
[email protected]840b1512009-07-21 16:53:462086 return renderer_preferences_;
[email protected]80d96fa2009-06-10 22:34:512087}
2088
[email protected]57c6a652009-05-04 07:58:342089TabContents* TabContents::GetAsTabContents() {
2090 return this;
2091}
2092
[email protected]7b291f92009-08-14 05:43:532093ViewType::Type TabContents::GetRenderViewType() const {
2094 return ViewType::TAB_CONTENTS;
2095}
2096
2097int TabContents::GetBrowserWindowID() const {
2098 return controller().window_id().id();
2099}
2100
[email protected]420ae012009-04-24 05:16:322101void TabContents::RenderViewCreated(RenderViewHost* render_view_host) {
[email protected]2e4633c2009-07-09 16:58:062102 NotificationService::current()->Notify(
2103 NotificationType::RENDER_VIEW_HOST_CREATED_FOR_TAB,
2104 Source<TabContents>(this),
2105 Details<RenderViewHost>(render_view_host));
[email protected]420ae012009-04-24 05:16:322106 NavigationEntry* entry = controller_.GetActiveEntry();
2107 if (!entry)
2108 return;
2109
2110 // When we're creating views, we're still doing initial setup, so we always
2111 // use the pending DOM UI rather than any possibly existing committed one.
2112 if (render_manager_.pending_dom_ui()) {
2113 render_manager_.pending_dom_ui()->RenderViewCreated(render_view_host);
2114 }
2115
2116 if (entry->IsViewSourceMode()) {
2117 // Put the renderer in view source mode.
2118 render_view_host->Send(
2119 new ViewMsg_EnableViewSourceMode(render_view_host->routing_id()));
2120 }
[email protected]0666aef2009-05-13 19:48:082121
2122 view()->RenderViewCreated(render_view_host);
[email protected]420ae012009-04-24 05:16:322123}
2124
2125void TabContents::RenderViewReady(RenderViewHost* rvh) {
2126 if (rvh != render_view_host()) {
2127 // Don't notify the world, since this came from a renderer in the
2128 // background.
2129 return;
2130 }
2131
2132 NotifyConnected();
[email protected]9d8a4642009-07-29 17:25:302133 bool was_crashed = is_crashed();
[email protected]420ae012009-04-24 05:16:322134 SetIsCrashed(false);
[email protected]9d8a4642009-07-29 17:25:302135
2136 // Restore the focus to the tab (otherwise the focus will be on the top
2137 // window).
2138 if (was_crashed && !FocusLocationBarByDefault())
2139 Focus();
[email protected]420ae012009-04-24 05:16:322140}
2141
2142void TabContents::RenderViewGone(RenderViewHost* rvh) {
2143 // Ask the print preview if this renderer was valuable.
2144 if (!printing_.OnRenderViewGone(rvh))
2145 return;
2146 if (rvh != render_view_host()) {
2147 // The pending page's RenderViewHost is gone.
2148 return;
2149 }
2150
2151 SetIsLoading(false, NULL);
2152 NotifyDisconnected();
2153 SetIsCrashed(true);
2154
[email protected]ff9efc62010-02-18 22:10:542155 // Remove all infobars.
[email protected]e5430c92010-02-18 22:39:212156 for (int i = infobar_delegate_count() - 1; i >=0 ; --i)
[email protected]ff9efc62010-02-18 22:10:542157 RemoveInfoBar(GetInfoBarDelegateAt(i));
2158
[email protected]8e537302009-06-10 21:19:582159 // Tell the view that we've crashed so it can prepare the sad tab page.
[email protected]cf68b092010-01-15 20:05:552160 // Only do this if we're not in browser shutdown, so that TabContents
2161 // objects that are not in a browser (e.g., HTML dialogs) and thus are
2162 // visible do not flash a sad tab page.
2163 if (browser_shutdown::GetShutdownType() == browser_shutdown::NOT_VALID)
2164 view_->OnTabCrashed();
[email protected]420ae012009-04-24 05:16:322165
2166 // Hide any visible hung renderer warning for this web contents' process.
[email protected]e7b8d832009-11-20 22:41:002167 hung_renderer_dialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:322168}
2169
[email protected]2e4633c2009-07-09 16:58:062170void TabContents::RenderViewDeleted(RenderViewHost* rvh) {
2171 NotificationService::current()->Notify(
2172 NotificationType::RENDER_VIEW_HOST_DELETED,
2173 Source<TabContents>(this),
2174 Details<RenderViewHost>(rvh));
[email protected]fb7b79f72009-11-06 18:00:482175 render_manager_.RenderViewDeleted(rvh);
[email protected]2e4633c2009-07-09 16:58:062176}
2177
[email protected]420ae012009-04-24 05:16:322178void TabContents::DidNavigate(RenderViewHost* rvh,
2179 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]8030f012009-09-25 18:09:372180 int extra_invalidate_flags = 0;
2181
2182 if (PageTransition::IsMainFrame(params.transition)) {
[email protected]20ede072009-10-06 00:28:352183 bool was_bookmark_bar_visible = ShouldShowBookmarkBar();
[email protected]8030f012009-09-25 18:09:372184 bool was_extension_shelf_visible = IsExtensionShelfAlwaysVisible();
2185
[email protected]420ae012009-04-24 05:16:322186 render_manager_.DidNavigateMainFrame(rvh);
2187
[email protected]20ede072009-10-06 00:28:352188 if (was_bookmark_bar_visible != ShouldShowBookmarkBar())
[email protected]8030f012009-09-25 18:09:372189 extra_invalidate_flags |= INVALIDATE_BOOKMARK_BAR;
2190 if (was_extension_shelf_visible != IsExtensionShelfAlwaysVisible())
2191 extra_invalidate_flags |= INVALIDATE_EXTENSION_SHELF;
2192 }
2193
[email protected]420ae012009-04-24 05:16:322194 // Update the site of the SiteInstance if it doesn't have one yet.
2195 if (!GetSiteInstance()->has_site())
2196 GetSiteInstance()->SetSite(params.url);
2197
2198 // Need to update MIME type here because it's referred to in
2199 // UpdateNavigationCommands() called by RendererDidNavigate() to
2200 // determine whether or not to enable the encoding menu.
2201 // It's updated only for the main frame. For a subframe,
2202 // RenderView::UpdateURL does not set params.contents_mime_type.
2203 // (see http://code.google.com/p/chromium/issues/detail?id=2929 )
2204 // TODO(jungshik): Add a test for the encoding menu to avoid
2205 // regressing it again.
2206 if (PageTransition::IsMainFrame(params.transition))
2207 contents_mime_type_ = params.contents_mime_type;
2208
2209 NavigationController::LoadCommittedDetails details;
[email protected]8030f012009-09-25 18:09:372210 bool did_navigate = controller_.RendererDidNavigate(
2211 params, extra_invalidate_flags, &details);
[email protected]3e90d4a2009-07-03 17:38:392212
2213 // Update history. Note that this needs to happen after the entry is complete,
2214 // which WillNavigate[Main,Sub]Frame will do before this function is called.
[email protected]2e29d922009-10-22 22:13:352215 if (params.should_update_history &&
2216 (!delegate() || delegate()->ShouldAddNavigationsToHistory())) {
[email protected]3e90d4a2009-07-03 17:38:392217 // Most of the time, the displayURL matches the loaded URL, but for about:
2218 // URLs, we use a data: URL as the real value. We actually want to save
2219 // the about: URL to the history db and keep the data: URL hidden. This is
2220 // what the TabContents' URL getter does.
2221 UpdateHistoryForNavigation(GetURL(), details, params);
2222 }
2223
[email protected]76543b92009-08-31 17:27:452224 if (!did_navigate)
2225 return; // No navigation happened.
[email protected]420ae012009-04-24 05:16:322226
2227 // DO NOT ADD MORE STUFF TO THIS FUNCTION! Your component should either listen
2228 // for the appropriate notification (best) or you can add it to
2229 // DidNavigateMainFramePostCommit / DidNavigateAnyFramePostCommit (only if
2230 // necessary, please).
2231
2232 // Run post-commit tasks.
2233 if (details.is_main_frame)
2234 DidNavigateMainFramePostCommit(details, params);
2235 DidNavigateAnyFramePostCommit(rvh, details, params);
2236}
2237
2238void TabContents::UpdateState(RenderViewHost* rvh,
2239 int32 page_id,
2240 const std::string& state) {
2241 DCHECK(rvh == render_view_host());
2242
2243 // We must be prepared to handle state updates for any page, these occur
2244 // when the user is scrolling and entering form data, as well as when we're
2245 // leaving a page, in which case our state may have already been moved to
2246 // the next page. The navigation controller will look up the appropriate
2247 // NavigationEntry and update it when it is notified via the delegate.
2248
2249 int entry_index = controller_.GetEntryIndexWithPageID(
2250 GetSiteInstance(), page_id);
2251 if (entry_index < 0)
2252 return;
2253 NavigationEntry* entry = controller_.GetEntryAtIndex(entry_index);
2254
2255 if (state == entry->content_state())
2256 return; // Nothing to update.
2257 entry->set_content_state(state);
2258 controller_.NotifyEntryChanged(entry, entry_index);
2259}
2260
2261void TabContents::UpdateTitle(RenderViewHost* rvh,
2262 int32 page_id, const std::wstring& title) {
2263 // If we have a title, that's a pretty good indication that we've started
2264 // getting useful data.
2265 SetNotWaitingForResponse();
2266
2267 DCHECK(rvh == render_view_host());
2268 NavigationEntry* entry = controller_.GetEntryWithPageID(GetSiteInstance(),
2269 page_id);
2270 if (!entry || !UpdateTitleForEntry(entry, title))
2271 return;
2272
2273 // Broadcast notifications when the UI should be updated.
2274 if (entry == controller_.GetEntryAtOffset(0))
[email protected]f1cd5e82009-10-23 17:19:032275 NotifyNavigationStateChanged(INVALIDATE_TITLE);
[email protected]420ae012009-04-24 05:16:322276}
2277
[email protected]420ae012009-04-24 05:16:322278void TabContents::UpdateEncoding(RenderViewHost* render_view_host,
[email protected]41fc0322009-09-04 22:23:402279 const std::string& encoding) {
[email protected]420ae012009-04-24 05:16:322280 set_encoding(encoding);
2281}
2282
2283void TabContents::UpdateTargetURL(int32 page_id, const GURL& url) {
2284 if (delegate())
2285 delegate()->UpdateTargetURL(this, url);
2286}
2287
2288void TabContents::UpdateThumbnail(const GURL& url,
2289 const SkBitmap& bitmap,
2290 const ThumbnailScore& score) {
2291 // Tell History about this thumbnail
[email protected]449478302009-06-09 20:04:282292 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kThumbnailStore)) {
[email protected]72d0fc72009-07-09 17:30:492293 if (!profile()->IsOffTheRecord())
[email protected]42339402009-07-17 17:27:192294 profile()->GetThumbnailStore()->SetPageThumbnail(url, bitmap,
2295 score, true);
[email protected]449478302009-06-09 20:04:282296 } else {
2297 HistoryService* hs;
2298 if (!profile()->IsOffTheRecord() &&
2299 (hs = profile()->GetHistoryService(Profile::IMPLICIT_ACCESS))) {
2300 hs->SetPageThumbnail(url, bitmap, score);
2301 }
[email protected]420ae012009-04-24 05:16:322302 }
2303}
2304
[email protected]8922e1f2009-10-03 05:01:262305void TabContents::UpdateInspectorSettings(const std::string& raw_settings) {
[email protected]0df30122009-06-03 12:13:082306 profile()->GetPrefs()->SetString(prefs::kWebKitInspectorSettings,
[email protected]8922e1f2009-10-03 05:01:262307 UTF8ToWide(raw_settings));
[email protected]0df30122009-06-03 12:13:082308}
2309
[email protected]420ae012009-04-24 05:16:322310void TabContents::Close(RenderViewHost* rvh) {
[email protected]07707302009-11-06 00:50:292311 // The UI may be in an event-tracking loop, such as between the
2312 // mouse-down and mouse-up in text selection or a button click.
2313 // Defer the close until after tracking is complete, so that we
2314 // don't free objects out from under the UI.
2315 // TODO(shess): This could probably be integrated with the
2316 // IsDoingDrag() test below. Punting for now because I need more
2317 // research to understand how this impacts platforms other than Mac.
2318 // TODO(shess): This could get more fine-grained. For instance,
2319 // closing a tab in another window while selecting text in the
2320 // current window's Omnibox should be just fine.
2321 if (view()->IsEventTracking()) {
2322 view()->CloseTabAfterEventTracking();
2323 return;
2324 }
2325
[email protected]24a4d1062009-07-10 23:10:422326 // If we close the tab while we're in the middle of a drag, we'll crash.
2327 // Instead, cancel the drag and close it as soon as the drag ends.
2328 if (view()->IsDoingDrag()) {
2329 view()->CancelDragAndCloseTab();
2330 return;
2331 }
2332
[email protected]420ae012009-04-24 05:16:322333 // Ignore this if it comes from a RenderViewHost that we aren't showing.
2334 if (delegate() && rvh == render_view_host())
2335 delegate()->CloseContents(this);
2336}
2337
2338void TabContents::RequestMove(const gfx::Rect& new_bounds) {
2339 if (delegate() && delegate()->IsPopup(this))
2340 delegate()->MoveContents(this, new_bounds);
2341}
2342
[email protected]7ab1e7d62009-10-14 23:32:012343void TabContents::DidStartLoading() {
[email protected]420ae012009-04-24 05:16:322344 SetIsLoading(true, NULL);
2345}
2346
[email protected]7ab1e7d62009-10-14 23:32:012347void TabContents::DidStopLoading() {
[email protected]420ae012009-04-24 05:16:322348 scoped_ptr<LoadNotificationDetails> details;
2349
2350 NavigationEntry* entry = controller_.GetActiveEntry();
2351 // An entry may not exist for a stop when loading an initial blank page or
2352 // if an iframe injected by script into a blank page finishes loading.
2353 if (entry) {
[email protected]420ae012009-04-24 05:16:322354 base::TimeDelta elapsed = base::TimeTicks::Now() - current_load_start_;
2355
2356 details.reset(new LoadNotificationDetails(
[email protected]ebe89e062009-08-13 23:16:542357 entry->virtual_url(),
[email protected]420ae012009-04-24 05:16:322358 entry->transition_type(),
2359 elapsed,
2360 &controller_,
2361 controller_.GetCurrentEntryIndex()));
2362 }
2363
2364 // Tell PasswordManager we've finished a page load, which serves as a
2365 // green light to save pending passwords and reset itself.
2366 GetPasswordManager()->DidStopLoading();
2367
2368 SetIsLoading(false, details.get());
2369}
2370
[email protected]420ae012009-04-24 05:16:322371void TabContents::DidRedirectProvisionalLoad(int32 page_id,
2372 const GURL& source_url,
2373 const GURL& target_url) {
2374 NavigationEntry* entry;
2375 if (page_id == -1)
2376 entry = controller_.pending_entry();
2377 else
2378 entry = controller_.GetEntryWithPageID(GetSiteInstance(), page_id);
2379 if (!entry || entry->url() != source_url)
2380 return;
2381 entry->set_url(target_url);
2382}
2383
[email protected]420ae012009-04-24 05:16:322384void TabContents::RequestOpenURL(const GURL& url, const GURL& referrer,
2385 WindowOpenDisposition disposition) {
2386 if (render_manager_.dom_ui()) {
2387 // When we're a DOM UI, it will provide a page transition type for us (this
2388 // is so the new tab page can specify AUTO_BOOKMARK for automatically
2389 // generated suggestions).
2390 //
2391 // Note also that we hide the referrer for DOM UI pages. We don't really
[email protected]60e448982009-05-06 04:21:162392 // want web sites to see a referrer of "chrome://blah" (and some
2393 // chrome: URLs might have search terms or other stuff we don't want to
[email protected]420ae012009-04-24 05:16:322394 // send to the site), so we send no referrer.
2395 OpenURL(url, GURL(), disposition,
2396 render_manager_.dom_ui()->link_transition_type());
2397 } else {
2398 OpenURL(url, referrer, disposition, PageTransition::LINK);
2399 }
2400}
2401
[email protected]1ee614862009-07-06 20:29:182402void TabContents::DomOperationResponse(const std::string& json_string,
2403 int automation_id) {
2404}
2405
[email protected]420ae012009-04-24 05:16:322406void TabContents::ProcessDOMUIMessage(const std::string& message,
[email protected]e4dad9fb2009-10-06 18:15:582407 const Value* content,
[email protected]811bfe32009-07-01 08:46:252408 int request_id,
2409 bool has_callback) {
[email protected]420ae012009-04-24 05:16:322410 if (!render_manager_.dom_ui()) {
[email protected]c2e74fe82009-09-03 17:57:442411 // This can happen if someone uses window.open() to open an extension URL
2412 // from a non-extension context.
2413 render_view_host()->BlockExtensionRequest(request_id);
[email protected]420ae012009-04-24 05:16:322414 return;
2415 }
[email protected]811bfe32009-07-01 08:46:252416 render_manager_.dom_ui()->ProcessDOMUIMessage(message, content, request_id,
2417 has_callback);
[email protected]420ae012009-04-24 05:16:322418}
2419
2420void TabContents::ProcessExternalHostMessage(const std::string& message,
2421 const std::string& origin,
2422 const std::string& target) {
2423 if (delegate())
2424 delegate()->ForwardMessageToExternalHost(message, origin, target);
2425}
2426
[email protected]420ae012009-04-24 05:16:322427void TabContents::RunFileChooser(bool multiple_files,
2428 const string16& title,
2429 const FilePath& default_file) {
2430 if (!select_file_dialog_.get())
2431 select_file_dialog_ = SelectFileDialog::Create(this);
2432 SelectFileDialog::Type dialog_type =
2433 multiple_files ? SelectFileDialog::SELECT_OPEN_MULTI_FILE :
2434 SelectFileDialog::SELECT_OPEN_FILE;
2435 select_file_dialog_->SelectFile(dialog_type, title, default_file,
2436 NULL, 0, FILE_PATH_LITERAL(""),
2437 view_->GetTopLevelNativeWindow(), NULL);
2438}
2439
2440void TabContents::RunJavaScriptMessage(
2441 const std::wstring& message,
2442 const std::wstring& default_prompt,
2443 const GURL& frame_url,
2444 const int flags,
2445 IPC::Message* reply_msg,
2446 bool* did_suppress_message) {
2447 // Suppress javascript messages when requested and when inside a constrained
2448 // popup window (because that activates them and breaks them out of the
2449 // constrained window jail).
[email protected]91ed49a2009-07-07 21:29:552450 // Also suppress messages when showing an interstitial. The interstitial is
2451 // shown over the previous page, we don't want the hidden page dialogs to
2452 // interfere with the interstitial.
2453 bool suppress_this_message = suppress_javascript_messages_ ||
2454 showing_interstitial_page();
[email protected]420ae012009-04-24 05:16:322455 if (delegate())
2456 suppress_this_message |=
[email protected]b6c874582009-05-08 19:38:312457 (delegate()->GetConstrainingContents(this) != this);
[email protected]420ae012009-04-24 05:16:322458
2459 *did_suppress_message = suppress_this_message;
2460
2461 if (!suppress_this_message) {
2462 base::TimeDelta time_since_last_message(
2463 base::TimeTicks::Now() - last_javascript_message_dismissal_);
2464 bool show_suppress_checkbox = false;
2465 // Show a checkbox offering to suppress further messages if this message is
2466 // being displayed within kJavascriptMessageExpectedDelay of the last one.
2467 if (time_since_last_message <
2468 base::TimeDelta::FromMilliseconds(kJavascriptMessageExpectedDelay))
2469 show_suppress_checkbox = true;
2470
[email protected]d82ed61e2009-06-16 02:46:222471 RunJavascriptMessageBox(this, frame_url, flags, message, default_prompt,
[email protected]420ae012009-04-24 05:16:322472 show_suppress_checkbox, reply_msg);
2473 } else {
2474 // If we are suppressing messages, just reply as is if the user immediately
2475 // pressed "Cancel".
[email protected]beb440c2009-11-06 04:08:542476 OnMessageBoxClosed(reply_msg, false, std::wstring());
[email protected]420ae012009-04-24 05:16:322477 }
2478}
2479
2480void TabContents::RunBeforeUnloadConfirm(const std::wstring& message,
2481 IPC::Message* reply_msg) {
[email protected]7ab1e7d62009-10-14 23:32:012482 is_showing_before_unload_dialog_ = true;
[email protected]d82ed61e2009-06-16 02:46:222483 RunBeforeUnloadDialog(this, message, reply_msg);
[email protected]420ae012009-04-24 05:16:322484}
2485
2486void TabContents::ShowModalHTMLDialog(const GURL& url, int width, int height,
2487 const std::string& json_arguments,
2488 IPC::Message* reply_msg) {
2489 if (delegate()) {
2490 HtmlDialogUIDelegate* dialog_delegate =
2491 new ModalHtmlDialogDelegate(url, width, height, json_arguments,
[email protected]d82ed61e2009-06-16 02:46:222492 reply_msg, this);
[email protected]420ae012009-04-24 05:16:322493 delegate()->ShowHtmlDialog(dialog_delegate, NULL);
2494 }
2495}
2496
2497void TabContents::PasswordFormsSeen(
[email protected]ed3fb032009-06-16 19:50:562498 const std::vector<webkit_glue::PasswordForm>& forms) {
[email protected]420ae012009-04-24 05:16:322499 GetPasswordManager()->PasswordFormsSeen(forms);
2500}
2501
[email protected]420ae012009-04-24 05:16:322502// Checks to see if we should generate a keyword based on the OSDD, and if
2503// necessary uses TemplateURLFetcher to download the OSDD and create a keyword.
2504void TabContents::PageHasOSDD(RenderViewHost* render_view_host,
2505 int32 page_id, const GURL& url,
2506 bool autodetected) {
2507 // Make sure page_id is the current page, and the TemplateURLModel is loaded.
2508 DCHECK(url.is_valid());
2509 if (!IsActiveEntry(page_id))
2510 return;
2511 TemplateURLModel* url_model = profile()->GetTemplateURLModel();
2512 if (!url_model)
2513 return;
2514 if (!url_model->loaded()) {
2515 url_model->Load();
2516 return;
2517 }
2518 if (!profile()->GetTemplateURLFetcher())
2519 return;
2520
2521 if (profile()->IsOffTheRecord())
2522 return;
2523
2524 const NavigationEntry* entry = controller_.GetLastCommittedEntry();
2525 DCHECK(entry);
2526
2527 const NavigationEntry* base_entry = entry;
2528 if (IsFormSubmit(base_entry)) {
2529 // If the current page is a form submit, find the last page that was not
2530 // a form submit and use its url to generate the keyword from.
2531 int index = controller_.last_committed_entry_index() - 1;
2532 while (index >= 0 && IsFormSubmit(controller_.GetEntryAtIndex(index)))
2533 index--;
2534 if (index >= 0)
2535 base_entry = controller_.GetEntryAtIndex(index);
2536 else
2537 base_entry = NULL;
2538 }
2539
2540 // We want to use the user typed URL if available since that represents what
2541 // the user typed to get here, and fall back on the regular URL if not.
2542 if (!base_entry)
2543 return;
2544 GURL keyword_url = base_entry->user_typed_url().is_valid() ?
2545 base_entry->user_typed_url() : base_entry->url();
2546 if (!keyword_url.is_valid())
2547 return;
2548 std::wstring keyword = TemplateURLModel::GenerateKeyword(keyword_url,
2549 autodetected);
[email protected]a88dbdd52010-01-15 19:25:532550
2551 // For JS added OSDD empty keyword is OK because we will generate keyword
2552 // later from OSDD content.
2553 if (autodetected && keyword.empty())
[email protected]420ae012009-04-24 05:16:322554 return;
2555 const TemplateURL* template_url =
2556 url_model->GetTemplateURLForKeyword(keyword);
2557 if (template_url && (!template_url->safe_for_autoreplace() ||
2558 template_url->originating_url() == url)) {
2559 // Either there is a user created TemplateURL for this keyword, or the
2560 // keyword has the same OSDD url and we've parsed it.
2561 return;
2562 }
2563
2564 // Download the OpenSearch description document. If this is successful a
2565 // new keyword will be created when done.
[email protected]420ae012009-04-24 05:16:322566 profile()->GetTemplateURLFetcher()->ScheduleDownload(
2567 keyword,
2568 url,
2569 base_entry->favicon().url(),
[email protected]d0cc9fb52009-06-23 02:26:162570 this,
[email protected]420ae012009-04-24 05:16:322571 autodetected);
2572}
2573
[email protected]420ae012009-04-24 05:16:322574GURL TabContents::GetAlternateErrorPageURL() const {
2575 GURL url;
2576 // Disable alternate error pages when in OffTheRecord/Incognito mode.
2577 if (profile()->IsOffTheRecord())
2578 return url;
2579
2580 PrefService* prefs = profile()->GetPrefs();
2581 DCHECK(prefs);
2582 if (prefs->GetBoolean(prefs::kAlternateErrorPagesEnabled)) {
[email protected]a33457b82009-09-21 23:07:152583 url = google_util::AppendGoogleLocaleParam(
2584 GURL(google_util::kLinkDoctorBaseURL));
[email protected]420ae012009-04-24 05:16:322585 url = google_util::AppendGoogleTLDParam(url);
2586 }
2587 return url;
2588}
2589
2590WebPreferences TabContents::GetWebkitPrefs() {
[email protected]6c32ce72010-03-08 05:18:062591 Profile* profile = render_view_host()->process()->profile();
[email protected]7ccddb8c2009-08-04 17:36:552592 bool is_dom_ui = false;
[email protected]6c32ce72010-03-08 05:18:062593 return RenderViewHostDelegateHelper::GetWebkitPrefs(profile, is_dom_ui);
[email protected]420ae012009-04-24 05:16:322594}
2595
[email protected]fa1cf0b82010-01-15 21:49:442596void TabContents::OnIgnoredUIEvent() {
2597 if (constrained_window_count()) {
2598 ConstrainedWindow* window = *constrained_window_begin();
2599 window->FocusConstrainedWindow();
2600 }
2601}
2602
[email protected]420ae012009-04-24 05:16:322603void TabContents::OnJSOutOfMemory() {
2604 AddInfoBar(new SimpleAlertInfoBarDelegate(
2605 this, l10n_util::GetString(IDS_JS_OUT_OF_MEMORY_PROMPT), NULL));
2606}
2607
[email protected]420ae012009-04-24 05:16:322608void TabContents::OnCrossSiteResponse(int new_render_process_host_id,
2609 int new_request_id) {
[email protected]57c6a652009-05-04 07:58:342610 // Allows the TabContents to react when a cross-site response is ready to be
[email protected]420ae012009-04-24 05:16:322611 // delivered to a pending RenderViewHost. We must first run the onunload
2612 // handler of the old RenderViewHost before we can allow it to proceed.
2613 render_manager_.OnCrossSiteResponse(new_render_process_host_id,
2614 new_request_id);
2615}
2616
2617bool TabContents::CanBlur() const {
2618 return delegate() ? delegate()->CanBlur() : true;
2619}
2620
2621gfx::Rect TabContents::GetRootWindowResizerRect() const {
2622 if (delegate())
2623 return delegate()->GetRootWindowResizerRect();
2624 return gfx::Rect();
2625}
2626
2627void TabContents::RendererUnresponsive(RenderViewHost* rvh,
2628 bool is_during_unload) {
2629 if (is_during_unload) {
2630 // Hang occurred while firing the beforeunload/unload handler.
2631 // Pretend the handler fired so tab closing continues as if it had.
[email protected]829e7612009-04-25 01:15:112632 rvh->set_sudden_termination_allowed(true);
[email protected]420ae012009-04-24 05:16:322633
2634 if (!render_manager_.ShouldCloseTabOnUnresponsiveRenderer())
2635 return;
2636
2637 // If the tab hangs in the beforeunload/unload handler there's really
2638 // nothing we can do to recover. Pretend the unload listeners have
2639 // all fired and close the tab. If the hang is in the beforeunload handler
2640 // then the user will not have the option of cancelling the close.
2641 Close(rvh);
2642 return;
2643 }
2644
2645 if (render_view_host() && render_view_host()->IsRenderViewLive())
[email protected]e7b8d832009-11-20 22:41:002646 hung_renderer_dialog::ShowForTabContents(this);
[email protected]420ae012009-04-24 05:16:322647}
2648
2649void TabContents::RendererResponsive(RenderViewHost* render_view_host) {
[email protected]e7b8d832009-11-20 22:41:002650 hung_renderer_dialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:322651}
2652
2653void TabContents::LoadStateChanged(const GURL& url,
[email protected]094e5b22009-09-25 04:23:562654 net::LoadState load_state,
2655 uint64 upload_position,
2656 uint64 upload_size) {
[email protected]420ae012009-04-24 05:16:322657 load_state_ = load_state;
[email protected]094e5b22009-09-25 04:23:562658 upload_position_ = upload_position;
2659 upload_size_ = upload_size;
[email protected]d686e812009-06-03 19:10:292660 std::wstring languages =
2661 profile()->GetPrefs()->GetString(prefs::kAcceptLanguages);
[email protected]d686e812009-06-03 19:10:292662 std::string host = url.host();
[email protected]ce85f602009-11-07 01:34:532663 load_state_host_ =
2664 net::IDNToUnicode(host.c_str(), host.size(), languages, NULL);
[email protected]420ae012009-04-24 05:16:322665 if (load_state_ == net::LOAD_STATE_READING_RESPONSE)
2666 SetNotWaitingForResponse();
2667 if (is_loading())
[email protected]c9cd2222009-05-06 05:16:502668 NotifyNavigationStateChanged(INVALIDATE_LOAD | INVALIDATE_TAB);
[email protected]420ae012009-04-24 05:16:322669}
2670
[email protected]1ee614862009-07-06 20:29:182671bool TabContents::IsExternalTabContainer() const {
2672 if (!delegate())
2673 return false;
2674
2675 return delegate()->IsExternalTabContainer();
2676}
2677
[email protected]1ee614862009-07-06 20:29:182678void TabContents::DidInsertCSS() {
2679 // This RVHDelegate function is used for extensions and not us.
[email protected]420ae012009-04-24 05:16:322680}
2681
[email protected]130efb02009-09-18 18:54:352682void TabContents::FocusedNodeChanged() {
2683 NotificationService::current()->Notify(
2684 NotificationType::FOCUS_CHANGED_IN_PAGE,
2685 Source<RenderViewHost>(render_view_host()),
2686 NotificationService::NoDetails());
2687}
2688
[email protected]420ae012009-04-24 05:16:322689void TabContents::FileSelected(const FilePath& path,
2690 int index, void* params) {
2691 render_view_host()->FileSelected(path);
2692}
2693
2694void TabContents::MultiFilesSelected(const std::vector<FilePath>& files,
2695 void* params) {
2696 render_view_host()->MultiFilesSelected(files);
2697}
2698
2699void TabContents::FileSelectionCanceled(void* params) {
2700 // If the user cancels choosing a file to upload we pass back an
2701 // empty vector.
2702 render_view_host()->MultiFilesSelected(std::vector<FilePath>());
2703}
2704
2705void TabContents::BeforeUnloadFiredFromRenderManager(
2706 bool proceed,
2707 bool* proceed_to_fire_unload) {
2708 if (delegate())
2709 delegate()->BeforeUnloadFired(this, proceed, proceed_to_fire_unload);
2710}
2711
2712void TabContents::UpdateRenderViewSizeForRenderManager() {
2713 // TODO(brettw) this is a hack. See TabContentsView::SizeContents.
[email protected]0323ee42010-02-17 22:03:262714 gfx::Size size = view_->GetContainerSize();
2715 // 0x0 isn't a valid window size (minimal window size is 1x1) but it may be
2716 // here during container initialization and normal window size will be set
2717 // later. In case of tab duplication this resizing to 0x0 prevents setting
2718 // normal size later so just ignore it.
2719 if (!size.IsEmpty())
2720 view_->SizeContents(size);
[email protected]420ae012009-04-24 05:16:322721}
2722
2723DOMUI* TabContents::CreateDOMUIForRenderManager(const GURL& url) {
[email protected]d82ed61e2009-06-16 02:46:222724 return DOMUIFactory::CreateDOMUIForURL(this, url);
[email protected]420ae012009-04-24 05:16:322725}
2726
2727NavigationEntry*
2728TabContents::GetLastCommittedNavigationEntryForRenderManager() {
2729 return controller_.GetLastCommittedEntry();
2730}
2731
2732bool TabContents::CreateRenderViewForRenderManager(
2733 RenderViewHost* render_view_host) {
[email protected]811bfe32009-07-01 08:46:252734 // If the pending navigation is to a DOMUI, tell the RenderView about any
2735 // bindings it will need enabled.
[email protected]420ae012009-04-24 05:16:322736 if (render_manager_.pending_dom_ui())
[email protected]811bfe32009-07-01 08:46:252737 render_view_host->AllowBindings(
2738 render_manager_.pending_dom_ui()->bindings());
[email protected]1adff062009-06-02 18:39:552739
[email protected]420ae012009-04-24 05:16:322740 RenderWidgetHostView* rwh_view = view_->CreateViewForWidget(render_view_host);
[email protected]1a98a932009-11-17 00:12:522741
2742 scoped_refptr<URLRequestContextGetter> request_context = request_context_;
2743 if (!request_context.get())
2744 request_context = profile()->GetRequestContext();
2745
2746 if (!render_view_host->CreateRenderView(request_context))
[email protected]420ae012009-04-24 05:16:322747 return false;
2748
2749 // Now that the RenderView has been created, we need to tell it its size.
2750 rwh_view->SetSize(view_->GetContainerSize());
2751
2752 UpdateMaxPageIDIfNecessary(render_view_host->site_instance(),
2753 render_view_host);
2754 return true;
2755}
2756
2757void TabContents::Observe(NotificationType type,
2758 const NotificationSource& source,
2759 const NotificationDetails& details) {
2760 switch (type.value) {
2761 case NotificationType::BOOKMARK_MODEL_LOADED:
2762 // BookmarkModel finished loading, fall through to update starred state.
2763 case NotificationType::URLS_STARRED: {
2764 // Somewhere, a URL has been starred.
2765 // Ignore notifications for profiles other than our current one.
2766 Profile* source_profile = Source<Profile>(source).ptr();
2767 if (!source_profile || !source_profile->IsSameProfile(profile()))
2768 return;
2769
2770 UpdateStarredStateForCurrentURL();
2771 break;
2772 }
2773 case NotificationType::PREF_CHANGED: {
2774 std::wstring* pref_name_in = Details<std::wstring>(details).ptr();
2775 DCHECK(Source<PrefService>(source).ptr() == profile()->GetPrefs());
2776 if (*pref_name_in == prefs::kAlternateErrorPagesEnabled) {
2777 UpdateAlternateErrorPageURL();
2778 } else if (*pref_name_in == prefs::kDefaultCharset ||
2779 StartsWithASCII(WideToUTF8(*pref_name_in), "webkit.webprefs.", true)
2780 ) {
2781 UpdateWebPreferences();
2782 } else {
2783 NOTREACHED() << "unexpected pref change notification" << *pref_name_in;
2784 }
2785 break;
2786 }
2787 case NotificationType::RENDER_WIDGET_HOST_DESTROYED:
2788 view_->RenderWidgetHostDestroyed(Source<RenderWidgetHost>(source).ptr());
2789 break;
2790
2791 case NotificationType::NAV_ENTRY_COMMITTED: {
2792 DCHECK(&controller_ == Source<NavigationController>(source).ptr());
2793
2794 NavigationController::LoadCommittedDetails& committed_details =
2795 *(Details<NavigationController::LoadCommittedDetails>(details).ptr());
2796 ExpireInfoBars(committed_details);
2797 break;
2798 }
2799
[email protected]d3ba77272009-09-03 00:06:092800#if defined(OS_LINUX)
2801 case NotificationType::BROWSER_THEME_CHANGED: {
[email protected]7ff431e2010-01-07 18:18:542802 renderer_preferences_util::UpdateFromSystemSettings(
2803 &renderer_preferences_, profile());
[email protected]d3ba77272009-09-03 00:06:092804 render_view_host()->SyncRendererPrefs();
2805 break;
2806 }
2807#endif
2808
[email protected]6c32ce72010-03-08 05:18:062809 case NotificationType::USER_STYLE_SHEET_UPDATED:
2810 UpdateWebPreferences();
2811 break;
2812
[email protected]79580c62010-02-02 02:36:252813 case NotificationType::CONTENT_SETTINGS_CHANGED: {
2814 Details<HostContentSettingsMap::ContentSettingsDetails>
2815 settings_details(details);
[email protected]79580c62010-02-02 02:36:252816 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]da0c8e92010-02-09 23:25:492817 GURL entry_url;
2818 std::string host;
2819 if (entry) {
2820 entry_url = entry->url();
2821 host = entry_url.host();
2822 }
[email protected]79580c62010-02-02 02:36:252823 Source<HostContentSettingsMap> content_settings(source);
2824 if (settings_details.ptr()->host().empty() ||
2825 settings_details.ptr()->host() == host) {
2826 render_view_host()->SendContentSettings(host,
[email protected]da0c8e92010-02-09 23:25:492827 content_settings.ptr()->GetContentSettings(entry_url));
[email protected]79580c62010-02-02 02:36:252828 }
2829 break;
2830 }
2831
[email protected]420ae012009-04-24 05:16:322832 default:
2833 NOTREACHED();
2834 }
2835}
[email protected]aacd7cc2009-09-02 14:51:102836
[email protected]beb440c2009-11-06 04:08:542837std::wstring TabContents::GetMessageBoxTitle(const GURL& frame_url,
2838 bool is_alert) {
2839 if (!frame_url.has_host())
2840 return l10n_util::GetString(
2841 is_alert ? IDS_JAVASCRIPT_ALERT_DEFAULT_TITLE
2842 : IDS_JAVASCRIPT_MESSAGEBOX_DEFAULT_TITLE);
2843
2844 // We really only want the scheme, hostname, and port.
2845 GURL::Replacements replacements;
2846 replacements.ClearUsername();
2847 replacements.ClearPassword();
2848 replacements.ClearPath();
2849 replacements.ClearQuery();
2850 replacements.ClearRef();
2851 GURL clean_url = frame_url.ReplaceComponents(replacements);
2852
2853 // TODO(brettw) it should be easier than this to do the correct language
2854 // handling without getting the accept language from the profile.
2855 std::wstring base_address = gfx::ElideUrl(clean_url, gfx::Font(), 0,
2856 profile()->GetPrefs()->GetString(prefs::kAcceptLanguages));
2857 // Force URL to have LTR directionality.
2858 if (l10n_util::GetTextDirection() == l10n_util::RIGHT_TO_LEFT)
2859 l10n_util::WrapStringWithLTRFormatting(&base_address);
2860
2861 return l10n_util::GetStringF(
2862 is_alert ? IDS_JAVASCRIPT_ALERT_TITLE : IDS_JAVASCRIPT_MESSAGEBOX_TITLE,
2863 base_address);
2864}
2865
2866gfx::NativeWindow TabContents::GetMessageBoxRootWindow() {
2867 return view_->GetTopLevelNativeWindow();
2868}
2869
2870void TabContents::OnMessageBoxClosed(IPC::Message* reply_msg,
2871 bool success,
2872 const std::wstring& prompt) {
2873 last_javascript_message_dismissal_ = base::TimeTicks::Now();
2874 if (is_showing_before_unload_dialog_ && !success) {
2875 // If a beforeunload dialog is canceled, we need to stop the throbber from
2876 // spinning, since we forced it to start spinning in Navigate.
2877 DidStopLoading();
2878
2879 tab_close_start_time_ = base::TimeTicks();
2880 }
2881 is_showing_before_unload_dialog_ = false;
2882 render_view_host()->JavaScriptMessageBoxClosed(reply_msg, success, prompt);
2883}
2884
2885void TabContents::SetSuppressMessageBoxes(bool suppress_message_boxes) {
2886 set_suppress_javascript_messages(suppress_message_boxes);
2887}
2888
[email protected]41fc0322009-09-04 22:23:402889void TabContents::set_encoding(const std::string& encoding) {
[email protected]aacd7cc2009-09-02 14:51:102890 encoding_ = CharacterEncoding::GetCanonicalEncodingNameByAliasName(encoding);
2891}
[email protected]f45d2a72010-03-08 23:28:352892
2893void TabContents::SetAppIcon(const SkBitmap& app_icon) {
2894 app_icon_ = app_icon;
2895 NotifyNavigationStateChanged(INVALIDATE_TITLE);
2896}