blob: db8e51ff8ee91b522d09951951007dbe0e73d708 [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]beb440c2009-11-06 04:08:547#include "app/gfx/text_elider.h"
[email protected]a92b8642009-05-05 23:38:568#include "app/l10n_util.h"
[email protected]9929da92009-05-05 02:05:119#include "app/resource_bundle.h"
[email protected]8cc8d492010-02-02 10:40:4910#include "base/auto_reset.h"
[email protected]420ae012009-04-24 05:16:3211#include "base/file_version_info.h"
12#include "base/process_util.h"
[email protected]96d185d2009-04-24 03:28:5413#include "base/string16.h"
[email protected]996fd702009-09-04 19:12:3714#include "base/string_util.h"
[email protected]96d185d2009-04-24 03:28:5415#include "base/time.h"
[email protected]6db0def92010-03-02 19:55:4516#include "chrome/browser/autocomplete_history_manager.h"
[email protected]21674df2009-10-28 19:51:2117#include "chrome/browser/autofill/autofill_manager.h"
[email protected]66ba4932009-06-04 19:22:1318#include "chrome/browser/blocked_popup_container.h"
[email protected]420ae012009-04-24 05:16:3219#include "chrome/browser/bookmarks/bookmark_model.h"
[email protected]96d185d2009-04-24 03:28:5420#include "chrome/browser/browser_process.h"
[email protected]cf68b092010-01-15 20:05:5521#include "chrome/browser/browser_shutdown.h"
initial.commit09911bf2008-07-26 23:55:2922#include "chrome/browser/cert_store.h"
[email protected]aacd7cc2009-09-02 14:51:1023#include "chrome/browser/character_encoding.h"
[email protected]96d185d2009-04-24 03:28:5424#include "chrome/browser/debugger/devtools_manager.h"
[email protected]420ae012009-04-24 05:16:3225#include "chrome/browser/dom_operation_notification_details.h"
[email protected]96d185d2009-04-24 03:28:5426#include "chrome/browser/dom_ui/dom_ui.h"
27#include "chrome/browser/dom_ui/dom_ui_factory.h"
[email protected]bcef0dc02009-02-28 00:35:0228#include "chrome/browser/download/download_item_model.h"
[email protected]420ae012009-04-24 05:16:3229#include "chrome/browser/download/download_manager.h"
30#include "chrome/browser/download/download_request_manager.h"
[email protected]e7eaedde2009-09-25 20:09:4931#include "chrome/browser/external_protocol_handler.h"
[email protected]92c6f9b92009-10-24 04:35:0832#include "chrome/browser/extensions/extensions_service.h"
[email protected]0189bc722009-08-28 21:56:4833#include "chrome/browser/favicon_service.h"
[email protected]420ae012009-04-24 05:16:3234#include "chrome/browser/google_util.h"
[email protected]79580c62010-02-02 02:36:2535#include "chrome/browser/host_content_settings_map.h"
[email protected]8897c382009-05-06 17:53:2636#include "chrome/browser/hung_renderer_dialog.h"
[email protected]12f74a92010-02-05 22:32:1437#include "chrome/browser/message_box_handler.h"
[email protected]420ae012009-04-24 05:16:3238#include "chrome/browser/load_from_memory_cache_details.h"
39#include "chrome/browser/load_notification_details.h"
[email protected]84abba62009-10-07 17:01:4440#include "chrome/browser/metrics/metric_event_duration_details.h"
[email protected]ea0e80892009-08-22 00:36:0541#include "chrome/browser/modal_html_dialog_delegate.h"
[email protected]c8865482009-07-23 20:40:1042#include "chrome/browser/omnibox_search_hint.h"
[email protected]fdd61c62009-04-22 19:22:5743#include "chrome/browser/password_manager/password_manager.h"
44#include "chrome/browser/plugin_installer.h"
[email protected]052313b2010-02-19 09:43:0845#include "chrome/browser/pref_service.h"
[email protected]ce560f82009-06-03 09:39:4446#include "chrome/browser/profile.h"
[email protected]1db6ff152009-10-12 15:32:0747#include "chrome/browser/renderer_host/render_process_host.h"
[email protected]8cb5d5b2010-02-09 11:36:1648#include "chrome/browser/renderer_host/render_view_host.h"
[email protected]96d185d2009-04-24 03:28:5449#include "chrome/browser/renderer_host/render_widget_host_view.h"
[email protected]fd694982009-07-03 17:32:3750#include "chrome/browser/renderer_host/resource_request_details.h"
[email protected]1db6ff152009-10-12 15:32:0751#include "chrome/browser/renderer_host/site_instance.h"
[email protected]d4a00a72010-01-29 01:44:4252#include "chrome/browser/renderer_host/translation_service.h"
[email protected]96d185d2009-04-24 03:28:5453#include "chrome/browser/renderer_host/web_cache_manager.h"
[email protected]93623c5d2009-12-10 21:40:3254#include "chrome/browser/renderer_preferences_util.h"
[email protected]c37e3b62010-01-22 00:15:1755#include "chrome/browser/sessions/session_types.h"
[email protected]bd1ad682009-05-15 22:19:1756#include "chrome/browser/tab_contents/infobar_delegate.h"
[email protected]7e383692009-06-12 19:14:5457#include "chrome/browser/tab_contents/interstitial_page.h"
[email protected]f3ec7742009-01-15 00:59:1658#include "chrome/browser/tab_contents/navigation_entry.h"
[email protected]de34a962009-06-26 23:22:4359#include "chrome/browser/tab_contents/provisional_load_details.h"
[email protected]f3ec7742009-01-15 00:59:1660#include "chrome/browser/tab_contents/tab_contents_delegate.h"
[email protected]fdd61c62009-04-22 19:22:5761#include "chrome/browser/tab_contents/tab_contents_view.h"
[email protected]58dca552009-06-17 00:35:0262#include "chrome/browser/tab_contents/thumbnail_generator.h"
[email protected]449478302009-06-09 20:04:2863#include "chrome/browser/thumbnail_store.h"
[email protected]420ae012009-04-24 05:16:3264#include "chrome/browser/search_engines/template_url_fetcher.h"
65#include "chrome/browser/search_engines/template_url_model.h"
[email protected]35f7d212009-04-29 21:19:2766#include "chrome/common/chrome_switches.h"
[email protected]3b355642010-02-05 16:01:4967#include "chrome/common/extensions/extension.h"
[email protected]5d246db22009-10-27 06:17:5768#include "chrome/common/extensions/extension_action.h"
[email protected]bfd04a62009-02-01 18:16:5669#include "chrome/common/notification_service.h"
[email protected]89d6e6e2009-12-03 23:06:5170#include "chrome/common/platform_util.h"
initial.commit09911bf2008-07-26 23:55:2971#include "chrome/common/pref_names.h"
[email protected]420ae012009-04-24 05:16:3272#include "chrome/common/render_messages.h"
[email protected]1ee614862009-07-06 20:29:1873#include "chrome/common/renderer_preferences.h"
[email protected]96d185d2009-04-24 03:28:5474#include "chrome/common/url_constants.h"
[email protected]8897c382009-05-06 17:53:2675#include "grit/generated_resources.h"
[email protected]420ae012009-04-24 05:16:3276#include "grit/locale_settings.h"
[email protected]fd626782010-01-22 05:26:0677#include "grit/platform_locale_settings.h"
[email protected]420ae012009-04-24 05:16:3278#include "net/base/mime_util.h"
79#include "net/base/net_errors.h"
[email protected]d686e812009-06-03 19:10:2980#include "net/base/net_util.h"
[email protected]420ae012009-04-24 05:16:3281#include "net/base/registry_controlled_domain.h"
[email protected]1ee614862009-07-06 20:29:1882#include "webkit/glue/password_form.h"
83#include "webkit/glue/webpreferences.h"
[email protected]3e45ba92009-02-20 21:09:0084
[email protected]304f8792009-08-19 21:28:0985#if defined(OS_CHROMEOS)
86// For GdkScreen
87#include <gdk/gdk.h>
[email protected]0ba5c5e2009-08-20 16:47:1388#endif // defined(OS_CHROMEOS)
89
[email protected]420ae012009-04-24 05:16:3290// Cross-Site Navigations
91//
92// If a TabContents is told to navigate to a different web site (as determined
93// by SiteInstance), it will replace its current RenderViewHost with a new
94// RenderViewHost dedicated to the new SiteInstance. This works as follows:
95//
96// - Navigate determines whether the destination is cross-site, and if so,
97// it creates a pending_render_view_host_ and moves into the PENDING
98// RendererState.
99// - The pending RVH is "suspended," so that no navigation messages are sent to
100// its renderer until the onbeforeunload JavaScript handler has a chance to
101// run in the current RVH.
102// - The pending RVH tells CrossSiteRequestManager (a thread-safe singleton)
103// that it has a pending cross-site request. ResourceDispatcherHost will
104// check for this when the response arrives.
105// - The current RVH runs its onbeforeunload handler. If it returns false, we
106// cancel all the pending logic and go back to NORMAL. Otherwise we allow
107// the pending RVH to send the navigation request to its renderer.
108// - ResourceDispatcherHost receives a ResourceRequest on the IO thread. It
109// checks CrossSiteRequestManager to see that the RVH responsible has a
110// pending cross-site request, and then installs a CrossSiteEventHandler.
111// - When RDH receives a response, the BufferedEventHandler determines whether
112// it is a download. If so, it sends a message to the new renderer causing
113// it to cancel the request, and the download proceeds in the download
114// thread. For now, we stay in a PENDING state (with a pending RVH) until
[email protected]57c6a652009-05-04 07:58:34115// the next DidNavigate event for this TabContents. This isn't ideal, but it
[email protected]420ae012009-04-24 05:16:32116// doesn't affect any functionality.
117// - After RDH receives a response and determines that it is safe and not a
118// download, it pauses the response to first run the old page's onunload
119// handler. It does this by asynchronously calling the OnCrossSiteResponse
[email protected]57c6a652009-05-04 07:58:34120// method of TabContents on the UI thread, which sends a ClosePage message
[email protected]420ae012009-04-24 05:16:32121// to the current RVH.
122// - Once the onunload handler is finished, a ClosePage_ACK message is sent to
123// the ResourceDispatcherHost, who unpauses the response. Data is then sent
124// to the pending RVH.
125// - The pending renderer sends a FrameNavigate message that invokes the
126// DidNavigate method. This replaces the current RVH with the
127// pending RVH and goes back to the NORMAL RendererState.
128
129namespace {
130
131// Amount of time we wait between when a key event is received and the renderer
132// is queried for its state and pushed to the NavigationEntry.
133const int kQueryStateDelay = 5000;
134
135const int kSyncWaitDelay = 40;
136
[email protected]304f8792009-08-19 21:28:09137#if defined(OS_CHROMEOS)
138// If a popup window is bigger than this fraction of the screen on chrome os,
139// turn it into a tab
140const float kMaxWidthFactor = 0.5;
141const float kMaxHeightFactor = 0.6;
142#endif
143
[email protected]420ae012009-04-24 05:16:32144// If another javascript message box is displayed within
145// kJavascriptMessageExpectedDelay of a previous javascript message box being
146// dismissed, display an option to suppress future message boxes from this
147// contents.
148const int kJavascriptMessageExpectedDelay = 1000;
149
[email protected]420ae012009-04-24 05:16:32150// The list of prefs we want to observe.
151const wchar_t* kPrefsToObserve[] = {
152 prefs::kAlternateErrorPagesEnabled,
153 prefs::kWebKitJavaEnabled,
154 prefs::kWebKitJavascriptEnabled,
155 prefs::kWebKitLoadsImagesAutomatically,
156 prefs::kWebKitPluginsEnabled,
157 prefs::kWebKitUsesUniversalDetector,
158 prefs::kWebKitSerifFontFamily,
159 prefs::kWebKitSansSerifFontFamily,
160 prefs::kWebKitFixedFontFamily,
161 prefs::kWebKitDefaultFontSize,
162 prefs::kWebKitDefaultFixedFontSize,
163 prefs::kDefaultCharset
164 // kWebKitStandardFontIsSerif needs to be added
165 // if we let users pick which font to use, serif or sans-serif when
166 // no font is specified or a CSS generic family (serif or sans-serif)
167 // is not specified.
168};
169
170const int kPrefsToObserveLength = arraysize(kPrefsToObserve);
171
[email protected]420ae012009-04-24 05:16:32172// Returns true if the entry's transition type is FORM_SUBMIT.
173bool IsFormSubmit(const NavigationEntry* entry) {
174 return (PageTransition::StripQualifier(entry->transition_type()) ==
175 PageTransition::FORM_SUBMIT);
176}
177
178#if defined(OS_WIN)
179
180BOOL CALLBACK InvalidateWindow(HWND hwnd, LPARAM lparam) {
181 // Note: erase is required to properly paint some widgets borders. This can
182 // be seen with textfields.
183 InvalidateRect(hwnd, NULL, TRUE);
184 return TRUE;
185}
186#endif
187
[email protected]5e369672009-11-03 23:48:30188ViewMsg_Navigate_Params::NavigationType GetNavigationType(
[email protected]1ccb3568d2010-02-19 10:51:16189 Profile* profile, const NavigationEntry& entry,
190 NavigationController::ReloadType reload_type) {
191 switch (reload_type) {
192 case NavigationController::RELOAD:
193 return ViewMsg_Navigate_Params::RELOAD;
194 case NavigationController::RELOAD_IGNORING_CACHE:
195 return ViewMsg_Navigate_Params::RELOAD_IGNORING_CACHE;
196 case NavigationController::NO_RELOAD:
197 break; // Fall through to rest of function.
198 }
[email protected]5e369672009-11-03 23:48:30199
200 if (entry.restore_type() == NavigationEntry::RESTORE_LAST_SESSION &&
201 profile->DidLastSessionExitCleanly())
202 return ViewMsg_Navigate_Params::RESTORE;
203
204 return ViewMsg_Navigate_Params::NORMAL;
205}
206
207void MakeNavigateParams(Profile* profile, const NavigationEntry& entry,
[email protected]1ccb3568d2010-02-19 10:51:16208 NavigationController::ReloadType reload_type,
209 ViewMsg_Navigate_Params* params) {
[email protected]056de2d2009-06-26 16:41:34210 params->page_id = entry.page_id();
211 params->url = entry.url();
212 params->referrer = entry.referrer();
213 params->transition = entry.transition_type();
214 params->state = entry.content_state();
[email protected]1ccb3568d2010-02-19 10:51:16215 params->navigation_type = GetNavigationType(profile, entry, reload_type);
[email protected]056de2d2009-06-26 16:41:34216 params->request_time = base::Time::Now();
217}
218
[email protected]420ae012009-04-24 05:16:32219} // namespace
220
221// -----------------------------------------------------------------------------
222
[email protected]fdd61c62009-04-22 19:22:57223// static
224int TabContents::find_request_id_counter_ = -1;
225
[email protected]420ae012009-04-24 05:16:32226
[email protected]420ae012009-04-24 05:16:32227TabContents::TabContents(Profile* profile,
228 SiteInstance* site_instance,
229 int routing_id,
[email protected]34ac70502009-09-25 17:07:23230 const TabContents* base_tab_contents)
[email protected]b680ad22009-04-15 23:19:42231 : delegate_(NULL),
[email protected]66ba4932009-06-04 19:22:13232 ALLOW_THIS_IN_INITIALIZER_LIST(controller_(this, profile)),
233 ALLOW_THIS_IN_INITIALIZER_LIST(view_(
[email protected]d82ed61e2009-06-16 02:46:22234 TabContentsView::Create(this))),
235 ALLOW_THIS_IN_INITIALIZER_LIST(render_manager_(this, this)),
[email protected]fdd61c62009-04-22 19:22:57236 property_bag_(),
237 registrar_(),
[email protected]d82ed61e2009-06-16 02:46:22238 ALLOW_THIS_IN_INITIALIZER_LIST(printing_(*this)),
[email protected]fdd61c62009-04-22 19:22:57239 save_package_(),
[email protected]6db0def92010-03-02 19:55:45240 autocomplete_history_manager_(),
[email protected]21674df2009-10-28 19:51:21241 autofill_manager_(),
[email protected]fdd61c62009-04-22 19:22:57242 password_manager_(),
243 plugin_installer_(),
[email protected]6f821dd2010-02-23 00:54:06244 bookmark_drag_(NULL),
[email protected]d82ed61e2009-06-16 02:46:22245 ALLOW_THIS_IN_INITIALIZER_LIST(fav_icon_helper_(this)),
[email protected]fdd61c62009-04-22 19:22:57246 select_file_dialog_(),
[email protected]d5f942ba2008-09-26 19:30:34247 is_loading_(false),
initial.commit09911bf2008-07-26 23:55:29248 is_crashed_(false),
[email protected]d5f942ba2008-09-26 19:30:34249 waiting_for_response_(false),
[email protected]d5f942ba2008-09-26 19:30:34250 max_page_id_(-1),
[email protected]fdd61c62009-04-22 19:22:57251 current_load_start_(),
252 load_state_(net::LOAD_STATE_IDLE),
253 load_state_host_(),
[email protected]094e5b22009-09-25 04:23:56254 upload_size_(0),
255 upload_position_(0),
[email protected]fdd61c62009-04-22 19:22:57256 received_page_title_(false),
257 is_starred_(false),
258 contents_mime_type_(),
259 encoding_(),
[email protected]332af7732009-03-11 13:21:35260 blocked_popups_(NULL),
[email protected]8cc8d492010-02-02 10:40:49261 dont_notify_render_view_(false),
[email protected]fdd61c62009-04-22 19:22:57262 infobar_delegates_(),
[email protected]fdd61c62009-04-22 19:22:57263 find_ui_active_(false),
264 find_op_aborted_(false),
265 current_find_request_id_(find_request_id_counter_++),
[email protected]e491f1c2009-05-22 20:28:12266 last_search_case_sensitive_(false),
267 last_search_prepopulate_text_(NULL),
268 last_search_result_(),
[email protected]fdd61c62009-04-22 19:22:57269 capturing_contents_(false),
270 is_being_destroyed_(false),
271 notify_disconnection_(false),
272 history_requests_(),
273#if defined(OS_WIN)
274 message_box_active_(CreateEvent(NULL, TRUE, FALSE, NULL)),
275#endif
276 last_javascript_message_dismissal_(),
[email protected]c2e74fe82009-09-03 17:57:44277 suppress_javascript_messages_(false),
[email protected]7ab1e7d62009-10-14 23:32:01278 is_showing_before_unload_dialog_(false),
[email protected]7ff431e2010-01-07 18:18:54279 renderer_preferences_(),
[email protected]1a242c32010-02-02 21:20:54280 opener_dom_ui_type_(DOMUIFactory::kNoDOMUI),
[email protected]3b1c1812010-02-12 21:53:55281 app_extension_(NULL),
282 language_state_(&controller_) {
[email protected]0de80162010-02-03 04:52:35283 ClearBlockedContentSettings();
[email protected]7ff431e2010-01-07 18:18:54284 renderer_preferences_util::UpdateFromSystemSettings(
285 &renderer_preferences_, profile);
286
[email protected]16779842009-07-08 23:45:29287#if defined(OS_CHROMEOS)
[email protected]58dca552009-06-17 00:35:02288 // Make sure the thumbnailer is started before starting the render manager.
289 // The thumbnailer will want to listen for RVH creations, one of which will
290 // happen in RVHManager::Init.
291 ThumbnailGenerator* generator = g_browser_process->GetThumbnailGenerator();
292 if (generator)
293 generator->StartThumbnailing();
294#endif
295
[email protected]12636df2009-09-28 22:32:21296 render_manager_.Init(profile, site_instance, routing_id);
[email protected]420ae012009-04-24 05:16:32297
[email protected]34ac70502009-09-25 17:07:23298 // We have the initial size of the view be based on the size of the passed in
299 // tab contents (normally a tab from the same window).
300 view_->CreateView(base_tab_contents ?
301 base_tab_contents->view()->GetContainerSize() : gfx::Size());
[email protected]420ae012009-04-24 05:16:32302
303 // Register for notifications about all interested prefs change.
304 PrefService* prefs = profile->GetPrefs();
305 if (prefs) {
306 for (int i = 0; i < kPrefsToObserveLength; ++i)
307 prefs->AddPrefObserver(kPrefsToObserve[i], this);
308 }
309
310 // Register for notifications about URL starredness changing on any profile.
311 registrar_.Add(this, NotificationType::URLS_STARRED,
312 NotificationService::AllSources());
313 registrar_.Add(this, NotificationType::BOOKMARK_MODEL_LOADED,
314 NotificationService::AllSources());
315 registrar_.Add(this, NotificationType::RENDER_WIDGET_HOST_DESTROYED,
316 NotificationService::AllSources());
[email protected]d3ba77272009-09-03 00:06:09317#if defined(OS_LINUX)
318 registrar_.Add(this, NotificationType::BROWSER_THEME_CHANGED,
319 NotificationService::AllSources());
320#endif
[email protected]420ae012009-04-24 05:16:32321
[email protected]6c32ce72010-03-08 05:18:06322 registrar_.Add(this, NotificationType::USER_STYLE_SHEET_UPDATED,
323 NotificationService::AllSources());
324
[email protected]79580c62010-02-02 02:36:25325 // Register for notifications about content setting changes.
326 registrar_.Add(this, NotificationType::CONTENT_SETTINGS_CHANGED,
327 NotificationService::AllSources());
328
[email protected]420ae012009-04-24 05:16:32329 // Keep a global copy of the previous search string (if any).
330 static string16 global_last_search = string16();
[email protected]e491f1c2009-05-22 20:28:12331 last_search_prepopulate_text_ = &global_last_search;
[email protected]840b1512009-07-21 16:53:46332
[email protected]c8865482009-07-23 20:40:10333 // Set-up the showing of the omnibox search infobar if applicable.
334 if (OmniboxSearchHint::IsEnabled(profile))
335 omnibox_search_hint_.reset(new OmniboxSearchHint(this));
[email protected]332af7732009-03-11 13:21:35336}
initial.commit09911bf2008-07-26 23:55:29337
338TabContents::~TabContents() {
[email protected]420ae012009-04-24 05:16:32339 is_being_destroyed_ = true;
340
[email protected]bfe4c1582009-10-02 18:11:09341 // We don't want any notifications while we're running our destructor.
[email protected]420ae012009-04-24 05:16:32342 registrar_.RemoveAll();
343
344 // Unregister the notifications of all observed prefs change.
345 PrefService* prefs = profile()->GetPrefs();
346 if (prefs) {
347 for (int i = 0; i < kPrefsToObserveLength; ++i)
348 prefs->RemovePrefObserver(kPrefsToObserve[i], this);
349 }
350
[email protected]420ae012009-04-24 05:16:32351 NotifyDisconnected();
[email protected]e7b8d832009-11-20 22:41:00352 hung_renderer_dialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:32353
[email protected]420ae012009-04-24 05:16:32354 // First cleanly close all child windows.
355 // TODO(mpcomplete): handle case if MaybeCloseChildWindows() already asked
356 // some of these to close. CloseWindows is async, so it might get called
357 // twice before it runs.
[email protected]8cc8d492010-02-02 10:40:49358 CloseConstrainedWindows();
[email protected]420ae012009-04-24 05:16:32359
[email protected]8cc8d492010-02-02 10:40:49360 // Close all blocked popups.
[email protected]66ba4932009-06-04 19:22:13361 if (blocked_popups_)
362 blocked_popups_->Destroy();
363
[email protected]420ae012009-04-24 05:16:32364 // Notify any observer that have a reference on this tab contents.
365 NotificationService::current()->Notify(
366 NotificationType::TAB_CONTENTS_DESTROYED,
367 Source<TabContents>(this),
368 NotificationService::NoDetails());
369
[email protected]1e5e74f2009-05-27 20:55:12370 // Notify any lasting InfobarDelegates that have not yet been removed that
371 // whatever infobar they were handling in this TabContents has closed,
372 // because the TabContents is going away entirely.
373 // This must happen after the TAB_CONTENTS_DESTROYED notification as the
374 // notification may trigger infobars calls that access their delegate. (and
375 // some implementations of InfoBarDelegate do delete themselves on
376 // InfoBarClosed()).
377 for (int i = 0; i < infobar_delegate_count(); ++i) {
378 InfoBarDelegate* delegate = GetInfoBarDelegateAt(i);
379 delegate->InfoBarClosed();
380 }
381 infobar_delegates_.clear();
382
[email protected]420ae012009-04-24 05:16:32383 // TODO(brettw) this should be moved to the view.
384#if defined(OS_WIN)
385 // If we still have a window handle, destroy it. GetNativeView can return
386 // NULL if this contents was part of a window that closed.
387 if (GetNativeView())
388 ::DestroyWindow(GetNativeView());
389#endif
[email protected]7ab1e7d62009-10-14 23:32:01390
391 // OnCloseStarted isn't called in unit tests.
392 if (!tab_close_start_time_.is_null()) {
393 UMA_HISTOGRAM_TIMES("Tab.Close",
394 base::TimeTicks::Now() - tab_close_start_time_);
395 }
initial.commit09911bf2008-07-26 23:55:29396}
397
[email protected]d5f942ba2008-09-26 19:30:34398// static
399void TabContents::RegisterUserPrefs(PrefService* prefs) {
[email protected]420ae012009-04-24 05:16:32400 prefs->RegisterBooleanPref(prefs::kAlternateErrorPagesEnabled, true);
401
402 WebPreferences pref_defaults;
403 prefs->RegisterBooleanPref(prefs::kWebKitJavascriptEnabled,
404 pref_defaults.javascript_enabled);
405 prefs->RegisterBooleanPref(prefs::kWebKitWebSecurityEnabled,
406 pref_defaults.web_security_enabled);
407 prefs->RegisterBooleanPref(
408 prefs::kWebKitJavascriptCanOpenWindowsAutomatically, true);
409 prefs->RegisterBooleanPref(prefs::kWebKitLoadsImagesAutomatically,
410 pref_defaults.loads_images_automatically);
411 prefs->RegisterBooleanPref(prefs::kWebKitPluginsEnabled,
412 pref_defaults.plugins_enabled);
413 prefs->RegisterBooleanPref(prefs::kWebKitDomPasteEnabled,
414 pref_defaults.dom_paste_enabled);
415 prefs->RegisterBooleanPref(prefs::kWebKitShrinksStandaloneImagesToFit,
416 pref_defaults.shrinks_standalone_images_to_fit);
[email protected]0df30122009-06-03 12:13:08417 prefs->RegisterStringPref(prefs::kWebKitInspectorSettings,
[email protected]8922e1f2009-10-03 05:01:26418 UTF8ToWide(pref_defaults.inspector_settings));
[email protected]420ae012009-04-24 05:16:32419 prefs->RegisterBooleanPref(prefs::kWebKitTextAreasAreResizable,
420 pref_defaults.text_areas_are_resizable);
421 prefs->RegisterBooleanPref(prefs::kWebKitJavaEnabled,
422 pref_defaults.java_enabled);
423
424 prefs->RegisterLocalizedStringPref(prefs::kAcceptLanguages,
425 IDS_ACCEPT_LANGUAGES);
426 prefs->RegisterLocalizedStringPref(prefs::kDefaultCharset,
427 IDS_DEFAULT_ENCODING);
428 prefs->RegisterLocalizedBooleanPref(prefs::kWebKitStandardFontIsSerif,
429 IDS_STANDARD_FONT_IS_SERIF);
430 prefs->RegisterLocalizedStringPref(prefs::kWebKitFixedFontFamily,
431 IDS_FIXED_FONT_FAMILY);
432 prefs->RegisterLocalizedStringPref(prefs::kWebKitSerifFontFamily,
433 IDS_SERIF_FONT_FAMILY);
434 prefs->RegisterLocalizedStringPref(prefs::kWebKitSansSerifFontFamily,
435 IDS_SANS_SERIF_FONT_FAMILY);
436 prefs->RegisterLocalizedStringPref(prefs::kWebKitCursiveFontFamily,
437 IDS_CURSIVE_FONT_FAMILY);
438 prefs->RegisterLocalizedStringPref(prefs::kWebKitFantasyFontFamily,
439 IDS_FANTASY_FONT_FAMILY);
440 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitDefaultFontSize,
441 IDS_DEFAULT_FONT_SIZE);
442 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitDefaultFixedFontSize,
443 IDS_DEFAULT_FIXED_FONT_SIZE);
444 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitMinimumFontSize,
445 IDS_MINIMUM_FONT_SIZE);
446 prefs->RegisterLocalizedIntegerPref(prefs::kWebKitMinimumLogicalFontSize,
447 IDS_MINIMUM_LOGICAL_FONT_SIZE);
448 prefs->RegisterLocalizedBooleanPref(prefs::kWebKitUsesUniversalDetector,
449 IDS_USES_UNIVERSAL_DETECTOR);
450 prefs->RegisterLocalizedStringPref(prefs::kStaticEncodings,
451 IDS_STATIC_ENCODING_LIST);
initial.commit09911bf2008-07-26 23:55:29452}
453
[email protected]13367f72009-07-08 16:39:02454// Returns true if contains content rendered by an extension.
455bool TabContents::HostsExtension() const {
456 return GetURL().SchemeIs(chrome::kExtensionScheme);
[email protected]d5f942ba2008-09-26 19:30:34457}
458
[email protected]6e95706d2009-04-23 22:55:01459PasswordManager* TabContents::GetPasswordManager() {
460 if (password_manager_.get() == NULL)
461 password_manager_.reset(new PasswordManager(this));
462 return password_manager_.get();
463}
464
465PluginInstaller* TabContents::GetPluginInstaller() {
466 if (plugin_installer_.get() == NULL)
467 plugin_installer_.reset(new PluginInstaller(this));
468 return plugin_installer_.get();
469}
470
[email protected]8cb5d5b2010-02-09 11:36:16471RenderProcessHost* TabContents::GetRenderProcessHost() const {
472 return render_manager_.current_host()->process();
473}
474
[email protected]3b355642010-02-05 16:01:49475void TabContents::SetAppExtension(Extension* extension) {
476 DCHECK(!extension || extension->IsApp());
477 app_extension_ = extension;
[email protected]fca656c2010-02-10 20:30:10478
479 NotificationService::current()->Notify(
480 NotificationType::TAB_CONTENTS_APPLICATION_EXTENSION_CHANGED,
481 Source<TabContents>(this),
482 NotificationService::NoDetails());
[email protected]3b355642010-02-05 16:01:49483}
484
[email protected]d5f942ba2008-09-26 19:30:34485const GURL& TabContents::GetURL() const {
486 // We may not have a navigation entry yet
[email protected]ce3fa3c2009-04-20 19:55:57487 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]ebe89e062009-08-13 23:16:54488 return entry ? entry->virtual_url() : GURL::EmptyGURL();
[email protected]d5f942ba2008-09-26 19:30:34489}
490
[email protected]96d185d2009-04-24 03:28:54491const string16& TabContents::GetTitle() const {
[email protected]4c6092c5b2009-06-06 00:23:55492 // Transient entries take precedence. They are used for interstitial pages
493 // that are shown on top of existing pages.
494 NavigationEntry* entry = controller_.GetTransientEntry();
495 if (entry)
496 return entry->GetTitleForDisplay(&controller_);
497
[email protected]96d185d2009-04-24 03:28:54498 DOMUI* our_dom_ui = render_manager_.pending_dom_ui() ?
499 render_manager_.pending_dom_ui() : render_manager_.dom_ui();
500 if (our_dom_ui) {
501 // Don't override the title in view source mode.
[email protected]4c6092c5b2009-06-06 00:23:55502 entry = controller_.GetActiveEntry();
[email protected]96d185d2009-04-24 03:28:54503 if (!(entry && entry->IsViewSourceMode())) {
504 // Give the DOM UI the chance to override our title.
505 const string16& title = our_dom_ui->overridden_title();
506 if (!title.empty())
507 return title;
508 }
509 }
510
511 // We use the title for the last committed entry rather than a pending
512 // navigation entry. For example, when the user types in a URL, we want to
513 // keep the old page's title until the new load has committed and we get a new
514 // title.
[email protected]96d185d2009-04-24 03:28:54515 entry = controller_.GetLastCommittedEntry();
516 if (entry)
517 return entry->GetTitleForDisplay(&controller_);
[email protected]96d185d2009-04-24 03:28:54518 return EmptyString16();
519}
520
[email protected]bbbc1ef2010-02-12 18:03:41521// static
522string16 TabContents::GetDefaultTitle() {
523 return l10n_util::GetStringUTF16(IDS_DEFAULT_TAB_TITLE);
524}
525
[email protected]d5f942ba2008-09-26 19:30:34526int32 TabContents::GetMaxPageID() {
527 if (GetSiteInstance())
528 return GetSiteInstance()->max_page_id();
529 else
530 return max_page_id_;
531}
532
533void TabContents::UpdateMaxPageID(int32 page_id) {
534 // Ensure both the SiteInstance and RenderProcessHost update their max page
[email protected]57c6a652009-05-04 07:58:34535 // IDs in sync. Only TabContents will also have site instances, except during
[email protected]d5f942ba2008-09-26 19:30:34536 // testing.
537 if (GetSiteInstance())
538 GetSiteInstance()->UpdateMaxPageID(page_id);
[email protected]8cb5d5b2010-02-09 11:36:16539 GetRenderProcessHost()->UpdateMaxPageID(page_id);
[email protected]d5f942ba2008-09-26 19:30:34540}
541
[email protected]96d185d2009-04-24 03:28:54542SiteInstance* TabContents::GetSiteInstance() const {
543 return render_manager_.current_host()->site_instance();
544}
545
[email protected]96d185d2009-04-24 03:28:54546bool TabContents::ShouldDisplayURL() {
[email protected]8ab24cc2009-11-13 20:56:15547 // Don't hide the url in view source mode and with interstitials.
[email protected]96d185d2009-04-24 03:28:54548 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]8ab24cc2009-11-13 20:56:15549 if (entry && (entry->IsViewSourceMode() ||
550 entry->page_type() == NavigationEntry::INTERSTITIAL_PAGE)) {
[email protected]96d185d2009-04-24 03:28:54551 return true;
[email protected]8ab24cc2009-11-13 20:56:15552 }
[email protected]83e3895b2009-06-11 00:07:16553
[email protected]96d185d2009-04-24 03:28:54554 DOMUI* dom_ui = GetDOMUIForCurrentState();
555 if (dom_ui)
556 return !dom_ui->should_hide_url();
557 return true;
558}
559
[email protected]d5f942ba2008-09-26 19:30:34560SkBitmap TabContents::GetFavIcon() const {
561 // Like GetTitle(), we also want to use the favicon for the last committed
562 // entry rather than a pending navigation entry.
[email protected]ce3fa3c2009-04-20 19:55:57563 NavigationEntry* entry = controller_.GetTransientEntry();
[email protected]cbab76d2008-10-13 22:42:47564 if (entry)
565 return entry->favicon().bitmap();
566
[email protected]ce3fa3c2009-04-20 19:55:57567 entry = controller_.GetLastCommittedEntry();
[email protected]d5f942ba2008-09-26 19:30:34568 if (entry)
569 return entry->favicon().bitmap();
[email protected]d5f942ba2008-09-26 19:30:34570 return SkBitmap();
571}
572
[email protected]0b303cc2009-09-28 22:35:15573bool TabContents::FavIconIsValid() const {
574 NavigationEntry* entry = controller_.GetTransientEntry();
575 if (entry)
576 return entry->favicon().is_valid();
577
578 entry = controller_.GetLastCommittedEntry();
579 if (entry)
580 return entry->favicon().is_valid();
581
582 return false;
583}
584
[email protected]96d185d2009-04-24 03:28:54585bool TabContents::ShouldDisplayFavIcon() {
586 // Always display a throbber during pending loads.
587 if (controller_.GetLastCommittedEntry() && controller_.pending_entry())
588 return true;
589
590 DOMUI* dom_ui = GetDOMUIForCurrentState();
591 if (dom_ui)
592 return !dom_ui->hide_favicon();
593 return true;
594}
595
[email protected]2d5a3592010-01-30 04:29:52596bool TabContents::IsContentBlocked(ContentSettingsType content_type) const {
[email protected]8cc8d492010-02-02 10:40:49597 if (content_type == CONTENT_SETTINGS_TYPE_POPUPS)
598 return blocked_popups_ != NULL;
599
[email protected]0de80162010-02-03 04:52:35600 if (content_type == CONTENT_SETTINGS_TYPE_IMAGES ||
601 content_type == CONTENT_SETTINGS_TYPE_JAVASCRIPT ||
[email protected]3dbb80b2010-02-09 22:41:20602 content_type == CONTENT_SETTINGS_TYPE_PLUGINS ||
603 content_type == CONTENT_SETTINGS_TYPE_COOKIES)
[email protected]0de80162010-02-03 04:52:35604 return content_blocked_[content_type];
605
[email protected]3dbb80b2010-02-09 22:41:20606 NOTREACHED();
[email protected]2d5a3592010-01-30 04:29:52607 return false;
608}
609
[email protected]96d185d2009-04-24 03:28:54610std::wstring TabContents::GetStatusText() const {
611 if (!is_loading() || load_state_ == net::LOAD_STATE_IDLE)
612 return std::wstring();
613
614 switch (load_state_) {
615 case net::LOAD_STATE_WAITING_FOR_CACHE:
616 return l10n_util::GetString(IDS_LOAD_STATE_WAITING_FOR_CACHE);
617 case net::LOAD_STATE_RESOLVING_PROXY_FOR_URL:
618 return l10n_util::GetString(IDS_LOAD_STATE_RESOLVING_PROXY_FOR_URL);
619 case net::LOAD_STATE_RESOLVING_HOST:
620 return l10n_util::GetString(IDS_LOAD_STATE_RESOLVING_HOST);
621 case net::LOAD_STATE_CONNECTING:
622 return l10n_util::GetString(IDS_LOAD_STATE_CONNECTING);
623 case net::LOAD_STATE_SENDING_REQUEST:
[email protected]094e5b22009-09-25 04:23:56624 if (upload_size_)
625 return l10n_util::GetStringF(
626 IDS_LOAD_STATE_SENDING_REQUEST_WITH_PROGRESS,
627 static_cast<int>((100 * upload_position_) / upload_size_));
628 else
629 return l10n_util::GetString(IDS_LOAD_STATE_SENDING_REQUEST);
[email protected]96d185d2009-04-24 03:28:54630 case net::LOAD_STATE_WAITING_FOR_RESPONSE:
631 return l10n_util::GetStringF(IDS_LOAD_STATE_WAITING_FOR_RESPONSE,
632 load_state_host_);
633 // Ignore net::LOAD_STATE_READING_RESPONSE and net::LOAD_STATE_IDLE
634 case net::LOAD_STATE_IDLE:
635 case net::LOAD_STATE_READING_RESPONSE:
636 break;
637 }
638
639 return std::wstring();
640}
641
[email protected]d5f942ba2008-09-26 19:30:34642void TabContents::SetIsCrashed(bool state) {
643 if (state == is_crashed_)
644 return;
645
646 is_crashed_ = state;
[email protected]c9cd2222009-05-06 05:16:50647 NotifyNavigationStateChanged(INVALIDATE_TAB);
[email protected]d5f942ba2008-09-26 19:30:34648}
649
[email protected]744ef172009-10-16 21:53:46650void TabContents::PageActionStateChanged() {
651 NotifyNavigationStateChanged(TabContents::INVALIDATE_PAGE_ACTIONS);
[email protected]d7eaf572009-07-01 21:57:00652}
[email protected]ce5c4502009-05-06 16:46:11653
[email protected]d5f942ba2008-09-26 19:30:34654void TabContents::NotifyNavigationStateChanged(unsigned changed_flags) {
655 if (delegate_)
656 delegate_->NavigationStateChanged(this, changed_flags);
657}
658
[email protected]96d185d2009-04-24 03:28:54659void TabContents::DidBecomeSelected() {
660 controller_.SetActive(true);
[email protected]8cb5d5b2010-02-09 11:36:16661 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
662 if (rwhv) {
663 rwhv->DidBecomeSelected();
[email protected]789e9152009-08-04 21:59:43664#if defined(OS_MACOSX)
[email protected]8cb5d5b2010-02-09 11:36:16665 rwhv->SetActive(true);
[email protected]789e9152009-08-04 21:59:43666#endif
667 }
[email protected]96d185d2009-04-24 03:28:54668
[email protected]8cb5d5b2010-02-09 11:36:16669 WebCacheManager::GetInstance()->ObserveActivity(GetRenderProcessHost()->id());
[email protected]96d185d2009-04-24 03:28:54670}
671
672void TabContents::WasHidden() {
673 if (!capturing_contents()) {
674 // |render_view_host()| can be NULL if the user middle clicks a link to open
675 // a tab in then background, then closes the tab before selecting it. This
676 // is because closing the tab calls TabContents::Destroy(), which removes
677 // the |render_view_host()|; then when we actually destroy the window,
678 // OnWindowPosChanged() notices and calls HideContents() (which calls us).
[email protected]8cb5d5b2010-02-09 11:36:16679 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
680 if (rwhv)
681 rwhv->WasHidden();
[email protected]96d185d2009-04-24 03:28:54682 }
683
684 NotificationService::current()->Notify(
685 NotificationType::TAB_CONTENTS_HIDDEN,
686 Source<TabContents>(this),
687 NotificationService::NoDetails());
688}
689
[email protected]d5f942ba2008-09-26 19:30:34690void TabContents::Activate() {
691 if (delegate_)
692 delegate_->ActivateContents(this);
693}
694
[email protected]96d185d2009-04-24 03:28:54695void TabContents::ShowContents() {
[email protected]8cb5d5b2010-02-09 11:36:16696 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
697 if (rwhv)
698 rwhv->DidBecomeSelected();
[email protected]96d185d2009-04-24 03:28:54699}
700
701void TabContents::HideContents() {
702 // TODO(pkasting): http://b/1239839 Right now we purposefully don't call
703 // our superclass HideContents(), because some callers want to be very picky
704 // about the order in which these get called. In addition to making the code
705 // here practically impossible to understand, this also means we end up
706 // calling TabContents::WasHidden() twice if callers call both versions of
[email protected]57c6a652009-05-04 07:58:34707 // HideContents() on a TabContents.
[email protected]96d185d2009-04-24 03:28:54708 WasHidden();
709}
710
[email protected]c0588052008-10-27 23:01:50711void TabContents::OpenURL(const GURL& url, const GURL& referrer,
[email protected]d5f942ba2008-09-26 19:30:34712 WindowOpenDisposition disposition,
713 PageTransition::Type transition) {
714 if (delegate_)
[email protected]c0588052008-10-27 23:01:50715 delegate_->OpenURLFromTab(this, url, referrer, disposition, transition);
[email protected]d5f942ba2008-09-26 19:30:34716}
717
[email protected]1ccb3568d2010-02-19 10:51:16718bool TabContents::NavigateToPendingEntry(
719 NavigationController::ReloadType reload_type) {
[email protected]96d185d2009-04-24 03:28:54720 const NavigationEntry& entry = *controller_.pending_entry();
721
722 RenderViewHost* dest_render_view_host = render_manager_.Navigate(entry);
723 if (!dest_render_view_host)
724 return false; // Unable to create the desired render view host.
725
726 // Tell DevTools agent that it is attached prior to the navigation.
[email protected]d9f9b792009-06-24 13:17:12727 DevToolsManager* devtools_manager = DevToolsManager::GetInstance();
[email protected]7e8e6b62009-05-08 11:28:32728 if (devtools_manager) { // NULL in unit tests.
[email protected]684b4ba22009-05-07 06:27:41729 devtools_manager->OnNavigatingToPendingEntry(
730 render_view_host(),
731 dest_render_view_host,
732 controller_.pending_entry()->url());
[email protected]7e8e6b62009-05-08 11:28:32733 }
[email protected]96d185d2009-04-24 03:28:54734
735 // Used for page load time metrics.
736 current_load_start_ = base::TimeTicks::Now();
737
738 // Navigate in the desired RenderViewHost.
[email protected]056de2d2009-06-26 16:41:34739 ViewMsg_Navigate_Params navigate_params;
[email protected]1ccb3568d2010-02-19 10:51:16740 MakeNavigateParams(profile(), entry, reload_type, &navigate_params);
[email protected]056de2d2009-06-26 16:41:34741 dest_render_view_host->Navigate(navigate_params);
[email protected]96d185d2009-04-24 03:28:54742
743 if (entry.page_id() == -1) {
744 // HACK!! This code suppresses javascript: URLs from being added to
745 // session history, which is what we want to do for javascript: URLs that
746 // do not generate content. What we really need is a message from the
747 // renderer telling us that a new page was not created. The same message
748 // could be used for mailto: URLs and the like.
749 if (entry.url().SchemeIs(chrome::kJavaScriptScheme))
750 return false;
751 }
752
753 // Clear any provisional password saves - this stops password infobars
754 // showing up on pages the user navigates to while the right page is
755 // loading.
756 GetPasswordManager()->ClearProvisionalSave();
757
[email protected]1ccb3568d2010-02-19 10:51:16758 if (reload_type != NavigationController::NO_RELOAD &&
759 !profile()->IsOffTheRecord()) {
[email protected]0189bc722009-08-28 21:56:48760 FaviconService* favicon_service =
761 profile()->GetFaviconService(Profile::IMPLICIT_ACCESS);
762 if (favicon_service)
763 favicon_service->SetFaviconOutOfDateForPage(entry.url());
[email protected]96d185d2009-04-24 03:28:54764 }
765
[email protected]d5f942ba2008-09-26 19:30:34766 return true;
767}
768
[email protected]96d185d2009-04-24 03:28:54769void TabContents::Stop() {
770 render_manager_.Stop();
771 printing_.Stop();
772}
773
[email protected]96d185d2009-04-24 03:28:54774void TabContents::DisassociateFromPopupCount() {
775 render_view_host()->DisassociateFromPopupCount();
776}
777
778TabContents* TabContents::Clone() {
779 // We create a new SiteInstance so that the new tab won't share processes
780 // with the old one. This can be changed in the future if we need it to share
781 // processes for some reason.
[email protected]420ae012009-04-24 05:16:32782 TabContents* tc = new TabContents(profile(),
[email protected]96d185d2009-04-24 03:28:54783 SiteInstance::CreateSiteInstance(profile()),
[email protected]12636df2009-09-28 22:32:21784 MSG_ROUTING_NONE, this);
[email protected]96d185d2009-04-24 03:28:54785 tc->controller().CopyStateFrom(controller_);
786 return tc;
787}
788
[email protected]4d677202009-07-19 07:37:12789void TabContents::ShowPageInfo(const GURL& url,
790 const NavigationEntry::SSLStatus& ssl,
791 bool show_history) {
792 if (!delegate_)
793 return;
794
[email protected]bb678332009-07-21 00:15:50795 delegate_->ShowPageInfo(profile(), url, ssl, show_history);
[email protected]4d677202009-07-19 07:37:12796}
797
[email protected]7e0831b2010-02-01 08:45:15798void TabContents::TranslatePage(const std::string& source_lang,
799 const std::string& target_lang) {
800 NavigationEntry* entry = controller_.GetActiveEntry();
801 if (!entry) {
802 NOTREACHED();
803 return;
804 }
[email protected]3b1c1812010-02-12 21:53:55805 language_state_.set_translation_pending(true);
[email protected]7e0831b2010-02-01 08:45:15806 render_view_host()->TranslatePage(entry->page_id(), source_lang, target_lang);
807}
808
initial.commit09911bf2008-07-26 23:55:29809ConstrainedWindow* TabContents::CreateConstrainedDialog(
[email protected]e8382172009-06-19 22:16:28810 ConstrainedWindowDelegate* delegate) {
initial.commit09911bf2008-07-26 23:55:29811 ConstrainedWindow* window =
[email protected]e8382172009-06-19 22:16:28812 ConstrainedWindow::CreateConstrainedDialog(this, delegate);
initial.commit09911bf2008-07-26 23:55:29813 child_windows_.push_back(window);
[email protected]fa1cf0b82010-01-15 21:49:44814
815 if (child_windows_.size() == 1) {
816 window->ShowConstrainedWindow();
817 BlockTabContent(true);
818 }
819
initial.commit09911bf2008-07-26 23:55:29820 return window;
821}
822
[email protected]fa1cf0b82010-01-15 21:49:44823void TabContents::BlockTabContent(bool blocked) {
[email protected]8dccd7cb2010-02-25 05:19:55824 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
825 if (rwhv)
826 rwhv->SetVisuallyDeemphasized(blocked);
[email protected]fa1cf0b82010-01-15 21:49:44827 render_view_host()->set_ignore_input_events(blocked);
828 if (delegate_)
829 delegate_->SetTabContentBlocked(this, blocked);
830}
831
initial.commit09911bf2008-07-26 23:55:29832void TabContents::AddNewContents(TabContents* new_contents,
833 WindowOpenDisposition disposition,
834 const gfx::Rect& initial_pos,
[email protected]7e7414ae2010-01-26 20:19:29835 bool user_gesture) {
initial.commit09911bf2008-07-26 23:55:29836 if (!delegate_)
837 return;
838
[email protected]35f7d212009-04-29 21:19:27839 if ((disposition == NEW_POPUP) && !user_gesture &&
840 !CommandLine::ForCurrentProcess()->HasSwitch(
841 switches::kDisablePopupBlocking)) {
[email protected]2c4410d2009-05-06 23:46:22842 // Unrequested popups from normal pages are constrained unless they're in
[email protected]b6c874582009-05-08 19:38:31843 // the whitelist. The popup owner will handle checking this.
[email protected]7e7414ae2010-01-26 20:19:29844 delegate_->GetConstrainingContents(this)->AddPopup(
845 new_contents, initial_pos);
[email protected]b6c874582009-05-08 19:38:31846 } else {
[email protected]304f8792009-08-19 21:28:09847#if defined(OS_CHROMEOS)
848 if (disposition == NEW_POPUP) {
849 // If the popup is bigger than a given factor of the screen, then
850 // turn it into a foreground tab (on chrome os only)
851 GdkScreen* screen = gdk_screen_get_default();
852 int max_width = gdk_screen_get_width(screen) * kMaxWidthFactor;
853 int max_height = gdk_screen_get_height(screen) * kMaxHeightFactor;
854 if (initial_pos.width() > max_width ||
855 initial_pos.height() > max_height) {
856 disposition = NEW_FOREGROUND_TAB;
857 }
858 }
859#endif
860
[email protected]0aa55312008-10-17 21:53:08861 new_contents->DisassociateFromPopupCount();
initial.commit09911bf2008-07-26 23:55:29862 delegate_->AddNewContents(this, new_contents, disposition, initial_pos,
863 user_gesture);
[email protected]7f37dd92009-09-15 16:35:49864 NotificationService::current()->Notify(
865 NotificationType::TAB_ADDED,
866 Source<TabContentsDelegate>(delegate_),
867 Details<TabContents>(this));
initial.commit09911bf2008-07-26 23:55:29868 }
initial.commit09911bf2008-07-26 23:55:29869
[email protected]8cc8d492010-02-02 10:40:49870 // TODO(pkasting): Why is this necessary?
871 PopupNotificationVisibilityChanged(blocked_popups_ != NULL);
initial.commit09911bf2008-07-26 23:55:29872}
873
[email protected]bcd9dc42010-01-23 16:38:18874bool TabContents::ExecuteCode(int request_id, const std::string& extension_id,
[email protected]61f5a7b2009-12-22 22:21:20875 const std::vector<URLPattern>& host_permissions,
[email protected]20ad2692009-11-20 18:27:20876 bool is_js_code, const std::string& code_string,
877 bool all_frames) {
[email protected]ebd0eae72010-02-16 23:50:51878 RenderViewHost* host = render_view_host();
879 if (!host)
880 return false;
881
882 return host->Send(new ViewMsg_ExecuteCode(host->routing_id(),
883 ViewMsg_ExecuteCode_Params(request_id, extension_id, host_permissions,
884 is_js_code, code_string, all_frames)));
[email protected]912256b32009-09-18 09:47:35885}
886
[email protected]96d185d2009-04-24 03:28:54887void TabContents::PopupNotificationVisibilityChanged(bool visible) {
[email protected]8cc8d492010-02-02 10:40:49888 if (is_being_destroyed_)
889 return;
890 if (!dont_notify_render_view_)
891 render_view_host()->PopupNotificationVisibilityChanged(visible);
[email protected]0de80162010-02-03 04:52:35892 if (delegate_)
893 delegate_->OnBlockedContentChange(this);
[email protected]96d185d2009-04-24 03:28:54894}
895
[email protected]5c9e97a2009-09-09 23:48:30896gfx::NativeView TabContents::GetContentNativeView() const {
[email protected]96d185d2009-04-24 03:28:54897 return view_->GetContentNativeView();
898}
899
900gfx::NativeView TabContents::GetNativeView() const {
901 return view_->GetNativeView();
902}
903
904void TabContents::GetContainerBounds(gfx::Rect *out) const {
905 view_->GetContainerBounds(out);
906}
907
908void TabContents::Focus() {
909 view_->Focus();
910}
911
[email protected]90daadb42009-06-08 21:27:28912void TabContents::FocusThroughTabTraversal(bool reverse) {
[email protected]7e383692009-06-12 19:14:54913 if (showing_interstitial_page()) {
914 render_manager_.interstitial_page()->FocusThroughTabTraversal(reverse);
915 return;
916 }
[email protected]96d185d2009-04-24 03:28:54917 render_view_host()->SetInitialFocus(reverse);
918}
919
920bool TabContents::FocusLocationBarByDefault() {
921 DOMUI* dom_ui = GetDOMUIForCurrentState();
922 if (dom_ui)
923 return dom_ui->focus_location_bar_by_default();
[email protected]3e3f0eb2009-06-22 18:33:43924 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]e0d481582009-09-15 21:06:25925 if (entry && entry->url() == GURL(chrome::kAboutBlankURL))
[email protected]3e3f0eb2009-06-22 18:33:43926 return true;
[email protected]96d185d2009-04-24 03:28:54927 return false;
928}
929
[email protected]616ed5a2008-11-21 22:27:24930void TabContents::AddInfoBar(InfoBarDelegate* delegate) {
931 // Look through the existing InfoBarDelegates we have for a match. If we've
932 // already got one that matches, then we don't add the new one.
[email protected]f86a07022008-11-25 01:06:05933 for (int i = 0; i < infobar_delegate_count(); ++i) {
[email protected]6d27a7b2009-12-18 23:25:45934 if (GetInfoBarDelegateAt(i)->EqualsDelegate(delegate)) {
935 // Tell the new infobar to close so that it can clean itself up.
936 delegate->InfoBarClosed();
[email protected]616ed5a2008-11-21 22:27:24937 return;
[email protected]6d27a7b2009-12-18 23:25:45938 }
[email protected]616ed5a2008-11-21 22:27:24939 }
940
941 infobar_delegates_.push_back(delegate);
[email protected]bfd04a62009-02-01 18:16:56942 NotificationService::current()->Notify(
943 NotificationType::TAB_CONTENTS_INFOBAR_ADDED,
944 Source<TabContents>(this),
945 Details<InfoBarDelegate>(delegate));
[email protected]616ed5a2008-11-21 22:27:24946
947 // Add ourselves as an observer for navigations the first time a delegate is
948 // added. We use this notification to expire InfoBars that need to expire on
949 // page transitions.
950 if (infobar_delegates_.size() == 1) {
[email protected]bfd04a62009-02-01 18:16:56951 registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED,
[email protected]ce3fa3c2009-04-20 19:55:57952 Source<NavigationController>(&controller_));
[email protected]616ed5a2008-11-21 22:27:24953 }
954}
955
956void TabContents::RemoveInfoBar(InfoBarDelegate* delegate) {
957 std::vector<InfoBarDelegate*>::iterator it =
958 find(infobar_delegates_.begin(), infobar_delegates_.end(), delegate);
959 if (it != infobar_delegates_.end()) {
960 InfoBarDelegate* delegate = *it;
[email protected]bfd04a62009-02-01 18:16:56961 NotificationService::current()->Notify(
962 NotificationType::TAB_CONTENTS_INFOBAR_REMOVED,
963 Source<TabContents>(this),
964 Details<InfoBarDelegate>(delegate));
[email protected]f86a07022008-11-25 01:06:05965 infobar_delegates_.erase(it);
[email protected]616ed5a2008-11-21 22:27:24966
[email protected]6a02963e2009-01-06 16:58:03967 // Remove ourselves as an observer if we are tracking no more InfoBars.
968 if (infobar_delegates_.empty()) {
[email protected]bfd04a62009-02-01 18:16:56969 registrar_.Remove(this, NotificationType::NAV_ENTRY_COMMITTED,
[email protected]ce3fa3c2009-04-20 19:55:57970 Source<NavigationController>(&controller_));
[email protected]6a02963e2009-01-06 16:58:03971 }
[email protected]616ed5a2008-11-21 22:27:24972 }
973}
[email protected]3edd9522009-03-04 22:19:41974
[email protected]f81821c2009-08-07 01:29:47975void TabContents::ReplaceInfoBar(InfoBarDelegate* old_delegate,
976 InfoBarDelegate* new_delegate) {
977 std::vector<InfoBarDelegate*>::iterator it =
978 find(infobar_delegates_.begin(), infobar_delegates_.end(), old_delegate);
979 DCHECK(it != infobar_delegates_.end());
980
981 // Notify the container about the change of plans.
982 scoped_ptr<std::pair<InfoBarDelegate*, InfoBarDelegate*> > details(
983 new std::pair<InfoBarDelegate*, InfoBarDelegate*>(
984 old_delegate, new_delegate));
985 NotificationService::current()->Notify(
986 NotificationType::TAB_CONTENTS_INFOBAR_REPLACED,
987 Source<TabContents>(this),
988 Details<std::pair<InfoBarDelegate*, InfoBarDelegate*> >(details.get()));
989
990 // Remove the old one.
991 infobar_delegates_.erase(it);
992
993 // Add the new one.
994 infobar_delegates_.push_back(new_delegate);
995}
996
[email protected]20ede072009-10-06 00:28:35997bool TabContents::ShouldShowBookmarkBar() {
[email protected]aa9196f2009-10-22 16:20:27998 if (showing_interstitial_page())
999 return false;
1000
[email protected]96d185d2009-04-24 03:28:541001 // See GetDOMUIForCurrentState() comment for more info. This case is very
1002 // similar, but for non-first loads, we want to use the committed entry. This
1003 // is so the bookmarks bar disappears at the same time the page does.
1004 if (controller_.GetLastCommittedEntry()) {
1005 // Not the first load, always use the committed DOM UI.
1006 if (render_manager_.dom_ui())
1007 return render_manager_.dom_ui()->force_bookmark_bar_visible();
1008 return false; // Default.
1009 }
1010
1011 // When it's the first load, we know either the pending one or the committed
1012 // one will have the DOM UI in it (see GetDOMUIForCurrentState), and only one
1013 // of them will be valid, so we can just check both.
1014 if (render_manager_.pending_dom_ui())
1015 return render_manager_.pending_dom_ui()->force_bookmark_bar_visible();
1016 if (render_manager_.dom_ui())
1017 return render_manager_.dom_ui()->force_bookmark_bar_visible();
1018 return false; // Default.
1019}
1020
[email protected]d8f16ae2009-08-30 03:47:261021bool TabContents::IsExtensionShelfAlwaysVisible() {
1022 // See GetDOMUIForCurrentState() comment for more info. This case is very
1023 // similar, but for non-first loads, we want to use the committed entry. This
1024 // is so the bookmarks bar disappears at the same time the page does.
1025 if (controller_.GetLastCommittedEntry()) {
1026 // Not the first load, always use the committed DOM UI.
1027 if (render_manager_.dom_ui())
1028 return render_manager_.dom_ui()->force_extension_shelf_visible();
1029 return false; // Default.
1030 }
1031
1032 // When it's the first load, we know either the pending one or the committed
1033 // one will have the DOM UI in it (see GetDOMUIForCurrentState), and only one
1034 // of them will be valid, so we can just check both.
1035 if (render_manager_.pending_dom_ui())
1036 return render_manager_.pending_dom_ui()->force_extension_shelf_visible();
1037 if (render_manager_.dom_ui())
1038 return render_manager_.dom_ui()->force_extension_shelf_visible();
1039 return false; // Default.
1040}
1041
[email protected]3edd9522009-03-04 22:19:411042void TabContents::ToolbarSizeChanged(bool is_animating) {
1043 TabContentsDelegate* d = delegate();
1044 if (d)
1045 d->ToolbarSizeChanged(this, is_animating);
1046}
[email protected]616ed5a2008-11-21 22:27:241047
[email protected]fc6fb7fb2009-11-07 02:35:041048bool TabContents::CanDownload(int request_id) {
1049 TabContentsDelegate* d = delegate();
1050 if (d)
1051 return d->CanDownload(request_id);
1052 return true;
1053}
1054
[email protected]d5f942ba2008-09-26 19:30:341055void TabContents::OnStartDownload(DownloadItem* download) {
1056 DCHECK(download);
[email protected]d5f942ba2008-09-26 19:30:341057
1058 // Download in a constrained popup is shown in the tab that opened it.
[email protected]b6c874582009-05-08 19:38:311059 TabContents* tab_contents = delegate()->GetConstrainingContents(this);
[email protected]d5f942ba2008-09-26 19:30:341060
[email protected]59560e0b2009-06-04 03:30:221061 if (tab_contents && tab_contents->delegate())
1062 tab_contents->delegate()->OnStartDownload(download);
[email protected]3edd9522009-03-04 22:19:411063}
[email protected]3edd9522009-03-04 22:19:411064
[email protected]d5f942ba2008-09-26 19:30:341065void TabContents::WillClose(ConstrainedWindow* window) {
1066 ConstrainedWindowList::iterator it =
1067 find(child_windows_.begin(), child_windows_.end(), window);
[email protected]fa1cf0b82010-01-15 21:49:441068 bool removed_topmost_window = it == child_windows_.begin();
[email protected]d5f942ba2008-09-26 19:30:341069 if (it != child_windows_.end())
1070 child_windows_.erase(it);
[email protected]fa1cf0b82010-01-15 21:49:441071 if (removed_topmost_window && child_windows_.size() > 0) {
1072 child_windows_[0]->ShowConstrainedWindow();
1073 BlockTabContent(true);
1074 } else {
1075 BlockTabContent(false);
1076 }
[email protected]66ba4932009-06-04 19:22:131077}
[email protected]d5f942ba2008-09-26 19:30:341078
[email protected]66ba4932009-06-04 19:22:131079void TabContents::WillCloseBlockedPopupContainer(
1080 BlockedPopupContainer* container) {
1081 DCHECK(blocked_popups_ == container);
1082 blocked_popups_ = NULL;
[email protected]8cc8d492010-02-02 10:40:491083 PopupNotificationVisibilityChanged(false);
[email protected]d5f942ba2008-09-26 19:30:341084}
1085
[email protected]d5f942ba2008-09-26 19:30:341086void TabContents::DidMoveOrResize(ConstrainedWindow* window) {
[email protected]332af7732009-03-11 13:21:351087#if defined(OS_WIN)
[email protected]92edc472009-02-10 20:32:061088 UpdateWindow(GetNativeView());
[email protected]332af7732009-03-11 13:21:351089#endif
[email protected]d5f942ba2008-09-26 19:30:341090}
1091
[email protected]c1dc9f132009-11-20 23:13:211092void TabContents::StartFinding(string16 search_string,
[email protected]e491f1c2009-05-22 20:28:121093 bool forward_direction,
1094 bool case_sensitive) {
[email protected]c1dc9f132009-11-20 23:13:211095 // If search_string is empty, it means FindNext was pressed with a keyboard
[email protected]6e95706d2009-04-23 22:55:011096 // shortcut so unless we have something to search for we return early.
[email protected]c1dc9f132009-11-20 23:13:211097 if (search_string.empty() && find_text_.empty()) {
[email protected]87fae512009-12-11 02:20:211098 // Try the last thing we searched for on this tab, then the last thing
1099 // searched for on any tab.
1100 if (!previous_find_text_.empty())
1101 search_string = previous_find_text_;
1102 else if (!last_search_prepopulate_text_->empty())
1103 search_string = *last_search_prepopulate_text_;
1104 else
[email protected]e491f1c2009-05-22 20:28:121105 return;
[email protected]e491f1c2009-05-22 20:28:121106 }
[email protected]6e95706d2009-04-23 22:55:011107
[email protected]101c90e2009-10-26 21:14:441108 // Keep track of the previous search.
1109 previous_find_text_ = find_text_;
1110
[email protected]6e95706d2009-04-23 22:55:011111 // This is a FindNext operation if we are searching for the same text again,
1112 // or if the passed in search text is empty (FindNext keyboard shortcut). The
1113 // exception to this is if the Find was aborted (then we don't want FindNext
1114 // because the highlighting has been cleared and we need it to reappear). We
1115 // therefore treat FindNext after an aborted Find operation as a full fledged
1116 // Find.
[email protected]c1dc9f132009-11-20 23:13:211117 bool find_next = (find_text_ == search_string || search_string.empty()) &&
[email protected]e491f1c2009-05-22 20:28:121118 (last_search_case_sensitive_ == case_sensitive) &&
[email protected]6e95706d2009-04-23 22:55:011119 !find_op_aborted_;
1120 if (!find_next)
1121 current_find_request_id_ = find_request_id_counter_++;
1122
[email protected]c1dc9f132009-11-20 23:13:211123 if (!search_string.empty())
1124 find_text_ = search_string;
[email protected]e491f1c2009-05-22 20:28:121125 last_search_case_sensitive_ = case_sensitive;
[email protected]6e95706d2009-04-23 22:55:011126
1127 find_op_aborted_ = false;
1128
1129 // Keep track of what the last search was across the tabs.
[email protected]c1dc9f132009-11-20 23:13:211130 *last_search_prepopulate_text_ = find_text_;
[email protected]6e95706d2009-04-23 22:55:011131 render_view_host()->StartFinding(current_find_request_id_,
1132 find_text_,
1133 forward_direction,
[email protected]e491f1c2009-05-22 20:28:121134 case_sensitive,
[email protected]6e95706d2009-04-23 22:55:011135 find_next);
1136}
1137
1138void TabContents::StopFinding(bool clear_selection) {
[email protected]2d2dffc2009-06-09 21:41:001139 // When |clear_selection| is true, it means the find string has been cleared
1140 // by the user, but the UI has not been dismissed.
1141 if (!clear_selection)
1142 find_ui_active_ = false;
[email protected]87fae512009-12-11 02:20:211143 previous_find_text_ = find_text_;
[email protected]101c90e2009-10-26 21:14:441144 find_text_.clear();
[email protected]6e95706d2009-04-23 22:55:011145 find_op_aborted_ = true;
[email protected]e491f1c2009-05-22 20:28:121146 last_search_result_ = FindNotificationDetails();
[email protected]6e95706d2009-04-23 22:55:011147 render_view_host()->StopFinding(clear_selection);
1148}
1149
[email protected]420ae012009-04-24 05:16:321150void TabContents::OnSavePage() {
1151 // If we can not save the page, try to download it.
1152 if (!SavePackage::IsSavableContents(contents_mime_type())) {
1153 DownloadManager* dlm = profile()->GetDownloadManager();
1154 const GURL& current_page_url = GetURL();
1155 if (dlm && current_page_url.is_valid())
[email protected]d82ed61e2009-06-16 02:46:221156 dlm->DownloadUrl(current_page_url, GURL(), "", this);
[email protected]420ae012009-04-24 05:16:321157 return;
1158 }
1159
1160 Stop();
1161
1162 // Create the save package and possibly prompt the user for the name to save
1163 // the page as. The user prompt is an asynchronous operation that runs on
1164 // another thread.
[email protected]d82ed61e2009-06-16 02:46:221165 save_package_ = new SavePackage(this);
[email protected]420ae012009-04-24 05:16:321166 save_package_->GetSaveInfo();
1167}
1168
1169// Used in automated testing to bypass prompting the user for file names.
1170// Instead, the names and paths are hard coded rather than running them through
1171// file name sanitation and extension / mime checking.
[email protected]828cabe2009-09-26 22:47:111172bool TabContents::SavePage(const FilePath& main_file, const FilePath& dir_path,
[email protected]420ae012009-04-24 05:16:321173 SavePackage::SavePackageType save_type) {
1174 // Stop the page from navigating.
1175 Stop();
1176
[email protected]828cabe2009-09-26 22:47:111177 save_package_ = new SavePackage(this, save_type, main_file, dir_path);
[email protected]cdf6cc82009-08-04 21:48:591178 return save_package_->Init();
[email protected]420ae012009-04-24 05:16:321179}
1180
[email protected]89d6e6e2009-12-03 23:06:511181void TabContents::EmailPageLocation() {
1182 std::string title = EscapeQueryParamValue(UTF16ToUTF8(GetTitle()), false);
1183 std::string page_url = EscapeQueryParamValue(GetURL().spec(), false);
1184 std::string mailto = std::string("mailto:?subject=Fwd:%20") +
1185 title + "&body=%0A%0A" + page_url;
1186 platform_util::OpenExternal(GURL(mailto));
1187}
1188
[email protected]420ae012009-04-24 05:16:321189void TabContents::PrintPreview() {
1190 // We don't show the print preview yet, only the print dialog.
1191 PrintNow();
1192}
1193
1194bool TabContents::PrintNow() {
1195 // We can't print interstitial page for now.
1196 if (showing_interstitial_page())
1197 return false;
1198
1199 return render_view_host()->PrintPages();
1200}
1201
[email protected]82270452009-06-19 15:58:011202void TabContents::PrintingDone(int document_cookie, bool success) {
1203 render_view_host()->PrintingDone(document_cookie, success);
1204}
1205
[email protected]420ae012009-04-24 05:16:321206bool TabContents::IsActiveEntry(int32 page_id) {
1207 NavigationEntry* active_entry = controller_.GetActiveEntry();
1208 return (active_entry != NULL &&
1209 active_entry->site_instance() == GetSiteInstance() &&
1210 active_entry->page_id() == page_id);
1211}
1212
[email protected]b2fe07d12010-02-09 14:38:081213void TabContents::SetOverrideEncoding(const std::string& encoding) {
[email protected]8cb5d5b2010-02-09 11:36:161214 set_encoding(encoding);
1215 render_view_host()->SetPageEncoding(encoding);
1216}
1217
[email protected]b2fe07d12010-02-09 14:38:081218void TabContents::ResetOverrideEncoding() {
[email protected]8cb5d5b2010-02-09 11:36:161219 reset_encoding();
1220 render_view_host()->ResetPageEncodingToDefault();
1221}
1222
1223void TabContents::WindowMoveOrResizeStarted() {
1224 render_view_host()->WindowMoveOrResizeStarted();
1225}
1226
[email protected]3f8eb7f2009-10-02 23:58:051227void TabContents::LogNewTabTime(const std::string& event_name) {
1228 // Not all new tab pages get timed. In those cases, we don't have a
1229 // new_tab_start_time_.
1230 if (new_tab_start_time_.is_null())
1231 return;
1232
1233 base::TimeDelta duration = base::TimeTicks::Now() - new_tab_start_time_;
[email protected]84abba62009-10-07 17:01:441234 MetricEventDurationDetails details(event_name,
1235 static_cast<int>(duration.InMilliseconds()));
1236
[email protected]7ab1e7d62009-10-14 23:32:011237 if (event_name == "Tab.NewTabScriptStart") {
1238 UMA_HISTOGRAM_TIMES("Tab.NewTabScriptStart", duration);
1239 } else if (event_name == "Tab.NewTabDOMContentLoaded") {
1240 UMA_HISTOGRAM_TIMES("Tab.NewTabDOMContentLoaded", duration);
1241 } else if (event_name == "Tab.NewTabOnload") {
1242 UMA_HISTOGRAM_TIMES("Tab.NewTabOnload", duration);
[email protected]3f8eb7f2009-10-02 23:58:051243 // The new tab page has finished loading; reset it.
1244 new_tab_start_time_ = base::TimeTicks();
[email protected]84abba62009-10-07 17:01:441245 } else {
1246 NOTREACHED();
[email protected]3f8eb7f2009-10-02 23:58:051247 }
[email protected]84abba62009-10-07 17:01:441248 NotificationService::current()->Notify(
1249 NotificationType::METRIC_EVENT_DURATION,
1250 Source<TabContents>(this),
1251 Details<MetricEventDurationDetails>(&details));
[email protected]3f8eb7f2009-10-02 23:58:051252}
1253
[email protected]7ab1e7d62009-10-14 23:32:011254void TabContents::OnCloseStarted() {
1255 if (tab_close_start_time_.is_null())
1256 tab_close_start_time_ = base::TimeTicks::Now();
1257}
1258
[email protected]c37e3b62010-01-22 00:15:171259TabContents* TabContents::CloneAndMakePhantom() {
[email protected]c37e3b62010-01-22 00:15:171260 TabNavigation tab_nav;
[email protected]df72ac72010-02-06 00:43:161261
1262 NavigationEntry* entry = controller().GetActiveEntry();
1263 if (app_extension_)
1264 tab_nav.set_url(app_extension_->app_launch_url());
1265 else if (entry)
1266 tab_nav.SetFromNavigationEntry(*entry);
1267
[email protected]c37e3b62010-01-22 00:15:171268 std::vector<TabNavigation> navigations;
1269 navigations.push_back(tab_nav);
1270
1271 TabContents* new_contents =
1272 new TabContents(profile(), NULL, MSG_ROUTING_NONE, NULL);
[email protected]3b355642010-02-05 16:01:491273 new_contents->SetAppExtension(app_extension_);
[email protected]c37e3b62010-01-22 00:15:171274 new_contents->controller().RestoreFromState(navigations, 0, false);
[email protected]c37e3b62010-01-22 00:15:171275
[email protected]df72ac72010-02-06 00:43:161276 if (!app_extension_ && entry)
1277 new_contents->controller().GetActiveEntry()->favicon() = entry->favicon();
1278
[email protected]c37e3b62010-01-22 00:15:171279 return new_contents;
1280}
1281
[email protected]0de80162010-02-03 04:52:351282// Resets the |content_blocked_| array.
1283void TabContents::ClearBlockedContentSettings() {
[email protected]15cf526b2010-02-12 06:33:491284 for (size_t i = 0; i < arraysize(content_blocked_); ++i)
[email protected]0de80162010-02-03 04:52:351285 content_blocked_[i] = false;
1286}
1287
[email protected]96d185d2009-04-24 03:28:541288// Notifies the RenderWidgetHost instance about the fact that the page is
1289// loading, or done loading and calls the base implementation.
1290void TabContents::SetIsLoading(bool is_loading,
1291 LoadNotificationDetails* details) {
1292 if (is_loading == is_loading_)
1293 return;
1294
1295 if (!is_loading) {
1296 load_state_ = net::LOAD_STATE_IDLE;
1297 load_state_host_.clear();
[email protected]094e5b22009-09-25 04:23:561298 upload_size_ = 0;
1299 upload_position_ = 0;
[email protected]96d185d2009-04-24 03:28:541300 }
1301
1302 render_manager_.SetIsLoading(is_loading);
1303
1304 is_loading_ = is_loading;
1305 waiting_for_response_ = is_loading;
1306
1307 if (delegate_)
1308 delegate_->LoadingStateChanged(this);
[email protected]329581b2009-04-28 06:52:351309 NotifyNavigationStateChanged(INVALIDATE_LOAD);
[email protected]96d185d2009-04-24 03:28:541310
1311 NotificationType type = is_loading ? NotificationType::LOAD_START :
1312 NotificationType::LOAD_STOP;
[email protected]ce5c4502009-05-06 16:46:111313 NotificationDetails det = NotificationService::NoDetails();
[email protected]96d185d2009-04-24 03:28:541314 if (details)
1315 det = Details<LoadNotificationDetails>(details);
1316 NotificationService::current()->Notify(type,
1317 Source<NavigationController>(&controller_),
1318 det);
1319}
1320
[email protected]b6c874582009-05-08 19:38:311321void TabContents::AddPopup(TabContents* new_contents,
[email protected]7e7414ae2010-01-26 20:19:291322 const gfx::Rect& initial_pos) {
[email protected]8cc8d492010-02-02 10:40:491323 GURL url(GetURL());
1324 if (url.is_valid() &&
1325 profile()->GetHostContentSettingsMap()->GetContentSetting(
[email protected]da0c8e92010-02-09 23:25:491326 url, CONTENT_SETTINGS_TYPE_POPUPS) == CONTENT_SETTING_ALLOW) {
[email protected]8cc8d492010-02-02 10:40:491327 AddNewContents(new_contents, NEW_POPUP, initial_pos, true);
1328 } else {
1329 if (!blocked_popups_)
1330 blocked_popups_ = new BlockedPopupContainer(this);
1331 blocked_popups_->AddTabContents(new_contents, initial_pos);
1332 }
[email protected]b9681312008-11-07 00:08:261333}
[email protected]616ed5a2008-11-21 22:27:241334
1335namespace {
1336bool TransitionIsReload(PageTransition::Type transition) {
1337 return PageTransition::StripQualifier(transition) == PageTransition::RELOAD;
1338}
1339}
1340
1341void TabContents::ExpireInfoBars(
1342 const NavigationController::LoadCommittedDetails& details) {
1343 // Only hide InfoBars when the user has done something that makes the main
1344 // frame load. We don't want various automatic or subframe navigations making
1345 // it disappear.
1346 if (!details.is_user_initiated_main_frame_load())
1347 return;
1348
[email protected]f86a07022008-11-25 01:06:051349 for (int i = infobar_delegate_count() - 1; i >= 0; --i) {
[email protected]616ed5a2008-11-21 22:27:241350 InfoBarDelegate* delegate = GetInfoBarDelegateAt(i);
[email protected]f86a07022008-11-25 01:06:051351 if (delegate->ShouldExpire(details))
[email protected]616ed5a2008-11-21 22:27:241352 RemoveInfoBar(delegate);
[email protected]616ed5a2008-11-21 22:27:241353 }
1354}
[email protected]fdd61c62009-04-22 19:22:571355
[email protected]96d185d2009-04-24 03:28:541356DOMUI* TabContents::GetDOMUIForCurrentState() {
1357 // When there is a pending navigation entry, we want to use the pending DOMUI
1358 // that goes along with it to control the basic flags. For example, we want to
1359 // show the pending URL in the URL bar, so we want the display_url flag to
1360 // be from the pending entry.
1361 //
1362 // The confusion comes because there are multiple possibilities for the
1363 // initial load in a tab as a side effect of the way the RenderViewHostManager
1364 // works.
1365 //
1366 // - For the very first tab the load looks "normal". The new tab DOM UI is
1367 // the pending one, and we want it to apply here.
1368 //
1369 // - For subsequent new tabs, they'll get a new SiteInstance which will then
1370 // get switched to the one previously associated with the new tab pages.
1371 // This switching will cause the manager to commit the RVH/DOMUI. So we'll
1372 // have a committed DOM UI in this case.
1373 //
1374 // This condition handles all of these cases:
1375 //
1376 // - First load in first tab: no committed nav entry + pending nav entry +
1377 // pending dom ui:
1378 // -> Use pending DOM UI if any.
1379 //
1380 // - First load in second tab: no committed nav entry + pending nav entry +
1381 // no pending DOM UI:
1382 // -> Use the committed DOM UI if any.
1383 //
1384 // - Second navigation in any tab: committed nav entry + pending nav entry:
1385 // -> Use pending DOM UI if any.
1386 //
1387 // - Normal state with no load: committed nav entry + no pending nav entry:
1388 // -> Use committed DOM UI.
1389 if (controller_.pending_entry() &&
1390 (controller_.GetLastCommittedEntry() ||
1391 render_manager_.pending_dom_ui()))
1392 return render_manager_.pending_dom_ui();
1393 return render_manager_.dom_ui();
1394}
[email protected]420ae012009-04-24 05:16:321395
1396void TabContents::DidNavigateMainFramePostCommit(
1397 const NavigationController::LoadCommittedDetails& details,
1398 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]c2e74fe82009-09-03 17:57:441399 if (opener_dom_ui_type_ != DOMUIFactory::kNoDOMUI) {
1400 // If this is a window.open navigation, use the same DOMUI as the renderer
1401 // that opened the window, as long as both renderers have the same
1402 // privileges.
1403 if (opener_dom_ui_type_ == DOMUIFactory::GetDOMUIType(GetURL())) {
1404 DOMUI* dom_ui = DOMUIFactory::CreateDOMUIForURL(this, GetURL());
1405 // dom_ui might be NULL if the URL refers to a non-existent extension.
1406 if (dom_ui) {
1407 render_manager_.SetDOMUIPostCommit(dom_ui);
1408 dom_ui->RenderViewCreated(render_view_host());
1409 }
1410 }
1411 opener_dom_ui_type_ = DOMUIFactory::kNoDOMUI;
1412 }
1413
[email protected]420ae012009-04-24 05:16:321414 if (details.is_user_initiated_main_frame_load()) {
1415 // Clear the status bubble. This is a workaround for a bug where WebKit
1416 // doesn't let us know that the cursor left an element during a
1417 // transition (this is also why the mouse cursor remains as a hand after
1418 // clicking on a link); see bugs 1184641 and 980803. We don't want to
1419 // clear the bubble when a user navigates to a named anchor in the same
1420 // page.
1421 UpdateTargetURL(details.entry->page_id(), GURL());
1422
1423 // UpdateHelpersForDidNavigate will handle the case where the password_form
1424 // origin is valid.
1425 // TODO(brettw) bug 1343111: Password manager stuff in here needs to be
1426 // cleaned up and covered by tests.
1427 if (!params.password_form.origin.is_valid())
1428 GetPasswordManager()->DidNavigate();
1429 }
1430
1431 // The keyword generator uses the navigation entries, so must be called after
1432 // the commit.
1433 GenerateKeywordIfNecessary(params);
1434
1435 // Allow the new page to set the title again.
1436 received_page_title_ = false;
1437
1438 // Get the favicon, either from history or request it from the net.
1439 fav_icon_helper_.FetchFavIcon(details.entry->url());
1440
[email protected]0de80162010-02-03 04:52:351441 // Clear all page actions, blocked content notifications and browser actions
1442 // for this tab, unless this is an in-page navigation.
[email protected]a6e82fc2010-02-24 22:28:141443 if (!details.is_in_page) {
[email protected]92c6f9b92009-10-24 04:35:081444 ExtensionsService* service = profile()->GetExtensionsService();
1445 if (service) {
1446 for (size_t i = 0; i < service->extensions()->size(); ++i) {
[email protected]5d246db22009-10-27 06:17:571447 ExtensionAction* browser_action =
[email protected]92c6f9b92009-10-24 04:35:081448 service->extensions()->at(i)->browser_action();
[email protected]56ce6e52009-10-27 00:10:521449 if (browser_action) {
1450 browser_action->ClearAllValuesForTab(controller().session_id().id());
1451 NotificationService::current()->Notify(
1452 NotificationType::EXTENSION_BROWSER_ACTION_UPDATED,
[email protected]5d246db22009-10-27 06:17:571453 Source<ExtensionAction>(browser_action),
[email protected]56ce6e52009-10-27 00:10:521454 NotificationService::NoDetails());
1455 }
[email protected]92c6f9b92009-10-24 04:35:081456
[email protected]5d246db22009-10-27 06:17:571457 ExtensionAction* page_action =
[email protected]56ce6e52009-10-27 00:10:521458 service->extensions()->at(i)->page_action();
1459 if (page_action) {
1460 page_action->ClearAllValuesForTab(controller().session_id().id());
1461 PageActionStateChanged();
1462 }
[email protected]92c6f9b92009-10-24 04:35:081463 }
[email protected]aece2c7f2009-08-27 20:43:171464 }
[email protected]0de80162010-02-03 04:52:351465
1466 // Close blocked popups.
1467 if (blocked_popups_) {
1468 AutoReset auto_reset(&dont_notify_render_view_, true);
1469 blocked_popups_->Destroy();
[email protected]da7b5262010-02-25 21:12:461470 blocked_popups_ = NULL;
[email protected]0de80162010-02-03 04:52:351471 }
1472
1473 // Clear "blocked" flags.
1474 ClearBlockedContentSettings();
1475 if (delegate_)
1476 delegate_->OnBlockedContentChange(this);
[email protected]aece2c7f2009-08-27 20:43:171477 }
[email protected]ce5c4502009-05-06 16:46:111478
[email protected]8cc8d492010-02-02 10:40:491479 // Close constrained windows if necessary.
1480 if (!net::RegistryControlledDomainService::SameDomainOrHost(
1481 details.previous_url, details.entry->url()))
1482 CloseConstrainedWindows();
1483
[email protected]420ae012009-04-24 05:16:321484 // Update the starred state.
1485 UpdateStarredStateForCurrentURL();
1486}
1487
1488void TabContents::DidNavigateAnyFramePostCommit(
1489 RenderViewHost* render_view_host,
1490 const NavigationController::LoadCommittedDetails& details,
1491 const ViewHostMsg_FrameNavigate_Params& params) {
1492 // If we navigate, start showing messages again. This does nothing to prevent
1493 // a malicious script from spamming messages, since the script could just
1494 // reload the page to stop blocking.
1495 suppress_javascript_messages_ = false;
1496
[email protected]420ae012009-04-24 05:16:321497 // Notify the password manager of the navigation or form submit.
1498 // TODO(brettw) bug 1343111: Password manager stuff in here needs to be
1499 // cleaned up and covered by tests.
1500 if (params.password_form.origin.is_valid())
1501 GetPasswordManager()->ProvisionallySavePassword(params.password_form);
[email protected]3b1c1812010-02-12 21:53:551502
1503 // Let the LanguageState clear its state.
[email protected]34cec8d82010-03-05 18:27:011504 bool reload = details.entry->transition_type() == PageTransition::RELOAD ||
1505 details.type == NavigationType::SAME_PAGE;
1506 language_state_.DidNavigate(reload, details.is_in_page);
[email protected]420ae012009-04-24 05:16:321507}
1508
[email protected]8cc8d492010-02-02 10:40:491509void TabContents::CloseConstrainedWindows() {
1510 // Clear out any constrained windows since we are leaving this page entirely.
[email protected]420ae012009-04-24 05:16:321511 // We use indices instead of iterators in case CloseWindow does something
1512 // that may invalidate an iterator.
1513 int size = static_cast<int>(child_windows_.size());
1514 for (int i = size - 1; i >= 0; --i) {
1515 ConstrainedWindow* window = child_windows_[i];
[email protected]fa1cf0b82010-01-15 21:49:441516 if (window) {
[email protected]420ae012009-04-24 05:16:321517 window->CloseConstrainedWindow();
[email protected]fa1cf0b82010-01-15 21:49:441518 BlockTabContent(false);
1519 }
[email protected]420ae012009-04-24 05:16:321520 }
1521}
1522
1523void TabContents::UpdateStarredStateForCurrentURL() {
1524 BookmarkModel* model = profile()->GetBookmarkModel();
1525 const bool old_state = is_starred_;
1526 is_starred_ = (model && model->IsBookmarked(GetURL()));
1527
1528 if (is_starred_ != old_state && delegate())
1529 delegate()->URLStarredChanged(this, is_starred_);
1530}
1531
1532void TabContents::UpdateAlternateErrorPageURL() {
1533 GURL url = GetAlternateErrorPageURL();
1534 render_view_host()->SetAlternateErrorPageURL(url);
1535}
1536
1537void TabContents::UpdateWebPreferences() {
1538 render_view_host()->UpdateWebPreferences(GetWebkitPrefs());
1539}
1540
1541void TabContents::UpdateMaxPageIDIfNecessary(SiteInstance* site_instance,
1542 RenderViewHost* rvh) {
1543 // If we are creating a RVH for a restored controller, then we might
1544 // have more page IDs than the SiteInstance's current max page ID. We must
1545 // make sure that the max page ID is larger than any restored page ID.
1546 // Note that it is ok for conflicting page IDs to exist in another tab
1547 // (i.e., NavigationController), but if any page ID is larger than the max,
1548 // the back/forward list will get confused.
1549 int max_restored_page_id = controller_.max_restored_page_id();
1550 if (max_restored_page_id > 0) {
1551 int curr_max_page_id = site_instance->max_page_id();
1552 if (max_restored_page_id > curr_max_page_id) {
1553 // Need to update the site instance immediately.
1554 site_instance->UpdateMaxPageID(max_restored_page_id);
1555
1556 // Also tell the renderer to update its internal representation. We
1557 // need to reserve enough IDs to make all restored page IDs less than
1558 // the max.
1559 if (curr_max_page_id < 0)
1560 curr_max_page_id = 0;
1561 rvh->ReservePageIDRange(max_restored_page_id - curr_max_page_id);
1562 }
1563 }
1564}
1565
1566void TabContents::UpdateHistoryForNavigation(
[email protected]ebe89e062009-08-13 23:16:541567 const GURL& virtual_url,
[email protected]befd8d822009-07-01 04:51:471568 const NavigationController::LoadCommittedDetails& details,
[email protected]420ae012009-04-24 05:16:321569 const ViewHostMsg_FrameNavigate_Params& params) {
1570 if (profile()->IsOffTheRecord())
1571 return;
1572
1573 // Add to history service.
1574 HistoryService* hs = profile()->GetHistoryService(Profile::IMPLICIT_ACCESS);
1575 if (hs) {
1576 if (PageTransition::IsMainFrame(params.transition) &&
[email protected]ebe89e062009-08-13 23:16:541577 virtual_url != params.url) {
1578 // Hack on the "virtual" URL so that it will appear in history. For some
[email protected]420ae012009-04-24 05:16:321579 // types of URLs, we will display a magic URL that is different from where
1580 // the page is actually navigated. We want the user to see in history
[email protected]ebe89e062009-08-13 23:16:541581 // what they saw in the URL bar, so we add the virtual URL as a redirect.
1582 // This only applies to the main frame, as the virtual URL doesn't apply
[email protected]420ae012009-04-24 05:16:321583 // to sub-frames.
1584 std::vector<GURL> redirects = params.redirects;
1585 if (!redirects.empty())
[email protected]ebe89e062009-08-13 23:16:541586 redirects.back() = virtual_url;
1587 hs->AddPage(virtual_url, this, params.page_id, params.referrer,
[email protected]befd8d822009-07-01 04:51:471588 params.transition, redirects, details.did_replace_entry);
[email protected]420ae012009-04-24 05:16:321589 } else {
1590 hs->AddPage(params.url, this, params.page_id, params.referrer,
[email protected]befd8d822009-07-01 04:51:471591 params.transition, params.redirects,
1592 details.did_replace_entry);
[email protected]420ae012009-04-24 05:16:321593 }
1594 }
1595}
1596
1597bool TabContents::UpdateTitleForEntry(NavigationEntry* entry,
1598 const std::wstring& title) {
1599 // For file URLs without a title, use the pathname instead. In the case of a
1600 // synthesized title, we don't want the update to count toward the "one set
1601 // per page of the title to history."
1602 std::wstring final_title;
1603 bool explicit_set;
1604 if (entry->url().SchemeIsFile() && title.empty()) {
1605 final_title = UTF8ToWide(entry->url().ExtractFileName());
1606 explicit_set = false; // Don't count synthetic titles toward the set limit.
1607 } else {
1608 TrimWhitespace(title, TRIM_ALL, &final_title);
1609 explicit_set = true;
1610 }
1611
1612 if (final_title == UTF16ToWideHack(entry->title()))
1613 return false; // Nothing changed, don't bother.
1614
1615 entry->set_title(WideToUTF16Hack(final_title));
1616
1617 // Update the history system for this page.
1618 if (!profile()->IsOffTheRecord() && !received_page_title_) {
1619 HistoryService* hs =
1620 profile()->GetHistoryService(Profile::IMPLICIT_ACCESS);
1621 if (hs)
[email protected]ebe89e062009-08-13 23:16:541622 hs->SetPageTitle(entry->virtual_url(), final_title);
[email protected]420ae012009-04-24 05:16:321623
1624 // Don't allow the title to be saved again for explicitly set ones.
1625 received_page_title_ = explicit_set;
1626 }
1627
1628 // Lastly, set the title for the view.
1629 view_->SetPageTitle(final_title);
1630
1631 return true;
1632}
1633
1634void TabContents::NotifySwapped() {
1635 // After sending out a swap notification, we need to send a disconnect
1636 // notification so that clients that pick up a pointer to |this| can NULL the
1637 // pointer. See Bug 1230284.
1638 notify_disconnection_ = true;
1639 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341640 NotificationType::TAB_CONTENTS_SWAPPED,
[email protected]d82ed61e2009-06-16 02:46:221641 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321642 NotificationService::NoDetails());
1643}
1644
1645void TabContents::NotifyConnected() {
1646 notify_disconnection_ = true;
1647 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341648 NotificationType::TAB_CONTENTS_CONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221649 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321650 NotificationService::NoDetails());
1651}
1652
1653void TabContents::NotifyDisconnected() {
1654 if (!notify_disconnection_)
1655 return;
1656
1657 notify_disconnection_ = false;
1658 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341659 NotificationType::TAB_CONTENTS_DISCONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221660 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321661 NotificationService::NoDetails());
1662}
1663
1664void TabContents::GenerateKeywordIfNecessary(
1665 const ViewHostMsg_FrameNavigate_Params& params) {
1666 if (!params.searchable_form_url.is_valid())
1667 return;
1668
1669 if (profile()->IsOffTheRecord())
1670 return;
1671
1672 int last_index = controller_.last_committed_entry_index();
1673 // When there was no previous page, the last index will be 0. This is
1674 // normally due to a form submit that opened in a new tab.
1675 // TODO(brettw) bug 916126: we should support keywords when form submits
1676 // happen in new tabs.
1677 if (last_index <= 0)
1678 return;
1679 const NavigationEntry* previous_entry =
1680 controller_.GetEntryAtIndex(last_index - 1);
1681 if (IsFormSubmit(previous_entry)) {
1682 // Only generate a keyword if the previous page wasn't itself a form
1683 // submit.
1684 return;
1685 }
1686
1687 GURL keyword_url = previous_entry->user_typed_url().is_valid() ?
1688 previous_entry->user_typed_url() : previous_entry->url();
1689 std::wstring keyword =
1690 TemplateURLModel::GenerateKeyword(keyword_url, true); // autodetected
1691 if (keyword.empty())
1692 return;
1693
1694 TemplateURLModel* url_model = profile()->GetTemplateURLModel();
1695 if (!url_model)
1696 return;
1697
1698 if (!url_model->loaded()) {
1699 url_model->Load();
1700 return;
1701 }
1702
1703 const TemplateURL* current_url;
1704 std::wstring url = UTF8ToWide(params.searchable_form_url.spec());
1705 if (!url_model->CanReplaceKeyword(keyword, url, &current_url))
1706 return;
1707
1708 if (current_url) {
1709 if (current_url->originating_url().is_valid()) {
1710 // The existing keyword was generated from an OpenSearch description
1711 // document, don't regenerate.
1712 return;
1713 }
1714 url_model->Remove(current_url);
1715 }
1716 TemplateURL* new_url = new TemplateURL();
1717 new_url->set_keyword(keyword);
1718 new_url->set_short_name(keyword);
1719 new_url->SetURL(url, 0, 0);
1720 new_url->add_input_encoding(params.searchable_form_encoding);
1721 DCHECK(controller_.GetLastCommittedEntry());
1722 const GURL& favicon_url =
1723 controller_.GetLastCommittedEntry()->favicon().url();
1724 if (favicon_url.is_valid()) {
1725 new_url->SetFavIconURL(favicon_url);
1726 } else {
1727 // The favicon url isn't valid. This means there really isn't a favicon,
1728 // or the favicon url wasn't obtained before the load started. This assumes
1729 // the later.
1730 // TODO(sky): Need a way to set the favicon that doesn't involve generating
1731 // its url.
1732 new_url->SetFavIconURL(TemplateURL::GenerateFaviconURL(params.referrer));
1733 }
1734 new_url->set_safe_for_autoreplace(true);
1735 url_model->Add(new_url);
1736}
1737
[email protected]bf5c2ff392009-07-08 16:24:331738void TabContents::OnUserGesture() {
1739 // See comment in RenderViewHostDelegate::OnUserGesture as to why we do this.
1740 DownloadRequestManager* drm = g_browser_process->download_request_manager();
1741 if (drm)
1742 drm->OnUserGesture(this);
[email protected]e7eaedde2009-09-25 20:09:491743 ExternalProtocolHandler::OnUserGesture();
[email protected]bf5c2ff392009-07-08 16:24:331744 controller_.OnUserGesture();
1745}
1746
1747void TabContents::OnFindReply(int request_id,
1748 int number_of_matches,
1749 const gfx::Rect& selection_rect,
1750 int active_match_ordinal,
1751 bool final_update) {
[email protected]3ce7ced2009-07-24 18:52:421752 // Ignore responses for requests that have been aborted.
1753 if (find_op_aborted_)
1754 return;
1755
[email protected]bf5c2ff392009-07-08 16:24:331756 // Ignore responses for requests other than the one we have most recently
1757 // issued. That way we won't act on stale results when the user has
1758 // already typed in another query.
1759 if (request_id != current_find_request_id_)
1760 return;
1761
1762 if (number_of_matches == -1)
1763 number_of_matches = last_search_result_.number_of_matches();
1764 if (active_match_ordinal == -1)
1765 active_match_ordinal = last_search_result_.active_match_ordinal();
1766
1767 gfx::Rect selection = selection_rect;
1768 if (selection.IsEmpty())
1769 selection = last_search_result_.selection_rect();
1770
1771 // Notify the UI, automation and any other observers that a find result was
1772 // found.
1773 last_search_result_ = FindNotificationDetails(request_id, number_of_matches,
1774 selection, active_match_ordinal,
1775 final_update);
1776 NotificationService::current()->Notify(
1777 NotificationType::FIND_RESULT_AVAILABLE,
1778 Source<TabContents>(this),
1779 Details<FindNotificationDetails>(&last_search_result_));
1780}
1781
1782void TabContents::GoToEntryAtOffset(int offset) {
[email protected]f9cc4c452009-10-13 14:56:381783 if (!delegate_ || delegate_->OnGoToEntryOffset(offset))
1784 controller_.GoToOffset(offset);
[email protected]bf5c2ff392009-07-08 16:24:331785}
1786
1787void TabContents::GetHistoryListCount(int* back_list_count,
1788 int* forward_list_count) {
1789 int current_index = controller_.last_committed_entry_index();
1790 *back_list_count = current_index;
1791 *forward_list_count = controller_.entry_count() - current_index - 1;
1792}
1793
1794void TabContents::OnMissingPluginStatus(int status) {
1795#if defined(OS_WIN)
1796// TODO(PORT): pull in when plug-ins work
1797 GetPluginInstaller()->OnMissingPluginStatus(status);
1798#endif
1799}
1800
1801void TabContents::OnCrashedPlugin(const FilePath& plugin_path) {
[email protected]bf5c2ff392009-07-08 16:24:331802 DCHECK(!plugin_path.value().empty());
1803
1804 std::wstring plugin_name = plugin_path.ToWStringHack();
[email protected]d32d46182009-07-23 00:09:351805#if defined(OS_WIN) || defined(OS_MACOSX)
[email protected]bf5c2ff392009-07-08 16:24:331806 scoped_ptr<FileVersionInfo> version_info(
1807 FileVersionInfo::CreateFileVersionInfo(plugin_path));
1808 if (version_info.get()) {
1809 const std::wstring& product_name = version_info->product_name();
[email protected]996fd702009-09-04 19:12:371810 if (!product_name.empty()) {
[email protected]bf5c2ff392009-07-08 16:24:331811 plugin_name = product_name;
[email protected]996fd702009-09-04 19:12:371812#if defined(OS_MACOSX)
1813 // Many plugins on the Mac have .plugin in the actual name, which looks
1814 // terrible, so look for that and strip it off if present.
1815 const std::wstring plugin_extension(L".plugin");
1816 if (EndsWith(plugin_name, plugin_extension, true))
1817 plugin_name.erase(plugin_name.length() - plugin_extension.length());
1818#endif // OS_MACOSX
1819 }
[email protected]bf5c2ff392009-07-08 16:24:331820 }
[email protected]4a560ed2009-07-13 23:35:261821#else
1822 NOTIMPLEMENTED() << " convert plugin path to plugin name";
1823#endif
[email protected]bf5c2ff392009-07-08 16:24:331824 AddInfoBar(new SimpleAlertInfoBarDelegate(
1825 this, l10n_util::GetStringF(IDS_PLUGIN_CRASHED_PROMPT, plugin_name),
1826 NULL));
[email protected]bf5c2ff392009-07-08 16:24:331827}
1828
1829void TabContents::OnCrashedWorker() {
1830 AddInfoBar(new SimpleAlertInfoBarDelegate(
1831 this, l10n_util::GetString(IDS_WEBWORKER_CRASHED_PROMPT),
1832 NULL));
1833}
1834
1835void TabContents::OnDidGetApplicationInfo(
1836 int32 page_id,
1837 const webkit_glue::WebApplicationInfo& info) {
[email protected]ed5431872009-11-17 08:39:511838 web_app_info_ = info;
[email protected]bf5c2ff392009-07-08 16:24:331839
[email protected]ed5431872009-11-17 08:39:511840 if (delegate())
1841 delegate()->OnDidGetApplicationInfo(this, page_id);
[email protected]bf5c2ff392009-07-08 16:24:331842}
1843
[email protected]a2cf65eb2010-01-18 08:20:381844void TabContents::OnPageContents(const GURL& url,
1845 int renderer_process_id,
1846 int32 page_id,
[email protected]a8a81292010-01-21 00:32:451847 const std::wstring& contents,
1848 const std::string& language) {
[email protected]a2cf65eb2010-01-18 08:20:381849 // Don't index any https pages. People generally don't want their bank
1850 // accounts, etc. indexed on their computer, especially since some of these
1851 // things are not marked cachable.
1852 // TODO(brettw) we may want to consider more elaborate heuristics such as
1853 // the cachability of the page. We may also want to consider subframes (this
1854 // test will still index subframes if the subframe is SSL).
1855 if (!url.SchemeIsSecure()) {
1856 Profile* p = profile();
1857 if (p && !p->IsOffTheRecord()) {
1858 HistoryService* hs = p->GetHistoryService(Profile::IMPLICIT_ACCESS);
1859 if (hs)
1860 hs->SetPageContents(url, contents);
1861 }
1862 }
[email protected]a8a81292010-01-21 00:32:451863
[email protected]3b1c1812010-02-12 21:53:551864 language_state_.LanguageDetermined(language);
[email protected]a8a81292010-01-21 00:32:451865
1866 std::string lang = language;
1867 NotificationService::current()->Notify(
1868 NotificationType::TAB_LANGUAGE_DETERMINED,
[email protected]7e0831b2010-02-01 08:45:151869 Source<TabContents>(this),
[email protected]a8a81292010-01-21 00:32:451870 Details<std::string>(&lang));
[email protected]a2cf65eb2010-01-18 08:20:381871}
1872
[email protected]7e0831b2010-02-01 08:45:151873void TabContents::OnPageTranslated(int32 page_id,
1874 const std::string& original_lang,
1875 const std::string& translated_lang) {
[email protected]3b1c1812010-02-12 21:53:551876 language_state_.set_current_language(translated_lang);
1877 language_state_.set_translation_pending(false);
[email protected]7e0831b2010-02-01 08:45:151878 std::pair<std::string, std::string> lang_pair =
1879 std::make_pair(original_lang, translated_lang);
1880 NotificationService::current()->Notify(
1881 NotificationType::PAGE_TRANSLATED,
1882 Source<TabContents>(this),
1883 Details<std::pair<std::string, std::string> >(&lang_pair));
1884}
1885
[email protected]bf5c2ff392009-07-08 16:24:331886void TabContents::DidStartProvisionalLoadForFrame(
1887 RenderViewHost* render_view_host,
1888 bool is_main_frame,
1889 const GURL& url) {
1890 ProvisionalLoadDetails details(is_main_frame,
1891 controller_.IsURLInPageNavigation(url),
1892 url, std::string(), false);
1893 NotificationService::current()->Notify(
1894 NotificationType::FRAME_PROVISIONAL_LOAD_START,
1895 Source<NavigationController>(&controller_),
1896 Details<ProvisionalLoadDetails>(&details));
1897}
1898
1899void TabContents::DidStartReceivingResourceResponse(
[email protected]3dbb80b2010-02-09 22:41:201900 const ResourceRequestDetails& details) {
[email protected]bf5c2ff392009-07-08 16:24:331901 NotificationService::current()->Notify(
1902 NotificationType::RESOURCE_RESPONSE_STARTED,
1903 Source<NavigationController>(&controller()),
[email protected]3dbb80b2010-02-09 22:41:201904 Details<const ResourceRequestDetails>(&details));
[email protected]bf5c2ff392009-07-08 16:24:331905}
1906
[email protected]3dbb80b2010-02-09 22:41:201907void TabContents::DidRedirectResource(
1908 const ResourceRedirectDetails& details) {
[email protected]bf5c2ff392009-07-08 16:24:331909 NotificationService::current()->Notify(
1910 NotificationType::RESOURCE_RECEIVED_REDIRECT,
1911 Source<NavigationController>(&controller()),
[email protected]3dbb80b2010-02-09 22:41:201912 Details<const ResourceRequestDetails>(&details));
[email protected]bf5c2ff392009-07-08 16:24:331913}
1914
1915void TabContents::DidLoadResourceFromMemoryCache(
1916 const GURL& url,
1917 const std::string& frame_origin,
1918 const std::string& main_frame_origin,
1919 const std::string& security_info) {
1920 // Send out a notification that we loaded a resource from our memory cache.
1921 int cert_id = 0, cert_status = 0, security_bits = 0;
1922 SSLManager::DeserializeSecurityInfo(security_info,
1923 &cert_id, &cert_status,
1924 &security_bits);
1925 LoadFromMemoryCacheDetails details(url, frame_origin, main_frame_origin,
[email protected]8cb5d5b2010-02-09 11:36:161926 GetRenderProcessHost()->id(), cert_id,
1927 cert_status);
[email protected]bf5c2ff392009-07-08 16:24:331928
1929 NotificationService::current()->Notify(
1930 NotificationType::LOAD_FROM_MEMORY_CACHE,
1931 Source<NavigationController>(&controller_),
1932 Details<LoadFromMemoryCacheDetails>(&details));
1933}
1934
[email protected]e3d60e5d2009-09-25 21:08:291935void TabContents::DidDisplayInsecureContent() {
[email protected]09843b5f2009-09-26 08:30:591936 controller_.ssl_manager()->DidDisplayInsecureContent();
[email protected]e3d60e5d2009-09-25 21:08:291937}
1938
1939void TabContents::DidRunInsecureContent(const std::string& security_origin) {
[email protected]09843b5f2009-09-26 08:30:591940 controller_.ssl_manager()->DidRunInsecureContent(security_origin);
[email protected]e3d60e5d2009-09-25 21:08:291941}
1942
[email protected]bf5c2ff392009-07-08 16:24:331943void TabContents::DidFailProvisionalLoadWithError(
1944 RenderViewHost* render_view_host,
1945 bool is_main_frame,
1946 int error_code,
1947 const GURL& url,
1948 bool showing_repost_interstitial) {
1949 if (net::ERR_ABORTED == error_code) {
1950 // EVIL HACK ALERT! Ignore failed loads when we're showing interstitials.
1951 // This means that the interstitial won't be torn down properly, which is
1952 // bad. But if we have an interstitial, go back to another tab type, and
1953 // then load the same interstitial again, we could end up getting the first
1954 // interstitial's "failed" message (as a result of the cancel) when we're on
1955 // the second one.
1956 //
1957 // We can't tell this apart, so we think we're tearing down the current page
1958 // which will cause a crash later one. There is also some code in
1959 // RenderViewHostManager::RendererAbortedProvisionalLoad that is commented
1960 // out because of this problem.
1961 //
1962 // http://code.google.com/p/chromium/issues/detail?id=2855
1963 // Because this will not tear down the interstitial properly, if "back" is
1964 // back to another tab type, the interstitial will still be somewhat alive
1965 // in the previous tab type. If you navigate somewhere that activates the
1966 // tab with the interstitial again, you'll see a flash before the new load
1967 // commits of the interstitial page.
1968 if (showing_interstitial_page()) {
1969 LOG(WARNING) << "Discarding message during interstitial.";
1970 return;
1971 }
1972
1973 // This will discard our pending entry if we cancelled the load (e.g., if we
1974 // decided to download the file instead of load it). Only discard the
1975 // pending entry if the URLs match, otherwise the user initiated a navigate
1976 // before the page loaded so that the discard would discard the wrong entry.
1977 NavigationEntry* pending_entry = controller_.pending_entry();
1978 if (pending_entry && pending_entry->url() == url) {
1979 controller_.DiscardNonCommittedEntries();
1980 // Update the URL display.
1981 NotifyNavigationStateChanged(TabContents::INVALIDATE_URL);
1982 }
1983
1984 render_manager_.RendererAbortedProvisionalLoad(render_view_host);
1985 }
1986
1987 // Send out a notification that we failed a provisional load with an error.
1988 ProvisionalLoadDetails details(is_main_frame,
1989 controller_.IsURLInPageNavigation(url),
1990 url, std::string(), false);
1991 details.set_error_code(error_code);
1992
1993 NotificationService::current()->Notify(
1994 NotificationType::FAIL_PROVISIONAL_LOAD_WITH_ERROR,
1995 Source<NavigationController>(&controller_),
1996 Details<ProvisionalLoadDetails>(&details));
1997}
1998
1999void TabContents::DocumentLoadedInFrame() {
2000 controller_.DocumentLoadedInFrame();
2001}
2002
[email protected]0de80162010-02-03 04:52:352003void TabContents::OnContentBlocked(ContentSettingsType type) {
2004 content_blocked_[type] = true;
2005 if (delegate_)
2006 delegate_->OnBlockedContentChange(this);
2007}
2008
[email protected]8d3347f2009-07-09 22:00:212009RenderViewHostDelegate::View* TabContents::GetViewDelegate() {
[email protected]420ae012009-04-24 05:16:322010 return view_.get();
2011}
2012
[email protected]8d3347f2009-07-09 22:00:212013RenderViewHostDelegate::RendererManagement*
2014TabContents::GetRendererManagementDelegate() {
2015 return &render_manager_;
2016}
2017
[email protected]bf5c2ff392009-07-08 16:24:332018RenderViewHostDelegate::BrowserIntegration*
[email protected]8d3347f2009-07-09 22:00:212019 TabContents::GetBrowserIntegrationDelegate() {
2020 return this;
[email protected]bf5c2ff392009-07-08 16:24:332021}
2022
[email protected]8d3347f2009-07-09 22:00:212023RenderViewHostDelegate::Resource* TabContents::GetResourceDelegate() {
2024 return this;
[email protected]bf5c2ff392009-07-08 16:24:332025}
2026
[email protected]8d3347f2009-07-09 22:00:212027RenderViewHostDelegate::Save* TabContents::GetSaveDelegate() {
[email protected]420ae012009-04-24 05:16:322028 return save_package_.get(); // May be NULL, but we can return NULL.
2029}
2030
[email protected]8d3347f2009-07-09 22:00:212031RenderViewHostDelegate::Printing* TabContents::GetPrintingDelegate() {
2032 return &printing_;
2033}
2034
2035RenderViewHostDelegate::FavIcon* TabContents::GetFavIconDelegate() {
2036 return &fav_icon_helper_;
2037}
2038
[email protected]c54d269e2009-10-21 18:07:382039RenderViewHostDelegate::FormFieldHistory*
2040TabContents::GetFormFieldHistoryDelegate() {
[email protected]6db0def92010-03-02 19:55:452041 if (autocomplete_history_manager_.get() == NULL)
2042 autocomplete_history_manager_.reset(new AutocompleteHistoryManager(this));
2043 return autocomplete_history_manager_.get();
[email protected]bf5c2ff392009-07-08 16:24:332044}
2045
[email protected]21674df2009-10-28 19:51:212046RenderViewHostDelegate::AutoFill* TabContents::GetAutoFillDelegate() {
2047 if (autofill_manager_.get() == NULL)
2048 autofill_manager_.reset(new AutoFillManager(this));
2049 return autofill_manager_.get();
2050}
2051
[email protected]ced90ae12010-02-20 02:06:162052RenderViewHostDelegate::BookmarkDrag* TabContents::GetBookmarkDragDelegate() {
2053 return bookmark_drag_;
2054}
2055
2056void TabContents::SetBookmarkDragDelegate(
2057 RenderViewHostDelegate::BookmarkDrag* bookmark_drag) {
2058 bookmark_drag_ = bookmark_drag;
2059}
2060
[email protected]93623c5d2009-12-10 21:40:322061RendererPreferences TabContents::GetRendererPrefs(Profile* profile) const {
[email protected]840b1512009-07-21 16:53:462062 return renderer_preferences_;
[email protected]80d96fa2009-06-10 22:34:512063}
2064
[email protected]57c6a652009-05-04 07:58:342065TabContents* TabContents::GetAsTabContents() {
2066 return this;
2067}
2068
[email protected]7b291f92009-08-14 05:43:532069ViewType::Type TabContents::GetRenderViewType() const {
2070 return ViewType::TAB_CONTENTS;
2071}
2072
2073int TabContents::GetBrowserWindowID() const {
2074 return controller().window_id().id();
2075}
2076
[email protected]420ae012009-04-24 05:16:322077void TabContents::RenderViewCreated(RenderViewHost* render_view_host) {
[email protected]2e4633c2009-07-09 16:58:062078 NotificationService::current()->Notify(
2079 NotificationType::RENDER_VIEW_HOST_CREATED_FOR_TAB,
2080 Source<TabContents>(this),
2081 Details<RenderViewHost>(render_view_host));
[email protected]420ae012009-04-24 05:16:322082 NavigationEntry* entry = controller_.GetActiveEntry();
2083 if (!entry)
2084 return;
2085
2086 // When we're creating views, we're still doing initial setup, so we always
2087 // use the pending DOM UI rather than any possibly existing committed one.
2088 if (render_manager_.pending_dom_ui()) {
2089 render_manager_.pending_dom_ui()->RenderViewCreated(render_view_host);
2090 }
2091
2092 if (entry->IsViewSourceMode()) {
2093 // Put the renderer in view source mode.
2094 render_view_host->Send(
2095 new ViewMsg_EnableViewSourceMode(render_view_host->routing_id()));
2096 }
[email protected]0666aef2009-05-13 19:48:082097
2098 view()->RenderViewCreated(render_view_host);
[email protected]420ae012009-04-24 05:16:322099}
2100
2101void TabContents::RenderViewReady(RenderViewHost* rvh) {
2102 if (rvh != render_view_host()) {
2103 // Don't notify the world, since this came from a renderer in the
2104 // background.
2105 return;
2106 }
2107
2108 NotifyConnected();
[email protected]9d8a4642009-07-29 17:25:302109 bool was_crashed = is_crashed();
[email protected]420ae012009-04-24 05:16:322110 SetIsCrashed(false);
[email protected]9d8a4642009-07-29 17:25:302111
2112 // Restore the focus to the tab (otherwise the focus will be on the top
2113 // window).
2114 if (was_crashed && !FocusLocationBarByDefault())
2115 Focus();
[email protected]420ae012009-04-24 05:16:322116}
2117
2118void TabContents::RenderViewGone(RenderViewHost* rvh) {
2119 // Ask the print preview if this renderer was valuable.
2120 if (!printing_.OnRenderViewGone(rvh))
2121 return;
2122 if (rvh != render_view_host()) {
2123 // The pending page's RenderViewHost is gone.
2124 return;
2125 }
2126
2127 SetIsLoading(false, NULL);
2128 NotifyDisconnected();
2129 SetIsCrashed(true);
2130
[email protected]ff9efc62010-02-18 22:10:542131 // Remove all infobars.
[email protected]e5430c92010-02-18 22:39:212132 for (int i = infobar_delegate_count() - 1; i >=0 ; --i)
[email protected]ff9efc62010-02-18 22:10:542133 RemoveInfoBar(GetInfoBarDelegateAt(i));
2134
[email protected]8e537302009-06-10 21:19:582135 // Tell the view that we've crashed so it can prepare the sad tab page.
[email protected]cf68b092010-01-15 20:05:552136 // Only do this if we're not in browser shutdown, so that TabContents
2137 // objects that are not in a browser (e.g., HTML dialogs) and thus are
2138 // visible do not flash a sad tab page.
2139 if (browser_shutdown::GetShutdownType() == browser_shutdown::NOT_VALID)
2140 view_->OnTabCrashed();
[email protected]420ae012009-04-24 05:16:322141
2142 // Hide any visible hung renderer warning for this web contents' process.
[email protected]e7b8d832009-11-20 22:41:002143 hung_renderer_dialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:322144}
2145
[email protected]2e4633c2009-07-09 16:58:062146void TabContents::RenderViewDeleted(RenderViewHost* rvh) {
2147 NotificationService::current()->Notify(
2148 NotificationType::RENDER_VIEW_HOST_DELETED,
2149 Source<TabContents>(this),
2150 Details<RenderViewHost>(rvh));
[email protected]fb7b79f72009-11-06 18:00:482151 render_manager_.RenderViewDeleted(rvh);
[email protected]2e4633c2009-07-09 16:58:062152}
2153
[email protected]420ae012009-04-24 05:16:322154void TabContents::DidNavigate(RenderViewHost* rvh,
2155 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]8030f012009-09-25 18:09:372156 int extra_invalidate_flags = 0;
2157
2158 if (PageTransition::IsMainFrame(params.transition)) {
[email protected]20ede072009-10-06 00:28:352159 bool was_bookmark_bar_visible = ShouldShowBookmarkBar();
[email protected]8030f012009-09-25 18:09:372160 bool was_extension_shelf_visible = IsExtensionShelfAlwaysVisible();
2161
[email protected]420ae012009-04-24 05:16:322162 render_manager_.DidNavigateMainFrame(rvh);
2163
[email protected]20ede072009-10-06 00:28:352164 if (was_bookmark_bar_visible != ShouldShowBookmarkBar())
[email protected]8030f012009-09-25 18:09:372165 extra_invalidate_flags |= INVALIDATE_BOOKMARK_BAR;
2166 if (was_extension_shelf_visible != IsExtensionShelfAlwaysVisible())
2167 extra_invalidate_flags |= INVALIDATE_EXTENSION_SHELF;
2168 }
2169
[email protected]420ae012009-04-24 05:16:322170 // Update the site of the SiteInstance if it doesn't have one yet.
2171 if (!GetSiteInstance()->has_site())
2172 GetSiteInstance()->SetSite(params.url);
2173
2174 // Need to update MIME type here because it's referred to in
2175 // UpdateNavigationCommands() called by RendererDidNavigate() to
2176 // determine whether or not to enable the encoding menu.
2177 // It's updated only for the main frame. For a subframe,
2178 // RenderView::UpdateURL does not set params.contents_mime_type.
2179 // (see http://code.google.com/p/chromium/issues/detail?id=2929 )
2180 // TODO(jungshik): Add a test for the encoding menu to avoid
2181 // regressing it again.
2182 if (PageTransition::IsMainFrame(params.transition))
2183 contents_mime_type_ = params.contents_mime_type;
2184
2185 NavigationController::LoadCommittedDetails details;
[email protected]8030f012009-09-25 18:09:372186 bool did_navigate = controller_.RendererDidNavigate(
2187 params, extra_invalidate_flags, &details);
[email protected]3e90d4a2009-07-03 17:38:392188
2189 // Update history. Note that this needs to happen after the entry is complete,
2190 // which WillNavigate[Main,Sub]Frame will do before this function is called.
[email protected]2e29d922009-10-22 22:13:352191 if (params.should_update_history &&
2192 (!delegate() || delegate()->ShouldAddNavigationsToHistory())) {
[email protected]3e90d4a2009-07-03 17:38:392193 // Most of the time, the displayURL matches the loaded URL, but for about:
2194 // URLs, we use a data: URL as the real value. We actually want to save
2195 // the about: URL to the history db and keep the data: URL hidden. This is
2196 // what the TabContents' URL getter does.
2197 UpdateHistoryForNavigation(GetURL(), details, params);
2198 }
2199
[email protected]76543b92009-08-31 17:27:452200 if (!did_navigate)
2201 return; // No navigation happened.
[email protected]420ae012009-04-24 05:16:322202
2203 // DO NOT ADD MORE STUFF TO THIS FUNCTION! Your component should either listen
2204 // for the appropriate notification (best) or you can add it to
2205 // DidNavigateMainFramePostCommit / DidNavigateAnyFramePostCommit (only if
2206 // necessary, please).
2207
2208 // Run post-commit tasks.
2209 if (details.is_main_frame)
2210 DidNavigateMainFramePostCommit(details, params);
2211 DidNavigateAnyFramePostCommit(rvh, details, params);
2212}
2213
2214void TabContents::UpdateState(RenderViewHost* rvh,
2215 int32 page_id,
2216 const std::string& state) {
2217 DCHECK(rvh == render_view_host());
2218
2219 // We must be prepared to handle state updates for any page, these occur
2220 // when the user is scrolling and entering form data, as well as when we're
2221 // leaving a page, in which case our state may have already been moved to
2222 // the next page. The navigation controller will look up the appropriate
2223 // NavigationEntry and update it when it is notified via the delegate.
2224
2225 int entry_index = controller_.GetEntryIndexWithPageID(
2226 GetSiteInstance(), page_id);
2227 if (entry_index < 0)
2228 return;
2229 NavigationEntry* entry = controller_.GetEntryAtIndex(entry_index);
2230
2231 if (state == entry->content_state())
2232 return; // Nothing to update.
2233 entry->set_content_state(state);
2234 controller_.NotifyEntryChanged(entry, entry_index);
2235}
2236
2237void TabContents::UpdateTitle(RenderViewHost* rvh,
2238 int32 page_id, const std::wstring& title) {
2239 // If we have a title, that's a pretty good indication that we've started
2240 // getting useful data.
2241 SetNotWaitingForResponse();
2242
2243 DCHECK(rvh == render_view_host());
2244 NavigationEntry* entry = controller_.GetEntryWithPageID(GetSiteInstance(),
2245 page_id);
2246 if (!entry || !UpdateTitleForEntry(entry, title))
2247 return;
2248
2249 // Broadcast notifications when the UI should be updated.
2250 if (entry == controller_.GetEntryAtOffset(0))
[email protected]f1cd5e82009-10-23 17:19:032251 NotifyNavigationStateChanged(INVALIDATE_TITLE);
[email protected]420ae012009-04-24 05:16:322252}
2253
[email protected]420ae012009-04-24 05:16:322254void TabContents::UpdateEncoding(RenderViewHost* render_view_host,
[email protected]41fc0322009-09-04 22:23:402255 const std::string& encoding) {
[email protected]420ae012009-04-24 05:16:322256 set_encoding(encoding);
2257}
2258
2259void TabContents::UpdateTargetURL(int32 page_id, const GURL& url) {
2260 if (delegate())
2261 delegate()->UpdateTargetURL(this, url);
2262}
2263
2264void TabContents::UpdateThumbnail(const GURL& url,
2265 const SkBitmap& bitmap,
2266 const ThumbnailScore& score) {
2267 // Tell History about this thumbnail
[email protected]449478302009-06-09 20:04:282268 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kThumbnailStore)) {
[email protected]72d0fc72009-07-09 17:30:492269 if (!profile()->IsOffTheRecord())
[email protected]42339402009-07-17 17:27:192270 profile()->GetThumbnailStore()->SetPageThumbnail(url, bitmap,
2271 score, true);
[email protected]449478302009-06-09 20:04:282272 } else {
2273 HistoryService* hs;
2274 if (!profile()->IsOffTheRecord() &&
2275 (hs = profile()->GetHistoryService(Profile::IMPLICIT_ACCESS))) {
2276 hs->SetPageThumbnail(url, bitmap, score);
2277 }
[email protected]420ae012009-04-24 05:16:322278 }
2279}
2280
[email protected]8922e1f2009-10-03 05:01:262281void TabContents::UpdateInspectorSettings(const std::string& raw_settings) {
[email protected]0df30122009-06-03 12:13:082282 profile()->GetPrefs()->SetString(prefs::kWebKitInspectorSettings,
[email protected]8922e1f2009-10-03 05:01:262283 UTF8ToWide(raw_settings));
[email protected]0df30122009-06-03 12:13:082284}
2285
[email protected]420ae012009-04-24 05:16:322286void TabContents::Close(RenderViewHost* rvh) {
[email protected]07707302009-11-06 00:50:292287 // The UI may be in an event-tracking loop, such as between the
2288 // mouse-down and mouse-up in text selection or a button click.
2289 // Defer the close until after tracking is complete, so that we
2290 // don't free objects out from under the UI.
2291 // TODO(shess): This could probably be integrated with the
2292 // IsDoingDrag() test below. Punting for now because I need more
2293 // research to understand how this impacts platforms other than Mac.
2294 // TODO(shess): This could get more fine-grained. For instance,
2295 // closing a tab in another window while selecting text in the
2296 // current window's Omnibox should be just fine.
2297 if (view()->IsEventTracking()) {
2298 view()->CloseTabAfterEventTracking();
2299 return;
2300 }
2301
[email protected]24a4d1062009-07-10 23:10:422302 // If we close the tab while we're in the middle of a drag, we'll crash.
2303 // Instead, cancel the drag and close it as soon as the drag ends.
2304 if (view()->IsDoingDrag()) {
2305 view()->CancelDragAndCloseTab();
2306 return;
2307 }
2308
[email protected]420ae012009-04-24 05:16:322309 // Ignore this if it comes from a RenderViewHost that we aren't showing.
2310 if (delegate() && rvh == render_view_host())
2311 delegate()->CloseContents(this);
2312}
2313
2314void TabContents::RequestMove(const gfx::Rect& new_bounds) {
2315 if (delegate() && delegate()->IsPopup(this))
2316 delegate()->MoveContents(this, new_bounds);
2317}
2318
[email protected]7ab1e7d62009-10-14 23:32:012319void TabContents::DidStartLoading() {
[email protected]420ae012009-04-24 05:16:322320 SetIsLoading(true, NULL);
2321}
2322
[email protected]7ab1e7d62009-10-14 23:32:012323void TabContents::DidStopLoading() {
[email protected]420ae012009-04-24 05:16:322324 scoped_ptr<LoadNotificationDetails> details;
2325
2326 NavigationEntry* entry = controller_.GetActiveEntry();
2327 // An entry may not exist for a stop when loading an initial blank page or
2328 // if an iframe injected by script into a blank page finishes loading.
2329 if (entry) {
[email protected]420ae012009-04-24 05:16:322330 base::TimeDelta elapsed = base::TimeTicks::Now() - current_load_start_;
2331
2332 details.reset(new LoadNotificationDetails(
[email protected]ebe89e062009-08-13 23:16:542333 entry->virtual_url(),
[email protected]420ae012009-04-24 05:16:322334 entry->transition_type(),
2335 elapsed,
2336 &controller_,
2337 controller_.GetCurrentEntryIndex()));
2338 }
2339
2340 // Tell PasswordManager we've finished a page load, which serves as a
2341 // green light to save pending passwords and reset itself.
2342 GetPasswordManager()->DidStopLoading();
2343
2344 SetIsLoading(false, details.get());
2345}
2346
[email protected]420ae012009-04-24 05:16:322347void TabContents::DidRedirectProvisionalLoad(int32 page_id,
2348 const GURL& source_url,
2349 const GURL& target_url) {
2350 NavigationEntry* entry;
2351 if (page_id == -1)
2352 entry = controller_.pending_entry();
2353 else
2354 entry = controller_.GetEntryWithPageID(GetSiteInstance(), page_id);
2355 if (!entry || entry->url() != source_url)
2356 return;
2357 entry->set_url(target_url);
2358}
2359
[email protected]420ae012009-04-24 05:16:322360void TabContents::RequestOpenURL(const GURL& url, const GURL& referrer,
2361 WindowOpenDisposition disposition) {
2362 if (render_manager_.dom_ui()) {
2363 // When we're a DOM UI, it will provide a page transition type for us (this
2364 // is so the new tab page can specify AUTO_BOOKMARK for automatically
2365 // generated suggestions).
2366 //
2367 // Note also that we hide the referrer for DOM UI pages. We don't really
[email protected]60e448982009-05-06 04:21:162368 // want web sites to see a referrer of "chrome://blah" (and some
2369 // chrome: URLs might have search terms or other stuff we don't want to
[email protected]420ae012009-04-24 05:16:322370 // send to the site), so we send no referrer.
2371 OpenURL(url, GURL(), disposition,
2372 render_manager_.dom_ui()->link_transition_type());
2373 } else {
2374 OpenURL(url, referrer, disposition, PageTransition::LINK);
2375 }
2376}
2377
[email protected]1ee614862009-07-06 20:29:182378void TabContents::DomOperationResponse(const std::string& json_string,
2379 int automation_id) {
2380}
2381
[email protected]420ae012009-04-24 05:16:322382void TabContents::ProcessDOMUIMessage(const std::string& message,
[email protected]e4dad9fb2009-10-06 18:15:582383 const Value* content,
[email protected]811bfe32009-07-01 08:46:252384 int request_id,
2385 bool has_callback) {
[email protected]420ae012009-04-24 05:16:322386 if (!render_manager_.dom_ui()) {
[email protected]c2e74fe82009-09-03 17:57:442387 // This can happen if someone uses window.open() to open an extension URL
2388 // from a non-extension context.
2389 render_view_host()->BlockExtensionRequest(request_id);
[email protected]420ae012009-04-24 05:16:322390 return;
2391 }
[email protected]811bfe32009-07-01 08:46:252392 render_manager_.dom_ui()->ProcessDOMUIMessage(message, content, request_id,
2393 has_callback);
[email protected]420ae012009-04-24 05:16:322394}
2395
2396void TabContents::ProcessExternalHostMessage(const std::string& message,
2397 const std::string& origin,
2398 const std::string& target) {
2399 if (delegate())
2400 delegate()->ForwardMessageToExternalHost(message, origin, target);
2401}
2402
[email protected]420ae012009-04-24 05:16:322403void TabContents::RunFileChooser(bool multiple_files,
2404 const string16& title,
2405 const FilePath& default_file) {
2406 if (!select_file_dialog_.get())
2407 select_file_dialog_ = SelectFileDialog::Create(this);
2408 SelectFileDialog::Type dialog_type =
2409 multiple_files ? SelectFileDialog::SELECT_OPEN_MULTI_FILE :
2410 SelectFileDialog::SELECT_OPEN_FILE;
2411 select_file_dialog_->SelectFile(dialog_type, title, default_file,
2412 NULL, 0, FILE_PATH_LITERAL(""),
2413 view_->GetTopLevelNativeWindow(), NULL);
2414}
2415
2416void TabContents::RunJavaScriptMessage(
2417 const std::wstring& message,
2418 const std::wstring& default_prompt,
2419 const GURL& frame_url,
2420 const int flags,
2421 IPC::Message* reply_msg,
2422 bool* did_suppress_message) {
2423 // Suppress javascript messages when requested and when inside a constrained
2424 // popup window (because that activates them and breaks them out of the
2425 // constrained window jail).
[email protected]91ed49a2009-07-07 21:29:552426 // Also suppress messages when showing an interstitial. The interstitial is
2427 // shown over the previous page, we don't want the hidden page dialogs to
2428 // interfere with the interstitial.
2429 bool suppress_this_message = suppress_javascript_messages_ ||
2430 showing_interstitial_page();
[email protected]420ae012009-04-24 05:16:322431 if (delegate())
2432 suppress_this_message |=
[email protected]b6c874582009-05-08 19:38:312433 (delegate()->GetConstrainingContents(this) != this);
[email protected]420ae012009-04-24 05:16:322434
2435 *did_suppress_message = suppress_this_message;
2436
2437 if (!suppress_this_message) {
2438 base::TimeDelta time_since_last_message(
2439 base::TimeTicks::Now() - last_javascript_message_dismissal_);
2440 bool show_suppress_checkbox = false;
2441 // Show a checkbox offering to suppress further messages if this message is
2442 // being displayed within kJavascriptMessageExpectedDelay of the last one.
2443 if (time_since_last_message <
2444 base::TimeDelta::FromMilliseconds(kJavascriptMessageExpectedDelay))
2445 show_suppress_checkbox = true;
2446
[email protected]d82ed61e2009-06-16 02:46:222447 RunJavascriptMessageBox(this, frame_url, flags, message, default_prompt,
[email protected]420ae012009-04-24 05:16:322448 show_suppress_checkbox, reply_msg);
2449 } else {
2450 // If we are suppressing messages, just reply as is if the user immediately
2451 // pressed "Cancel".
[email protected]beb440c2009-11-06 04:08:542452 OnMessageBoxClosed(reply_msg, false, std::wstring());
[email protected]420ae012009-04-24 05:16:322453 }
2454}
2455
2456void TabContents::RunBeforeUnloadConfirm(const std::wstring& message,
2457 IPC::Message* reply_msg) {
[email protected]7ab1e7d62009-10-14 23:32:012458 is_showing_before_unload_dialog_ = true;
[email protected]d82ed61e2009-06-16 02:46:222459 RunBeforeUnloadDialog(this, message, reply_msg);
[email protected]420ae012009-04-24 05:16:322460}
2461
2462void TabContents::ShowModalHTMLDialog(const GURL& url, int width, int height,
2463 const std::string& json_arguments,
2464 IPC::Message* reply_msg) {
2465 if (delegate()) {
2466 HtmlDialogUIDelegate* dialog_delegate =
2467 new ModalHtmlDialogDelegate(url, width, height, json_arguments,
[email protected]d82ed61e2009-06-16 02:46:222468 reply_msg, this);
[email protected]420ae012009-04-24 05:16:322469 delegate()->ShowHtmlDialog(dialog_delegate, NULL);
2470 }
2471}
2472
2473void TabContents::PasswordFormsSeen(
[email protected]ed3fb032009-06-16 19:50:562474 const std::vector<webkit_glue::PasswordForm>& forms) {
[email protected]420ae012009-04-24 05:16:322475 GetPasswordManager()->PasswordFormsSeen(forms);
2476}
2477
[email protected]420ae012009-04-24 05:16:322478// Checks to see if we should generate a keyword based on the OSDD, and if
2479// necessary uses TemplateURLFetcher to download the OSDD and create a keyword.
2480void TabContents::PageHasOSDD(RenderViewHost* render_view_host,
2481 int32 page_id, const GURL& url,
2482 bool autodetected) {
2483 // Make sure page_id is the current page, and the TemplateURLModel is loaded.
2484 DCHECK(url.is_valid());
2485 if (!IsActiveEntry(page_id))
2486 return;
2487 TemplateURLModel* url_model = profile()->GetTemplateURLModel();
2488 if (!url_model)
2489 return;
2490 if (!url_model->loaded()) {
2491 url_model->Load();
2492 return;
2493 }
2494 if (!profile()->GetTemplateURLFetcher())
2495 return;
2496
2497 if (profile()->IsOffTheRecord())
2498 return;
2499
2500 const NavigationEntry* entry = controller_.GetLastCommittedEntry();
2501 DCHECK(entry);
2502
2503 const NavigationEntry* base_entry = entry;
2504 if (IsFormSubmit(base_entry)) {
2505 // If the current page is a form submit, find the last page that was not
2506 // a form submit and use its url to generate the keyword from.
2507 int index = controller_.last_committed_entry_index() - 1;
2508 while (index >= 0 && IsFormSubmit(controller_.GetEntryAtIndex(index)))
2509 index--;
2510 if (index >= 0)
2511 base_entry = controller_.GetEntryAtIndex(index);
2512 else
2513 base_entry = NULL;
2514 }
2515
2516 // We want to use the user typed URL if available since that represents what
2517 // the user typed to get here, and fall back on the regular URL if not.
2518 if (!base_entry)
2519 return;
2520 GURL keyword_url = base_entry->user_typed_url().is_valid() ?
2521 base_entry->user_typed_url() : base_entry->url();
2522 if (!keyword_url.is_valid())
2523 return;
2524 std::wstring keyword = TemplateURLModel::GenerateKeyword(keyword_url,
2525 autodetected);
[email protected]a88dbdd52010-01-15 19:25:532526
2527 // For JS added OSDD empty keyword is OK because we will generate keyword
2528 // later from OSDD content.
2529 if (autodetected && keyword.empty())
[email protected]420ae012009-04-24 05:16:322530 return;
2531 const TemplateURL* template_url =
2532 url_model->GetTemplateURLForKeyword(keyword);
2533 if (template_url && (!template_url->safe_for_autoreplace() ||
2534 template_url->originating_url() == url)) {
2535 // Either there is a user created TemplateURL for this keyword, or the
2536 // keyword has the same OSDD url and we've parsed it.
2537 return;
2538 }
2539
2540 // Download the OpenSearch description document. If this is successful a
2541 // new keyword will be created when done.
[email protected]420ae012009-04-24 05:16:322542 profile()->GetTemplateURLFetcher()->ScheduleDownload(
2543 keyword,
2544 url,
2545 base_entry->favicon().url(),
[email protected]d0cc9fb52009-06-23 02:26:162546 this,
[email protected]420ae012009-04-24 05:16:322547 autodetected);
2548}
2549
[email protected]420ae012009-04-24 05:16:322550GURL TabContents::GetAlternateErrorPageURL() const {
2551 GURL url;
2552 // Disable alternate error pages when in OffTheRecord/Incognito mode.
2553 if (profile()->IsOffTheRecord())
2554 return url;
2555
2556 PrefService* prefs = profile()->GetPrefs();
2557 DCHECK(prefs);
2558 if (prefs->GetBoolean(prefs::kAlternateErrorPagesEnabled)) {
[email protected]a33457b82009-09-21 23:07:152559 url = google_util::AppendGoogleLocaleParam(
2560 GURL(google_util::kLinkDoctorBaseURL));
[email protected]420ae012009-04-24 05:16:322561 url = google_util::AppendGoogleTLDParam(url);
2562 }
2563 return url;
2564}
2565
2566WebPreferences TabContents::GetWebkitPrefs() {
[email protected]6c32ce72010-03-08 05:18:062567 Profile* profile = render_view_host()->process()->profile();
[email protected]7ccddb8c2009-08-04 17:36:552568 bool is_dom_ui = false;
[email protected]6c32ce72010-03-08 05:18:062569 return RenderViewHostDelegateHelper::GetWebkitPrefs(profile, is_dom_ui);
[email protected]420ae012009-04-24 05:16:322570}
2571
[email protected]fa1cf0b82010-01-15 21:49:442572void TabContents::OnIgnoredUIEvent() {
2573 if (constrained_window_count()) {
2574 ConstrainedWindow* window = *constrained_window_begin();
2575 window->FocusConstrainedWindow();
2576 }
2577}
2578
[email protected]420ae012009-04-24 05:16:322579void TabContents::OnJSOutOfMemory() {
2580 AddInfoBar(new SimpleAlertInfoBarDelegate(
2581 this, l10n_util::GetString(IDS_JS_OUT_OF_MEMORY_PROMPT), NULL));
2582}
2583
[email protected]420ae012009-04-24 05:16:322584void TabContents::OnCrossSiteResponse(int new_render_process_host_id,
2585 int new_request_id) {
[email protected]57c6a652009-05-04 07:58:342586 // Allows the TabContents to react when a cross-site response is ready to be
[email protected]420ae012009-04-24 05:16:322587 // delivered to a pending RenderViewHost. We must first run the onunload
2588 // handler of the old RenderViewHost before we can allow it to proceed.
2589 render_manager_.OnCrossSiteResponse(new_render_process_host_id,
2590 new_request_id);
2591}
2592
2593bool TabContents::CanBlur() const {
2594 return delegate() ? delegate()->CanBlur() : true;
2595}
2596
2597gfx::Rect TabContents::GetRootWindowResizerRect() const {
2598 if (delegate())
2599 return delegate()->GetRootWindowResizerRect();
2600 return gfx::Rect();
2601}
2602
2603void TabContents::RendererUnresponsive(RenderViewHost* rvh,
2604 bool is_during_unload) {
2605 if (is_during_unload) {
2606 // Hang occurred while firing the beforeunload/unload handler.
2607 // Pretend the handler fired so tab closing continues as if it had.
[email protected]829e7612009-04-25 01:15:112608 rvh->set_sudden_termination_allowed(true);
[email protected]420ae012009-04-24 05:16:322609
2610 if (!render_manager_.ShouldCloseTabOnUnresponsiveRenderer())
2611 return;
2612
2613 // If the tab hangs in the beforeunload/unload handler there's really
2614 // nothing we can do to recover. Pretend the unload listeners have
2615 // all fired and close the tab. If the hang is in the beforeunload handler
2616 // then the user will not have the option of cancelling the close.
2617 Close(rvh);
2618 return;
2619 }
2620
2621 if (render_view_host() && render_view_host()->IsRenderViewLive())
[email protected]e7b8d832009-11-20 22:41:002622 hung_renderer_dialog::ShowForTabContents(this);
[email protected]420ae012009-04-24 05:16:322623}
2624
2625void TabContents::RendererResponsive(RenderViewHost* render_view_host) {
[email protected]e7b8d832009-11-20 22:41:002626 hung_renderer_dialog::HideForTabContents(this);
[email protected]420ae012009-04-24 05:16:322627}
2628
2629void TabContents::LoadStateChanged(const GURL& url,
[email protected]094e5b22009-09-25 04:23:562630 net::LoadState load_state,
2631 uint64 upload_position,
2632 uint64 upload_size) {
[email protected]420ae012009-04-24 05:16:322633 load_state_ = load_state;
[email protected]094e5b22009-09-25 04:23:562634 upload_position_ = upload_position;
2635 upload_size_ = upload_size;
[email protected]d686e812009-06-03 19:10:292636 std::wstring languages =
2637 profile()->GetPrefs()->GetString(prefs::kAcceptLanguages);
[email protected]d686e812009-06-03 19:10:292638 std::string host = url.host();
[email protected]ce85f602009-11-07 01:34:532639 load_state_host_ =
2640 net::IDNToUnicode(host.c_str(), host.size(), languages, NULL);
[email protected]420ae012009-04-24 05:16:322641 if (load_state_ == net::LOAD_STATE_READING_RESPONSE)
2642 SetNotWaitingForResponse();
2643 if (is_loading())
[email protected]c9cd2222009-05-06 05:16:502644 NotifyNavigationStateChanged(INVALIDATE_LOAD | INVALIDATE_TAB);
[email protected]420ae012009-04-24 05:16:322645}
2646
[email protected]1ee614862009-07-06 20:29:182647bool TabContents::IsExternalTabContainer() const {
2648 if (!delegate())
2649 return false;
2650
2651 return delegate()->IsExternalTabContainer();
2652}
2653
[email protected]1ee614862009-07-06 20:29:182654void TabContents::DidInsertCSS() {
2655 // This RVHDelegate function is used for extensions and not us.
[email protected]420ae012009-04-24 05:16:322656}
2657
[email protected]130efb02009-09-18 18:54:352658void TabContents::FocusedNodeChanged() {
2659 NotificationService::current()->Notify(
2660 NotificationType::FOCUS_CHANGED_IN_PAGE,
2661 Source<RenderViewHost>(render_view_host()),
2662 NotificationService::NoDetails());
2663}
2664
[email protected]420ae012009-04-24 05:16:322665void TabContents::FileSelected(const FilePath& path,
2666 int index, void* params) {
2667 render_view_host()->FileSelected(path);
2668}
2669
2670void TabContents::MultiFilesSelected(const std::vector<FilePath>& files,
2671 void* params) {
2672 render_view_host()->MultiFilesSelected(files);
2673}
2674
2675void TabContents::FileSelectionCanceled(void* params) {
2676 // If the user cancels choosing a file to upload we pass back an
2677 // empty vector.
2678 render_view_host()->MultiFilesSelected(std::vector<FilePath>());
2679}
2680
2681void TabContents::BeforeUnloadFiredFromRenderManager(
2682 bool proceed,
2683 bool* proceed_to_fire_unload) {
2684 if (delegate())
2685 delegate()->BeforeUnloadFired(this, proceed, proceed_to_fire_unload);
2686}
2687
2688void TabContents::UpdateRenderViewSizeForRenderManager() {
2689 // TODO(brettw) this is a hack. See TabContentsView::SizeContents.
[email protected]0323ee42010-02-17 22:03:262690 gfx::Size size = view_->GetContainerSize();
2691 // 0x0 isn't a valid window size (minimal window size is 1x1) but it may be
2692 // here during container initialization and normal window size will be set
2693 // later. In case of tab duplication this resizing to 0x0 prevents setting
2694 // normal size later so just ignore it.
2695 if (!size.IsEmpty())
2696 view_->SizeContents(size);
[email protected]420ae012009-04-24 05:16:322697}
2698
2699DOMUI* TabContents::CreateDOMUIForRenderManager(const GURL& url) {
[email protected]d82ed61e2009-06-16 02:46:222700 return DOMUIFactory::CreateDOMUIForURL(this, url);
[email protected]420ae012009-04-24 05:16:322701}
2702
2703NavigationEntry*
2704TabContents::GetLastCommittedNavigationEntryForRenderManager() {
2705 return controller_.GetLastCommittedEntry();
2706}
2707
2708bool TabContents::CreateRenderViewForRenderManager(
2709 RenderViewHost* render_view_host) {
[email protected]811bfe32009-07-01 08:46:252710 // If the pending navigation is to a DOMUI, tell the RenderView about any
2711 // bindings it will need enabled.
[email protected]420ae012009-04-24 05:16:322712 if (render_manager_.pending_dom_ui())
[email protected]811bfe32009-07-01 08:46:252713 render_view_host->AllowBindings(
2714 render_manager_.pending_dom_ui()->bindings());
[email protected]1adff062009-06-02 18:39:552715
[email protected]420ae012009-04-24 05:16:322716 RenderWidgetHostView* rwh_view = view_->CreateViewForWidget(render_view_host);
[email protected]1a98a932009-11-17 00:12:522717
2718 scoped_refptr<URLRequestContextGetter> request_context = request_context_;
2719 if (!request_context.get())
2720 request_context = profile()->GetRequestContext();
2721
2722 if (!render_view_host->CreateRenderView(request_context))
[email protected]420ae012009-04-24 05:16:322723 return false;
2724
2725 // Now that the RenderView has been created, we need to tell it its size.
2726 rwh_view->SetSize(view_->GetContainerSize());
2727
2728 UpdateMaxPageIDIfNecessary(render_view_host->site_instance(),
2729 render_view_host);
2730 return true;
2731}
2732
2733void TabContents::Observe(NotificationType type,
2734 const NotificationSource& source,
2735 const NotificationDetails& details) {
2736 switch (type.value) {
2737 case NotificationType::BOOKMARK_MODEL_LOADED:
2738 // BookmarkModel finished loading, fall through to update starred state.
2739 case NotificationType::URLS_STARRED: {
2740 // Somewhere, a URL has been starred.
2741 // Ignore notifications for profiles other than our current one.
2742 Profile* source_profile = Source<Profile>(source).ptr();
2743 if (!source_profile || !source_profile->IsSameProfile(profile()))
2744 return;
2745
2746 UpdateStarredStateForCurrentURL();
2747 break;
2748 }
2749 case NotificationType::PREF_CHANGED: {
2750 std::wstring* pref_name_in = Details<std::wstring>(details).ptr();
2751 DCHECK(Source<PrefService>(source).ptr() == profile()->GetPrefs());
2752 if (*pref_name_in == prefs::kAlternateErrorPagesEnabled) {
2753 UpdateAlternateErrorPageURL();
2754 } else if (*pref_name_in == prefs::kDefaultCharset ||
2755 StartsWithASCII(WideToUTF8(*pref_name_in), "webkit.webprefs.", true)
2756 ) {
2757 UpdateWebPreferences();
2758 } else {
2759 NOTREACHED() << "unexpected pref change notification" << *pref_name_in;
2760 }
2761 break;
2762 }
2763 case NotificationType::RENDER_WIDGET_HOST_DESTROYED:
2764 view_->RenderWidgetHostDestroyed(Source<RenderWidgetHost>(source).ptr());
2765 break;
2766
2767 case NotificationType::NAV_ENTRY_COMMITTED: {
2768 DCHECK(&controller_ == Source<NavigationController>(source).ptr());
2769
2770 NavigationController::LoadCommittedDetails& committed_details =
2771 *(Details<NavigationController::LoadCommittedDetails>(details).ptr());
2772 ExpireInfoBars(committed_details);
2773 break;
2774 }
2775
[email protected]d3ba77272009-09-03 00:06:092776#if defined(OS_LINUX)
2777 case NotificationType::BROWSER_THEME_CHANGED: {
[email protected]7ff431e2010-01-07 18:18:542778 renderer_preferences_util::UpdateFromSystemSettings(
2779 &renderer_preferences_, profile());
[email protected]d3ba77272009-09-03 00:06:092780 render_view_host()->SyncRendererPrefs();
2781 break;
2782 }
2783#endif
2784
[email protected]6c32ce72010-03-08 05:18:062785 case NotificationType::USER_STYLE_SHEET_UPDATED:
2786 UpdateWebPreferences();
2787 break;
2788
[email protected]79580c62010-02-02 02:36:252789 case NotificationType::CONTENT_SETTINGS_CHANGED: {
2790 Details<HostContentSettingsMap::ContentSettingsDetails>
2791 settings_details(details);
[email protected]79580c62010-02-02 02:36:252792 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]da0c8e92010-02-09 23:25:492793 GURL entry_url;
2794 std::string host;
2795 if (entry) {
2796 entry_url = entry->url();
2797 host = entry_url.host();
2798 }
[email protected]79580c62010-02-02 02:36:252799 Source<HostContentSettingsMap> content_settings(source);
2800 if (settings_details.ptr()->host().empty() ||
2801 settings_details.ptr()->host() == host) {
2802 render_view_host()->SendContentSettings(host,
[email protected]da0c8e92010-02-09 23:25:492803 content_settings.ptr()->GetContentSettings(entry_url));
[email protected]79580c62010-02-02 02:36:252804 }
2805 break;
2806 }
2807
[email protected]420ae012009-04-24 05:16:322808 default:
2809 NOTREACHED();
2810 }
2811}
[email protected]aacd7cc2009-09-02 14:51:102812
[email protected]beb440c2009-11-06 04:08:542813std::wstring TabContents::GetMessageBoxTitle(const GURL& frame_url,
2814 bool is_alert) {
2815 if (!frame_url.has_host())
2816 return l10n_util::GetString(
2817 is_alert ? IDS_JAVASCRIPT_ALERT_DEFAULT_TITLE
2818 : IDS_JAVASCRIPT_MESSAGEBOX_DEFAULT_TITLE);
2819
2820 // We really only want the scheme, hostname, and port.
2821 GURL::Replacements replacements;
2822 replacements.ClearUsername();
2823 replacements.ClearPassword();
2824 replacements.ClearPath();
2825 replacements.ClearQuery();
2826 replacements.ClearRef();
2827 GURL clean_url = frame_url.ReplaceComponents(replacements);
2828
2829 // TODO(brettw) it should be easier than this to do the correct language
2830 // handling without getting the accept language from the profile.
2831 std::wstring base_address = gfx::ElideUrl(clean_url, gfx::Font(), 0,
2832 profile()->GetPrefs()->GetString(prefs::kAcceptLanguages));
2833 // Force URL to have LTR directionality.
2834 if (l10n_util::GetTextDirection() == l10n_util::RIGHT_TO_LEFT)
2835 l10n_util::WrapStringWithLTRFormatting(&base_address);
2836
2837 return l10n_util::GetStringF(
2838 is_alert ? IDS_JAVASCRIPT_ALERT_TITLE : IDS_JAVASCRIPT_MESSAGEBOX_TITLE,
2839 base_address);
2840}
2841
2842gfx::NativeWindow TabContents::GetMessageBoxRootWindow() {
2843 return view_->GetTopLevelNativeWindow();
2844}
2845
2846void TabContents::OnMessageBoxClosed(IPC::Message* reply_msg,
2847 bool success,
2848 const std::wstring& prompt) {
2849 last_javascript_message_dismissal_ = base::TimeTicks::Now();
2850 if (is_showing_before_unload_dialog_ && !success) {
2851 // If a beforeunload dialog is canceled, we need to stop the throbber from
2852 // spinning, since we forced it to start spinning in Navigate.
2853 DidStopLoading();
2854
2855 tab_close_start_time_ = base::TimeTicks();
2856 }
2857 is_showing_before_unload_dialog_ = false;
2858 render_view_host()->JavaScriptMessageBoxClosed(reply_msg, success, prompt);
2859}
2860
2861void TabContents::SetSuppressMessageBoxes(bool suppress_message_boxes) {
2862 set_suppress_javascript_messages(suppress_message_boxes);
2863}
2864
[email protected]41fc0322009-09-04 22:23:402865void TabContents::set_encoding(const std::string& encoding) {
[email protected]aacd7cc2009-09-02 14:51:102866 encoding_ = CharacterEncoding::GetCanonicalEncodingNameByAliasName(encoding);
2867}
[email protected]f45d2a72010-03-08 23:28:352868
2869void TabContents::SetAppIcon(const SkBitmap& app_icon) {
2870 app_icon_ = app_icon;
2871 NotifyNavigationStateChanged(INVALIDATE_TITLE);
2872}