blob: f739a9e701a62cf4f5aa7ab354c76ff2fbcc51c5 [file] [log] [blame]
[email protected]7e7414ae2010-01-26 20:19:291// Copyright (c) 2010 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]f3ec7742009-01-15 00:59:165#include "chrome/browser/tab_contents/tab_contents.h"
initial.commit09911bf2008-07-26 23:55:296
[email protected]a92b8642009-05-05 23:38:567#include "app/l10n_util.h"
[email protected]9929da92009-05-05 02:05:118#include "app/resource_bundle.h"
[email protected]614519812010-03-19 08:20:569#include "app/text_elider.h"
[email protected]8cc8d492010-02-02 10:40:4910#include "base/auto_reset.h"
[email protected]420ae012009-04-24 05:16:3211#include "base/file_version_info.h"
[email protected]7cf1b6ce2010-03-20 06:37:0112#include "base/i18n/rtl.h"
[email protected]420ae012009-04-24 05:16:3213#include "base/process_util.h"
[email protected]96d185d2009-04-24 03:28:5414#include "base/string16.h"
[email protected]996fd702009-09-04 19:12:3715#include "base/string_util.h"
[email protected]96d185d2009-04-24 03:28:5416#include "base/time.h"
[email protected]6db0def92010-03-02 19:55:4517#include "chrome/browser/autocomplete_history_manager.h"
[email protected]21674df2009-10-28 19:51:2118#include "chrome/browser/autofill/autofill_manager.h"
[email protected]66ba4932009-06-04 19:22:1319#include "chrome/browser/blocked_popup_container.h"
[email protected]420ae012009-04-24 05:16:3220#include "chrome/browser/bookmarks/bookmark_model.h"
[email protected]96d185d2009-04-24 03:28:5421#include "chrome/browser/browser_process.h"
[email protected]cf68b092010-01-15 20:05:5522#include "chrome/browser/browser_shutdown.h"
initial.commit09911bf2008-07-26 23:55:2923#include "chrome/browser/cert_store.h"
[email protected]aacd7cc2009-09-02 14:51:1024#include "chrome/browser/character_encoding.h"
[email protected]96d185d2009-04-24 03:28:5425#include "chrome/browser/debugger/devtools_manager.h"
[email protected]420ae012009-04-24 05:16:3226#include "chrome/browser/dom_operation_notification_details.h"
[email protected]96d185d2009-04-24 03:28:5427#include "chrome/browser/dom_ui/dom_ui.h"
28#include "chrome/browser/dom_ui/dom_ui_factory.h"
[email protected]bcef0dc02009-02-28 00:35:0229#include "chrome/browser/download/download_item_model.h"
[email protected]420ae012009-04-24 05:16:3230#include "chrome/browser/download/download_manager.h"
31#include "chrome/browser/download/download_request_manager.h"
[email protected]e7eaedde2009-09-25 20:09:4932#include "chrome/browser/external_protocol_handler.h"
[email protected]92c6f9b92009-10-24 04:35:0833#include "chrome/browser/extensions/extensions_service.h"
[email protected]0189bc722009-08-28 21:56:4834#include "chrome/browser/favicon_service.h"
[email protected]420ae012009-04-24 05:16:3235#include "chrome/browser/google_util.h"
[email protected]79580c62010-02-02 02:36:2536#include "chrome/browser/host_content_settings_map.h"
[email protected]8897c382009-05-06 17:53:2637#include "chrome/browser/hung_renderer_dialog.h"
[email protected]12f74a92010-02-05 22:32:1438#include "chrome/browser/message_box_handler.h"
[email protected]420ae012009-04-24 05:16:3239#include "chrome/browser/load_from_memory_cache_details.h"
40#include "chrome/browser/load_notification_details.h"
[email protected]84abba62009-10-07 17:01:4441#include "chrome/browser/metrics/metric_event_duration_details.h"
[email protected]ea0e80892009-08-22 00:36:0542#include "chrome/browser/modal_html_dialog_delegate.h"
[email protected]c8865482009-07-23 20:40:1043#include "chrome/browser/omnibox_search_hint.h"
[email protected]fdd61c62009-04-22 19:22:5744#include "chrome/browser/password_manager/password_manager.h"
45#include "chrome/browser/plugin_installer.h"
[email protected]052313b2010-02-19 09:43:0846#include "chrome/browser/pref_service.h"
[email protected]ce560f82009-06-03 09:39:4447#include "chrome/browser/profile.h"
[email protected]1db6ff152009-10-12 15:32:0748#include "chrome/browser/renderer_host/render_process_host.h"
[email protected]8cb5d5b2010-02-09 11:36:1649#include "chrome/browser/renderer_host/render_view_host.h"
[email protected]96d185d2009-04-24 03:28:5450#include "chrome/browser/renderer_host/render_widget_host_view.h"
[email protected]fd694982009-07-03 17:32:3751#include "chrome/browser/renderer_host/resource_request_details.h"
[email protected]1db6ff152009-10-12 15:32:0752#include "chrome/browser/renderer_host/site_instance.h"
[email protected]d4a00a72010-01-29 01:44:4253#include "chrome/browser/renderer_host/translation_service.h"
[email protected]96d185d2009-04-24 03:28:5454#include "chrome/browser/renderer_host/web_cache_manager.h"
[email protected]93623c5d2009-12-10 21:40:3255#include "chrome/browser/renderer_preferences_util.h"
[email protected]c37e3b62010-01-22 00:15:1756#include "chrome/browser/sessions/session_types.h"
[email protected]bd1ad682009-05-15 22:19:1757#include "chrome/browser/tab_contents/infobar_delegate.h"
[email protected]7e383692009-06-12 19:14:5458#include "chrome/browser/tab_contents/interstitial_page.h"
[email protected]f3ec7742009-01-15 00:59:1659#include "chrome/browser/tab_contents/navigation_entry.h"
[email protected]de34a962009-06-26 23:22:4360#include "chrome/browser/tab_contents/provisional_load_details.h"
[email protected]f3ec7742009-01-15 00:59:1661#include "chrome/browser/tab_contents/tab_contents_delegate.h"
[email protected]fdd61c62009-04-22 19:22:5762#include "chrome/browser/tab_contents/tab_contents_view.h"
[email protected]58dca552009-06-17 00:35:0263#include "chrome/browser/tab_contents/thumbnail_generator.h"
[email protected]449478302009-06-09 20:04:2864#include "chrome/browser/thumbnail_store.h"
[email protected]420ae012009-04-24 05:16:3265#include "chrome/browser/search_engines/template_url_fetcher.h"
66#include "chrome/browser/search_engines/template_url_model.h"
[email protected]35f7d212009-04-29 21:19:2767#include "chrome/common/chrome_switches.h"
[email protected]3b355642010-02-05 16:01:4968#include "chrome/common/extensions/extension.h"
[email protected]5d246db22009-10-27 06:17:5769#include "chrome/common/extensions/extension_action.h"
[email protected]bfd04a62009-02-01 18:16:5670#include "chrome/common/notification_service.h"
[email protected]89d6e6e2009-12-03 23:06:5171#include "chrome/common/platform_util.h"
initial.commit09911bf2008-07-26 23:55:2972#include "chrome/common/pref_names.h"
[email protected]420ae012009-04-24 05:16:3273#include "chrome/common/render_messages.h"
[email protected]1ee614862009-07-06 20:29:1874#include "chrome/common/renderer_preferences.h"
[email protected]96d185d2009-04-24 03:28:5475#include "chrome/common/url_constants.h"
[email protected]8897c382009-05-06 17:53:2676#include "grit/generated_resources.h"
[email protected]420ae012009-04-24 05:16:3277#include "grit/locale_settings.h"
[email protected]fd626782010-01-22 05:26:0678#include "grit/platform_locale_settings.h"
[email protected]420ae012009-04-24 05:16:3279#include "net/base/mime_util.h"
80#include "net/base/net_errors.h"
[email protected]d686e812009-06-03 19:10:2981#include "net/base/net_util.h"
[email protected]420ae012009-04-24 05:16:3282#include "net/base/registry_controlled_domain.h"
[email protected]1ee614862009-07-06 20:29:1883#include "webkit/glue/password_form.h"
84#include "webkit/glue/webpreferences.h"
[email protected]3e45ba92009-02-20 21:09:0085
[email protected]304f8792009-08-19 21:28:0986#if defined(OS_CHROMEOS)
87// For GdkScreen
88#include <gdk/gdk.h>
[email protected]0ba5c5e2009-08-20 16:47:1389#endif // defined(OS_CHROMEOS)
90
[email protected]420ae012009-04-24 05:16:3291// Cross-Site Navigations
92//
93// If a TabContents is told to navigate to a different web site (as determined
94// by SiteInstance), it will replace its current RenderViewHost with a new
95// RenderViewHost dedicated to the new SiteInstance. This works as follows:
96//
97// - Navigate determines whether the destination is cross-site, and if so,
98// it creates a pending_render_view_host_ and moves into the PENDING
99// RendererState.
100// - The pending RVH is "suspended," so that no navigation messages are sent to
101// its renderer until the onbeforeunload JavaScript handler has a chance to
102// run in the current RVH.
103// - The pending RVH tells CrossSiteRequestManager (a thread-safe singleton)
104// that it has a pending cross-site request. ResourceDispatcherHost will
105// check for this when the response arrives.
106// - The current RVH runs its onbeforeunload handler. If it returns false, we
107// cancel all the pending logic and go back to NORMAL. Otherwise we allow
108// the pending RVH to send the navigation request to its renderer.
109// - ResourceDispatcherHost receives a ResourceRequest on the IO thread. It
110// checks CrossSiteRequestManager to see that the RVH responsible has a
111// pending cross-site request, and then installs a CrossSiteEventHandler.
112// - When RDH receives a response, the BufferedEventHandler determines whether
113// it is a download. If so, it sends a message to the new renderer causing
114// it to cancel the request, and the download proceeds in the download
115// thread. For now, we stay in a PENDING state (with a pending RVH) until
[email protected]57c6a652009-05-04 07:58:34116// the next DidNavigate event for this TabContents. This isn't ideal, but it
[email protected]420ae012009-04-24 05:16:32117// doesn't affect any functionality.
118// - After RDH receives a response and determines that it is safe and not a
119// download, it pauses the response to first run the old page's onunload
120// handler. It does this by asynchronously calling the OnCrossSiteResponse
[email protected]57c6a652009-05-04 07:58:34121// method of TabContents on the UI thread, which sends a ClosePage message
[email protected]420ae012009-04-24 05:16:32122// to the current RVH.
123// - Once the onunload handler is finished, a ClosePage_ACK message is sent to
124// the ResourceDispatcherHost, who unpauses the response. Data is then sent
125// to the pending RVH.
126// - The pending renderer sends a FrameNavigate message that invokes the
127// DidNavigate method. This replaces the current RVH with the
128// pending RVH and goes back to the NORMAL RendererState.
129
130namespace {
131
132// Amount of time we wait between when a key event is received and the renderer
133// is queried for its state and pushed to the NavigationEntry.
134const int kQueryStateDelay = 5000;
135
136const int kSyncWaitDelay = 40;
137
[email protected]304f8792009-08-19 21:28:09138#if defined(OS_CHROMEOS)
139// If a popup window is bigger than this fraction of the screen on chrome os,
140// turn it into a tab
141const float kMaxWidthFactor = 0.5;
142const float kMaxHeightFactor = 0.6;
143#endif
144
[email protected]420ae012009-04-24 05:16:32145// If another javascript message box is displayed within
146// kJavascriptMessageExpectedDelay of a previous javascript message box being
147// dismissed, display an option to suppress future message boxes from this
148// contents.
149const int kJavascriptMessageExpectedDelay = 1000;
150
[email protected]420ae012009-04-24 05:16:32151// The list of prefs we want to observe.
152const wchar_t* kPrefsToObserve[] = {
153 prefs::kAlternateErrorPagesEnabled,
154 prefs::kWebKitJavaEnabled,
155 prefs::kWebKitJavascriptEnabled,
156 prefs::kWebKitLoadsImagesAutomatically,
157 prefs::kWebKitPluginsEnabled,
158 prefs::kWebKitUsesUniversalDetector,
159 prefs::kWebKitSerifFontFamily,
160 prefs::kWebKitSansSerifFontFamily,
161 prefs::kWebKitFixedFontFamily,
162 prefs::kWebKitDefaultFontSize,
163 prefs::kWebKitDefaultFixedFontSize,
164 prefs::kDefaultCharset
165 // kWebKitStandardFontIsSerif needs to be added
166 // if we let users pick which font to use, serif or sans-serif when
167 // no font is specified or a CSS generic family (serif or sans-serif)
168 // is not specified.
169};
170
171const int kPrefsToObserveLength = arraysize(kPrefsToObserve);
172
[email protected]420ae012009-04-24 05:16:32173// Returns true if the entry's transition type is FORM_SUBMIT.
174bool IsFormSubmit(const NavigationEntry* entry) {
175 return (PageTransition::StripQualifier(entry->transition_type()) ==
176 PageTransition::FORM_SUBMIT);
177}
178
179#if defined(OS_WIN)
180
181BOOL CALLBACK InvalidateWindow(HWND hwnd, LPARAM lparam) {
182 // Note: erase is required to properly paint some widgets borders. This can
183 // be seen with textfields.
184 InvalidateRect(hwnd, NULL, TRUE);
185 return TRUE;
186}
187#endif
188
[email protected]5e369672009-11-03 23:48:30189ViewMsg_Navigate_Params::NavigationType GetNavigationType(
[email protected]1ccb3568d2010-02-19 10:51:16190 Profile* profile, const NavigationEntry& entry,
191 NavigationController::ReloadType reload_type) {
192 switch (reload_type) {
193 case NavigationController::RELOAD:
194 return ViewMsg_Navigate_Params::RELOAD;
195 case NavigationController::RELOAD_IGNORING_CACHE:
196 return ViewMsg_Navigate_Params::RELOAD_IGNORING_CACHE;
197 case NavigationController::NO_RELOAD:
198 break; // Fall through to rest of function.
199 }
[email protected]5e369672009-11-03 23:48:30200
201 if (entry.restore_type() == NavigationEntry::RESTORE_LAST_SESSION &&
202 profile->DidLastSessionExitCleanly())
203 return ViewMsg_Navigate_Params::RESTORE;
204
205 return ViewMsg_Navigate_Params::NORMAL;
206}
207
[email protected]3cc72b12010-03-18 23:03:00208void MakeNavigateParams(const NavigationController& controller,
[email protected]1ccb3568d2010-02-19 10:51:16209 NavigationController::ReloadType reload_type,
210 ViewMsg_Navigate_Params* params) {
[email protected]3cc72b12010-03-18 23:03:00211 const NavigationEntry& entry = *controller.pending_entry();
[email protected]056de2d2009-06-26 16:41:34212 params->page_id = entry.page_id();
[email protected]3cc72b12010-03-18 23:03:00213 params->pending_history_list_offset = controller.pending_entry_index();
214 params->current_history_list_offset = controller.last_committed_entry_index();
215 params->current_history_list_length = controller.entry_count();
[email protected]056de2d2009-06-26 16:41:34216 params->url = entry.url();
217 params->referrer = entry.referrer();
218 params->transition = entry.transition_type();
219 params->state = entry.content_state();
[email protected]3cc72b12010-03-18 23:03:00220 params->navigation_type =
221 GetNavigationType(controller.profile(), entry, reload_type);
[email protected]056de2d2009-06-26 16:41:34222 params->request_time = base::Time::Now();
223}
224
[email protected]420ae012009-04-24 05:16:32225} // namespace
226
227// -----------------------------------------------------------------------------
228
[email protected]fdd61c62009-04-22 19:22:57229// static
230int TabContents::find_request_id_counter_ = -1;
231
[email protected]420ae012009-04-24 05:16:32232
[email protected]420ae012009-04-24 05:16:32233TabContents::TabContents(Profile* profile,
234 SiteInstance* site_instance,
235 int routing_id,
[email protected]34ac70502009-09-25 17:07:23236 const TabContents* base_tab_contents)
[email protected]b680ad22009-04-15 23:19:42237 : delegate_(NULL),
[email protected]66ba4932009-06-04 19:22:13238 ALLOW_THIS_IN_INITIALIZER_LIST(controller_(this, profile)),
239 ALLOW_THIS_IN_INITIALIZER_LIST(view_(
[email protected]d82ed61e2009-06-16 02:46:22240 TabContentsView::Create(this))),
241 ALLOW_THIS_IN_INITIALIZER_LIST(render_manager_(this, this)),
[email protected]fdd61c62009-04-22 19:22:57242 property_bag_(),
243 registrar_(),
[email protected]d82ed61e2009-06-16 02:46:22244 ALLOW_THIS_IN_INITIALIZER_LIST(printing_(*this)),
[email protected]fdd61c62009-04-22 19:22:57245 save_package_(),
[email protected]6db0def92010-03-02 19:55:45246 autocomplete_history_manager_(),
[email protected]21674df2009-10-28 19:51:21247 autofill_manager_(),
[email protected]fdd61c62009-04-22 19:22:57248 password_manager_(),
249 plugin_installer_(),
[email protected]6f821dd2010-02-23 00:54:06250 bookmark_drag_(NULL),
[email protected]d82ed61e2009-06-16 02:46:22251 ALLOW_THIS_IN_INITIALIZER_LIST(fav_icon_helper_(this)),
[email protected]fdd61c62009-04-22 19:22:57252 select_file_dialog_(),
[email protected]d5f942ba2008-09-26 19:30:34253 is_loading_(false),
initial.commit09911bf2008-07-26 23:55:29254 is_crashed_(false),
[email protected]d5f942ba2008-09-26 19:30:34255 waiting_for_response_(false),
[email protected]d5f942ba2008-09-26 19:30:34256 max_page_id_(-1),
[email protected]fdd61c62009-04-22 19:22:57257 current_load_start_(),
258 load_state_(net::LOAD_STATE_IDLE),
259 load_state_host_(),
[email protected]094e5b22009-09-25 04:23:56260 upload_size_(0),
261 upload_position_(0),
[email protected]fdd61c62009-04-22 19:22:57262 received_page_title_(false),
263 is_starred_(false),
264 contents_mime_type_(),
265 encoding_(),
[email protected]332af7732009-03-11 13:21:35266 blocked_popups_(NULL),
[email protected]8cc8d492010-02-02 10:40:49267 dont_notify_render_view_(false),
[email protected]fdd61c62009-04-22 19:22:57268 infobar_delegates_(),
[email protected]fdd61c62009-04-22 19:22:57269 find_ui_active_(false),
270 find_op_aborted_(false),
271 current_find_request_id_(find_request_id_counter_++),
[email protected]e491f1c2009-05-22 20:28:12272 last_search_case_sensitive_(false),
273 last_search_prepopulate_text_(NULL),
274 last_search_result_(),
[email protected]fdd61c62009-04-22 19:22:57275 capturing_contents_(false),
276 is_being_destroyed_(false),
277 notify_disconnection_(false),
278 history_requests_(),
279#if defined(OS_WIN)
280 message_box_active_(CreateEvent(NULL, TRUE, FALSE, NULL)),
281#endif
282 last_javascript_message_dismissal_(),
[email protected]c2e74fe82009-09-03 17:57:44283 suppress_javascript_messages_(false),
[email protected]7ab1e7d62009-10-14 23:32:01284 is_showing_before_unload_dialog_(false),
[email protected]7ff431e2010-01-07 18:18:54285 renderer_preferences_(),
[email protected]1a242c32010-02-02 21:20:54286 opener_dom_ui_type_(DOMUIFactory::kNoDOMUI),
[email protected]3b1c1812010-02-12 21:53:55287 app_extension_(NULL),
288 language_state_(&controller_) {
[email protected]0de80162010-02-03 04:52:35289 ClearBlockedContentSettings();
[email protected]7ff431e2010-01-07 18:18:54290 renderer_preferences_util::UpdateFromSystemSettings(
291 &renderer_preferences_, profile);
292
[email protected]16779842009-07-08 23:45:29293#if defined(OS_CHROMEOS)
[email protected]58dca552009-06-17 00:35:02294 // Make sure the thumbnailer is started before starting the render manager.
295 // The thumbnailer will want to listen for RVH creations, one of which will
296 // happen in RVHManager::Init.
297 ThumbnailGenerator* generator = g_browser_process->GetThumbnailGenerator();
298 if (generator)
299 generator->StartThumbnailing();
300#endif
301
[email protected]12636df2009-09-28 22:32:21302 render_manager_.Init(profile, site_instance, routing_id);
[email protected]420ae012009-04-24 05:16:32303
[email protected]34ac70502009-09-25 17:07:23304 // We have the initial size of the view be based on the size of the passed in
305 // tab contents (normally a tab from the same window).
306 view_->CreateView(base_tab_contents ?
307 base_tab_contents->view()->GetContainerSize() : gfx::Size());
[email protected]420ae012009-04-24 05:16:32308
309 // Register for notifications about all interested prefs change.
310 PrefService* prefs = profile->GetPrefs();
311 if (prefs) {
312 for (int i = 0; i < kPrefsToObserveLength; ++i)
313 prefs->AddPrefObserver(kPrefsToObserve[i], this);
314 }
315
316 // Register for notifications about URL starredness changing on any profile.
317 registrar_.Add(this, NotificationType::URLS_STARRED,
318 NotificationService::AllSources());
319 registrar_.Add(this, NotificationType::BOOKMARK_MODEL_LOADED,
320 NotificationService::AllSources());
321 registrar_.Add(this, NotificationType::RENDER_WIDGET_HOST_DESTROYED,
322 NotificationService::AllSources());
[email protected]d3ba77272009-09-03 00:06:09323#if defined(OS_LINUX)
324 registrar_.Add(this, NotificationType::BROWSER_THEME_CHANGED,
325 NotificationService::AllSources());
326#endif
[email protected]420ae012009-04-24 05:16:32327
[email protected]6c32ce72010-03-08 05:18:06328 registrar_.Add(this, NotificationType::USER_STYLE_SHEET_UPDATED,
329 NotificationService::AllSources());
330
[email protected]79580c62010-02-02 02:36:25331 // Register for notifications about content setting changes.
332 registrar_.Add(this, NotificationType::CONTENT_SETTINGS_CHANGED,
333 NotificationService::AllSources());
334
[email protected]420ae012009-04-24 05:16:32335 // Keep a global copy of the previous search string (if any).
336 static string16 global_last_search = string16();
[email protected]e491f1c2009-05-22 20:28:12337 last_search_prepopulate_text_ = &global_last_search;
[email protected]840b1512009-07-21 16:53:46338
[email protected]c8865482009-07-23 20:40:10339 // Set-up the showing of the omnibox search infobar if applicable.
340 if (OmniboxSearchHint::IsEnabled(profile))
341 omnibox_search_hint_.reset(new OmniboxSearchHint(this));
[email protected]332af7732009-03-11 13:21:35342}
initial.commit09911bf2008-07-26 23:55:29343
344TabContents::~TabContents() {
[email protected]420ae012009-04-24 05:16:32345 is_being_destroyed_ = true;
346
[email protected]bfe4c1582009-10-02 18:11:09347 // We don't want any notifications while we're running our destructor.
[email protected]420ae012009-04-24 05:16:32348 registrar_.RemoveAll();
349
350 // Unregister the notifications of all observed prefs change.
351 PrefService* prefs = profile()->GetPrefs();
352 if (prefs) {
353 for (int i = 0; i < kPrefsToObserveLength; ++i)
354 prefs->RemovePrefObserver(kPrefsToObserve[i], this);
355 }
356
[email protected]420ae012009-04-24 05:16:32357 NotifyDisconnected();
[email protected]e7b8d832009-11-20 22:41:00358 hung_renderer_dialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:32359
[email protected]420ae012009-04-24 05:16:32360 // First cleanly close all child windows.
361 // TODO(mpcomplete): handle case if MaybeCloseChildWindows() already asked
362 // some of these to close. CloseWindows is async, so it might get called
363 // twice before it runs.
[email protected]8cc8d492010-02-02 10:40:49364 CloseConstrainedWindows();
[email protected]420ae012009-04-24 05:16:32365
[email protected]8cc8d492010-02-02 10:40:49366 // Close all blocked popups.
[email protected]66ba4932009-06-04 19:22:13367 if (blocked_popups_)
368 blocked_popups_->Destroy();
369
[email protected]420ae012009-04-24 05:16:32370 // Notify any observer that have a reference on this tab contents.
371 NotificationService::current()->Notify(
372 NotificationType::TAB_CONTENTS_DESTROYED,
373 Source<TabContents>(this),
374 NotificationService::NoDetails());
375
[email protected]1e5e74f2009-05-27 20:55:12376 // Notify any lasting InfobarDelegates that have not yet been removed that
377 // whatever infobar they were handling in this TabContents has closed,
378 // because the TabContents is going away entirely.
379 // This must happen after the TAB_CONTENTS_DESTROYED notification as the
380 // notification may trigger infobars calls that access their delegate. (and
381 // some implementations of InfoBarDelegate do delete themselves on
382 // InfoBarClosed()).
383 for (int i = 0; i < infobar_delegate_count(); ++i) {
384 InfoBarDelegate* delegate = GetInfoBarDelegateAt(i);
385 delegate->InfoBarClosed();
386 }
387 infobar_delegates_.clear();
388
[email protected]420ae012009-04-24 05:16:32389 // TODO(brettw) this should be moved to the view.
390#if defined(OS_WIN)
391 // If we still have a window handle, destroy it. GetNativeView can return
392 // NULL if this contents was part of a window that closed.
393 if (GetNativeView())
394 ::DestroyWindow(GetNativeView());
395#endif
[email protected]7ab1e7d62009-10-14 23:32:01396
397 // OnCloseStarted isn't called in unit tests.
398 if (!tab_close_start_time_.is_null()) {
399 UMA_HISTOGRAM_TIMES("Tab.Close",
400 base::TimeTicks::Now() - tab_close_start_time_);
401 }
initial.commit09911bf2008-07-26 23:55:29402}
403
[email protected]d5f942ba2008-09-26 19:30:34404// static
405void TabContents::RegisterUserPrefs(PrefService* prefs) {
[email protected]420ae012009-04-24 05:16:32406 prefs->RegisterBooleanPref(prefs::kAlternateErrorPagesEnabled, true);
407
408 WebPreferences pref_defaults;
409 prefs->RegisterBooleanPref(prefs::kWebKitJavascriptEnabled,
410 pref_defaults.javascript_enabled);
411 prefs->RegisterBooleanPref(prefs::kWebKitWebSecurityEnabled,
412 pref_defaults.web_security_enabled);
413 prefs->RegisterBooleanPref(
414 prefs::kWebKitJavascriptCanOpenWindowsAutomatically, true);
415 prefs->RegisterBooleanPref(prefs::kWebKitLoadsImagesAutomatically,
416 pref_defaults.loads_images_automatically);
417 prefs->RegisterBooleanPref(prefs::kWebKitPluginsEnabled,
418 pref_defaults.plugins_enabled);
419 prefs->RegisterBooleanPref(prefs::kWebKitDomPasteEnabled,
420 pref_defaults.dom_paste_enabled);
421 prefs->RegisterBooleanPref(prefs::kWebKitShrinksStandaloneImagesToFit,
422 pref_defaults.shrinks_standalone_images_to_fit);
[email protected]0df30122009-06-03 12:13:08423 prefs->RegisterStringPref(prefs::kWebKitInspectorSettings,
[email protected]8922e1f2009-10-03 05:01:26424 UTF8ToWide(pref_defaults.inspector_settings));
[email protected]420ae012009-04-24 05:16:32425 prefs->RegisterBooleanPref(prefs::kWebKitTextAreasAreResizable,
426 pref_defaults.text_areas_are_resizable);
427 prefs->RegisterBooleanPref(prefs::kWebKitJavaEnabled,
428 pref_defaults.java_enabled);
429
430 prefs->RegisterLocalizedStringPref(prefs::kAcceptLanguages,
431 IDS_ACCEPT_LANGUAGES);
432 prefs->RegisterLocalizedStringPref(prefs::kDefaultCharset,
433 IDS_DEFAULT_ENCODING);
434 prefs->RegisterLocalizedBooleanPref(prefs::kWebKitStandardFontIsSerif,
435 IDS_STANDARD_FONT_IS_SERIF);
436 prefs->RegisterLocalizedStringPref(prefs::kWebKitFixedFontFamily,
437 IDS_FIXED_FONT_FAMILY);
438 prefs->RegisterLocalizedStringPref(prefs::kWebKitSerifFontFamily,
439 IDS_SERIF_FONT_FAMILY);
440 prefs->RegisterLocalizedStringPref(prefs::kWebKitSansSerifFontFamily,
441 IDS_SANS_SERIF_FONT_FAMILY);
442 prefs->RegisterLocalizedStringPref(prefs::kWebKitCursiveFontFamily,
443 IDS_CURSIVE_FONT_FAMILY);
444 prefs->RegisterLocalizedStringPref(prefs::kWebKitFantasyFontFamily,
445 IDS_FANTASY_FONT_FAMILY);
446 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitDefaultFontSize,
447 IDS_DEFAULT_FONT_SIZE);
448 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitDefaultFixedFontSize,
449 IDS_DEFAULT_FIXED_FONT_SIZE);
450 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitMinimumFontSize,
451 IDS_MINIMUM_FONT_SIZE);
452 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitMinimumLogicalFontSize,
453 IDS_MINIMUM_LOGICAL_FONT_SIZE);
454 prefs->RegisterLocalizedBooleanPref(prefs::kWebKitUsesUniversalDetector,
455 IDS_USES_UNIVERSAL_DETECTOR);
456 prefs->RegisterLocalizedStringPref(prefs::kStaticEncodings,
457 IDS_STATIC_ENCODING_LIST);
initial.commit09911bf2008-07-26 23:55:29458}
459
[email protected]13367f72009-07-08 16:39:02460// Returns true if contains content rendered by an extension.
461bool TabContents::HostsExtension() const {
462 return GetURL().SchemeIs(chrome::kExtensionScheme);
[email protected]d5f942ba2008-09-26 19:30:34463}
464
[email protected]6e95706d2009-04-23 22:55:01465PasswordManager* TabContents::GetPasswordManager() {
466 if (password_manager_.get() == NULL)
467 password_manager_.reset(new PasswordManager(this));
468 return password_manager_.get();
469}
470
471PluginInstaller* TabContents::GetPluginInstaller() {
472 if (plugin_installer_.get() == NULL)
473 plugin_installer_.reset(new PluginInstaller(this));
474 return plugin_installer_.get();
475}
476
[email protected]8cb5d5b2010-02-09 11:36:16477RenderProcessHost* TabContents::GetRenderProcessHost() const {
478 return render_manager_.current_host()->process();
479}
480
[email protected]3b355642010-02-05 16:01:49481void TabContents::SetAppExtension(Extension* extension) {
[email protected]867a73e12010-03-19 20:45:46482 DCHECK(!extension || extension->GetFullLaunchURL().is_valid());
[email protected]3b355642010-02-05 16:01:49483 app_extension_ = extension;
[email protected]fca656c2010-02-10 20:30:10484
485 NotificationService::current()->Notify(
486 NotificationType::TAB_CONTENTS_APPLICATION_EXTENSION_CHANGED,
487 Source<TabContents>(this),
488 NotificationService::NoDetails());
[email protected]3b355642010-02-05 16:01:49489}
490
[email protected]911f8152010-03-18 16:46:40491void TabContents::SetAppExtensionById(const std::string& app_extension_id) {
492 if (app_extension_id.empty())
493 return;
494
495 ExtensionsService* extension_service = profile()->GetExtensionsService();
496 if (extension_service && extension_service->is_ready()) {
497 Extension* extension =
498 extension_service->GetExtensionById(app_extension_id, false);
499 if (extension)
500 SetAppExtension(extension);
501 }
502}
503
[email protected]d5f942ba2008-09-26 19:30:34504const GURL& TabContents::GetURL() const {
505 // We may not have a navigation entry yet
[email protected]ce3fa3c2009-04-20 19:55:57506 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]ebe89e062009-08-13 23:16:54507 return entry ? entry->virtual_url() : GURL::EmptyGURL();
[email protected]d5f942ba2008-09-26 19:30:34508}
509
[email protected]96d185d2009-04-24 03:28:54510const string16& TabContents::GetTitle() const {
[email protected]4c6092c5b2009-06-06 00:23:55511 // Transient entries take precedence. They are used for interstitial pages
512 // that are shown on top of existing pages.
513 NavigationEntry* entry = controller_.GetTransientEntry();
514 if (entry)
515 return entry->GetTitleForDisplay(&controller_);
516
[email protected]96d185d2009-04-24 03:28:54517 DOMUI* our_dom_ui = render_manager_.pending_dom_ui() ?
518 render_manager_.pending_dom_ui() : render_manager_.dom_ui();
519 if (our_dom_ui) {
520 // Don't override the title in view source mode.
[email protected]4c6092c5b2009-06-06 00:23:55521 entry = controller_.GetActiveEntry();
[email protected]96d185d2009-04-24 03:28:54522 if (!(entry && entry->IsViewSourceMode())) {
523 // Give the DOM UI the chance to override our title.
524 const string16& title = our_dom_ui->overridden_title();
525 if (!title.empty())
526 return title;
527 }
528 }
529
530 // We use the title for the last committed entry rather than a pending
531 // navigation entry. For example, when the user types in a URL, we want to
532 // keep the old page's title until the new load has committed and we get a new
533 // title.
[email protected]96d185d2009-04-24 03:28:54534 entry = controller_.GetLastCommittedEntry();
535 if (entry)
536 return entry->GetTitleForDisplay(&controller_);
[email protected]96d185d2009-04-24 03:28:54537 return EmptyString16();
538}
539
[email protected]bbbc1ef2010-02-12 18:03:41540// static
541string16 TabContents::GetDefaultTitle() {
542 return l10n_util::GetStringUTF16(IDS_DEFAULT_TAB_TITLE);
543}
544
[email protected]d5f942ba2008-09-26 19:30:34545int32 TabContents::GetMaxPageID() {
546 if (GetSiteInstance())
547 return GetSiteInstance()->max_page_id();
548 else
549 return max_page_id_;
550}
551
552void TabContents::UpdateMaxPageID(int32 page_id) {
553 // Ensure both the SiteInstance and RenderProcessHost update their max page
[email protected]57c6a652009-05-04 07:58:34554 // IDs in sync. Only TabContents will also have site instances, except during
[email protected]d5f942ba2008-09-26 19:30:34555 // testing.
556 if (GetSiteInstance())
557 GetSiteInstance()->UpdateMaxPageID(page_id);
[email protected]8cb5d5b2010-02-09 11:36:16558 GetRenderProcessHost()->UpdateMaxPageID(page_id);
[email protected]d5f942ba2008-09-26 19:30:34559}
560
[email protected]96d185d2009-04-24 03:28:54561SiteInstance* TabContents::GetSiteInstance() const {
562 return render_manager_.current_host()->site_instance();
563}
564
[email protected]96d185d2009-04-24 03:28:54565bool TabContents::ShouldDisplayURL() {
[email protected]8ab24cc2009-11-13 20:56:15566 // Don't hide the url in view source mode and with interstitials.
[email protected]96d185d2009-04-24 03:28:54567 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]8ab24cc2009-11-13 20:56:15568 if (entry && (entry->IsViewSourceMode() ||
569 entry->page_type() == NavigationEntry::INTERSTITIAL_PAGE)) {
[email protected]96d185d2009-04-24 03:28:54570 return true;
[email protected]8ab24cc2009-11-13 20:56:15571 }
[email protected]83e3895b2009-06-11 00:07:16572
[email protected]96d185d2009-04-24 03:28:54573 DOMUI* dom_ui = GetDOMUIForCurrentState();
574 if (dom_ui)
575 return !dom_ui->should_hide_url();
576 return true;
577}
578
[email protected]d5f942ba2008-09-26 19:30:34579SkBitmap TabContents::GetFavIcon() const {
580 // Like GetTitle(), we also want to use the favicon for the last committed
581 // entry rather than a pending navigation entry.
[email protected]ce3fa3c2009-04-20 19:55:57582 NavigationEntry* entry = controller_.GetTransientEntry();
[email protected]cbab76d2008-10-13 22:42:47583 if (entry)
584 return entry->favicon().bitmap();
585
[email protected]ce3fa3c2009-04-20 19:55:57586 entry = controller_.GetLastCommittedEntry();
[email protected]d5f942ba2008-09-26 19:30:34587 if (entry)
588 return entry->favicon().bitmap();
[email protected]d5f942ba2008-09-26 19:30:34589 return SkBitmap();
590}
591
[email protected]0b303cc2009-09-28 22:35:15592bool TabContents::FavIconIsValid() const {
593 NavigationEntry* entry = controller_.GetTransientEntry();
594 if (entry)
595 return entry->favicon().is_valid();
596
597 entry = controller_.GetLastCommittedEntry();
598 if (entry)
599 return entry->favicon().is_valid();
600
601 return false;
602}
603
[email protected]96d185d2009-04-24 03:28:54604bool TabContents::ShouldDisplayFavIcon() {
605 // Always display a throbber during pending loads.
606 if (controller_.GetLastCommittedEntry() && controller_.pending_entry())
607 return true;
608
609 DOMUI* dom_ui = GetDOMUIForCurrentState();
610 if (dom_ui)
611 return !dom_ui->hide_favicon();
612 return true;
613}
614
[email protected]2d5a3592010-01-30 04:29:52615bool TabContents::IsContentBlocked(ContentSettingsType content_type) const {
[email protected]8cc8d492010-02-02 10:40:49616 if (content_type == CONTENT_SETTINGS_TYPE_POPUPS)
617 return blocked_popups_ != NULL;
618
[email protected]0de80162010-02-03 04:52:35619 if (content_type == CONTENT_SETTINGS_TYPE_IMAGES ||
620 content_type == CONTENT_SETTINGS_TYPE_JAVASCRIPT ||
[email protected]3dbb80b2010-02-09 22:41:20621 content_type == CONTENT_SETTINGS_TYPE_PLUGINS ||
622 content_type == CONTENT_SETTINGS_TYPE_COOKIES)
[email protected]0de80162010-02-03 04:52:35623 return content_blocked_[content_type];
624
[email protected]b1235062010-03-25 17:31:27625 // TODO(joth): remove once fully implemented.
626 if (content_type == CONTENT_SETTINGS_TYPE_GEOLOCATION)
627 return false;
628
[email protected]3dbb80b2010-02-09 22:41:20629 NOTREACHED();
[email protected]2d5a3592010-01-30 04:29:52630 return false;
631}
632
[email protected]96d185d2009-04-24 03:28:54633std::wstring TabContents::GetStatusText() const {
634 if (!is_loading() || load_state_ == net::LOAD_STATE_IDLE)
635 return std::wstring();
636
637 switch (load_state_) {
638 case net::LOAD_STATE_WAITING_FOR_CACHE:
639 return l10n_util::GetString(IDS_LOAD_STATE_WAITING_FOR_CACHE);
640 case net::LOAD_STATE_RESOLVING_PROXY_FOR_URL:
641 return l10n_util::GetString(IDS_LOAD_STATE_RESOLVING_PROXY_FOR_URL);
642 case net::LOAD_STATE_RESOLVING_HOST:
643 return l10n_util::GetString(IDS_LOAD_STATE_RESOLVING_HOST);
644 case net::LOAD_STATE_CONNECTING:
645 return l10n_util::GetString(IDS_LOAD_STATE_CONNECTING);
646 case net::LOAD_STATE_SENDING_REQUEST:
[email protected]094e5b22009-09-25 04:23:56647 if (upload_size_)
648 return l10n_util::GetStringF(
649 IDS_LOAD_STATE_SENDING_REQUEST_WITH_PROGRESS,
650 static_cast<int>((100 * upload_position_) / upload_size_));
651 else
652 return l10n_util::GetString(IDS_LOAD_STATE_SENDING_REQUEST);
[email protected]96d185d2009-04-24 03:28:54653 case net::LOAD_STATE_WAITING_FOR_RESPONSE:
654 return l10n_util::GetStringF(IDS_LOAD_STATE_WAITING_FOR_RESPONSE,
655 load_state_host_);
656 // Ignore net::LOAD_STATE_READING_RESPONSE and net::LOAD_STATE_IDLE
657 case net::LOAD_STATE_IDLE:
658 case net::LOAD_STATE_READING_RESPONSE:
659 break;
660 }
661
662 return std::wstring();
663}
664
[email protected]d5f942ba2008-09-26 19:30:34665void TabContents::SetIsCrashed(bool state) {
666 if (state == is_crashed_)
667 return;
668
669 is_crashed_ = state;
[email protected]c9cd2222009-05-06 05:16:50670 NotifyNavigationStateChanged(INVALIDATE_TAB);
[email protected]d5f942ba2008-09-26 19:30:34671}
672
[email protected]744ef172009-10-16 21:53:46673void TabContents::PageActionStateChanged() {
674 NotifyNavigationStateChanged(TabContents::INVALIDATE_PAGE_ACTIONS);
[email protected]d7eaf572009-07-01 21:57:00675}
[email protected]ce5c4502009-05-06 16:46:11676
[email protected]d5f942ba2008-09-26 19:30:34677void TabContents::NotifyNavigationStateChanged(unsigned changed_flags) {
678 if (delegate_)
679 delegate_->NavigationStateChanged(this, changed_flags);
680}
681
[email protected]96d185d2009-04-24 03:28:54682void TabContents::DidBecomeSelected() {
683 controller_.SetActive(true);
[email protected]8cb5d5b2010-02-09 11:36:16684 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
685 if (rwhv) {
686 rwhv->DidBecomeSelected();
[email protected]789e9152009-08-04 21:59:43687#if defined(OS_MACOSX)
[email protected]8cb5d5b2010-02-09 11:36:16688 rwhv->SetActive(true);
[email protected]789e9152009-08-04 21:59:43689#endif
690 }
[email protected]96d185d2009-04-24 03:28:54691
[email protected]8cb5d5b2010-02-09 11:36:16692 WebCacheManager::GetInstance()->ObserveActivity(GetRenderProcessHost()->id());
[email protected]96d185d2009-04-24 03:28:54693}
694
695void TabContents::WasHidden() {
696 if (!capturing_contents()) {
697 // |render_view_host()| can be NULL if the user middle clicks a link to open
698 // a tab in then background, then closes the tab before selecting it. This
699 // is because closing the tab calls TabContents::Destroy(), which removes
700 // the |render_view_host()|; then when we actually destroy the window,
701 // OnWindowPosChanged() notices and calls HideContents() (which calls us).
[email protected]8cb5d5b2010-02-09 11:36:16702 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
703 if (rwhv)
704 rwhv->WasHidden();
[email protected]96d185d2009-04-24 03:28:54705 }
706
707 NotificationService::current()->Notify(
708 NotificationType::TAB_CONTENTS_HIDDEN,
709 Source<TabContents>(this),
710 NotificationService::NoDetails());
711}
712
[email protected]d5f942ba2008-09-26 19:30:34713void TabContents::Activate() {
714 if (delegate_)
715 delegate_->ActivateContents(this);
716}
717
[email protected]96d185d2009-04-24 03:28:54718void TabContents::ShowContents() {
[email protected]8cb5d5b2010-02-09 11:36:16719 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
720 if (rwhv)
721 rwhv->DidBecomeSelected();
[email protected]96d185d2009-04-24 03:28:54722}
723
724void TabContents::HideContents() {
725 // TODO(pkasting): http://b/1239839 Right now we purposefully don't call
726 // our superclass HideContents(), because some callers want to be very picky
727 // about the order in which these get called. In addition to making the code
728 // here practically impossible to understand, this also means we end up
729 // calling TabContents::WasHidden() twice if callers call both versions of
[email protected]57c6a652009-05-04 07:58:34730 // HideContents() on a TabContents.
[email protected]96d185d2009-04-24 03:28:54731 WasHidden();
732}
733
[email protected]c0588052008-10-27 23:01:50734void TabContents::OpenURL(const GURL& url, const GURL& referrer,
[email protected]d5f942ba2008-09-26 19:30:34735 WindowOpenDisposition disposition,
736 PageTransition::Type transition) {
737 if (delegate_)
[email protected]c0588052008-10-27 23:01:50738 delegate_->OpenURLFromTab(this, url, referrer, disposition, transition);
[email protected]d5f942ba2008-09-26 19:30:34739}
740
[email protected]1ccb3568d2010-02-19 10:51:16741bool TabContents::NavigateToPendingEntry(
742 NavigationController::ReloadType reload_type) {
[email protected]96d185d2009-04-24 03:28:54743 const NavigationEntry& entry = *controller_.pending_entry();
744
745 RenderViewHost* dest_render_view_host = render_manager_.Navigate(entry);
746 if (!dest_render_view_host)
747 return false; // Unable to create the desired render view host.
748
749 // Tell DevTools agent that it is attached prior to the navigation.
[email protected]d9f9b792009-06-24 13:17:12750 DevToolsManager* devtools_manager = DevToolsManager::GetInstance();
[email protected]7e8e6b62009-05-08 11:28:32751 if (devtools_manager) { // NULL in unit tests.
[email protected]3cc72b12010-03-18 23:03:00752 devtools_manager->OnNavigatingToPendingEntry(render_view_host(),
753 dest_render_view_host,
754 entry.url());
[email protected]7e8e6b62009-05-08 11:28:32755 }
[email protected]96d185d2009-04-24 03:28:54756
757 // Used for page load time metrics.
758 current_load_start_ = base::TimeTicks::Now();
759
760 // Navigate in the desired RenderViewHost.
[email protected]056de2d2009-06-26 16:41:34761 ViewMsg_Navigate_Params navigate_params;
[email protected]3cc72b12010-03-18 23:03:00762 MakeNavigateParams(controller_, reload_type, &navigate_params);
[email protected]056de2d2009-06-26 16:41:34763 dest_render_view_host->Navigate(navigate_params);
[email protected]96d185d2009-04-24 03:28:54764
765 if (entry.page_id() == -1) {
766 // HACK!! This code suppresses javascript: URLs from being added to
767 // session history, which is what we want to do for javascript: URLs that
768 // do not generate content. What we really need is a message from the
769 // renderer telling us that a new page was not created. The same message
770 // could be used for mailto: URLs and the like.
771 if (entry.url().SchemeIs(chrome::kJavaScriptScheme))
772 return false;
773 }
774
775 // Clear any provisional password saves - this stops password infobars
776 // showing up on pages the user navigates to while the right page is
777 // loading.
778 GetPasswordManager()->ClearProvisionalSave();
779
[email protected]1ccb3568d2010-02-19 10:51:16780 if (reload_type != NavigationController::NO_RELOAD &&
781 !profile()->IsOffTheRecord()) {
[email protected]0189bc722009-08-28 21:56:48782 FaviconService* favicon_service =
783 profile()->GetFaviconService(Profile::IMPLICIT_ACCESS);
784 if (favicon_service)
785 favicon_service->SetFaviconOutOfDateForPage(entry.url());
[email protected]96d185d2009-04-24 03:28:54786 }
787
[email protected]d5f942ba2008-09-26 19:30:34788 return true;
789}
790
[email protected]96d185d2009-04-24 03:28:54791void TabContents::Stop() {
792 render_manager_.Stop();
793 printing_.Stop();
794}
795
[email protected]96d185d2009-04-24 03:28:54796void TabContents::DisassociateFromPopupCount() {
797 render_view_host()->DisassociateFromPopupCount();
798}
799
800TabContents* TabContents::Clone() {
801 // We create a new SiteInstance so that the new tab won't share processes
802 // with the old one. This can be changed in the future if we need it to share
803 // processes for some reason.
[email protected]420ae012009-04-24 05:16:32804 TabContents* tc = new TabContents(profile(),
[email protected]96d185d2009-04-24 03:28:54805 SiteInstance::CreateSiteInstance(profile()),
[email protected]12636df2009-09-28 22:32:21806 MSG_ROUTING_NONE, this);
[email protected]96d185d2009-04-24 03:28:54807 tc->controller().CopyStateFrom(controller_);
808 return tc;
809}
810
[email protected]4d677202009-07-19 07:37:12811void TabContents::ShowPageInfo(const GURL& url,
812 const NavigationEntry::SSLStatus& ssl,
813 bool show_history) {
814 if (!delegate_)
815 return;
816
[email protected]bb678332009-07-21 00:15:50817 delegate_->ShowPageInfo(profile(), url, ssl, show_history);
[email protected]4d677202009-07-19 07:37:12818}
819
[email protected]7e0831b2010-02-01 08:45:15820void TabContents::TranslatePage(const std::string& source_lang,
821 const std::string& target_lang) {
822 NavigationEntry* entry = controller_.GetActiveEntry();
823 if (!entry) {
824 NOTREACHED();
825 return;
826 }
[email protected]3b1c1812010-02-12 21:53:55827 language_state_.set_translation_pending(true);
[email protected]7e0831b2010-02-01 08:45:15828 render_view_host()->TranslatePage(entry->page_id(), source_lang, target_lang);
829}
830
[email protected]d7f380f2010-03-15 21:48:50831void TabContents::RevertTranslatedPage() {
832 // TODO(jcampan): revert translated page to original and remove translate
833 // infobar.
834}
835
initial.commit09911bf2008-07-26 23:55:29836ConstrainedWindow* TabContents::CreateConstrainedDialog(
[email protected]e8382172009-06-19 22:16:28837 ConstrainedWindowDelegate* delegate) {
initial.commit09911bf2008-07-26 23:55:29838 ConstrainedWindow* window =
[email protected]e8382172009-06-19 22:16:28839 ConstrainedWindow::CreateConstrainedDialog(this, delegate);
initial.commit09911bf2008-07-26 23:55:29840 child_windows_.push_back(window);
[email protected]fa1cf0b82010-01-15 21:49:44841
842 if (child_windows_.size() == 1) {
843 window->ShowConstrainedWindow();
844 BlockTabContent(true);
845 }
846
initial.commit09911bf2008-07-26 23:55:29847 return window;
848}
849
[email protected]fa1cf0b82010-01-15 21:49:44850void TabContents::BlockTabContent(bool blocked) {
[email protected]8dccd7cb2010-02-25 05:19:55851 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
852 if (rwhv)
853 rwhv->SetVisuallyDeemphasized(blocked);
[email protected]fa1cf0b82010-01-15 21:49:44854 render_view_host()->set_ignore_input_events(blocked);
855 if (delegate_)
856 delegate_->SetTabContentBlocked(this, blocked);
857}
858
initial.commit09911bf2008-07-26 23:55:29859void TabContents::AddNewContents(TabContents* new_contents,
860 WindowOpenDisposition disposition,
861 const gfx::Rect& initial_pos,
[email protected]7e7414ae2010-01-26 20:19:29862 bool user_gesture) {
initial.commit09911bf2008-07-26 23:55:29863 if (!delegate_)
864 return;
865
[email protected]35f7d212009-04-29 21:19:27866 if ((disposition == NEW_POPUP) && !user_gesture &&
867 !CommandLine::ForCurrentProcess()->HasSwitch(
868 switches::kDisablePopupBlocking)) {
[email protected]2c4410d2009-05-06 23:46:22869 // Unrequested popups from normal pages are constrained unless they're in
[email protected]b6c874582009-05-08 19:38:31870 // the whitelist. The popup owner will handle checking this.
[email protected]7e7414ae2010-01-26 20:19:29871 delegate_->GetConstrainingContents(this)->AddPopup(
872 new_contents, initial_pos);
[email protected]b6c874582009-05-08 19:38:31873 } else {
[email protected]304f8792009-08-19 21:28:09874#if defined(OS_CHROMEOS)
875 if (disposition == NEW_POPUP) {
876 // If the popup is bigger than a given factor of the screen, then
877 // turn it into a foreground tab (on chrome os only)
878 GdkScreen* screen = gdk_screen_get_default();
879 int max_width = gdk_screen_get_width(screen) * kMaxWidthFactor;
880 int max_height = gdk_screen_get_height(screen) * kMaxHeightFactor;
881 if (initial_pos.width() > max_width ||
882 initial_pos.height() > max_height) {
883 disposition = NEW_FOREGROUND_TAB;
884 }
885 }
886#endif
887
[email protected]0aa55312008-10-17 21:53:08888 new_contents->DisassociateFromPopupCount();
initial.commit09911bf2008-07-26 23:55:29889 delegate_->AddNewContents(this, new_contents, disposition, initial_pos,
890 user_gesture);
[email protected]7f37dd92009-09-15 16:35:49891 NotificationService::current()->Notify(
892 NotificationType::TAB_ADDED,
893 Source<TabContentsDelegate>(delegate_),
894 Details<TabContents>(this));
initial.commit09911bf2008-07-26 23:55:29895 }
initial.commit09911bf2008-07-26 23:55:29896
[email protected]8cc8d492010-02-02 10:40:49897 // TODO(pkasting): Why is this necessary?
898 PopupNotificationVisibilityChanged(blocked_popups_ != NULL);
initial.commit09911bf2008-07-26 23:55:29899}
900
[email protected]bcd9dc42010-01-23 16:38:18901bool TabContents::ExecuteCode(int request_id, const std::string& extension_id,
[email protected]61f5a7b2009-12-22 22:21:20902 const std::vector<URLPattern>& host_permissions,
[email protected]20ad2692009-11-20 18:27:20903 bool is_js_code, const std::string& code_string,
904 bool all_frames) {
[email protected]ebd0eae72010-02-16 23:50:51905 RenderViewHost* host = render_view_host();
906 if (!host)
907 return false;
908
909 return host->Send(new ViewMsg_ExecuteCode(host->routing_id(),
910 ViewMsg_ExecuteCode_Params(request_id, extension_id, host_permissions,
911 is_js_code, code_string, all_frames)));
[email protected]912256b32009-09-18 09:47:35912}
913
[email protected]96d185d2009-04-24 03:28:54914void TabContents::PopupNotificationVisibilityChanged(bool visible) {
[email protected]8cc8d492010-02-02 10:40:49915 if (is_being_destroyed_)
916 return;
917 if (!dont_notify_render_view_)
918 render_view_host()->PopupNotificationVisibilityChanged(visible);
[email protected]0de80162010-02-03 04:52:35919 if (delegate_)
920 delegate_->OnBlockedContentChange(this);
[email protected]96d185d2009-04-24 03:28:54921}
922
[email protected]5c9e97a2009-09-09 23:48:30923gfx::NativeView TabContents::GetContentNativeView() const {
[email protected]96d185d2009-04-24 03:28:54924 return view_->GetContentNativeView();
925}
926
927gfx::NativeView TabContents::GetNativeView() const {
928 return view_->GetNativeView();
929}
930
931void TabContents::GetContainerBounds(gfx::Rect *out) const {
932 view_->GetContainerBounds(out);
933}
934
935void TabContents::Focus() {
936 view_->Focus();
937}
938
[email protected]90daadb42009-06-08 21:27:28939void TabContents::FocusThroughTabTraversal(bool reverse) {
[email protected]7e383692009-06-12 19:14:54940 if (showing_interstitial_page()) {
941 render_manager_.interstitial_page()->FocusThroughTabTraversal(reverse);
942 return;
943 }
[email protected]96d185d2009-04-24 03:28:54944 render_view_host()->SetInitialFocus(reverse);
945}
946
947bool TabContents::FocusLocationBarByDefault() {
948 DOMUI* dom_ui = GetDOMUIForCurrentState();
949 if (dom_ui)
950 return dom_ui->focus_location_bar_by_default();
[email protected]3e3f0eb2009-06-22 18:33:43951 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]e0d481582009-09-15 21:06:25952 if (entry && entry->url() == GURL(chrome::kAboutBlankURL))
[email protected]3e3f0eb2009-06-22 18:33:43953 return true;
[email protected]96d185d2009-04-24 03:28:54954 return false;
955}
956
[email protected]616ed5a2008-11-21 22:27:24957void TabContents::AddInfoBar(InfoBarDelegate* delegate) {
[email protected]f7019302010-03-26 19:58:32958 if (delegate_ && !delegate_->infobars_enabled()) {
959 delegate->InfoBarClosed();
960 return;
961 }
962
[email protected]616ed5a2008-11-21 22:27:24963 // Look through the existing InfoBarDelegates we have for a match. If we've
964 // already got one that matches, then we don't add the new one.
[email protected]f86a07022008-11-25 01:06:05965 for (int i = 0; i < infobar_delegate_count(); ++i) {
[email protected]6d27a7b2009-12-18 23:25:45966 if (GetInfoBarDelegateAt(i)->EqualsDelegate(delegate)) {
967 // Tell the new infobar to close so that it can clean itself up.
968 delegate->InfoBarClosed();
[email protected]616ed5a2008-11-21 22:27:24969 return;
[email protected]6d27a7b2009-12-18 23:25:45970 }
[email protected]616ed5a2008-11-21 22:27:24971 }
972
973 infobar_delegates_.push_back(delegate);
[email protected]bfd04a62009-02-01 18:16:56974 NotificationService::current()->Notify(
975 NotificationType::TAB_CONTENTS_INFOBAR_ADDED,
976 Source<TabContents>(this),
977 Details<InfoBarDelegate>(delegate));
[email protected]616ed5a2008-11-21 22:27:24978
979 // Add ourselves as an observer for navigations the first time a delegate is
980 // added. We use this notification to expire InfoBars that need to expire on
981 // page transitions.
982 if (infobar_delegates_.size() == 1) {
[email protected]bfd04a62009-02-01 18:16:56983 registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED,
[email protected]ce3fa3c2009-04-20 19:55:57984 Source<NavigationController>(&controller_));
[email protected]616ed5a2008-11-21 22:27:24985 }
986}
987
988void TabContents::RemoveInfoBar(InfoBarDelegate* delegate) {
[email protected]f7019302010-03-26 19:58:32989 if (delegate_ && !delegate_->infobars_enabled()) {
990 return;
991 }
992
[email protected]616ed5a2008-11-21 22:27:24993 std::vector<InfoBarDelegate*>::iterator it =
994 find(infobar_delegates_.begin(), infobar_delegates_.end(), delegate);
995 if (it != infobar_delegates_.end()) {
996 InfoBarDelegate* delegate = *it;
[email protected]bfd04a62009-02-01 18:16:56997 NotificationService::current()->Notify(
998 NotificationType::TAB_CONTENTS_INFOBAR_REMOVED,
999 Source<TabContents>(this),
1000 Details<InfoBarDelegate>(delegate));
[email protected]f86a07022008-11-25 01:06:051001 infobar_delegates_.erase(it);
[email protected]616ed5a2008-11-21 22:27:241002
[email protected]6a02963e2009-01-06 16:58:031003 // Remove ourselves as an observer if we are tracking no more InfoBars.
1004 if (infobar_delegates_.empty()) {
[email protected]bfd04a62009-02-01 18:16:561005 registrar_.Remove(this, NotificationType::NAV_ENTRY_COMMITTED,
[email protected]ce3fa3c2009-04-20 19:55:571006 Source<NavigationController>(&controller_));
[email protected]6a02963e2009-01-06 16:58:031007 }
[email protected]616ed5a2008-11-21 22:27:241008 }
1009}
[email protected]3edd9522009-03-04 22:19:411010
[email protected]f81821c2009-08-07 01:29:471011void TabContents::ReplaceInfoBar(InfoBarDelegate* old_delegate,
1012 InfoBarDelegate* new_delegate) {
[email protected]f7019302010-03-26 19:58:321013 if (delegate_ && !delegate_->infobars_enabled()) {
1014 new_delegate->InfoBarClosed();
1015 return;
1016 }
1017
[email protected]f81821c2009-08-07 01:29:471018 std::vector<InfoBarDelegate*>::iterator it =
1019 find(infobar_delegates_.begin(), infobar_delegates_.end(), old_delegate);
1020 DCHECK(it != infobar_delegates_.end());
1021
1022 // Notify the container about the change of plans.
1023 scoped_ptr<std::pair<InfoBarDelegate*, InfoBarDelegate*> > details(
1024 new std::pair<InfoBarDelegate*, InfoBarDelegate*>(
1025 old_delegate, new_delegate));
1026 NotificationService::current()->Notify(
1027 NotificationType::TAB_CONTENTS_INFOBAR_REPLACED,
1028 Source<TabContents>(this),
1029 Details<std::pair<InfoBarDelegate*, InfoBarDelegate*> >(details.get()));
1030
1031 // Remove the old one.
1032 infobar_delegates_.erase(it);
1033
1034 // Add the new one.
1035 infobar_delegates_.push_back(new_delegate);
1036}
1037
[email protected]20ede072009-10-06 00:28:351038bool TabContents::ShouldShowBookmarkBar() {
[email protected]aa9196f2009-10-22 16:20:271039 if (showing_interstitial_page())
1040 return false;
1041
[email protected]96d185d2009-04-24 03:28:541042 // See GetDOMUIForCurrentState() comment for more info. This case is very
1043 // similar, but for non-first loads, we want to use the committed entry. This
1044 // is so the bookmarks bar disappears at the same time the page does.
1045 if (controller_.GetLastCommittedEntry()) {
1046 // Not the first load, always use the committed DOM UI.
1047 if (render_manager_.dom_ui())
1048 return render_manager_.dom_ui()->force_bookmark_bar_visible();
1049 return false; // Default.
1050 }
1051
1052 // When it's the first load, we know either the pending one or the committed
1053 // one will have the DOM UI in it (see GetDOMUIForCurrentState), and only one
1054 // of them will be valid, so we can just check both.
1055 if (render_manager_.pending_dom_ui())
1056 return render_manager_.pending_dom_ui()->force_bookmark_bar_visible();
1057 if (render_manager_.dom_ui())
1058 return render_manager_.dom_ui()->force_bookmark_bar_visible();
1059 return false; // Default.
1060}
1061
[email protected]d8f16ae2009-08-30 03:47:261062bool TabContents::IsExtensionShelfAlwaysVisible() {
1063 // See GetDOMUIForCurrentState() comment for more info. This case is very
1064 // similar, but for non-first loads, we want to use the committed entry. This
1065 // is so the bookmarks bar disappears at the same time the page does.
1066 if (controller_.GetLastCommittedEntry()) {
1067 // Not the first load, always use the committed DOM UI.
1068 if (render_manager_.dom_ui())
1069 return render_manager_.dom_ui()->force_extension_shelf_visible();
1070 return false; // Default.
1071 }
1072
1073 // When it's the first load, we know either the pending one or the committed
1074 // one will have the DOM UI in it (see GetDOMUIForCurrentState), and only one
1075 // of them will be valid, so we can just check both.
1076 if (render_manager_.pending_dom_ui())
1077 return render_manager_.pending_dom_ui()->force_extension_shelf_visible();
1078 if (render_manager_.dom_ui())
1079 return render_manager_.dom_ui()->force_extension_shelf_visible();
1080 return false; // Default.
1081}
1082
[email protected]3edd9522009-03-04 22:19:411083void TabContents::ToolbarSizeChanged(bool is_animating) {
1084 TabContentsDelegate* d = delegate();
1085 if (d)
1086 d->ToolbarSizeChanged(this, is_animating);
1087}
[email protected]616ed5a2008-11-21 22:27:241088
[email protected]fc6fb7fb2009-11-07 02:35:041089bool TabContents::CanDownload(int request_id) {
1090 TabContentsDelegate* d = delegate();
1091 if (d)
1092 return d->CanDownload(request_id);
1093 return true;
1094}
1095
[email protected]d5f942ba2008-09-26 19:30:341096void TabContents::OnStartDownload(DownloadItem* download) {
1097 DCHECK(download);
[email protected]d5f942ba2008-09-26 19:30:341098
1099 // Download in a constrained popup is shown in the tab that opened it.
[email protected]b6c874582009-05-08 19:38:311100 TabContents* tab_contents = delegate()->GetConstrainingContents(this);
[email protected]d5f942ba2008-09-26 19:30:341101
[email protected]59560e0b2009-06-04 03:30:221102 if (tab_contents && tab_contents->delegate())
1103 tab_contents->delegate()->OnStartDownload(download);
[email protected]3edd9522009-03-04 22:19:411104}
[email protected]3edd9522009-03-04 22:19:411105
[email protected]d5f942ba2008-09-26 19:30:341106void TabContents::WillClose(ConstrainedWindow* window) {
1107 ConstrainedWindowList::iterator it =
1108 find(child_windows_.begin(), child_windows_.end(), window);
[email protected]fa1cf0b82010-01-15 21:49:441109 bool removed_topmost_window = it == child_windows_.begin();
[email protected]d5f942ba2008-09-26 19:30:341110 if (it != child_windows_.end())
1111 child_windows_.erase(it);
[email protected]55098792010-03-24 18:20:371112 if (child_windows_.size() > 0) {
1113 if (removed_topmost_window) {
1114 child_windows_[0]->ShowConstrainedWindow();
1115 }
[email protected]fa1cf0b82010-01-15 21:49:441116 BlockTabContent(true);
1117 } else {
1118 BlockTabContent(false);
1119 }
[email protected]66ba4932009-06-04 19:22:131120}
[email protected]d5f942ba2008-09-26 19:30:341121
[email protected]66ba4932009-06-04 19:22:131122void TabContents::WillCloseBlockedPopupContainer(
1123 BlockedPopupContainer* container) {
1124 DCHECK(blocked_popups_ == container);
1125 blocked_popups_ = NULL;
[email protected]8cc8d492010-02-02 10:40:491126 PopupNotificationVisibilityChanged(false);
[email protected]d5f942ba2008-09-26 19:30:341127}
1128
[email protected]d5f942ba2008-09-26 19:30:341129void TabContents::DidMoveOrResize(ConstrainedWindow* window) {
[email protected]332af7732009-03-11 13:21:351130#if defined(OS_WIN)
[email protected]92edc472009-02-10 20:32:061131 UpdateWindow(GetNativeView());
[email protected]332af7732009-03-11 13:21:351132#endif
[email protected]d5f942ba2008-09-26 19:30:341133}
1134
[email protected]c1dc9f132009-11-20 23:13:211135void TabContents::StartFinding(string16 search_string,
[email protected]e491f1c2009-05-22 20:28:121136 bool forward_direction,
1137 bool case_sensitive) {
[email protected]c1dc9f132009-11-20 23:13:211138 // If search_string is empty, it means FindNext was pressed with a keyboard
[email protected]6e95706d2009-04-23 22:55:011139 // shortcut so unless we have something to search for we return early.
[email protected]c1dc9f132009-11-20 23:13:211140 if (search_string.empty() && find_text_.empty()) {
[email protected]87fae512009-12-11 02:20:211141 // Try the last thing we searched for on this tab, then the last thing
1142 // searched for on any tab.
1143 if (!previous_find_text_.empty())
1144 search_string = previous_find_text_;
1145 else if (!last_search_prepopulate_text_->empty())
1146 search_string = *last_search_prepopulate_text_;
1147 else
[email protected]e491f1c2009-05-22 20:28:121148 return;
[email protected]e491f1c2009-05-22 20:28:121149 }
[email protected]6e95706d2009-04-23 22:55:011150
[email protected]101c90e2009-10-26 21:14:441151 // Keep track of the previous search.
1152 previous_find_text_ = find_text_;
1153
[email protected]6e95706d2009-04-23 22:55:011154 // This is a FindNext operation if we are searching for the same text again,
1155 // or if the passed in search text is empty (FindNext keyboard shortcut). The
1156 // exception to this is if the Find was aborted (then we don't want FindNext
1157 // because the highlighting has been cleared and we need it to reappear). We
1158 // therefore treat FindNext after an aborted Find operation as a full fledged
1159 // Find.
[email protected]c1dc9f132009-11-20 23:13:211160 bool find_next = (find_text_ == search_string || search_string.empty()) &&
[email protected]e491f1c2009-05-22 20:28:121161 (last_search_case_sensitive_ == case_sensitive) &&
[email protected]6e95706d2009-04-23 22:55:011162 !find_op_aborted_;
1163 if (!find_next)
1164 current_find_request_id_ = find_request_id_counter_++;
1165
[email protected]c1dc9f132009-11-20 23:13:211166 if (!search_string.empty())
1167 find_text_ = search_string;
[email protected]e491f1c2009-05-22 20:28:121168 last_search_case_sensitive_ = case_sensitive;
[email protected]6e95706d2009-04-23 22:55:011169
1170 find_op_aborted_ = false;
1171
1172 // Keep track of what the last search was across the tabs.
[email protected]c1dc9f132009-11-20 23:13:211173 *last_search_prepopulate_text_ = find_text_;
[email protected]6e95706d2009-04-23 22:55:011174 render_view_host()->StartFinding(current_find_request_id_,
1175 find_text_,
1176 forward_direction,
[email protected]e491f1c2009-05-22 20:28:121177 case_sensitive,
[email protected]6e95706d2009-04-23 22:55:011178 find_next);
1179}
1180
[email protected]5bc8fe92010-03-11 18:19:001181void TabContents::StopFinding(
1182 FindBarController::SelectionAction selection_action) {
1183 // When the action is kClearAction, it means the find string has been cleared
[email protected]2d2dffc2009-06-09 21:41:001184 // by the user, but the UI has not been dismissed.
[email protected]5bc8fe92010-03-11 18:19:001185 if (selection_action != FindBarController::kClearSelection)
[email protected]2d2dffc2009-06-09 21:41:001186 find_ui_active_ = false;
[email protected]87fae512009-12-11 02:20:211187 previous_find_text_ = find_text_;
[email protected]101c90e2009-10-26 21:14:441188 find_text_.clear();
[email protected]6e95706d2009-04-23 22:55:011189 find_op_aborted_ = true;
[email protected]e491f1c2009-05-22 20:28:121190 last_search_result_ = FindNotificationDetails();
[email protected]5bc8fe92010-03-11 18:19:001191 render_view_host()->StopFinding(selection_action);
[email protected]6e95706d2009-04-23 22:55:011192}
1193
[email protected]420ae012009-04-24 05:16:321194void TabContents::OnSavePage() {
1195 // If we can not save the page, try to download it.
1196 if (!SavePackage::IsSavableContents(contents_mime_type())) {
1197 DownloadManager* dlm = profile()->GetDownloadManager();
1198 const GURL& current_page_url = GetURL();
1199 if (dlm && current_page_url.is_valid())
[email protected]d82ed61e2009-06-16 02:46:221200 dlm->DownloadUrl(current_page_url, GURL(), "", this);
[email protected]420ae012009-04-24 05:16:321201 return;
1202 }
1203
1204 Stop();
1205
1206 // Create the save package and possibly prompt the user for the name to save
1207 // the page as. The user prompt is an asynchronous operation that runs on
1208 // another thread.
[email protected]d82ed61e2009-06-16 02:46:221209 save_package_ = new SavePackage(this);
[email protected]420ae012009-04-24 05:16:321210 save_package_->GetSaveInfo();
1211}
1212
1213// Used in automated testing to bypass prompting the user for file names.
1214// Instead, the names and paths are hard coded rather than running them through
1215// file name sanitation and extension / mime checking.
[email protected]828cabe2009-09-26 22:47:111216bool TabContents::SavePage(const FilePath& main_file, const FilePath& dir_path,
[email protected]420ae012009-04-24 05:16:321217 SavePackage::SavePackageType save_type) {
1218 // Stop the page from navigating.
1219 Stop();
1220
[email protected]828cabe2009-09-26 22:47:111221 save_package_ = new SavePackage(this, save_type, main_file, dir_path);
[email protected]cdf6cc82009-08-04 21:48:591222 return save_package_->Init();
[email protected]420ae012009-04-24 05:16:321223}
1224
[email protected]89d6e6e2009-12-03 23:06:511225void TabContents::EmailPageLocation() {
1226 std::string title = EscapeQueryParamValue(UTF16ToUTF8(GetTitle()), false);
1227 std::string page_url = EscapeQueryParamValue(GetURL().spec(), false);
1228 std::string mailto = std::string("mailto:?subject=Fwd:%20") +
1229 title + "&body=%0A%0A" + page_url;
1230 platform_util::OpenExternal(GURL(mailto));
1231}
1232
[email protected]420ae012009-04-24 05:16:321233void TabContents::PrintPreview() {
1234 // We don't show the print preview yet, only the print dialog.
1235 PrintNow();
1236}
1237
1238bool TabContents::PrintNow() {
1239 // We can't print interstitial page for now.
1240 if (showing_interstitial_page())
1241 return false;
1242
1243 return render_view_host()->PrintPages();
1244}
1245
[email protected]82270452009-06-19 15:58:011246void TabContents::PrintingDone(int document_cookie, bool success) {
1247 render_view_host()->PrintingDone(document_cookie, success);
1248}
1249
[email protected]420ae012009-04-24 05:16:321250bool TabContents::IsActiveEntry(int32 page_id) {
1251 NavigationEntry* active_entry = controller_.GetActiveEntry();
1252 return (active_entry != NULL &&
1253 active_entry->site_instance() == GetSiteInstance() &&
1254 active_entry->page_id() == page_id);
1255}
1256
[email protected]b2fe07d12010-02-09 14:38:081257void TabContents::SetOverrideEncoding(const std::string& encoding) {
[email protected]8cb5d5b2010-02-09 11:36:161258 set_encoding(encoding);
1259 render_view_host()->SetPageEncoding(encoding);
1260}
1261
[email protected]b2fe07d12010-02-09 14:38:081262void TabContents::ResetOverrideEncoding() {
[email protected]8cb5d5b2010-02-09 11:36:161263 reset_encoding();
1264 render_view_host()->ResetPageEncodingToDefault();
1265}
1266
1267void TabContents::WindowMoveOrResizeStarted() {
1268 render_view_host()->WindowMoveOrResizeStarted();
1269}
1270
[email protected]3f8eb7f2009-10-02 23:58:051271void TabContents::LogNewTabTime(const std::string& event_name) {
1272 // Not all new tab pages get timed. In those cases, we don't have a
1273 // new_tab_start_time_.
1274 if (new_tab_start_time_.is_null())
1275 return;
1276
1277 base::TimeDelta duration = base::TimeTicks::Now() - new_tab_start_time_;
[email protected]84abba62009-10-07 17:01:441278 MetricEventDurationDetails details(event_name,
1279 static_cast<int>(duration.InMilliseconds()));
1280
[email protected]7ab1e7d62009-10-14 23:32:011281 if (event_name == "Tab.NewTabScriptStart") {
1282 UMA_HISTOGRAM_TIMES("Tab.NewTabScriptStart", duration);
1283 } else if (event_name == "Tab.NewTabDOMContentLoaded") {
1284 UMA_HISTOGRAM_TIMES("Tab.NewTabDOMContentLoaded", duration);
1285 } else if (event_name == "Tab.NewTabOnload") {
1286 UMA_HISTOGRAM_TIMES("Tab.NewTabOnload", duration);
[email protected]3f8eb7f2009-10-02 23:58:051287 // The new tab page has finished loading; reset it.
1288 new_tab_start_time_ = base::TimeTicks();
[email protected]84abba62009-10-07 17:01:441289 } else {
1290 NOTREACHED();
[email protected]3f8eb7f2009-10-02 23:58:051291 }
[email protected]84abba62009-10-07 17:01:441292 NotificationService::current()->Notify(
1293 NotificationType::METRIC_EVENT_DURATION,
1294 Source<TabContents>(this),
1295 Details<MetricEventDurationDetails>(&details));
[email protected]3f8eb7f2009-10-02 23:58:051296}
1297
[email protected]7ab1e7d62009-10-14 23:32:011298void TabContents::OnCloseStarted() {
1299 if (tab_close_start_time_.is_null())
1300 tab_close_start_time_ = base::TimeTicks::Now();
1301}
1302
[email protected]c37e3b62010-01-22 00:15:171303TabContents* TabContents::CloneAndMakePhantom() {
[email protected]c37e3b62010-01-22 00:15:171304 TabNavigation tab_nav;
[email protected]df72ac72010-02-06 00:43:161305
1306 NavigationEntry* entry = controller().GetActiveEntry();
1307 if (app_extension_)
[email protected]867a73e12010-03-19 20:45:461308 tab_nav.set_url(app_extension_->GetFullLaunchURL());
[email protected]df72ac72010-02-06 00:43:161309 else if (entry)
1310 tab_nav.SetFromNavigationEntry(*entry);
1311
[email protected]c37e3b62010-01-22 00:15:171312 std::vector<TabNavigation> navigations;
1313 navigations.push_back(tab_nav);
1314
1315 TabContents* new_contents =
1316 new TabContents(profile(), NULL, MSG_ROUTING_NONE, NULL);
[email protected]3b355642010-02-05 16:01:491317 new_contents->SetAppExtension(app_extension_);
[email protected]c37e3b62010-01-22 00:15:171318 new_contents->controller().RestoreFromState(navigations, 0, false);
[email protected]c37e3b62010-01-22 00:15:171319
[email protected]df72ac72010-02-06 00:43:161320 if (!app_extension_ && entry)
1321 new_contents->controller().GetActiveEntry()->favicon() = entry->favicon();
1322
[email protected]c37e3b62010-01-22 00:15:171323 return new_contents;
1324}
1325
[email protected]0de80162010-02-03 04:52:351326// Resets the |content_blocked_| array.
1327void TabContents::ClearBlockedContentSettings() {
[email protected]15cf526b2010-02-12 06:33:491328 for (size_t i = 0; i < arraysize(content_blocked_); ++i)
[email protected]0de80162010-02-03 04:52:351329 content_blocked_[i] = false;
1330}
1331
[email protected]614c0e12010-03-25 10:12:561332// Resets the |geolocation_settings_| map.
1333void TabContents::ClearGeolocationContentSettings() {
1334 geolocation_content_settings_.clear();
1335}
1336
[email protected]96d185d2009-04-24 03:28:541337// Notifies the RenderWidgetHost instance about the fact that the page is
1338// loading, or done loading and calls the base implementation.
1339void TabContents::SetIsLoading(bool is_loading,
1340 LoadNotificationDetails* details) {
1341 if (is_loading == is_loading_)
1342 return;
1343
1344 if (!is_loading) {
1345 load_state_ = net::LOAD_STATE_IDLE;
1346 load_state_host_.clear();
[email protected]094e5b22009-09-25 04:23:561347 upload_size_ = 0;
1348 upload_position_ = 0;
[email protected]96d185d2009-04-24 03:28:541349 }
1350
1351 render_manager_.SetIsLoading(is_loading);
1352
1353 is_loading_ = is_loading;
1354 waiting_for_response_ = is_loading;
1355
1356 if (delegate_)
1357 delegate_->LoadingStateChanged(this);
[email protected]329581b2009-04-28 06:52:351358 NotifyNavigationStateChanged(INVALIDATE_LOAD);
[email protected]96d185d2009-04-24 03:28:541359
1360 NotificationType type = is_loading ? NotificationType::LOAD_START :
1361 NotificationType::LOAD_STOP;
[email protected]ce5c4502009-05-06 16:46:111362 NotificationDetails det = NotificationService::NoDetails();
[email protected]96d185d2009-04-24 03:28:541363 if (details)
1364 det = Details<LoadNotificationDetails>(details);
1365 NotificationService::current()->Notify(type,
1366 Source<NavigationController>(&controller_),
1367 det);
1368}
1369
[email protected]b6c874582009-05-08 19:38:311370void TabContents::AddPopup(TabContents* new_contents,
[email protected]7e7414ae2010-01-26 20:19:291371 const gfx::Rect& initial_pos) {
[email protected]8cc8d492010-02-02 10:40:491372 GURL url(GetURL());
1373 if (url.is_valid() &&
1374 profile()->GetHostContentSettingsMap()->GetContentSetting(
[email protected]da0c8e92010-02-09 23:25:491375 url, CONTENT_SETTINGS_TYPE_POPUPS) == CONTENT_SETTING_ALLOW) {
[email protected]8cc8d492010-02-02 10:40:491376 AddNewContents(new_contents, NEW_POPUP, initial_pos, true);
1377 } else {
1378 if (!blocked_popups_)
1379 blocked_popups_ = new BlockedPopupContainer(this);
1380 blocked_popups_->AddTabContents(new_contents, initial_pos);
1381 }
[email protected]b9681312008-11-07 00:08:261382}
[email protected]616ed5a2008-11-21 22:27:241383
1384namespace {
1385bool TransitionIsReload(PageTransition::Type transition) {
1386 return PageTransition::StripQualifier(transition) == PageTransition::RELOAD;
1387}
1388}
1389
1390void TabContents::ExpireInfoBars(
1391 const NavigationController::LoadCommittedDetails& details) {
1392 // Only hide InfoBars when the user has done something that makes the main
1393 // frame load. We don't want various automatic or subframe navigations making
1394 // it disappear.
1395 if (!details.is_user_initiated_main_frame_load())
1396 return;
1397
[email protected]f86a07022008-11-25 01:06:051398 for (int i = infobar_delegate_count() - 1; i >= 0; --i) {
[email protected]616ed5a2008-11-21 22:27:241399 InfoBarDelegate* delegate = GetInfoBarDelegateAt(i);
[email protected]f86a07022008-11-25 01:06:051400 if (delegate->ShouldExpire(details))
[email protected]616ed5a2008-11-21 22:27:241401 RemoveInfoBar(delegate);
[email protected]616ed5a2008-11-21 22:27:241402 }
1403}
[email protected]fdd61c62009-04-22 19:22:571404
[email protected]96d185d2009-04-24 03:28:541405DOMUI* TabContents::GetDOMUIForCurrentState() {
1406 // When there is a pending navigation entry, we want to use the pending DOMUI
1407 // that goes along with it to control the basic flags. For example, we want to
1408 // show the pending URL in the URL bar, so we want the display_url flag to
1409 // be from the pending entry.
1410 //
1411 // The confusion comes because there are multiple possibilities for the
1412 // initial load in a tab as a side effect of the way the RenderViewHostManager
1413 // works.
1414 //
1415 // - For the very first tab the load looks "normal". The new tab DOM UI is
1416 // the pending one, and we want it to apply here.
1417 //
1418 // - For subsequent new tabs, they'll get a new SiteInstance which will then
1419 // get switched to the one previously associated with the new tab pages.
1420 // This switching will cause the manager to commit the RVH/DOMUI. So we'll
1421 // have a committed DOM UI in this case.
1422 //
1423 // This condition handles all of these cases:
1424 //
1425 // - First load in first tab: no committed nav entry + pending nav entry +
1426 // pending dom ui:
1427 // -> Use pending DOM UI if any.
1428 //
1429 // - First load in second tab: no committed nav entry + pending nav entry +
1430 // no pending DOM UI:
1431 // -> Use the committed DOM UI if any.
1432 //
1433 // - Second navigation in any tab: committed nav entry + pending nav entry:
1434 // -> Use pending DOM UI if any.
1435 //
1436 // - Normal state with no load: committed nav entry + no pending nav entry:
1437 // -> Use committed DOM UI.
1438 if (controller_.pending_entry() &&
1439 (controller_.GetLastCommittedEntry() ||
1440 render_manager_.pending_dom_ui()))
1441 return render_manager_.pending_dom_ui();
1442 return render_manager_.dom_ui();
1443}
[email protected]420ae012009-04-24 05:16:321444
1445void TabContents::DidNavigateMainFramePostCommit(
1446 const NavigationController::LoadCommittedDetails& details,
1447 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]c2e74fe82009-09-03 17:57:441448 if (opener_dom_ui_type_ != DOMUIFactory::kNoDOMUI) {
1449 // If this is a window.open navigation, use the same DOMUI as the renderer
1450 // that opened the window, as long as both renderers have the same
1451 // privileges.
1452 if (opener_dom_ui_type_ == DOMUIFactory::GetDOMUIType(GetURL())) {
1453 DOMUI* dom_ui = DOMUIFactory::CreateDOMUIForURL(this, GetURL());
1454 // dom_ui might be NULL if the URL refers to a non-existent extension.
1455 if (dom_ui) {
1456 render_manager_.SetDOMUIPostCommit(dom_ui);
1457 dom_ui->RenderViewCreated(render_view_host());
1458 }
1459 }
1460 opener_dom_ui_type_ = DOMUIFactory::kNoDOMUI;
1461 }
1462
[email protected]420ae012009-04-24 05:16:321463 if (details.is_user_initiated_main_frame_load()) {
1464 // Clear the status bubble. This is a workaround for a bug where WebKit
1465 // doesn't let us know that the cursor left an element during a
1466 // transition (this is also why the mouse cursor remains as a hand after
1467 // clicking on a link); see bugs 1184641 and 980803. We don't want to
1468 // clear the bubble when a user navigates to a named anchor in the same
1469 // page.
1470 UpdateTargetURL(details.entry->page_id(), GURL());
1471
1472 // UpdateHelpersForDidNavigate will handle the case where the password_form
1473 // origin is valid.
1474 // TODO(brettw) bug 1343111: Password manager stuff in here needs to be
1475 // cleaned up and covered by tests.
1476 if (!params.password_form.origin.is_valid())
1477 GetPasswordManager()->DidNavigate();
1478 }
1479
1480 // The keyword generator uses the navigation entries, so must be called after
1481 // the commit.
1482 GenerateKeywordIfNecessary(params);
1483
1484 // Allow the new page to set the title again.
1485 received_page_title_ = false;
1486
1487 // Get the favicon, either from history or request it from the net.
1488 fav_icon_helper_.FetchFavIcon(details.entry->url());
1489
[email protected]0de80162010-02-03 04:52:351490 // Clear all page actions, blocked content notifications and browser actions
1491 // for this tab, unless this is an in-page navigation.
[email protected]a6e82fc2010-02-24 22:28:141492 if (!details.is_in_page) {
[email protected]92c6f9b92009-10-24 04:35:081493 ExtensionsService* service = profile()->GetExtensionsService();
1494 if (service) {
1495 for (size_t i = 0; i < service->extensions()->size(); ++i) {
[email protected]5d246db22009-10-27 06:17:571496 ExtensionAction* browser_action =
[email protected]92c6f9b92009-10-24 04:35:081497 service->extensions()->at(i)->browser_action();
[email protected]56ce6e52009-10-27 00:10:521498 if (browser_action) {
1499 browser_action->ClearAllValuesForTab(controller().session_id().id());
1500 NotificationService::current()->Notify(
1501 NotificationType::EXTENSION_BROWSER_ACTION_UPDATED,
[email protected]5d246db22009-10-27 06:17:571502 Source<ExtensionAction>(browser_action),
[email protected]56ce6e52009-10-27 00:10:521503 NotificationService::NoDetails());
1504 }
[email protected]92c6f9b92009-10-24 04:35:081505
[email protected]5d246db22009-10-27 06:17:571506 ExtensionAction* page_action =
[email protected]56ce6e52009-10-27 00:10:521507 service->extensions()->at(i)->page_action();
1508 if (page_action) {
1509 page_action->ClearAllValuesForTab(controller().session_id().id());
1510 PageActionStateChanged();
1511 }
[email protected]92c6f9b92009-10-24 04:35:081512 }
[email protected]aece2c7f2009-08-27 20:43:171513 }
[email protected]0de80162010-02-03 04:52:351514
1515 // Close blocked popups.
1516 if (blocked_popups_) {
1517 AutoReset auto_reset(&dont_notify_render_view_, true);
1518 blocked_popups_->Destroy();
[email protected]da7b5262010-02-25 21:12:461519 blocked_popups_ = NULL;
[email protected]0de80162010-02-03 04:52:351520 }
1521
1522 // Clear "blocked" flags.
1523 ClearBlockedContentSettings();
[email protected]614c0e12010-03-25 10:12:561524 ClearGeolocationContentSettings();
[email protected]0de80162010-02-03 04:52:351525 if (delegate_)
1526 delegate_->OnBlockedContentChange(this);
[email protected]aece2c7f2009-08-27 20:43:171527 }
[email protected]ce5c4502009-05-06 16:46:111528
[email protected]8cc8d492010-02-02 10:40:491529 // Close constrained windows if necessary.
1530 if (!net::RegistryControlledDomainService::SameDomainOrHost(
1531 details.previous_url, details.entry->url()))
1532 CloseConstrainedWindows();
1533
[email protected]420ae012009-04-24 05:16:321534 // Update the starred state.
1535 UpdateStarredStateForCurrentURL();
[email protected]c07c9752010-03-12 05:57:531536
1537 // Clear the cache of forms in AutoFill.
1538 if (autofill_manager_.get())
1539 autofill_manager_->Reset();
[email protected]420ae012009-04-24 05:16:321540}
1541
1542void TabContents::DidNavigateAnyFramePostCommit(
1543 RenderViewHost* render_view_host,
1544 const NavigationController::LoadCommittedDetails& details,
1545 const ViewHostMsg_FrameNavigate_Params& params) {
1546 // If we navigate, start showing messages again. This does nothing to prevent
1547 // a malicious script from spamming messages, since the script could just
1548 // reload the page to stop blocking.
1549 suppress_javascript_messages_ = false;
1550
[email protected]420ae012009-04-24 05:16:321551 // Notify the password manager of the navigation or form submit.
1552 // TODO(brettw) bug 1343111: Password manager stuff in here needs to be
1553 // cleaned up and covered by tests.
1554 if (params.password_form.origin.is_valid())
1555 GetPasswordManager()->ProvisionallySavePassword(params.password_form);
[email protected]3b1c1812010-02-12 21:53:551556
1557 // Let the LanguageState clear its state.
[email protected]34cec8d82010-03-05 18:27:011558 bool reload = details.entry->transition_type() == PageTransition::RELOAD ||
1559 details.type == NavigationType::SAME_PAGE;
1560 language_state_.DidNavigate(reload, details.is_in_page);
[email protected]420ae012009-04-24 05:16:321561}
1562
[email protected]8cc8d492010-02-02 10:40:491563void TabContents::CloseConstrainedWindows() {
1564 // Clear out any constrained windows since we are leaving this page entirely.
[email protected]420ae012009-04-24 05:16:321565 // We use indices instead of iterators in case CloseWindow does something
1566 // that may invalidate an iterator.
1567 int size = static_cast<int>(child_windows_.size());
1568 for (int i = size - 1; i >= 0; --i) {
1569 ConstrainedWindow* window = child_windows_[i];
[email protected]fa1cf0b82010-01-15 21:49:441570 if (window) {
[email protected]420ae012009-04-24 05:16:321571 window->CloseConstrainedWindow();
[email protected]fa1cf0b82010-01-15 21:49:441572 BlockTabContent(false);
1573 }
[email protected]420ae012009-04-24 05:16:321574 }
1575}
1576
1577void TabContents::UpdateStarredStateForCurrentURL() {
1578 BookmarkModel* model = profile()->GetBookmarkModel();
1579 const bool old_state = is_starred_;
1580 is_starred_ = (model && model->IsBookmarked(GetURL()));
1581
1582 if (is_starred_ != old_state && delegate())
1583 delegate()->URLStarredChanged(this, is_starred_);
1584}
1585
1586void TabContents::UpdateAlternateErrorPageURL() {
1587 GURL url = GetAlternateErrorPageURL();
1588 render_view_host()->SetAlternateErrorPageURL(url);
1589}
1590
1591void TabContents::UpdateWebPreferences() {
1592 render_view_host()->UpdateWebPreferences(GetWebkitPrefs());
1593}
1594
1595void TabContents::UpdateMaxPageIDIfNecessary(SiteInstance* site_instance,
1596 RenderViewHost* rvh) {
1597 // If we are creating a RVH for a restored controller, then we might
1598 // have more page IDs than the SiteInstance's current max page ID. We must
1599 // make sure that the max page ID is larger than any restored page ID.
1600 // Note that it is ok for conflicting page IDs to exist in another tab
1601 // (i.e., NavigationController), but if any page ID is larger than the max,
1602 // the back/forward list will get confused.
1603 int max_restored_page_id = controller_.max_restored_page_id();
1604 if (max_restored_page_id > 0) {
1605 int curr_max_page_id = site_instance->max_page_id();
1606 if (max_restored_page_id > curr_max_page_id) {
1607 // Need to update the site instance immediately.
1608 site_instance->UpdateMaxPageID(max_restored_page_id);
1609
1610 // Also tell the renderer to update its internal representation. We
1611 // need to reserve enough IDs to make all restored page IDs less than
1612 // the max.
1613 if (curr_max_page_id < 0)
1614 curr_max_page_id = 0;
1615 rvh->ReservePageIDRange(max_restored_page_id - curr_max_page_id);
1616 }
1617 }
1618}
1619
1620void TabContents::UpdateHistoryForNavigation(
[email protected]ebe89e062009-08-13 23:16:541621 const GURL& virtual_url,
[email protected]befd8d822009-07-01 04:51:471622 const NavigationController::LoadCommittedDetails& details,
[email protected]420ae012009-04-24 05:16:321623 const ViewHostMsg_FrameNavigate_Params& params) {
1624 if (profile()->IsOffTheRecord())
1625 return;
1626
1627 // Add to history service.
1628 HistoryService* hs = profile()->GetHistoryService(Profile::IMPLICIT_ACCESS);
1629 if (hs) {
1630 if (PageTransition::IsMainFrame(params.transition) &&
[email protected]ebe89e062009-08-13 23:16:541631 virtual_url != params.url) {
1632 // Hack on the "virtual" URL so that it will appear in history. For some
[email protected]420ae012009-04-24 05:16:321633 // types of URLs, we will display a magic URL that is different from where
1634 // the page is actually navigated. We want the user to see in history
[email protected]ebe89e062009-08-13 23:16:541635 // what they saw in the URL bar, so we add the virtual URL as a redirect.
1636 // This only applies to the main frame, as the virtual URL doesn't apply
[email protected]420ae012009-04-24 05:16:321637 // to sub-frames.
1638 std::vector<GURL> redirects = params.redirects;
1639 if (!redirects.empty())
[email protected]ebe89e062009-08-13 23:16:541640 redirects.back() = virtual_url;
1641 hs->AddPage(virtual_url, this, params.page_id, params.referrer,
[email protected]befd8d822009-07-01 04:51:471642 params.transition, redirects, details.did_replace_entry);
[email protected]420ae012009-04-24 05:16:321643 } else {
1644 hs->AddPage(params.url, this, params.page_id, params.referrer,
[email protected]befd8d822009-07-01 04:51:471645 params.transition, params.redirects,
1646 details.did_replace_entry);
[email protected]420ae012009-04-24 05:16:321647 }
1648 }
1649}
1650
1651bool TabContents::UpdateTitleForEntry(NavigationEntry* entry,
1652 const std::wstring& title) {
1653 // For file URLs without a title, use the pathname instead. In the case of a
1654 // synthesized title, we don't want the update to count toward the "one set
1655 // per page of the title to history."
1656 std::wstring final_title;
1657 bool explicit_set;
1658 if (entry->url().SchemeIsFile() && title.empty()) {
1659 final_title = UTF8ToWide(entry->url().ExtractFileName());
1660 explicit_set = false; // Don't count synthetic titles toward the set limit.
1661 } else {
1662 TrimWhitespace(title, TRIM_ALL, &final_title);
1663 explicit_set = true;
1664 }
1665
1666 if (final_title == UTF16ToWideHack(entry->title()))
1667 return false; // Nothing changed, don't bother.
1668
1669 entry->set_title(WideToUTF16Hack(final_title));
1670
1671 // Update the history system for this page.
1672 if (!profile()->IsOffTheRecord() && !received_page_title_) {
1673 HistoryService* hs =
1674 profile()->GetHistoryService(Profile::IMPLICIT_ACCESS);
1675 if (hs)
[email protected]ebe89e062009-08-13 23:16:541676 hs->SetPageTitle(entry->virtual_url(), final_title);
[email protected]420ae012009-04-24 05:16:321677
1678 // Don't allow the title to be saved again for explicitly set ones.
1679 received_page_title_ = explicit_set;
1680 }
1681
1682 // Lastly, set the title for the view.
1683 view_->SetPageTitle(final_title);
1684
1685 return true;
1686}
1687
1688void TabContents::NotifySwapped() {
1689 // After sending out a swap notification, we need to send a disconnect
1690 // notification so that clients that pick up a pointer to |this| can NULL the
1691 // pointer. See Bug 1230284.
1692 notify_disconnection_ = true;
1693 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341694 NotificationType::TAB_CONTENTS_SWAPPED,
[email protected]d82ed61e2009-06-16 02:46:221695 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321696 NotificationService::NoDetails());
1697}
1698
1699void TabContents::NotifyConnected() {
1700 notify_disconnection_ = true;
1701 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341702 NotificationType::TAB_CONTENTS_CONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221703 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321704 NotificationService::NoDetails());
1705}
1706
1707void TabContents::NotifyDisconnected() {
1708 if (!notify_disconnection_)
1709 return;
1710
1711 notify_disconnection_ = false;
1712 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341713 NotificationType::TAB_CONTENTS_DISCONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221714 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321715 NotificationService::NoDetails());
1716}
1717
1718void TabContents::GenerateKeywordIfNecessary(
1719 const ViewHostMsg_FrameNavigate_Params& params) {
1720 if (!params.searchable_form_url.is_valid())
1721 return;
1722
1723 if (profile()->IsOffTheRecord())
1724 return;
1725
1726 int last_index = controller_.last_committed_entry_index();
1727 // When there was no previous page, the last index will be 0. This is
1728 // normally due to a form submit that opened in a new tab.
1729 // TODO(brettw) bug 916126: we should support keywords when form submits
1730 // happen in new tabs.
1731 if (last_index <= 0)
1732 return;
1733 const NavigationEntry* previous_entry =
1734 controller_.GetEntryAtIndex(last_index - 1);
1735 if (IsFormSubmit(previous_entry)) {
1736 // Only generate a keyword if the previous page wasn't itself a form
1737 // submit.
1738 return;
1739 }
1740
1741 GURL keyword_url = previous_entry->user_typed_url().is_valid() ?
1742 previous_entry->user_typed_url() : previous_entry->url();
1743 std::wstring keyword =
1744 TemplateURLModel::GenerateKeyword(keyword_url, true); // autodetected
1745 if (keyword.empty())
1746 return;
1747
1748 TemplateURLModel* url_model = profile()->GetTemplateURLModel();
1749 if (!url_model)
1750 return;
1751
1752 if (!url_model->loaded()) {
1753 url_model->Load();
1754 return;
1755 }
1756
1757 const TemplateURL* current_url;
1758 std::wstring url = UTF8ToWide(params.searchable_form_url.spec());
1759 if (!url_model->CanReplaceKeyword(keyword, url, &current_url))
1760 return;
1761
1762 if (current_url) {
1763 if (current_url->originating_url().is_valid()) {
1764 // The existing keyword was generated from an OpenSearch description
1765 // document, don't regenerate.
1766 return;
1767 }
1768 url_model->Remove(current_url);
1769 }
1770 TemplateURL* new_url = new TemplateURL();
1771 new_url->set_keyword(keyword);
1772 new_url->set_short_name(keyword);
1773 new_url->SetURL(url, 0, 0);
1774 new_url->add_input_encoding(params.searchable_form_encoding);
1775 DCHECK(controller_.GetLastCommittedEntry());
1776 const GURL& favicon_url =
1777 controller_.GetLastCommittedEntry()->favicon().url();
1778 if (favicon_url.is_valid()) {
1779 new_url->SetFavIconURL(favicon_url);
1780 } else {
1781 // The favicon url isn't valid. This means there really isn't a favicon,
1782 // or the favicon url wasn't obtained before the load started. This assumes
1783 // the later.
1784 // TODO(sky): Need a way to set the favicon that doesn't involve generating
1785 // its url.
1786 new_url->SetFavIconURL(TemplateURL::GenerateFaviconURL(params.referrer));
1787 }
1788 new_url->set_safe_for_autoreplace(true);
1789 url_model->Add(new_url);
1790}
1791
[email protected]bf5c2ff392009-07-08 16:24:331792void TabContents::OnUserGesture() {
1793 // See comment in RenderViewHostDelegate::OnUserGesture as to why we do this.
1794 DownloadRequestManager* drm = g_browser_process->download_request_manager();
1795 if (drm)
1796 drm->OnUserGesture(this);
[email protected]e7eaedde2009-09-25 20:09:491797 ExternalProtocolHandler::OnUserGesture();
[email protected]bf5c2ff392009-07-08 16:24:331798 controller_.OnUserGesture();
1799}
1800
1801void TabContents::OnFindReply(int request_id,
1802 int number_of_matches,
1803 const gfx::Rect& selection_rect,
1804 int active_match_ordinal,
1805 bool final_update) {
[email protected]3ce7ced2009-07-24 18:52:421806 // Ignore responses for requests that have been aborted.
1807 if (find_op_aborted_)
1808 return;
1809
[email protected]bf5c2ff392009-07-08 16:24:331810 // Ignore responses for requests other than the one we have most recently
1811 // issued. That way we won't act on stale results when the user has
1812 // already typed in another query.
1813 if (request_id != current_find_request_id_)
1814 return;
1815
1816 if (number_of_matches == -1)
1817 number_of_matches = last_search_result_.number_of_matches();
1818 if (active_match_ordinal == -1)
1819 active_match_ordinal = last_search_result_.active_match_ordinal();
1820
1821 gfx::Rect selection = selection_rect;
1822 if (selection.IsEmpty())
1823 selection = last_search_result_.selection_rect();
1824
1825 // Notify the UI, automation and any other observers that a find result was
1826 // found.
1827 last_search_result_ = FindNotificationDetails(request_id, number_of_matches,
1828 selection, active_match_ordinal,
1829 final_update);
1830 NotificationService::current()->Notify(
1831 NotificationType::FIND_RESULT_AVAILABLE,
1832 Source<TabContents>(this),
1833 Details<FindNotificationDetails>(&last_search_result_));
1834}
1835
1836void TabContents::GoToEntryAtOffset(int offset) {
[email protected]f9cc4c452009-10-13 14:56:381837 if (!delegate_ || delegate_->OnGoToEntryOffset(offset))
1838 controller_.GoToOffset(offset);
[email protected]bf5c2ff392009-07-08 16:24:331839}
1840
[email protected]bf5c2ff392009-07-08 16:24:331841void TabContents::OnMissingPluginStatus(int status) {
1842#if defined(OS_WIN)
1843// TODO(PORT): pull in when plug-ins work
1844 GetPluginInstaller()->OnMissingPluginStatus(status);
1845#endif
1846}
1847
1848void TabContents::OnCrashedPlugin(const FilePath& plugin_path) {
[email protected]bf5c2ff392009-07-08 16:24:331849 DCHECK(!plugin_path.value().empty());
1850
1851 std::wstring plugin_name = plugin_path.ToWStringHack();
[email protected]d32d46182009-07-23 00:09:351852#if defined(OS_WIN) || defined(OS_MACOSX)
[email protected]bf5c2ff392009-07-08 16:24:331853 scoped_ptr<FileVersionInfo> version_info(
1854 FileVersionInfo::CreateFileVersionInfo(plugin_path));
1855 if (version_info.get()) {
1856 const std::wstring& product_name = version_info->product_name();
[email protected]996fd702009-09-04 19:12:371857 if (!product_name.empty()) {
[email protected]bf5c2ff392009-07-08 16:24:331858 plugin_name = product_name;
[email protected]996fd702009-09-04 19:12:371859#if defined(OS_MACOSX)
1860 // Many plugins on the Mac have .plugin in the actual name, which looks
1861 // terrible, so look for that and strip it off if present.
1862 const std::wstring plugin_extension(L".plugin");
1863 if (EndsWith(plugin_name, plugin_extension, true))
1864 plugin_name.erase(plugin_name.length() - plugin_extension.length());
1865#endif // OS_MACOSX
1866 }
[email protected]bf5c2ff392009-07-08 16:24:331867 }
[email protected]4a560ed2009-07-13 23:35:261868#else
1869 NOTIMPLEMENTED() << " convert plugin path to plugin name";
1870#endif
[email protected]bf5c2ff392009-07-08 16:24:331871 AddInfoBar(new SimpleAlertInfoBarDelegate(
1872 this, l10n_util::GetStringF(IDS_PLUGIN_CRASHED_PROMPT, plugin_name),
1873 NULL));
[email protected]bf5c2ff392009-07-08 16:24:331874}
1875
1876void TabContents::OnCrashedWorker() {
1877 AddInfoBar(new SimpleAlertInfoBarDelegate(
1878 this, l10n_util::GetString(IDS_WEBWORKER_CRASHED_PROMPT),
1879 NULL));
1880}
1881
1882void TabContents::OnDidGetApplicationInfo(
1883 int32 page_id,
1884 const webkit_glue::WebApplicationInfo& info) {
[email protected]ed5431872009-11-17 08:39:511885 web_app_info_ = info;
[email protected]bf5c2ff392009-07-08 16:24:331886
[email protected]ed5431872009-11-17 08:39:511887 if (delegate())
1888 delegate()->OnDidGetApplicationInfo(this, page_id);
[email protected]bf5c2ff392009-07-08 16:24:331889}
1890
[email protected]a2cf65eb2010-01-18 08:20:381891void TabContents::OnPageContents(const GURL& url,
1892 int renderer_process_id,
1893 int32 page_id,
[email protected]a8a81292010-01-21 00:32:451894 const std::wstring& contents,
1895 const std::string& language) {
[email protected]a2cf65eb2010-01-18 08:20:381896 // Don't index any https pages. People generally don't want their bank
1897 // accounts, etc. indexed on their computer, especially since some of these
1898 // things are not marked cachable.
1899 // TODO(brettw) we may want to consider more elaborate heuristics such as
1900 // the cachability of the page. We may also want to consider subframes (this
1901 // test will still index subframes if the subframe is SSL).
1902 if (!url.SchemeIsSecure()) {
1903 Profile* p = profile();
1904 if (p && !p->IsOffTheRecord()) {
1905 HistoryService* hs = p->GetHistoryService(Profile::IMPLICIT_ACCESS);
1906 if (hs)
1907 hs->SetPageContents(url, contents);
1908 }
1909 }
[email protected]a8a81292010-01-21 00:32:451910
[email protected]3b1c1812010-02-12 21:53:551911 language_state_.LanguageDetermined(language);
[email protected]a8a81292010-01-21 00:32:451912
1913 std::string lang = language;
1914 NotificationService::current()->Notify(
1915 NotificationType::TAB_LANGUAGE_DETERMINED,
[email protected]7e0831b2010-02-01 08:45:151916 Source<TabContents>(this),
[email protected]a8a81292010-01-21 00:32:451917 Details<std::string>(&lang));
[email protected]a2cf65eb2010-01-18 08:20:381918}
1919
[email protected]7e0831b2010-02-01 08:45:151920void TabContents::OnPageTranslated(int32 page_id,
1921 const std::string& original_lang,
1922 const std::string& translated_lang) {
[email protected]3b1c1812010-02-12 21:53:551923 language_state_.set_current_language(translated_lang);
1924 language_state_.set_translation_pending(false);
[email protected]7e0831b2010-02-01 08:45:151925 std::pair<std::string, std::string> lang_pair =
1926 std::make_pair(original_lang, translated_lang);
1927 NotificationService::current()->Notify(
1928 NotificationType::PAGE_TRANSLATED,
1929 Source<TabContents>(this),
1930 Details<std::pair<std::string, std::string> >(&lang_pair));
1931}
1932
[email protected]bf5c2ff392009-07-08 16:24:331933void TabContents::DidStartProvisionalLoadForFrame(
1934 RenderViewHost* render_view_host,
1935 bool is_main_frame,
1936 const GURL& url) {
1937 ProvisionalLoadDetails details(is_main_frame,
1938 controller_.IsURLInPageNavigation(url),
1939 url, std::string(), false);
1940 NotificationService::current()->Notify(
1941 NotificationType::FRAME_PROVISIONAL_LOAD_START,
1942 Source<NavigationController>(&controller_),
1943 Details<ProvisionalLoadDetails>(&details));
1944}
1945
1946void TabContents::DidStartReceivingResourceResponse(
[email protected]3dbb80b2010-02-09 22:41:201947 const ResourceRequestDetails& details) {
[email protected]bf5c2ff392009-07-08 16:24:331948 NotificationService::current()->Notify(
1949 NotificationType::RESOURCE_RESPONSE_STARTED,
1950 Source<NavigationController>(&controller()),
[email protected]3dbb80b2010-02-09 22:41:201951 Details<const ResourceRequestDetails>(&details));
[email protected]bf5c2ff392009-07-08 16:24:331952}
1953
[email protected]3dbb80b2010-02-09 22:41:201954void TabContents::DidRedirectResource(
1955 const ResourceRedirectDetails& details) {
[email protected]bf5c2ff392009-07-08 16:24:331956 NotificationService::current()->Notify(
1957 NotificationType::RESOURCE_RECEIVED_REDIRECT,
1958 Source<NavigationController>(&controller()),
[email protected]3dbb80b2010-02-09 22:41:201959 Details<const ResourceRequestDetails>(&details));
[email protected]bf5c2ff392009-07-08 16:24:331960}
1961
1962void TabContents::DidLoadResourceFromMemoryCache(
1963 const GURL& url,
1964 const std::string& frame_origin,
1965 const std::string& main_frame_origin,
1966 const std::string& security_info) {
1967 // Send out a notification that we loaded a resource from our memory cache.
1968 int cert_id = 0, cert_status = 0, security_bits = 0;
1969 SSLManager::DeserializeSecurityInfo(security_info,
1970 &cert_id, &cert_status,
1971 &security_bits);
1972 LoadFromMemoryCacheDetails details(url, frame_origin, main_frame_origin,
[email protected]8cb5d5b2010-02-09 11:36:161973 GetRenderProcessHost()->id(), cert_id,
1974 cert_status);
[email protected]bf5c2ff392009-07-08 16:24:331975
1976 NotificationService::current()->Notify(
1977 NotificationType::LOAD_FROM_MEMORY_CACHE,
1978 Source<NavigationController>(&controller_),
1979 Details<LoadFromMemoryCacheDetails>(&details));
1980}
1981
[email protected]e3d60e5d2009-09-25 21:08:291982void TabContents::DidDisplayInsecureContent() {
[email protected]09843b5f2009-09-26 08:30:591983 controller_.ssl_manager()->DidDisplayInsecureContent();
[email protected]e3d60e5d2009-09-25 21:08:291984}
1985
1986void TabContents::DidRunInsecureContent(const std::string& security_origin) {
[email protected]09843b5f2009-09-26 08:30:591987 controller_.ssl_manager()->DidRunInsecureContent(security_origin);
[email protected]e3d60e5d2009-09-25 21:08:291988}
1989
[email protected]bf5c2ff392009-07-08 16:24:331990void TabContents::DidFailProvisionalLoadWithError(
1991 RenderViewHost* render_view_host,
1992 bool is_main_frame,
1993 int error_code,
1994 const GURL& url,
1995 bool showing_repost_interstitial) {
1996 if (net::ERR_ABORTED == error_code) {
1997 // EVIL HACK ALERT! Ignore failed loads when we're showing interstitials.
1998 // This means that the interstitial won't be torn down properly, which is
1999 // bad. But if we have an interstitial, go back to another tab type, and
2000 // then load the same interstitial again, we could end up getting the first
2001 // interstitial's "failed" message (as a result of the cancel) when we're on
2002 // the second one.
2003 //
2004 // We can't tell this apart, so we think we're tearing down the current page
2005 // which will cause a crash later one. There is also some code in
2006 // RenderViewHostManager::RendererAbortedProvisionalLoad that is commented
2007 // out because of this problem.
2008 //
2009 // http://code.google.com/p/chromium/issues/detail?id=2855
2010 // Because this will not tear down the interstitial properly, if "back" is
2011 // back to another tab type, the interstitial will still be somewhat alive
2012 // in the previous tab type. If you navigate somewhere that activates the
2013 // tab with the interstitial again, you'll see a flash before the new load
2014 // commits of the interstitial page.
2015 if (showing_interstitial_page()) {
2016 LOG(WARNING) << "Discarding message during interstitial.";
2017 return;
2018 }
2019
2020 // This will discard our pending entry if we cancelled the load (e.g., if we
2021 // decided to download the file instead of load it). Only discard the
2022 // pending entry if the URLs match, otherwise the user initiated a navigate
2023 // before the page loaded so that the discard would discard the wrong entry.
2024 NavigationEntry* pending_entry = controller_.pending_entry();
2025 if (pending_entry && pending_entry->url() == url) {
2026 controller_.DiscardNonCommittedEntries();
2027 // Update the URL display.
2028 NotifyNavigationStateChanged(TabContents::INVALIDATE_URL);
2029 }
2030
2031 render_manager_.RendererAbortedProvisionalLoad(render_view_host);
2032 }
2033
2034 // Send out a notification that we failed a provisional load with an error.
2035 ProvisionalLoadDetails details(is_main_frame,
2036 controller_.IsURLInPageNavigation(url),
2037 url, std::string(), false);
2038 details.set_error_code(error_code);
2039
2040 NotificationService::current()->Notify(
2041 NotificationType::FAIL_PROVISIONAL_LOAD_WITH_ERROR,
2042 Source<NavigationController>(&controller_),
2043 Details<ProvisionalLoadDetails>(&details));
2044}
2045
2046void TabContents::DocumentLoadedInFrame() {
2047 controller_.DocumentLoadedInFrame();
2048}
2049
[email protected]0de80162010-02-03 04:52:352050void TabContents::OnContentBlocked(ContentSettingsType type) {
[email protected]b1235062010-03-25 17:31:272051 // TODO(joth): remove once fully implemented.
2052 DCHECK(type != CONTENT_SETTINGS_TYPE_GEOLOCATION);
[email protected]0de80162010-02-03 04:52:352053 content_blocked_[type] = true;
2054 if (delegate_)
2055 delegate_->OnBlockedContentChange(this);
2056}
2057
[email protected]614c0e12010-03-25 10:12:562058void TabContents::OnGeolocationPermissionSet(const GURL& requesting_origin,
2059 bool allowed) {
2060 geolocation_content_settings_[requesting_origin] =
2061 allowed ? CONTENT_SETTING_ALLOW : CONTENT_SETTING_BLOCK;
2062 // TODO(bulach): rename OnBlockedContentChange to OnContentSettingsChange.
2063 if (delegate_)
2064 delegate_->OnBlockedContentChange(this);
2065}
2066
[email protected]8d3347f2009-07-09 22:00:212067RenderViewHostDelegate::View* TabContents::GetViewDelegate() {
[email protected]420ae012009-04-24 05:16:322068 return view_.get();
2069}
2070
[email protected]8d3347f2009-07-09 22:00:212071RenderViewHostDelegate::RendererManagement*
2072TabContents::GetRendererManagementDelegate() {
2073 return &render_manager_;
2074}
2075
[email protected]bf5c2ff392009-07-08 16:24:332076RenderViewHostDelegate::BrowserIntegration*
[email protected]8d3347f2009-07-09 22:00:212077 TabContents::GetBrowserIntegrationDelegate() {
2078 return this;
[email protected]bf5c2ff392009-07-08 16:24:332079}
2080
[email protected]8d3347f2009-07-09 22:00:212081RenderViewHostDelegate::Resource* TabContents::GetResourceDelegate() {
2082 return this;
[email protected]bf5c2ff392009-07-08 16:24:332083}
2084
[email protected]8d3347f2009-07-09 22:00:212085RenderViewHostDelegate::Save* TabContents::GetSaveDelegate() {
[email protected]420ae012009-04-24 05:16:322086 return save_package_.get(); // May be NULL, but we can return NULL.
2087}
2088
[email protected]8d3347f2009-07-09 22:00:212089RenderViewHostDelegate::Printing* TabContents::GetPrintingDelegate() {
2090 return &printing_;
2091}
2092
2093RenderViewHostDelegate::FavIcon* TabContents::GetFavIconDelegate() {
2094 return &fav_icon_helper_;
2095}
2096
[email protected]e8dbd674b2010-03-11 18:34:402097RenderViewHostDelegate::Autocomplete* TabContents::GetAutocompleteDelegate() {
[email protected]6db0def92010-03-02 19:55:452098 if (autocomplete_history_manager_.get() == NULL)
2099 autocomplete_history_manager_.reset(new AutocompleteHistoryManager(this));
2100 return autocomplete_history_manager_.get();
[email protected]bf5c2ff392009-07-08 16:24:332101}
2102
[email protected]21674df2009-10-28 19:51:212103RenderViewHostDelegate::AutoFill* TabContents::GetAutoFillDelegate() {
2104 if (autofill_manager_.get() == NULL)
2105 autofill_manager_.reset(new AutoFillManager(this));
2106 return autofill_manager_.get();
2107}
2108
[email protected]fd571bb2010-03-19 01:23:542109AutomationResourceRoutingDelegate*
2110TabContents::GetAutomationResourceRoutingDelegate() {
2111 return delegate();
2112}
2113
[email protected]ced90ae12010-02-20 02:06:162114RenderViewHostDelegate::BookmarkDrag* TabContents::GetBookmarkDragDelegate() {
2115 return bookmark_drag_;
2116}
2117
2118void TabContents::SetBookmarkDragDelegate(
2119 RenderViewHostDelegate::BookmarkDrag* bookmark_drag) {
2120 bookmark_drag_ = bookmark_drag;
2121}
2122
[email protected]93623c5d2009-12-10 21:40:322123RendererPreferences TabContents::GetRendererPrefs(Profile* profile) const {
[email protected]840b1512009-07-21 16:53:462124 return renderer_preferences_;
[email protected]80d96fa2009-06-10 22:34:512125}
2126
[email protected]57c6a652009-05-04 07:58:342127TabContents* TabContents::GetAsTabContents() {
2128 return this;
2129}
2130
[email protected]7b291f92009-08-14 05:43:532131ViewType::Type TabContents::GetRenderViewType() const {
2132 return ViewType::TAB_CONTENTS;
2133}
2134
2135int TabContents::GetBrowserWindowID() const {
2136 return controller().window_id().id();
2137}
2138
[email protected]420ae012009-04-24 05:16:322139void TabContents::RenderViewCreated(RenderViewHost* render_view_host) {
[email protected]2e4633c2009-07-09 16:58:062140 NotificationService::current()->Notify(
2141 NotificationType::RENDER_VIEW_HOST_CREATED_FOR_TAB,
2142 Source<TabContents>(this),
2143 Details<RenderViewHost>(render_view_host));
[email protected]420ae012009-04-24 05:16:322144 NavigationEntry* entry = controller_.GetActiveEntry();
2145 if (!entry)
2146 return;
2147
2148 // When we're creating views, we're still doing initial setup, so we always
2149 // use the pending DOM UI rather than any possibly existing committed one.
2150 if (render_manager_.pending_dom_ui()) {
2151 render_manager_.pending_dom_ui()->RenderViewCreated(render_view_host);
2152 }
2153
2154 if (entry->IsViewSourceMode()) {
2155 // Put the renderer in view source mode.
2156 render_view_host->Send(
2157 new ViewMsg_EnableViewSourceMode(render_view_host->routing_id()));
2158 }
[email protected]0666aef2009-05-13 19:48:082159
2160 view()->RenderViewCreated(render_view_host);
[email protected]420ae012009-04-24 05:16:322161}
2162
2163void TabContents::RenderViewReady(RenderViewHost* rvh) {
2164 if (rvh != render_view_host()) {
2165 // Don't notify the world, since this came from a renderer in the
2166 // background.
2167 return;
2168 }
2169
2170 NotifyConnected();
[email protected]9d8a4642009-07-29 17:25:302171 bool was_crashed = is_crashed();
[email protected]420ae012009-04-24 05:16:322172 SetIsCrashed(false);
[email protected]9d8a4642009-07-29 17:25:302173
2174 // Restore the focus to the tab (otherwise the focus will be on the top
2175 // window).
2176 if (was_crashed && !FocusLocationBarByDefault())
2177 Focus();
[email protected]420ae012009-04-24 05:16:322178}
2179
2180void TabContents::RenderViewGone(RenderViewHost* rvh) {
2181 // Ask the print preview if this renderer was valuable.
2182 if (!printing_.OnRenderViewGone(rvh))
2183 return;
2184 if (rvh != render_view_host()) {
2185 // The pending page's RenderViewHost is gone.
2186 return;
2187 }
2188
2189 SetIsLoading(false, NULL);
2190 NotifyDisconnected();
2191 SetIsCrashed(true);
2192
[email protected]ff9efc62010-02-18 22:10:542193 // Remove all infobars.
[email protected]e5430c92010-02-18 22:39:212194 for (int i = infobar_delegate_count() - 1; i >=0 ; --i)
[email protected]ff9efc62010-02-18 22:10:542195 RemoveInfoBar(GetInfoBarDelegateAt(i));
2196
[email protected]8e537302009-06-10 21:19:582197 // Tell the view that we've crashed so it can prepare the sad tab page.
[email protected]cf68b092010-01-15 20:05:552198 // Only do this if we're not in browser shutdown, so that TabContents
2199 // objects that are not in a browser (e.g., HTML dialogs) and thus are
2200 // visible do not flash a sad tab page.
2201 if (browser_shutdown::GetShutdownType() == browser_shutdown::NOT_VALID)
2202 view_->OnTabCrashed();
[email protected]420ae012009-04-24 05:16:322203
2204 // Hide any visible hung renderer warning for this web contents' process.
[email protected]e7b8d832009-11-20 22:41:002205 hung_renderer_dialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:322206}
2207
[email protected]2e4633c2009-07-09 16:58:062208void TabContents::RenderViewDeleted(RenderViewHost* rvh) {
2209 NotificationService::current()->Notify(
2210 NotificationType::RENDER_VIEW_HOST_DELETED,
2211 Source<TabContents>(this),
2212 Details<RenderViewHost>(rvh));
[email protected]fb7b79f72009-11-06 18:00:482213 render_manager_.RenderViewDeleted(rvh);
[email protected]2e4633c2009-07-09 16:58:062214}
2215
[email protected]420ae012009-04-24 05:16:322216void TabContents::DidNavigate(RenderViewHost* rvh,
2217 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]8030f012009-09-25 18:09:372218 int extra_invalidate_flags = 0;
2219
2220 if (PageTransition::IsMainFrame(params.transition)) {
[email protected]20ede072009-10-06 00:28:352221 bool was_bookmark_bar_visible = ShouldShowBookmarkBar();
[email protected]8030f012009-09-25 18:09:372222 bool was_extension_shelf_visible = IsExtensionShelfAlwaysVisible();
2223
[email protected]420ae012009-04-24 05:16:322224 render_manager_.DidNavigateMainFrame(rvh);
2225
[email protected]20ede072009-10-06 00:28:352226 if (was_bookmark_bar_visible != ShouldShowBookmarkBar())
[email protected]8030f012009-09-25 18:09:372227 extra_invalidate_flags |= INVALIDATE_BOOKMARK_BAR;
2228 if (was_extension_shelf_visible != IsExtensionShelfAlwaysVisible())
2229 extra_invalidate_flags |= INVALIDATE_EXTENSION_SHELF;
2230 }
2231
[email protected]420ae012009-04-24 05:16:322232 // Update the site of the SiteInstance if it doesn't have one yet.
2233 if (!GetSiteInstance()->has_site())
2234 GetSiteInstance()->SetSite(params.url);
2235
2236 // Need to update MIME type here because it's referred to in
2237 // UpdateNavigationCommands() called by RendererDidNavigate() to
2238 // determine whether or not to enable the encoding menu.
2239 // It's updated only for the main frame. For a subframe,
2240 // RenderView::UpdateURL does not set params.contents_mime_type.
2241 // (see http://code.google.com/p/chromium/issues/detail?id=2929 )
2242 // TODO(jungshik): Add a test for the encoding menu to avoid
2243 // regressing it again.
2244 if (PageTransition::IsMainFrame(params.transition))
2245 contents_mime_type_ = params.contents_mime_type;
2246
2247 NavigationController::LoadCommittedDetails details;
[email protected]8030f012009-09-25 18:09:372248 bool did_navigate = controller_.RendererDidNavigate(
2249 params, extra_invalidate_flags, &details);
[email protected]3e90d4a2009-07-03 17:38:392250
2251 // Update history. Note that this needs to happen after the entry is complete,
2252 // which WillNavigate[Main,Sub]Frame will do before this function is called.
[email protected]2e29d922009-10-22 22:13:352253 if (params.should_update_history &&
2254 (!delegate() || delegate()->ShouldAddNavigationsToHistory())) {
[email protected]3e90d4a2009-07-03 17:38:392255 // Most of the time, the displayURL matches the loaded URL, but for about:
2256 // URLs, we use a data: URL as the real value. We actually want to save
2257 // the about: URL to the history db and keep the data: URL hidden. This is
2258 // what the TabContents' URL getter does.
2259 UpdateHistoryForNavigation(GetURL(), details, params);
2260 }
2261
[email protected]76543b92009-08-31 17:27:452262 if (!did_navigate)
2263 return; // No navigation happened.
[email protected]420ae012009-04-24 05:16:322264
2265 // DO NOT ADD MORE STUFF TO THIS FUNCTION! Your component should either listen
2266 // for the appropriate notification (best) or you can add it to
2267 // DidNavigateMainFramePostCommit / DidNavigateAnyFramePostCommit (only if
2268 // necessary, please).
2269
2270 // Run post-commit tasks.
2271 if (details.is_main_frame)
2272 DidNavigateMainFramePostCommit(details, params);
2273 DidNavigateAnyFramePostCommit(rvh, details, params);
2274}
2275
2276void TabContents::UpdateState(RenderViewHost* rvh,
2277 int32 page_id,
2278 const std::string& state) {
2279 DCHECK(rvh == render_view_host());
2280
2281 // We must be prepared to handle state updates for any page, these occur
2282 // when the user is scrolling and entering form data, as well as when we're
2283 // leaving a page, in which case our state may have already been moved to
2284 // the next page. The navigation controller will look up the appropriate
2285 // NavigationEntry and update it when it is notified via the delegate.
2286
2287 int entry_index = controller_.GetEntryIndexWithPageID(
2288 GetSiteInstance(), page_id);
2289 if (entry_index < 0)
2290 return;
2291 NavigationEntry* entry = controller_.GetEntryAtIndex(entry_index);
2292
2293 if (state == entry->content_state())
2294 return; // Nothing to update.
2295 entry->set_content_state(state);
2296 controller_.NotifyEntryChanged(entry, entry_index);
2297}
2298
2299void TabContents::UpdateTitle(RenderViewHost* rvh,
2300 int32 page_id, const std::wstring& title) {
2301 // If we have a title, that's a pretty good indication that we've started
2302 // getting useful data.
2303 SetNotWaitingForResponse();
2304
2305 DCHECK(rvh == render_view_host());
2306 NavigationEntry* entry = controller_.GetEntryWithPageID(GetSiteInstance(),
2307 page_id);
2308 if (!entry || !UpdateTitleForEntry(entry, title))
2309 return;
2310
2311 // Broadcast notifications when the UI should be updated.
2312 if (entry == controller_.GetEntryAtOffset(0))
[email protected]f1cd5e82009-10-23 17:19:032313 NotifyNavigationStateChanged(INVALIDATE_TITLE);
[email protected]420ae012009-04-24 05:16:322314}
2315
[email protected]420ae012009-04-24 05:16:322316void TabContents::UpdateEncoding(RenderViewHost* render_view_host,
[email protected]41fc0322009-09-04 22:23:402317 const std::string& encoding) {
[email protected]420ae012009-04-24 05:16:322318 set_encoding(encoding);
2319}
2320
2321void TabContents::UpdateTargetURL(int32 page_id, const GURL& url) {
2322 if (delegate())
2323 delegate()->UpdateTargetURL(this, url);
2324}
2325
2326void TabContents::UpdateThumbnail(const GURL& url,
2327 const SkBitmap& bitmap,
2328 const ThumbnailScore& score) {
2329 // Tell History about this thumbnail
[email protected]449478302009-06-09 20:04:282330 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kThumbnailStore)) {
[email protected]72d0fc72009-07-09 17:30:492331 if (!profile()->IsOffTheRecord())
[email protected]42339402009-07-17 17:27:192332 profile()->GetThumbnailStore()->SetPageThumbnail(url, bitmap,
2333 score, true);
[email protected]449478302009-06-09 20:04:282334 } else {
2335 HistoryService* hs;
2336 if (!profile()->IsOffTheRecord() &&
2337 (hs = profile()->GetHistoryService(Profile::IMPLICIT_ACCESS))) {
2338 hs->SetPageThumbnail(url, bitmap, score);
2339 }
[email protected]420ae012009-04-24 05:16:322340 }
2341}
2342
[email protected]8922e1f2009-10-03 05:01:262343void TabContents::UpdateInspectorSettings(const std::string& raw_settings) {
[email protected]0df30122009-06-03 12:13:082344 profile()->GetPrefs()->SetString(prefs::kWebKitInspectorSettings,
[email protected]8922e1f2009-10-03 05:01:262345 UTF8ToWide(raw_settings));
[email protected]0df30122009-06-03 12:13:082346}
2347
[email protected]420ae012009-04-24 05:16:322348void TabContents::Close(RenderViewHost* rvh) {
[email protected]07707302009-11-06 00:50:292349 // The UI may be in an event-tracking loop, such as between the
2350 // mouse-down and mouse-up in text selection or a button click.
2351 // Defer the close until after tracking is complete, so that we
2352 // don't free objects out from under the UI.
2353 // TODO(shess): This could probably be integrated with the
2354 // IsDoingDrag() test below. Punting for now because I need more
2355 // research to understand how this impacts platforms other than Mac.
2356 // TODO(shess): This could get more fine-grained. For instance,
2357 // closing a tab in another window while selecting text in the
2358 // current window's Omnibox should be just fine.
2359 if (view()->IsEventTracking()) {
2360 view()->CloseTabAfterEventTracking();
2361 return;
2362 }
2363
[email protected]24a4d1062009-07-10 23:10:422364 // If we close the tab while we're in the middle of a drag, we'll crash.
2365 // Instead, cancel the drag and close it as soon as the drag ends.
2366 if (view()->IsDoingDrag()) {
2367 view()->CancelDragAndCloseTab();
2368 return;
2369 }
2370
[email protected]420ae012009-04-24 05:16:322371 // Ignore this if it comes from a RenderViewHost that we aren't showing.
2372 if (delegate() && rvh == render_view_host())
2373 delegate()->CloseContents(this);
2374}
2375
2376void TabContents::RequestMove(const gfx::Rect& new_bounds) {
2377 if (delegate() && delegate()->IsPopup(this))
2378 delegate()->MoveContents(this, new_bounds);
2379}
2380
[email protected]7ab1e7d62009-10-14 23:32:012381void TabContents::DidStartLoading() {
[email protected]420ae012009-04-24 05:16:322382 SetIsLoading(true, NULL);
2383}
2384
[email protected]7ab1e7d62009-10-14 23:32:012385void TabContents::DidStopLoading() {
[email protected]420ae012009-04-24 05:16:322386 scoped_ptr<LoadNotificationDetails> details;
2387
2388 NavigationEntry* entry = controller_.GetActiveEntry();
2389 // An entry may not exist for a stop when loading an initial blank page or
2390 // if an iframe injected by script into a blank page finishes loading.
2391 if (entry) {
[email protected]420ae012009-04-24 05:16:322392 base::TimeDelta elapsed = base::TimeTicks::Now() - current_load_start_;
2393
2394 details.reset(new LoadNotificationDetails(
[email protected]ebe89e062009-08-13 23:16:542395 entry->virtual_url(),
[email protected]420ae012009-04-24 05:16:322396 entry->transition_type(),
2397 elapsed,
2398 &controller_,
2399 controller_.GetCurrentEntryIndex()));
2400 }
2401
2402 // Tell PasswordManager we've finished a page load, which serves as a
2403 // green light to save pending passwords and reset itself.
2404 GetPasswordManager()->DidStopLoading();
2405
2406 SetIsLoading(false, details.get());
2407}
2408
[email protected]420ae012009-04-24 05:16:322409void TabContents::DidRedirectProvisionalLoad(int32 page_id,
2410 const GURL& source_url,
2411 const GURL& target_url) {
2412 NavigationEntry* entry;
2413 if (page_id == -1)
2414 entry = controller_.pending_entry();
2415 else
2416 entry = controller_.GetEntryWithPageID(GetSiteInstance(), page_id);
2417 if (!entry || entry->url() != source_url)
2418 return;
2419 entry->set_url(target_url);
2420}
2421
[email protected]420ae012009-04-24 05:16:322422void TabContents::RequestOpenURL(const GURL& url, const GURL& referrer,
2423 WindowOpenDisposition disposition) {
2424 if (render_manager_.dom_ui()) {
2425 // When we're a DOM UI, it will provide a page transition type for us (this
2426 // is so the new tab page can specify AUTO_BOOKMARK for automatically
2427 // generated suggestions).
2428 //
2429 // Note also that we hide the referrer for DOM UI pages. We don't really
[email protected]60e448982009-05-06 04:21:162430 // want web sites to see a referrer of "chrome://blah" (and some
2431 // chrome: URLs might have search terms or other stuff we don't want to
[email protected]420ae012009-04-24 05:16:322432 // send to the site), so we send no referrer.
2433 OpenURL(url, GURL(), disposition,
2434 render_manager_.dom_ui()->link_transition_type());
2435 } else {
2436 OpenURL(url, referrer, disposition, PageTransition::LINK);
2437 }
2438}
2439
[email protected]1ee614862009-07-06 20:29:182440void TabContents::DomOperationResponse(const std::string& json_string,
2441 int automation_id) {
2442}
2443
[email protected]420ae012009-04-24 05:16:322444void TabContents::ProcessDOMUIMessage(const std::string& message,
[email protected]e4dad9fb2009-10-06 18:15:582445 const Value* content,
[email protected]811bfe32009-07-01 08:46:252446 int request_id,
2447 bool has_callback) {
[email protected]420ae012009-04-24 05:16:322448 if (!render_manager_.dom_ui()) {
[email protected]c2e74fe82009-09-03 17:57:442449 // This can happen if someone uses window.open() to open an extension URL
2450 // from a non-extension context.
2451 render_view_host()->BlockExtensionRequest(request_id);
[email protected]420ae012009-04-24 05:16:322452 return;
2453 }
[email protected]811bfe32009-07-01 08:46:252454 render_manager_.dom_ui()->ProcessDOMUIMessage(message, content, request_id,
2455 has_callback);
[email protected]420ae012009-04-24 05:16:322456}
2457
2458void TabContents::ProcessExternalHostMessage(const std::string& message,
2459 const std::string& origin,
2460 const std::string& target) {
2461 if (delegate())
2462 delegate()->ForwardMessageToExternalHost(message, origin, target);
2463}
2464
[email protected]420ae012009-04-24 05:16:322465void TabContents::RunFileChooser(bool multiple_files,
2466 const string16& title,
2467 const FilePath& default_file) {
2468 if (!select_file_dialog_.get())
2469 select_file_dialog_ = SelectFileDialog::Create(this);
2470 SelectFileDialog::Type dialog_type =
2471 multiple_files ? SelectFileDialog::SELECT_OPEN_MULTI_FILE :
2472 SelectFileDialog::SELECT_OPEN_FILE;
2473 select_file_dialog_->SelectFile(dialog_type, title, default_file,
2474 NULL, 0, FILE_PATH_LITERAL(""),
2475 view_->GetTopLevelNativeWindow(), NULL);
2476}
2477
2478void TabContents::RunJavaScriptMessage(
2479 const std::wstring& message,
2480 const std::wstring& default_prompt,
2481 const GURL& frame_url,
2482 const int flags,
2483 IPC::Message* reply_msg,
2484 bool* did_suppress_message) {
2485 // Suppress javascript messages when requested and when inside a constrained
2486 // popup window (because that activates them and breaks them out of the
2487 // constrained window jail).
[email protected]91ed49a2009-07-07 21:29:552488 // Also suppress messages when showing an interstitial. The interstitial is
2489 // shown over the previous page, we don't want the hidden page dialogs to
2490 // interfere with the interstitial.
2491 bool suppress_this_message = suppress_javascript_messages_ ||
2492 showing_interstitial_page();
[email protected]420ae012009-04-24 05:16:322493 if (delegate())
2494 suppress_this_message |=
[email protected]b6c874582009-05-08 19:38:312495 (delegate()->GetConstrainingContents(this) != this);
[email protected]420ae012009-04-24 05:16:322496
2497 *did_suppress_message = suppress_this_message;
2498
2499 if (!suppress_this_message) {
2500 base::TimeDelta time_since_last_message(
2501 base::TimeTicks::Now() - last_javascript_message_dismissal_);
2502 bool show_suppress_checkbox = false;
2503 // Show a checkbox offering to suppress further messages if this message is
2504 // being displayed within kJavascriptMessageExpectedDelay of the last one.
2505 if (time_since_last_message <
2506 base::TimeDelta::FromMilliseconds(kJavascriptMessageExpectedDelay))
2507 show_suppress_checkbox = true;
2508
[email protected]d82ed61e2009-06-16 02:46:222509 RunJavascriptMessageBox(this, frame_url, flags, message, default_prompt,
[email protected]420ae012009-04-24 05:16:322510 show_suppress_checkbox, reply_msg);
2511 } else {
2512 // If we are suppressing messages, just reply as is if the user immediately
2513 // pressed "Cancel".
[email protected]beb440c2009-11-06 04:08:542514 OnMessageBoxClosed(reply_msg, false, std::wstring());
[email protected]420ae012009-04-24 05:16:322515 }
2516}
2517
2518void TabContents::RunBeforeUnloadConfirm(const std::wstring& message,
2519 IPC::Message* reply_msg) {
[email protected]7ab1e7d62009-10-14 23:32:012520 is_showing_before_unload_dialog_ = true;
[email protected]d82ed61e2009-06-16 02:46:222521 RunBeforeUnloadDialog(this, message, reply_msg);
[email protected]420ae012009-04-24 05:16:322522}
2523
2524void TabContents::ShowModalHTMLDialog(const GURL& url, int width, int height,
2525 const std::string& json_arguments,
2526 IPC::Message* reply_msg) {
2527 if (delegate()) {
2528 HtmlDialogUIDelegate* dialog_delegate =
2529 new ModalHtmlDialogDelegate(url, width, height, json_arguments,
[email protected]d82ed61e2009-06-16 02:46:222530 reply_msg, this);
[email protected]420ae012009-04-24 05:16:322531 delegate()->ShowHtmlDialog(dialog_delegate, NULL);
2532 }
2533}
2534
2535void TabContents::PasswordFormsSeen(
[email protected]ed3fb032009-06-16 19:50:562536 const std::vector<webkit_glue::PasswordForm>& forms) {
[email protected]420ae012009-04-24 05:16:322537 GetPasswordManager()->PasswordFormsSeen(forms);
2538}
2539
[email protected]420ae012009-04-24 05:16:322540// Checks to see if we should generate a keyword based on the OSDD, and if
2541// necessary uses TemplateURLFetcher to download the OSDD and create a keyword.
2542void TabContents::PageHasOSDD(RenderViewHost* render_view_host,
2543 int32 page_id, const GURL& url,
2544 bool autodetected) {
2545 // Make sure page_id is the current page, and the TemplateURLModel is loaded.
2546 DCHECK(url.is_valid());
2547 if (!IsActiveEntry(page_id))
2548 return;
2549 TemplateURLModel* url_model = profile()->GetTemplateURLModel();
2550 if (!url_model)
2551 return;
2552 if (!url_model->loaded()) {
2553 url_model->Load();
2554 return;
2555 }
2556 if (!profile()->GetTemplateURLFetcher())
2557 return;
2558
2559 if (profile()->IsOffTheRecord())
2560 return;
2561
2562 const NavigationEntry* entry = controller_.GetLastCommittedEntry();
2563 DCHECK(entry);
2564
2565 const NavigationEntry* base_entry = entry;
2566 if (IsFormSubmit(base_entry)) {
2567 // If the current page is a form submit, find the last page that was not
2568 // a form submit and use its url to generate the keyword from.
2569 int index = controller_.last_committed_entry_index() - 1;
2570 while (index >= 0 && IsFormSubmit(controller_.GetEntryAtIndex(index)))
2571 index--;
2572 if (index >= 0)
2573 base_entry = controller_.GetEntryAtIndex(index);
2574 else
2575 base_entry = NULL;
2576 }
2577
2578 // We want to use the user typed URL if available since that represents what
2579 // the user typed to get here, and fall back on the regular URL if not.
2580 if (!base_entry)
2581 return;
2582 GURL keyword_url = base_entry->user_typed_url().is_valid() ?
2583 base_entry->user_typed_url() : base_entry->url();
2584 if (!keyword_url.is_valid())
2585 return;
2586 std::wstring keyword = TemplateURLModel::GenerateKeyword(keyword_url,
2587 autodetected);
[email protected]a88dbdd52010-01-15 19:25:532588
2589 // For JS added OSDD empty keyword is OK because we will generate keyword
2590 // later from OSDD content.
2591 if (autodetected && keyword.empty())
[email protected]420ae012009-04-24 05:16:322592 return;
2593 const TemplateURL* template_url =
2594 url_model->GetTemplateURLForKeyword(keyword);
2595 if (template_url && (!template_url->safe_for_autoreplace() ||
2596 template_url->originating_url() == url)) {
2597 // Either there is a user created TemplateURL for this keyword, or the
2598 // keyword has the same OSDD url and we've parsed it.
2599 return;
2600 }
2601
2602 // Download the OpenSearch description document. If this is successful a
2603 // new keyword will be created when done.
[email protected]420ae012009-04-24 05:16:322604 profile()->GetTemplateURLFetcher()->ScheduleDownload(
2605 keyword,
2606 url,
2607 base_entry->favicon().url(),
[email protected]d0cc9fb52009-06-23 02:26:162608 this,
[email protected]420ae012009-04-24 05:16:322609 autodetected);
2610}
2611
[email protected]420ae012009-04-24 05:16:322612GURL TabContents::GetAlternateErrorPageURL() const {
2613 GURL url;
2614 // Disable alternate error pages when in OffTheRecord/Incognito mode.
2615 if (profile()->IsOffTheRecord())
2616 return url;
2617
2618 PrefService* prefs = profile()->GetPrefs();
2619 DCHECK(prefs);
2620 if (prefs->GetBoolean(prefs::kAlternateErrorPagesEnabled)) {
[email protected]a33457b82009-09-21 23:07:152621 url = google_util::AppendGoogleLocaleParam(
2622 GURL(google_util::kLinkDoctorBaseURL));
[email protected]420ae012009-04-24 05:16:322623 url = google_util::AppendGoogleTLDParam(url);
2624 }
2625 return url;
2626}
2627
2628WebPreferences TabContents::GetWebkitPrefs() {
[email protected]6c32ce72010-03-08 05:18:062629 Profile* profile = render_view_host()->process()->profile();
[email protected]7ccddb8c2009-08-04 17:36:552630 bool is_dom_ui = false;
[email protected]6c32ce72010-03-08 05:18:062631 return RenderViewHostDelegateHelper::GetWebkitPrefs(profile, is_dom_ui);
[email protected]420ae012009-04-24 05:16:322632}
2633
[email protected]fa1cf0b82010-01-15 21:49:442634void TabContents::OnIgnoredUIEvent() {
2635 if (constrained_window_count()) {
2636 ConstrainedWindow* window = *constrained_window_begin();
2637 window->FocusConstrainedWindow();
2638 }
2639}
2640
[email protected]420ae012009-04-24 05:16:322641void TabContents::OnJSOutOfMemory() {
2642 AddInfoBar(new SimpleAlertInfoBarDelegate(
2643 this, l10n_util::GetString(IDS_JS_OUT_OF_MEMORY_PROMPT), NULL));
2644}
2645
[email protected]420ae012009-04-24 05:16:322646void TabContents::OnCrossSiteResponse(int new_render_process_host_id,
2647 int new_request_id) {
[email protected]57c6a652009-05-04 07:58:342648 // Allows the TabContents to react when a cross-site response is ready to be
[email protected]420ae012009-04-24 05:16:322649 // delivered to a pending RenderViewHost. We must first run the onunload
2650 // handler of the old RenderViewHost before we can allow it to proceed.
2651 render_manager_.OnCrossSiteResponse(new_render_process_host_id,
2652 new_request_id);
2653}
2654
2655bool TabContents::CanBlur() const {
2656 return delegate() ? delegate()->CanBlur() : true;
2657}
2658
2659gfx::Rect TabContents::GetRootWindowResizerRect() const {
2660 if (delegate())
2661 return delegate()->GetRootWindowResizerRect();
2662 return gfx::Rect();
2663}
2664
2665void TabContents::RendererUnresponsive(RenderViewHost* rvh,
2666 bool is_during_unload) {
2667 if (is_during_unload) {
2668 // Hang occurred while firing the beforeunload/unload handler.
2669 // Pretend the handler fired so tab closing continues as if it had.
[email protected]829e7612009-04-25 01:15:112670 rvh->set_sudden_termination_allowed(true);
[email protected]420ae012009-04-24 05:16:322671
2672 if (!render_manager_.ShouldCloseTabOnUnresponsiveRenderer())
2673 return;
2674
2675 // If the tab hangs in the beforeunload/unload handler there's really
2676 // nothing we can do to recover. Pretend the unload listeners have
2677 // all fired and close the tab. If the hang is in the beforeunload handler
2678 // then the user will not have the option of cancelling the close.
2679 Close(rvh);
2680 return;
2681 }
2682
2683 if (render_view_host() && render_view_host()->IsRenderViewLive())
[email protected]e7b8d832009-11-20 22:41:002684 hung_renderer_dialog::ShowForTabContents(this);
[email protected]420ae012009-04-24 05:16:322685}
2686
2687void TabContents::RendererResponsive(RenderViewHost* render_view_host) {
[email protected]e7b8d832009-11-20 22:41:002688 hung_renderer_dialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:322689}
2690
2691void TabContents::LoadStateChanged(const GURL& url,
[email protected]094e5b22009-09-25 04:23:562692 net::LoadState load_state,
2693 uint64 upload_position,
2694 uint64 upload_size) {
[email protected]420ae012009-04-24 05:16:322695 load_state_ = load_state;
[email protected]094e5b22009-09-25 04:23:562696 upload_position_ = upload_position;
2697 upload_size_ = upload_size;
[email protected]d686e812009-06-03 19:10:292698 std::wstring languages =
2699 profile()->GetPrefs()->GetString(prefs::kAcceptLanguages);
[email protected]d686e812009-06-03 19:10:292700 std::string host = url.host();
[email protected]ce85f602009-11-07 01:34:532701 load_state_host_ =
2702 net::IDNToUnicode(host.c_str(), host.size(), languages, NULL);
[email protected]420ae012009-04-24 05:16:322703 if (load_state_ == net::LOAD_STATE_READING_RESPONSE)
2704 SetNotWaitingForResponse();
2705 if (is_loading())
[email protected]c9cd2222009-05-06 05:16:502706 NotifyNavigationStateChanged(INVALIDATE_LOAD | INVALIDATE_TAB);
[email protected]420ae012009-04-24 05:16:322707}
2708
[email protected]1ee614862009-07-06 20:29:182709bool TabContents::IsExternalTabContainer() const {
2710 if (!delegate())
2711 return false;
2712
2713 return delegate()->IsExternalTabContainer();
2714}
2715
[email protected]1ee614862009-07-06 20:29:182716void TabContents::DidInsertCSS() {
2717 // This RVHDelegate function is used for extensions and not us.
[email protected]420ae012009-04-24 05:16:322718}
2719
[email protected]130efb02009-09-18 18:54:352720void TabContents::FocusedNodeChanged() {
2721 NotificationService::current()->Notify(
2722 NotificationType::FOCUS_CHANGED_IN_PAGE,
2723 Source<RenderViewHost>(render_view_host()),
2724 NotificationService::NoDetails());
2725}
2726
[email protected]420ae012009-04-24 05:16:322727void TabContents::FileSelected(const FilePath& path,
2728 int index, void* params) {
2729 render_view_host()->FileSelected(path);
2730}
2731
2732void TabContents::MultiFilesSelected(const std::vector<FilePath>& files,
2733 void* params) {
2734 render_view_host()->MultiFilesSelected(files);
2735}
2736
2737void TabContents::FileSelectionCanceled(void* params) {
2738 // If the user cancels choosing a file to upload we pass back an
2739 // empty vector.
2740 render_view_host()->MultiFilesSelected(std::vector<FilePath>());
2741}
2742
2743void TabContents::BeforeUnloadFiredFromRenderManager(
2744 bool proceed,
2745 bool* proceed_to_fire_unload) {
2746 if (delegate())
2747 delegate()->BeforeUnloadFired(this, proceed, proceed_to_fire_unload);
2748}
2749
2750void TabContents::UpdateRenderViewSizeForRenderManager() {
2751 // TODO(brettw) this is a hack. See TabContentsView::SizeContents.
[email protected]0323ee42010-02-17 22:03:262752 gfx::Size size = view_->GetContainerSize();
2753 // 0x0 isn't a valid window size (minimal window size is 1x1) but it may be
2754 // here during container initialization and normal window size will be set
2755 // later. In case of tab duplication this resizing to 0x0 prevents setting
2756 // normal size later so just ignore it.
2757 if (!size.IsEmpty())
2758 view_->SizeContents(size);
[email protected]420ae012009-04-24 05:16:322759}
2760
2761DOMUI* TabContents::CreateDOMUIForRenderManager(const GURL& url) {
[email protected]d82ed61e2009-06-16 02:46:222762 return DOMUIFactory::CreateDOMUIForURL(this, url);
[email protected]420ae012009-04-24 05:16:322763}
2764
2765NavigationEntry*
2766TabContents::GetLastCommittedNavigationEntryForRenderManager() {
2767 return controller_.GetLastCommittedEntry();
2768}
2769
2770bool TabContents::CreateRenderViewForRenderManager(
2771 RenderViewHost* render_view_host) {
[email protected]811bfe32009-07-01 08:46:252772 // If the pending navigation is to a DOMUI, tell the RenderView about any
2773 // bindings it will need enabled.
[email protected]420ae012009-04-24 05:16:322774 if (render_manager_.pending_dom_ui())
[email protected]811bfe32009-07-01 08:46:252775 render_view_host->AllowBindings(
2776 render_manager_.pending_dom_ui()->bindings());
[email protected]1adff062009-06-02 18:39:552777
[email protected]420ae012009-04-24 05:16:322778 RenderWidgetHostView* rwh_view = view_->CreateViewForWidget(render_view_host);
[email protected]1a98a932009-11-17 00:12:522779
2780 scoped_refptr<URLRequestContextGetter> request_context = request_context_;
2781 if (!request_context.get())
2782 request_context = profile()->GetRequestContext();
2783
2784 if (!render_view_host->CreateRenderView(request_context))
[email protected]420ae012009-04-24 05:16:322785 return false;
2786
2787 // Now that the RenderView has been created, we need to tell it its size.
2788 rwh_view->SetSize(view_->GetContainerSize());
2789
2790 UpdateMaxPageIDIfNecessary(render_view_host->site_instance(),
2791 render_view_host);
2792 return true;
2793}
2794
2795void TabContents::Observe(NotificationType type,
2796 const NotificationSource& source,
2797 const NotificationDetails& details) {
2798 switch (type.value) {
2799 case NotificationType::BOOKMARK_MODEL_LOADED:
2800 // BookmarkModel finished loading, fall through to update starred state.
2801 case NotificationType::URLS_STARRED: {
2802 // Somewhere, a URL has been starred.
2803 // Ignore notifications for profiles other than our current one.
2804 Profile* source_profile = Source<Profile>(source).ptr();
2805 if (!source_profile || !source_profile->IsSameProfile(profile()))
2806 return;
2807
2808 UpdateStarredStateForCurrentURL();
2809 break;
2810 }
2811 case NotificationType::PREF_CHANGED: {
2812 std::wstring* pref_name_in = Details<std::wstring>(details).ptr();
2813 DCHECK(Source<PrefService>(source).ptr() == profile()->GetPrefs());
2814 if (*pref_name_in == prefs::kAlternateErrorPagesEnabled) {
2815 UpdateAlternateErrorPageURL();
2816 } else if (*pref_name_in == prefs::kDefaultCharset ||
2817 StartsWithASCII(WideToUTF8(*pref_name_in), "webkit.webprefs.", true)
2818 ) {
2819 UpdateWebPreferences();
2820 } else {
2821 NOTREACHED() << "unexpected pref change notification" << *pref_name_in;
2822 }
2823 break;
2824 }
2825 case NotificationType::RENDER_WIDGET_HOST_DESTROYED:
2826 view_->RenderWidgetHostDestroyed(Source<RenderWidgetHost>(source).ptr());
2827 break;
2828
2829 case NotificationType::NAV_ENTRY_COMMITTED: {
2830 DCHECK(&controller_ == Source<NavigationController>(source).ptr());
2831
2832 NavigationController::LoadCommittedDetails& committed_details =
2833 *(Details<NavigationController::LoadCommittedDetails>(details).ptr());
2834 ExpireInfoBars(committed_details);
2835 break;
2836 }
2837
[email protected]d3ba77272009-09-03 00:06:092838#if defined(OS_LINUX)
2839 case NotificationType::BROWSER_THEME_CHANGED: {
[email protected]7ff431e2010-01-07 18:18:542840 renderer_preferences_util::UpdateFromSystemSettings(
2841 &renderer_preferences_, profile());
[email protected]d3ba77272009-09-03 00:06:092842 render_view_host()->SyncRendererPrefs();
2843 break;
2844 }
2845#endif
2846
[email protected]6c32ce72010-03-08 05:18:062847 case NotificationType::USER_STYLE_SHEET_UPDATED:
2848 UpdateWebPreferences();
2849 break;
2850
[email protected]79580c62010-02-02 02:36:252851 case NotificationType::CONTENT_SETTINGS_CHANGED: {
2852 Details<HostContentSettingsMap::ContentSettingsDetails>
2853 settings_details(details);
[email protected]79580c62010-02-02 02:36:252854 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]da0c8e92010-02-09 23:25:492855 GURL entry_url;
2856 std::string host;
2857 if (entry) {
2858 entry_url = entry->url();
2859 host = entry_url.host();
2860 }
[email protected]79580c62010-02-02 02:36:252861 Source<HostContentSettingsMap> content_settings(source);
2862 if (settings_details.ptr()->host().empty() ||
2863 settings_details.ptr()->host() == host) {
2864 render_view_host()->SendContentSettings(host,
[email protected]da0c8e92010-02-09 23:25:492865 content_settings.ptr()->GetContentSettings(entry_url));
[email protected]79580c62010-02-02 02:36:252866 }
2867 break;
2868 }
2869
[email protected]420ae012009-04-24 05:16:322870 default:
2871 NOTREACHED();
2872 }
2873}
[email protected]aacd7cc2009-09-02 14:51:102874
[email protected]beb440c2009-11-06 04:08:542875std::wstring TabContents::GetMessageBoxTitle(const GURL& frame_url,
2876 bool is_alert) {
2877 if (!frame_url.has_host())
2878 return l10n_util::GetString(
2879 is_alert ? IDS_JAVASCRIPT_ALERT_DEFAULT_TITLE
2880 : IDS_JAVASCRIPT_MESSAGEBOX_DEFAULT_TITLE);
2881
2882 // We really only want the scheme, hostname, and port.
2883 GURL::Replacements replacements;
2884 replacements.ClearUsername();
2885 replacements.ClearPassword();
2886 replacements.ClearPath();
2887 replacements.ClearQuery();
2888 replacements.ClearRef();
2889 GURL clean_url = frame_url.ReplaceComponents(replacements);
2890
2891 // TODO(brettw) it should be easier than this to do the correct language
2892 // handling without getting the accept language from the profile.
2893 std::wstring base_address = gfx::ElideUrl(clean_url, gfx::Font(), 0,
2894 profile()->GetPrefs()->GetString(prefs::kAcceptLanguages));
2895 // Force URL to have LTR directionality.
[email protected]7cf1b6ce2010-03-20 06:37:012896 if (base::i18n::IsRTL())
2897 base::i18n::WrapStringWithLTRFormatting(&base_address);
[email protected]beb440c2009-11-06 04:08:542898
2899 return l10n_util::GetStringF(
2900 is_alert ? IDS_JAVASCRIPT_ALERT_TITLE : IDS_JAVASCRIPT_MESSAGEBOX_TITLE,
2901 base_address);
2902}
2903
2904gfx::NativeWindow TabContents::GetMessageBoxRootWindow() {
2905 return view_->GetTopLevelNativeWindow();
2906}
2907
2908void TabContents::OnMessageBoxClosed(IPC::Message* reply_msg,
2909 bool success,
2910 const std::wstring& prompt) {
2911 last_javascript_message_dismissal_ = base::TimeTicks::Now();
2912 if (is_showing_before_unload_dialog_ && !success) {
2913 // If a beforeunload dialog is canceled, we need to stop the throbber from
2914 // spinning, since we forced it to start spinning in Navigate.
2915 DidStopLoading();
2916
2917 tab_close_start_time_ = base::TimeTicks();
2918 }
2919 is_showing_before_unload_dialog_ = false;
2920 render_view_host()->JavaScriptMessageBoxClosed(reply_msg, success, prompt);
2921}
2922
2923void TabContents::SetSuppressMessageBoxes(bool suppress_message_boxes) {
2924 set_suppress_javascript_messages(suppress_message_boxes);
2925}
2926
[email protected]41fc0322009-09-04 22:23:402927void TabContents::set_encoding(const std::string& encoding) {
[email protected]aacd7cc2009-09-02 14:51:102928 encoding_ = CharacterEncoding::GetCanonicalEncodingNameByAliasName(encoding);
2929}
[email protected]f45d2a72010-03-08 23:28:352930
2931void TabContents::SetAppIcon(const SkBitmap& app_icon) {
2932 app_icon_ = app_icon;
2933 NotifyNavigationStateChanged(INVALIDATE_TITLE);
2934}