blob: 40694941d70f681e9d6715bde1880e9d8657b035 [file] [log] [blame]
[email protected]61d68ef12011-01-13 14:02:561// Copyright (c) 2011 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]0dd3a0ab2011-02-18 08:17:445#include "content/browser/tab_contents/tab_contents.h"
[email protected]39526562011-02-05 03:41:516
[email protected]b75b8292010-10-01 07:28:257#include <cmath>
8
[email protected]8cc8d492010-02-02 10:40:499#include "base/auto_reset.h"
[email protected]835d7c82010-10-14 04:38:3810#include "base/metrics/histogram.h"
[email protected]724159a2010-12-30 01:11:1811#include "base/metrics/stats_counters.h"
[email protected]96d185d2009-04-24 03:28:5412#include "base/string16.h"
[email protected]996fd702009-09-04 19:12:3713#include "base/string_util.h"
[email protected]96d185d2009-04-24 03:28:5414#include "base/time.h"
[email protected]3c733bde2010-12-21 19:56:3115#include "base/utf_string_conversions.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]c65e2f152010-10-14 15:30:4018#include "chrome/browser/blocked_content_container.h"
[email protected]96d185d2009-04-24 03:28:5419#include "chrome/browser/browser_process.h"
[email protected]cf68b092010-01-15 20:05:5520#include "chrome/browser/browser_shutdown.h"
[email protected]aacd7cc2009-09-02 14:51:1021#include "chrome/browser/character_encoding.h"
[email protected]5f24f212010-12-02 14:47:4522#include "chrome/browser/content_settings/content_settings_details.h"
[email protected]9d01a6a2010-11-30 12:03:3323#include "chrome/browser/content_settings/host_content_settings_map.h"
[email protected]96d185d2009-04-24 03:28:5424#include "chrome/browser/debugger/devtools_manager.h"
[email protected]c8f74942010-09-03 10:04:1425#include "chrome/browser/defaults.h"
[email protected]74da5bd2011-01-27 22:03:2726#include "chrome/browser/desktop_notification_handler.h"
[email protected]420ae012009-04-24 05:16:3227#include "chrome/browser/dom_operation_notification_details.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"
[email protected]41291322010-07-15 17:09:0130#include "chrome/browser/download/download_request_limiter.h"
[email protected]eaa7dd182010-12-14 11:09:0031#include "chrome/browser/extensions/extension_service.h"
[email protected]3c733bde2010-12-21 19:56:3132#include "chrome/browser/external_protocol_handler.h"
33#include "chrome/browser/favicon_service.h"
34#include "chrome/browser/file_select_helper.h"
35#include "chrome/browser/google/google_util.h"
[email protected]ef0d04c2010-12-15 23:34:2136#include "chrome/browser/history/history.h"
[email protected]ec0b6c42010-08-26 03:16:5837#include "chrome/browser/history/history_types.h"
[email protected]523623c2010-05-19 16:03:4238#include "chrome/browser/history/top_sites.h"
[email protected]420ae012009-04-24 05:16:3239#include "chrome/browser/load_from_memory_cache_details.h"
40#include "chrome/browser/load_notification_details.h"
[email protected]84abba62009-10-07 17:01:4441#include "chrome/browser/metrics/metric_event_duration_details.h"
[email protected]724159a2010-12-30 01:11:1842#include "chrome/browser/metrics/user_metrics.h"
[email protected]c8865482009-07-23 20:40:1043#include "chrome/browser/omnibox_search_hint.h"
[email protected]c8f73ab2011-01-22 00:05:1744#include "chrome/browser/pdf_unsupported_feature.h"
[email protected]14a000d2010-04-29 21:44:2445#include "chrome/browser/platform_util.h"
[email protected]67c052e2011-01-31 16:33:5846#include "chrome/browser/plugin_observer.h"
[email protected]33f74972010-12-08 16:40:3647#include "chrome/browser/prerender/prerender_manager.h"
[email protected]973407b2011-01-26 23:18:2348#include "chrome/browser/prerender/prerender_plt_recorder.h"
[email protected]eb15e24e2011-02-03 22:34:3449#include "chrome/browser/printing/print_preview_message_handler.h"
[email protected]dbeebd52010-11-16 20:34:1650#include "chrome/browser/printing/print_preview_tab_controller.h"
[email protected]12802702010-07-09 19:43:0951#include "chrome/browser/printing/print_view_manager.h"
[email protected]8ecad5e2010-12-02 21:18:3352#include "chrome/browser/profiles/profile.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]fa685ff2011-02-17 19:47:1355#include "chrome/browser/safe_browsing/client_side_detection_host.h"
[email protected]c37e3b62010-01-22 00:15:1756#include "chrome/browser/sessions/session_types.h"
[email protected]1f18184a2010-07-21 19:34:4957#include "chrome/browser/tab_contents/tab_contents_ssl_helper.h"
[email protected]d54169e92011-01-21 09:19:5258#include "chrome/browser/tab_contents/thumbnail_generator.h"
[email protected]152b8ba32010-03-29 19:40:1659#include "chrome/browser/translate/page_translated_details.h"
[email protected]1caa1522010-12-01 20:23:3560#include "chrome/browser/ui/app_modal_dialogs/message_box_handler.h"
[email protected]7ec6bef2011-02-26 02:22:2961#include "chrome/browser/ui/browser_dialogs.h"
[email protected]54ec6472010-04-09 19:39:5862#include "chrome/common/bindings_policy.h"
[email protected]35f7d212009-04-29 21:19:2763#include "chrome/common/chrome_switches.h"
[email protected]de4761b2010-12-07 21:05:2164#include "chrome/common/content_restriction.h"
[email protected]3b355642010-02-05 16:01:4965#include "chrome/common/extensions/extension.h"
[email protected]5d246db22009-10-27 06:17:5766#include "chrome/common/extensions/extension_action.h"
[email protected]807871f2010-09-16 01:04:4867#include "chrome/common/extensions/extension_icon_set.h"
[email protected]942690b132010-05-11 06:42:1468#include "chrome/common/extensions/extension_resource.h"
[email protected]12802702010-07-09 19:43:0969#include "chrome/common/extensions/url_pattern.h"
70#include "chrome/common/navigation_types.h"
71#include "chrome/common/net/url_request_context_getter.h"
[email protected]bfd04a62009-02-01 18:16:5672#include "chrome/common/notification_service.h"
initial.commit09911bf2008-07-26 23:55:2973#include "chrome/common/pref_names.h"
[email protected]420ae012009-04-24 05:16:3274#include "chrome/common/render_messages.h"
[email protected]939856a2010-08-24 20:29:0275#include "chrome/common/render_messages_params.h"
[email protected]96d185d2009-04-24 03:28:5476#include "chrome/common/url_constants.h"
[email protected]567812d2011-02-24 17:40:5077#include "content/browser/child_process_security_policy.h"
78#include "content/browser/host_zoom_map.h"
79#include "content/browser/in_process_webkit/session_storage_namespace.h"
[email protected]b6021d32011-02-26 01:25:5480#include "content/browser/modal_html_dialog_delegate.h"
[email protected]567812d2011-02-24 17:40:5081#include "content/browser/renderer_host/render_process_host.h"
82#include "content/browser/renderer_host/render_view_host.h"
83#include "content/browser/renderer_host/render_widget_host_view.h"
84#include "content/browser/renderer_host/resource_request_details.h"
85#include "content/browser/site_instance.h"
[email protected]0dd3a0ab2011-02-18 08:17:4486#include "content/browser/tab_contents/infobar_delegate.h"
87#include "content/browser/tab_contents/interstitial_page.h"
88#include "content/browser/tab_contents/navigation_entry.h"
89#include "content/browser/tab_contents/provisional_load_details.h"
90#include "content/browser/tab_contents/tab_contents_delegate.h"
91#include "content/browser/tab_contents/tab_contents_observer.h"
92#include "content/browser/tab_contents/tab_contents_view.h"
[email protected]67fc0392011-02-25 02:56:5793#include "content/browser/webui/web_ui.h"
[email protected]d686e812009-06-03 19:10:2994#include "net/base/net_util.h"
[email protected]420ae012009-04-24 05:16:3295#include "net/base/registry_controlled_domain.h"
[email protected]8bd0fe62011-01-17 06:44:3796#include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
[email protected]42ce29d2011-01-20 23:19:4697#include "ui/base/resource/resource_bundle.h"
[email protected]08397d52011-02-05 01:53:3898#include "ui/gfx/codec/png_codec.h"
[email protected]d5e311b2010-07-20 17:15:0399#include "webkit/glue/password_form.h"
[email protected]3c733bde2010-12-21 19:56:31100#include "webkit/glue/webpreferences.h"
101
102#if defined(OS_MACOSX)
103#include "app/surface/io_surface_support_mac.h"
104#endif // defined(OS_MACOSX)
[email protected]3e45ba92009-02-20 21:09:00105
[email protected]61d68ef12011-01-13 14:02:56106#if defined(OS_CHROMEOS)
107#include "chrome/browser/chromeos/locale_change_guard.h"
108#endif // defined(OS_CHROMEOS)
109
[email protected]420ae012009-04-24 05:16:32110// Cross-Site Navigations
111//
112// If a TabContents is told to navigate to a different web site (as determined
113// by SiteInstance), it will replace its current RenderViewHost with a new
114// RenderViewHost dedicated to the new SiteInstance. This works as follows:
115//
116// - Navigate determines whether the destination is cross-site, and if so,
117// it creates a pending_render_view_host_ and moves into the PENDING
118// RendererState.
119// - The pending RVH is "suspended," so that no navigation messages are sent to
120// its renderer until the onbeforeunload JavaScript handler has a chance to
121// run in the current RVH.
122// - The pending RVH tells CrossSiteRequestManager (a thread-safe singleton)
123// that it has a pending cross-site request. ResourceDispatcherHost will
124// check for this when the response arrives.
125// - The current RVH runs its onbeforeunload handler. If it returns false, we
126// cancel all the pending logic and go back to NORMAL. Otherwise we allow
127// the pending RVH to send the navigation request to its renderer.
128// - ResourceDispatcherHost receives a ResourceRequest on the IO thread. It
129// checks CrossSiteRequestManager to see that the RVH responsible has a
130// pending cross-site request, and then installs a CrossSiteEventHandler.
131// - When RDH receives a response, the BufferedEventHandler determines whether
132// it is a download. If so, it sends a message to the new renderer causing
133// it to cancel the request, and the download proceeds in the download
134// thread. For now, we stay in a PENDING state (with a pending RVH) until
[email protected]57c6a652009-05-04 07:58:34135// the next DidNavigate event for this TabContents. This isn't ideal, but it
[email protected]420ae012009-04-24 05:16:32136// doesn't affect any functionality.
137// - After RDH receives a response and determines that it is safe and not a
138// download, it pauses the response to first run the old page's onunload
139// handler. It does this by asynchronously calling the OnCrossSiteResponse
[email protected]57c6a652009-05-04 07:58:34140// method of TabContents on the UI thread, which sends a ClosePage message
[email protected]420ae012009-04-24 05:16:32141// to the current RVH.
142// - Once the onunload handler is finished, a ClosePage_ACK message is sent to
143// the ResourceDispatcherHost, who unpauses the response. Data is then sent
144// to the pending RVH.
145// - The pending renderer sends a FrameNavigate message that invokes the
146// DidNavigate method. This replaces the current RVH with the
147// pending RVH and goes back to the NORMAL RendererState.
148
149namespace {
150
151// Amount of time we wait between when a key event is received and the renderer
152// is queried for its state and pushed to the NavigationEntry.
153const int kQueryStateDelay = 5000;
154
[email protected]6ebdc9b2010-09-27 16:55:57155const int kSyncWaitDelay = 40;
156
[email protected]420ae012009-04-24 05:16:32157// If another javascript message box is displayed within
158// kJavascriptMessageExpectedDelay of a previous javascript message box being
159// dismissed, display an option to suppress future message boxes from this
160// contents.
161const int kJavascriptMessageExpectedDelay = 1000;
162
[email protected]420ae012009-04-24 05:16:32163// The list of prefs we want to observe.
[email protected]57ecc4b2010-08-11 03:02:51164const char* kPrefsToObserve[] = {
[email protected]420ae012009-04-24 05:16:32165 prefs::kAlternateErrorPagesEnabled,
[email protected]d0b8d092010-10-25 04:05:17166 prefs::kDefaultZoomLevel,
[email protected]420ae012009-04-24 05:16:32167 prefs::kWebKitJavaEnabled,
168 prefs::kWebKitJavascriptEnabled,
169 prefs::kWebKitLoadsImagesAutomatically,
170 prefs::kWebKitPluginsEnabled,
171 prefs::kWebKitUsesUniversalDetector,
[email protected]16480f62011-03-01 23:39:10172 prefs::kWebKitStandardFontFamily,
[email protected]420ae012009-04-24 05:16:32173 prefs::kWebKitSerifFontFamily,
174 prefs::kWebKitSansSerifFontFamily,
175 prefs::kWebKitFixedFontFamily,
176 prefs::kWebKitDefaultFontSize,
177 prefs::kWebKitDefaultFixedFontSize,
[email protected]6bdf05b2010-10-13 20:34:13178 prefs::kWebKitMinimumFontSize,
179 prefs::kWebKitMinimumLogicalFontSize,
[email protected]1661a3c02010-05-20 16:32:36180 prefs::kWebkitTabsToLinks,
[email protected]420ae012009-04-24 05:16:32181 prefs::kDefaultCharset
[email protected]420ae012009-04-24 05:16:32182};
183
184const int kPrefsToObserveLength = arraysize(kPrefsToObserve);
185
[email protected]420ae012009-04-24 05:16:32186#if defined(OS_WIN)
187
188BOOL CALLBACK InvalidateWindow(HWND hwnd, LPARAM lparam) {
189 // Note: erase is required to properly paint some widgets borders. This can
190 // be seen with textfields.
191 InvalidateRect(hwnd, NULL, TRUE);
192 return TRUE;
193}
194#endif
195
[email protected]5e369672009-11-03 23:48:30196ViewMsg_Navigate_Params::NavigationType GetNavigationType(
[email protected]1ccb3568d2010-02-19 10:51:16197 Profile* profile, const NavigationEntry& entry,
198 NavigationController::ReloadType reload_type) {
199 switch (reload_type) {
200 case NavigationController::RELOAD:
201 return ViewMsg_Navigate_Params::RELOAD;
202 case NavigationController::RELOAD_IGNORING_CACHE:
203 return ViewMsg_Navigate_Params::RELOAD_IGNORING_CACHE;
204 case NavigationController::NO_RELOAD:
205 break; // Fall through to rest of function.
206 }
[email protected]5e369672009-11-03 23:48:30207
208 if (entry.restore_type() == NavigationEntry::RESTORE_LAST_SESSION &&
209 profile->DidLastSessionExitCleanly())
210 return ViewMsg_Navigate_Params::RESTORE;
211
212 return ViewMsg_Navigate_Params::NORMAL;
213}
214
[email protected]876bc832010-09-07 16:29:54215void MakeNavigateParams(const NavigationEntry& entry,
216 const NavigationController& controller,
[email protected]1ccb3568d2010-02-19 10:51:16217 NavigationController::ReloadType reload_type,
218 ViewMsg_Navigate_Params* params) {
[email protected]056de2d2009-06-26 16:41:34219 params->page_id = entry.page_id();
[email protected]876bc832010-09-07 16:29:54220 params->pending_history_list_offset = controller.GetIndexOfEntry(&entry);
[email protected]3cc72b12010-03-18 23:03:00221 params->current_history_list_offset = controller.last_committed_entry_index();
222 params->current_history_list_length = controller.entry_count();
[email protected]056de2d2009-06-26 16:41:34223 params->url = entry.url();
224 params->referrer = entry.referrer();
225 params->transition = entry.transition_type();
226 params->state = entry.content_state();
[email protected]3cc72b12010-03-18 23:03:00227 params->navigation_type =
228 GetNavigationType(controller.profile(), entry, reload_type);
[email protected]056de2d2009-06-26 16:41:34229 params->request_time = base::Time::Now();
230}
231
[email protected]420ae012009-04-24 05:16:32232} // namespace
233
[email protected]f4f50ef2011-01-21 19:01:19234
235// TabContents ----------------------------------------------------------------
[email protected]420ae012009-04-24 05:16:32236
[email protected]420ae012009-04-24 05:16:32237TabContents::TabContents(Profile* profile,
238 SiteInstance* site_instance,
239 int routing_id,
[email protected]6ee12c42010-09-14 09:36:07240 const TabContents* base_tab_contents,
241 SessionStorageNamespace* session_storage_namespace)
[email protected]b680ad22009-04-15 23:19:42242 : delegate_(NULL),
[email protected]6ee12c42010-09-14 09:36:07243 ALLOW_THIS_IN_INITIALIZER_LIST(controller_(
244 this, profile, session_storage_namespace)),
[email protected]66ba4932009-06-04 19:22:13245 ALLOW_THIS_IN_INITIALIZER_LIST(view_(
[email protected]d82ed61e2009-06-16 02:46:22246 TabContentsView::Create(this))),
247 ALLOW_THIS_IN_INITIALIZER_LIST(render_manager_(this, this)),
[email protected]6f821dd2010-02-23 00:54:06248 bookmark_drag_(NULL),
[email protected]d5f942ba2008-09-26 19:30:34249 is_loading_(false),
[email protected]443b80e2010-12-14 00:42:23250 crashed_status_(base::TERMINATION_STATUS_STILL_RUNNING),
251 crashed_error_code_(0),
[email protected]d5f942ba2008-09-26 19:30:34252 waiting_for_response_(false),
[email protected]d5f942ba2008-09-26 19:30:34253 max_page_id_(-1),
[email protected]fdd61c62009-04-22 19:22:57254 load_state_(net::LOAD_STATE_IDLE),
[email protected]094e5b22009-09-25 04:23:56255 upload_size_(0),
256 upload_position_(0),
[email protected]fdd61c62009-04-22 19:22:57257 received_page_title_(false),
[email protected]c65e2f152010-10-14 15:30:40258 blocked_contents_(NULL),
259 all_contents_blocked_(false),
[email protected]8cc8d492010-02-02 10:40:49260 dont_notify_render_view_(false),
[email protected]f17a0ee2010-05-17 17:38:47261 displayed_insecure_content_(false),
[email protected]98aa0b52010-05-06 17:03:08262 extension_app_(NULL),
[email protected]fdd61c62009-04-22 19:22:57263 capturing_contents_(false),
264 is_being_destroyed_(false),
265 notify_disconnection_(false),
[email protected]fdd61c62009-04-22 19:22:57266#if defined(OS_WIN)
267 message_box_active_(CreateEvent(NULL, TRUE, FALSE, NULL)),
268#endif
[email protected]c2e74fe82009-09-03 17:57:44269 suppress_javascript_messages_(false),
[email protected]7ab1e7d62009-10-14 23:32:01270 is_showing_before_unload_dialog_(false),
[email protected]80a8fad2011-01-29 04:02:38271 opener_web_ui_type_(WebUIFactory::kNoWebUI),
[email protected]777a7b62010-05-11 13:28:16272 language_state_(&controller_),
[email protected]ebf40a72010-07-22 01:46:38273 closed_by_user_gesture_(false),
[email protected]b75b8292010-10-01 07:28:25274 minimum_zoom_percent_(
275 static_cast<int>(WebKit::WebView::minTextSizeMultiplier * 100)),
276 maximum_zoom_percent_(
277 static_cast<int>(WebKit::WebView::maxTextSizeMultiplier * 100)),
[email protected]9e823662010-10-13 23:36:00278 temporary_zoom_settings_(false),
[email protected]f72298c2011-02-22 19:56:14279 content_restrictions_(0),
280 was_prerendered_(false) {
[email protected]7ff431e2010-01-07 18:18:54281 renderer_preferences_util::UpdateFromSystemSettings(
282 &renderer_preferences_, profile);
283
[email protected]9fb83e82010-07-02 18:24:55284 content_settings_delegate_.reset(
285 new TabSpecificContentSettings(this, profile));
286
[email protected]12636df2009-09-28 22:32:21287 render_manager_.Init(profile, site_instance, routing_id);
[email protected]420ae012009-04-24 05:16:32288
[email protected]34ac70502009-09-25 17:07:23289 // We have the initial size of the view be based on the size of the passed in
290 // tab contents (normally a tab from the same window).
291 view_->CreateView(base_tab_contents ?
292 base_tab_contents->view()->GetContainerSize() : gfx::Size());
[email protected]420ae012009-04-24 05:16:32293
294 // Register for notifications about all interested prefs change.
295 PrefService* prefs = profile->GetPrefs();
[email protected]2fb7dc982010-09-29 12:24:28296 pref_change_registrar_.Init(prefs);
[email protected]420ae012009-04-24 05:16:32297 if (prefs) {
298 for (int i = 0; i < kPrefsToObserveLength; ++i)
[email protected]2fb7dc982010-09-29 12:24:28299 pref_change_registrar_.Add(kPrefsToObserve[i], this);
[email protected]420ae012009-04-24 05:16:32300 }
301
[email protected]420ae012009-04-24 05:16:32302 registrar_.Add(this, NotificationType::RENDER_WIDGET_HOST_DESTROYED,
303 NotificationService::AllSources());
[email protected]d3ba77272009-09-03 00:06:09304#if defined(OS_LINUX)
305 registrar_.Add(this, NotificationType::BROWSER_THEME_CHANGED,
306 NotificationService::AllSources());
307#endif
[email protected]420ae012009-04-24 05:16:32308
[email protected]6c32ce72010-03-08 05:18:06309 registrar_.Add(this, NotificationType::USER_STYLE_SHEET_UPDATED,
310 NotificationService::AllSources());
311
[email protected]79580c62010-02-02 02:36:25312 // Register for notifications about content setting changes.
313 registrar_.Add(this, NotificationType::CONTENT_SETTINGS_CHANGED,
314 NotificationService::AllSources());
315
[email protected]a888b29e62010-04-01 13:38:57316 // Listen for extension changes so we can update extension_for_current_page_.
317 registrar_.Add(this, NotificationType::EXTENSION_LOADED,
318 NotificationService::AllSources());
319 registrar_.Add(this, NotificationType::EXTENSION_UNLOADED,
320 NotificationService::AllSources());
[email protected]a888b29e62010-04-01 13:38:57321
[email protected]88152052010-10-01 04:05:18322 // Listen for Google URL changes
323 registrar_.Add(this, NotificationType::GOOGLE_URL_UPDATED,
324 NotificationService::AllSources());
325
[email protected]c8865482009-07-23 20:40:10326 // Set-up the showing of the omnibox search infobar if applicable.
327 if (OmniboxSearchHint::IsEnabled(profile))
328 omnibox_search_hint_.reset(new OmniboxSearchHint(this));
[email protected]403415a2011-01-10 18:57:53329
[email protected]b5a1d11c2011-02-17 03:09:42330 // Can only add observers after render_manager_.Init() is called, since that's
331 // what sets up the render_view_host which TabContentObserver's constructor
332 // uses to get the routing_id.
333 AddObservers();
[email protected]332af7732009-03-11 13:21:35334}
initial.commit09911bf2008-07-26 23:55:29335
336TabContents::~TabContents() {
[email protected]420ae012009-04-24 05:16:32337 is_being_destroyed_ = true;
338
[email protected]bfe4c1582009-10-02 18:11:09339 // We don't want any notifications while we're running our destructor.
[email protected]420ae012009-04-24 05:16:32340 registrar_.RemoveAll();
[email protected]2fb7dc982010-09-29 12:24:28341 pref_change_registrar_.RemoveAll();
[email protected]420ae012009-04-24 05:16:32342
[email protected]420ae012009-04-24 05:16:32343 NotifyDisconnected();
[email protected]7ec6bef2011-02-26 02:22:29344 browser::HideHungRendererDialog(this);
[email protected]420ae012009-04-24 05:16:32345
[email protected]420ae012009-04-24 05:16:32346 // First cleanly close all child windows.
347 // TODO(mpcomplete): handle case if MaybeCloseChildWindows() already asked
348 // some of these to close. CloseWindows is async, so it might get called
349 // twice before it runs.
[email protected]8cc8d492010-02-02 10:40:49350 CloseConstrainedWindows();
[email protected]420ae012009-04-24 05:16:32351
[email protected]c65e2f152010-10-14 15:30:40352 // Close all blocked contents.
353 if (blocked_contents_)
354 blocked_contents_->Destroy();
[email protected]66ba4932009-06-04 19:22:13355
[email protected]420ae012009-04-24 05:16:32356 // Notify any observer that have a reference on this tab contents.
357 NotificationService::current()->Notify(
358 NotificationType::TAB_CONTENTS_DESTROYED,
359 Source<TabContents>(this),
360 NotificationService::NoDetails());
361
[email protected]1e5e74f2009-05-27 20:55:12362 // Notify any lasting InfobarDelegates that have not yet been removed that
363 // whatever infobar they were handling in this TabContents has closed,
364 // because the TabContents is going away entirely.
365 // This must happen after the TAB_CONTENTS_DESTROYED notification as the
366 // notification may trigger infobars calls that access their delegate. (and
367 // some implementations of InfoBarDelegate do delete themselves on
368 // InfoBarClosed()).
[email protected]ec8f51162011-02-08 04:20:40369 for (size_t i = 0; i < infobar_count(); ++i) {
[email protected]1e5e74f2009-05-27 20:55:12370 InfoBarDelegate* delegate = GetInfoBarDelegateAt(i);
371 delegate->InfoBarClosed();
372 }
373 infobar_delegates_.clear();
374
[email protected]420ae012009-04-24 05:16:32375 // TODO(brettw) this should be moved to the view.
376#if defined(OS_WIN)
377 // If we still have a window handle, destroy it. GetNativeView can return
378 // NULL if this contents was part of a window that closed.
[email protected]50bd6452010-11-27 19:39:42379 if (GetNativeView()) {
380 RenderViewHost* host = render_view_host();
381 if (host && host->view()) {
382 host->view()->WillWmDestroy();
383 }
[email protected]420ae012009-04-24 05:16:32384 ::DestroyWindow(GetNativeView());
[email protected]50bd6452010-11-27 19:39:42385 }
[email protected]420ae012009-04-24 05:16:32386#endif
[email protected]7ab1e7d62009-10-14 23:32:01387
388 // OnCloseStarted isn't called in unit tests.
389 if (!tab_close_start_time_.is_null()) {
390 UMA_HISTOGRAM_TIMES("Tab.Close",
391 base::TimeTicks::Now() - tab_close_start_time_);
392 }
[email protected]b5a1d11c2011-02-17 03:09:42393
394 FOR_EACH_OBSERVER(TabContentsObserver, observers_, set_tab_contents(NULL));
395}
396
397void TabContents::AddObservers() {
398 printing_.reset(new printing::PrintViewManager(this));
399 print_preview_.reset(new printing::PrintPreviewMessageHandler(this));
400 fav_icon_helper_.reset(new FavIconHelper(this));
401 autofill_manager_.reset(new AutoFillManager(this));
402 autocomplete_history_manager_.reset(new AutocompleteHistoryManager(this));
403 prerender_plt_recorder_.reset(new prerender::PrerenderPLTRecorder(this));
404 desktop_notification_handler_.reset(
405 new DesktopNotificationHandlerForTC(this, GetRenderProcessHost()));
406 plugin_observer_.reset(new PluginObserver(this));
[email protected]fa685ff2011-02-17 19:47:13407 safebrowsing_detection_host_.reset(new safe_browsing::ClientSideDetectionHost(
408 this));
initial.commit09911bf2008-07-26 23:55:29409}
410
[email protected]724159a2010-12-30 01:11:18411bool TabContents::OnMessageReceived(const IPC::Message& message) {
[email protected]585b30362011-01-28 02:30:17412 ObserverListBase<TabContentsObserver>::Iterator it(observers_);
413 TabContentsObserver* observer;
[email protected]0f180ee2011-01-21 18:23:10414 while ((observer = it.GetNext()) != NULL)
415 if (observer->OnMessageReceived(message))
[email protected]403415a2011-01-10 18:57:53416 return true;
[email protected]403415a2011-01-10 18:57:53417
[email protected]724159a2010-12-30 01:11:18418 bool handled = true;
419 bool message_is_ok = true;
420 IPC_BEGIN_MESSAGE_MAP_EX(TabContents, message, message_is_ok)
421 IPC_MESSAGE_HANDLER(ViewHostMsg_DidStartProvisionalLoadForFrame,
422 OnDidStartProvisionalLoadForFrame)
423 IPC_MESSAGE_HANDLER(ViewHostMsg_DidRedirectProvisionalLoad,
424 OnDidRedirectProvisionalLoad)
425 IPC_MESSAGE_HANDLER(ViewHostMsg_DidFailProvisionalLoadWithError,
426 OnDidFailProvisionalLoadWithError)
427 IPC_MESSAGE_HANDLER(ViewHostMsg_DidLoadResourceFromMemoryCache,
428 OnDidLoadResourceFromMemoryCache)
429 IPC_MESSAGE_HANDLER(ViewHostMsg_DidDisplayInsecureContent,
430 OnDidDisplayInsecureContent)
431 IPC_MESSAGE_HANDLER(ViewHostMsg_DidRunInsecureContent,
432 OnDidRunInsecureContent)
433 IPC_MESSAGE_HANDLER(ViewHostMsg_DocumentLoadedInFrame,
434 OnDocumentLoadedInFrame)
435 IPC_MESSAGE_HANDLER(ViewHostMsg_DidFinishLoad, OnDidFinishLoad)
[email protected]c8f73ab2011-01-22 00:05:17436 IPC_MESSAGE_HANDLER(ViewHostMsg_UpdateContentRestrictions,
437 OnUpdateContentRestrictions)
438 IPC_MESSAGE_HANDLER(ViewHostMsg_PDFHasUnsupportedFeature,
439 OnPDFHasUnsupportedFeature)
[email protected]7d472472011-01-22 01:30:25440 IPC_MESSAGE_HANDLER(ViewHostMsg_GoToEntryAtOffset, OnGoToEntryAtOffset)
[email protected]7d472472011-01-22 01:30:25441 IPC_MESSAGE_HANDLER(ViewHostMsg_DidGetApplicationInfo,
442 OnDidGetApplicationInfo)
443 IPC_MESSAGE_HANDLER(ViewHostMsg_InstallApplication,
444 OnInstallApplication)
445 IPC_MESSAGE_HANDLER(ViewHostMsg_PageContents, OnPageContents)
446 IPC_MESSAGE_HANDLER(ViewHostMsg_PageTranslated, OnPageTranslated)
447 IPC_MESSAGE_HANDLER(ViewHostMsg_SetSuggestions, OnSetSuggestions)
448 IPC_MESSAGE_HANDLER(ViewHostMsg_InstantSupportDetermined,
449 OnInstantSupportDetermined)
[email protected]34f128d2011-01-25 19:07:44450 IPC_MESSAGE_HANDLER(ViewHostMsg_RunFileChooser, OnRunFileChooser)
[email protected]724159a2010-12-30 01:11:18451 IPC_MESSAGE_UNHANDLED(handled = false)
452 IPC_END_MESSAGE_MAP_EX()
453
454 if (!message_is_ok) {
455 UserMetrics::RecordAction(UserMetricsAction("BadMessageTerminate_RVD"));
456 GetRenderProcessHost()->ReceivedBadMessage();
457 }
458
459 return handled;
460}
461
[email protected]13367f72009-07-08 16:39:02462// Returns true if contains content rendered by an extension.
463bool TabContents::HostsExtension() const {
464 return GetURL().SchemeIs(chrome::kExtensionScheme);
[email protected]d5f942ba2008-09-26 19:30:34465}
466
[email protected]1f18184a2010-07-21 19:34:49467TabContentsSSLHelper* TabContents::GetSSLHelper() {
468 if (ssl_helper_.get() == NULL)
469 ssl_helper_.reset(new TabContentsSSLHelper(this));
470 return ssl_helper_.get();
471}
472
[email protected]8cb5d5b2010-02-09 11:36:16473RenderProcessHost* TabContents::GetRenderProcessHost() const {
474 return render_manager_.current_host()->process();
475}
476
[email protected]9adb9692010-10-29 23:14:02477void TabContents::SetExtensionApp(const Extension* extension) {
[email protected]867a73e12010-03-19 20:45:46478 DCHECK(!extension || extension->GetFullLaunchURL().is_valid());
[email protected]98aa0b52010-05-06 17:03:08479 extension_app_ = extension;
[email protected]fca656c2010-02-10 20:30:10480
[email protected]98aa0b52010-05-06 17:03:08481 UpdateExtensionAppIcon(extension_app_);
[email protected]a888b29e62010-04-01 13:38:57482
[email protected]fca656c2010-02-10 20:30:10483 NotificationService::current()->Notify(
484 NotificationType::TAB_CONTENTS_APPLICATION_EXTENSION_CHANGED,
485 Source<TabContents>(this),
486 NotificationService::NoDetails());
[email protected]3b355642010-02-05 16:01:49487}
488
[email protected]98aa0b52010-05-06 17:03:08489void TabContents::SetExtensionAppById(const std::string& extension_app_id) {
490 if (extension_app_id.empty())
[email protected]911f8152010-03-18 16:46:40491 return;
492
[email protected]eaa7dd182010-12-14 11:09:00493 ExtensionService* extension_service = profile()->GetExtensionService();
[email protected]fa6a9102010-11-22 15:38:50494 if (!extension_service || !extension_service->is_ready())
495 return;
496
497 const Extension* extension =
498 extension_service->GetExtensionById(extension_app_id, false);
499 if (extension)
500 SetExtensionApp(extension);
[email protected]911f8152010-03-18 16:46:40501}
502
[email protected]98aa0b52010-05-06 17:03:08503SkBitmap* TabContents::GetExtensionAppIcon() {
[email protected]98aa0b52010-05-06 17:03:08504 if (extension_app_icon_.empty())
[email protected]8eb043b2010-04-30 07:19:51505 return NULL;
506
[email protected]98aa0b52010-05-06 17:03:08507 return &extension_app_icon_;
[email protected]8eb043b2010-04-30 07:19:51508}
509
[email protected]d5f942ba2008-09-26 19:30:34510const GURL& TabContents::GetURL() const {
511 // We may not have a navigation entry yet
[email protected]ce3fa3c2009-04-20 19:55:57512 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]ebe89e062009-08-13 23:16:54513 return entry ? entry->virtual_url() : GURL::EmptyGURL();
[email protected]d5f942ba2008-09-26 19:30:34514}
515
[email protected]96d185d2009-04-24 03:28:54516const string16& TabContents::GetTitle() const {
[email protected]4c6092c5b2009-06-06 00:23:55517 // Transient entries take precedence. They are used for interstitial pages
518 // that are shown on top of existing pages.
519 NavigationEntry* entry = controller_.GetTransientEntry();
[email protected]45d0ef7f2011-01-05 13:46:23520 if (entry) {
521 return entry->GetTitleForDisplay(profile()->GetPrefs()->
522 GetString(prefs::kAcceptLanguages));
523 }
[email protected]7ade2732011-02-10 00:13:58524 WebUI* our_web_ui = render_manager_.pending_web_ui() ?
525 render_manager_.pending_web_ui() : render_manager_.web_ui();
526 if (our_web_ui) {
[email protected]96d185d2009-04-24 03:28:54527 // Don't override the title in view source mode.
[email protected]4c6092c5b2009-06-06 00:23:55528 entry = controller_.GetActiveEntry();
[email protected]96d185d2009-04-24 03:28:54529 if (!(entry && entry->IsViewSourceMode())) {
[email protected]e0112912011-02-02 22:54:35530 // Give the Web UI the chance to override our title.
[email protected]7ade2732011-02-10 00:13:58531 const string16& title = our_web_ui->overridden_title();
[email protected]96d185d2009-04-24 03:28:54532 if (!title.empty())
533 return title;
534 }
535 }
536
537 // We use the title for the last committed entry rather than a pending
538 // navigation entry. For example, when the user types in a URL, we want to
539 // keep the old page's title until the new load has committed and we get a new
540 // title.
[email protected]96d185d2009-04-24 03:28:54541 entry = controller_.GetLastCommittedEntry();
[email protected]45d0ef7f2011-01-05 13:46:23542 if (entry) {
543 return entry->GetTitleForDisplay(profile()->GetPrefs()->
544 GetString(prefs::kAcceptLanguages));
545 }
[email protected]96d185d2009-04-24 03:28:54546 return EmptyString16();
547}
548
[email protected]d5f942ba2008-09-26 19:30:34549int32 TabContents::GetMaxPageID() {
550 if (GetSiteInstance())
551 return GetSiteInstance()->max_page_id();
552 else
553 return max_page_id_;
554}
555
556void TabContents::UpdateMaxPageID(int32 page_id) {
557 // Ensure both the SiteInstance and RenderProcessHost update their max page
[email protected]57c6a652009-05-04 07:58:34558 // IDs in sync. Only TabContents will also have site instances, except during
[email protected]d5f942ba2008-09-26 19:30:34559 // testing.
560 if (GetSiteInstance())
561 GetSiteInstance()->UpdateMaxPageID(page_id);
[email protected]8cb5d5b2010-02-09 11:36:16562 GetRenderProcessHost()->UpdateMaxPageID(page_id);
[email protected]d5f942ba2008-09-26 19:30:34563}
564
[email protected]96d185d2009-04-24 03:28:54565SiteInstance* TabContents::GetSiteInstance() const {
566 return render_manager_.current_host()->site_instance();
567}
568
[email protected]96d185d2009-04-24 03:28:54569bool TabContents::ShouldDisplayURL() {
[email protected]8ab24cc2009-11-13 20:56:15570 // Don't hide the url in view source mode and with interstitials.
[email protected]96d185d2009-04-24 03:28:54571 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]8ab24cc2009-11-13 20:56:15572 if (entry && (entry->IsViewSourceMode() ||
[email protected]cccd3762010-11-12 18:40:01573 entry->page_type() == INTERSTITIAL_PAGE)) {
[email protected]96d185d2009-04-24 03:28:54574 return true;
[email protected]8ab24cc2009-11-13 20:56:15575 }
[email protected]83e3895b2009-06-11 00:07:16576
[email protected]2b9a06402011-02-16 04:57:10577 // We always display the URL for non-WebUI URLs to prevent spoofing.
578 if (entry && !WebUIFactory::HasWebUIScheme(entry->url()))
579 return true;
580
[email protected]d0980792011-02-13 19:41:40581 WebUI* web_ui = GetWebUIForCurrentState();
[email protected]7ade2732011-02-10 00:13:58582 if (web_ui)
583 return !web_ui->should_hide_url();
[email protected]96d185d2009-04-24 03:28:54584 return true;
585}
586
[email protected]d5f942ba2008-09-26 19:30:34587SkBitmap TabContents::GetFavIcon() const {
588 // Like GetTitle(), we also want to use the favicon for the last committed
589 // entry rather than a pending navigation entry.
[email protected]ce3fa3c2009-04-20 19:55:57590 NavigationEntry* entry = controller_.GetTransientEntry();
[email protected]cbab76d2008-10-13 22:42:47591 if (entry)
592 return entry->favicon().bitmap();
593
[email protected]ce3fa3c2009-04-20 19:55:57594 entry = controller_.GetLastCommittedEntry();
[email protected]d5f942ba2008-09-26 19:30:34595 if (entry)
596 return entry->favicon().bitmap();
[email protected]d5f942ba2008-09-26 19:30:34597 return SkBitmap();
598}
599
[email protected]0b303cc2009-09-28 22:35:15600bool TabContents::FavIconIsValid() const {
601 NavigationEntry* entry = controller_.GetTransientEntry();
602 if (entry)
603 return entry->favicon().is_valid();
604
605 entry = controller_.GetLastCommittedEntry();
606 if (entry)
607 return entry->favicon().is_valid();
608
609 return false;
610}
611
[email protected]96d185d2009-04-24 03:28:54612bool TabContents::ShouldDisplayFavIcon() {
613 // Always display a throbber during pending loads.
614 if (controller_.GetLastCommittedEntry() && controller_.pending_entry())
615 return true;
616
[email protected]d0980792011-02-13 19:41:40617 WebUI* web_ui = GetWebUIForCurrentState();
[email protected]7ade2732011-02-10 00:13:58618 if (web_ui)
619 return !web_ui->hide_favicon();
[email protected]96d185d2009-04-24 03:28:54620 return true;
621}
622
[email protected]585b30362011-01-28 02:30:17623void TabContents::AddObserver(TabContentsObserver* observer) {
624 observers_.AddObserver(observer);
[email protected]3c9e1872010-11-18 16:17:49625}
626
[email protected]585b30362011-01-28 02:30:17627void TabContents::RemoveObserver(TabContentsObserver* observer) {
628 observers_.RemoveObserver(observer);
[email protected]3c9e1872010-11-18 16:17:49629}
630
[email protected]443b80e2010-12-14 00:42:23631void TabContents::SetIsCrashed(base::TerminationStatus status, int error_code) {
632 if (status == crashed_status_)
[email protected]d5f942ba2008-09-26 19:30:34633 return;
634
[email protected]443b80e2010-12-14 00:42:23635 crashed_status_ = status;
636 crashed_error_code_ = error_code;
[email protected]c9cd2222009-05-06 05:16:50637 NotifyNavigationStateChanged(INVALIDATE_TAB);
[email protected]d5f942ba2008-09-26 19:30:34638}
639
[email protected]744ef172009-10-16 21:53:46640void TabContents::PageActionStateChanged() {
641 NotifyNavigationStateChanged(TabContents::INVALIDATE_PAGE_ACTIONS);
[email protected]d7eaf572009-07-01 21:57:00642}
[email protected]ce5c4502009-05-06 16:46:11643
[email protected]d5f942ba2008-09-26 19:30:34644void TabContents::NotifyNavigationStateChanged(unsigned changed_flags) {
645 if (delegate_)
646 delegate_->NavigationStateChanged(this, changed_flags);
647}
648
[email protected]96d185d2009-04-24 03:28:54649void TabContents::DidBecomeSelected() {
650 controller_.SetActive(true);
[email protected]8cb5d5b2010-02-09 11:36:16651 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
652 if (rwhv) {
653 rwhv->DidBecomeSelected();
[email protected]789e9152009-08-04 21:59:43654#if defined(OS_MACOSX)
[email protected]8cb5d5b2010-02-09 11:36:16655 rwhv->SetActive(true);
[email protected]789e9152009-08-04 21:59:43656#endif
657 }
[email protected]96d185d2009-04-24 03:28:54658
[email protected]8cb5d5b2010-02-09 11:36:16659 WebCacheManager::GetInstance()->ObserveActivity(GetRenderProcessHost()->id());
[email protected]5ac20162010-11-24 23:33:11660 last_selected_time_ = base::TimeTicks::Now();
[email protected]61d68ef12011-01-13 14:02:56661#if defined(OS_CHROMEOS)
662 chromeos::LocaleChangeGuard::Check(this);
663#endif
[email protected]96d185d2009-04-24 03:28:54664}
665
[email protected]a94454b2010-12-16 04:16:03666void TabContents::FadeForInstant(bool animate) {
[email protected]1b072562010-12-13 19:37:45667 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
668 SkColor whitish = SkColorSetARGB(192, 255, 255, 255);
669 if (rwhv)
[email protected]a94454b2010-12-16 04:16:03670 rwhv->SetVisuallyDeemphasized(&whitish, animate);
[email protected]1b072562010-12-13 19:37:45671}
672
673void TabContents::CancelInstantFade() {
674 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
675 if (rwhv)
676 rwhv->SetVisuallyDeemphasized(NULL, false);
677}
678
[email protected]96d185d2009-04-24 03:28:54679void TabContents::WasHidden() {
680 if (!capturing_contents()) {
681 // |render_view_host()| can be NULL if the user middle clicks a link to open
682 // a tab in then background, then closes the tab before selecting it. This
683 // is because closing the tab calls TabContents::Destroy(), which removes
684 // the |render_view_host()|; then when we actually destroy the window,
685 // OnWindowPosChanged() notices and calls HideContents() (which calls us).
[email protected]8cb5d5b2010-02-09 11:36:16686 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
687 if (rwhv)
688 rwhv->WasHidden();
[email protected]96d185d2009-04-24 03:28:54689 }
690
691 NotificationService::current()->Notify(
692 NotificationType::TAB_CONTENTS_HIDDEN,
693 Source<TabContents>(this),
694 NotificationService::NoDetails());
695}
696
[email protected]d5f942ba2008-09-26 19:30:34697void TabContents::Activate() {
698 if (delegate_)
699 delegate_->ActivateContents(this);
700}
701
[email protected]ea42e7782010-08-23 23:58:12702void TabContents::Deactivate() {
703 if (delegate_)
704 delegate_->DeactivateContents(this);
705}
706
[email protected]96d185d2009-04-24 03:28:54707void TabContents::ShowContents() {
[email protected]8cb5d5b2010-02-09 11:36:16708 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
709 if (rwhv)
710 rwhv->DidBecomeSelected();
[email protected]96d185d2009-04-24 03:28:54711}
712
713void TabContents::HideContents() {
714 // TODO(pkasting): http://b/1239839 Right now we purposefully don't call
715 // our superclass HideContents(), because some callers want to be very picky
716 // about the order in which these get called. In addition to making the code
717 // here practically impossible to understand, this also means we end up
718 // calling TabContents::WasHidden() twice if callers call both versions of
[email protected]57c6a652009-05-04 07:58:34719 // HideContents() on a TabContents.
[email protected]96d185d2009-04-24 03:28:54720 WasHidden();
721}
722
[email protected]5aab5e22010-12-08 22:13:29723bool TabContents::NeedToFireBeforeUnload() {
724 // TODO(creis): Should we fire even for interstitial pages?
725 return notify_disconnection() &&
726 !showing_interstitial_page() &&
727 !render_view_host()->SuddenTerminationAllowed();
728}
729
[email protected]c0588052008-10-27 23:01:50730void TabContents::OpenURL(const GURL& url, const GURL& referrer,
[email protected]d5f942ba2008-09-26 19:30:34731 WindowOpenDisposition disposition,
732 PageTransition::Type transition) {
733 if (delegate_)
[email protected]c0588052008-10-27 23:01:50734 delegate_->OpenURLFromTab(this, url, referrer, disposition, transition);
[email protected]d5f942ba2008-09-26 19:30:34735}
736
[email protected]1ccb3568d2010-02-19 10:51:16737bool TabContents::NavigateToPendingEntry(
738 NavigationController::ReloadType reload_type) {
[email protected]876bc832010-09-07 16:29:54739 return NavigateToEntry(*controller_.pending_entry(), reload_type);
740}
[email protected]96d185d2009-04-24 03:28:54741
[email protected]876bc832010-09-07 16:29:54742bool TabContents::NavigateToEntry(
743 const NavigationEntry& entry,
744 NavigationController::ReloadType reload_type) {
[email protected]96d185d2009-04-24 03:28:54745 RenderViewHost* dest_render_view_host = render_manager_.Navigate(entry);
746 if (!dest_render_view_host)
747 return false; // Unable to create the desired render view host.
748
[email protected]770dd8b2010-05-24 18:11:39749 if (delegate_ && delegate_->ShouldEnablePreferredSizeNotifications()) {
750 dest_render_view_host->EnablePreferredSizeChangedMode(
751 kPreferredSizeWidth | kPreferredSizeHeightThisIsSlow);
752 }
[email protected]9fb325e2010-05-06 18:23:24753
[email protected]80a8fad2011-01-29 04:02:38754 // For security, we should never send non-Web-UI URLs to a Web UI renderer.
[email protected]05fdd492010-11-15 17:52:07755 // Double check that here.
[email protected]54ec6472010-04-09 19:39:58756 int enabled_bindings = dest_render_view_host->enabled_bindings();
[email protected]c09163a2011-02-15 00:05:55757 bool is_allowed_in_web_ui_renderer =
[email protected]80a8fad2011-01-29 04:02:38758 WebUIFactory::IsURLAcceptableForWebUI(profile(), entry.url());
[email protected]c09163a2011-02-15 00:05:55759 CHECK(!BindingsPolicy::is_web_ui_enabled(enabled_bindings) ||
760 is_allowed_in_web_ui_renderer);
[email protected]54ec6472010-04-09 19:39:58761
[email protected]96d185d2009-04-24 03:28:54762 // Tell DevTools agent that it is attached prior to the navigation.
[email protected]d9f9b792009-06-24 13:17:12763 DevToolsManager* devtools_manager = DevToolsManager::GetInstance();
[email protected]7e8e6b62009-05-08 11:28:32764 if (devtools_manager) { // NULL in unit tests.
[email protected]3cc72b12010-03-18 23:03:00765 devtools_manager->OnNavigatingToPendingEntry(render_view_host(),
766 dest_render_view_host,
767 entry.url());
[email protected]7e8e6b62009-05-08 11:28:32768 }
[email protected]96d185d2009-04-24 03:28:54769
770 // Used for page load time metrics.
771 current_load_start_ = base::TimeTicks::Now();
772
773 // Navigate in the desired RenderViewHost.
[email protected]056de2d2009-06-26 16:41:34774 ViewMsg_Navigate_Params navigate_params;
[email protected]876bc832010-09-07 16:29:54775 MakeNavigateParams(entry, controller_, reload_type, &navigate_params);
[email protected]52c68652010-12-07 17:47:04776 if (delegate_) {
777 navigate_params.extra_headers =
778 delegate_->GetNavigationHeaders(navigate_params.url);
779 }
[email protected]056de2d2009-06-26 16:41:34780 dest_render_view_host->Navigate(navigate_params);
[email protected]96d185d2009-04-24 03:28:54781
782 if (entry.page_id() == -1) {
783 // HACK!! This code suppresses javascript: URLs from being added to
784 // session history, which is what we want to do for javascript: URLs that
785 // do not generate content. What we really need is a message from the
786 // renderer telling us that a new page was not created. The same message
787 // could be used for mailto: URLs and the like.
788 if (entry.url().SchemeIs(chrome::kJavaScriptScheme))
789 return false;
790 }
791
[email protected]3c9e1872010-11-18 16:17:49792 // Notify observers about navigation.
[email protected]585b30362011-01-28 02:30:17793 FOR_EACH_OBSERVER(TabContentsObserver, observers_, NavigateToPendingEntry());
[email protected]96d185d2009-04-24 03:28:54794
[email protected]1ccb3568d2010-02-19 10:51:16795 if (reload_type != NavigationController::NO_RELOAD &&
796 !profile()->IsOffTheRecord()) {
[email protected]0189bc722009-08-28 21:56:48797 FaviconService* favicon_service =
798 profile()->GetFaviconService(Profile::IMPLICIT_ACCESS);
799 if (favicon_service)
800 favicon_service->SetFaviconOutOfDateForPage(entry.url());
[email protected]96d185d2009-04-24 03:28:54801 }
802
[email protected]d5f942ba2008-09-26 19:30:34803 return true;
804}
805
[email protected]96d185d2009-04-24 03:28:54806void TabContents::Stop() {
807 render_manager_.Stop();
[email protected]12802702010-07-09 19:43:09808 printing_->Stop();
[email protected]96d185d2009-04-24 03:28:54809}
810
[email protected]96d185d2009-04-24 03:28:54811void TabContents::DisassociateFromPopupCount() {
812 render_view_host()->DisassociateFromPopupCount();
813}
814
815TabContents* TabContents::Clone() {
816 // We create a new SiteInstance so that the new tab won't share processes
817 // with the old one. This can be changed in the future if we need it to share
818 // processes for some reason.
[email protected]420ae012009-04-24 05:16:32819 TabContents* tc = new TabContents(profile(),
[email protected]96d185d2009-04-24 03:28:54820 SiteInstance::CreateSiteInstance(profile()),
[email protected]6ee12c42010-09-14 09:36:07821 MSG_ROUTING_NONE, this, NULL);
[email protected]96d185d2009-04-24 03:28:54822 tc->controller().CopyStateFrom(controller_);
[email protected]98aa0b52010-05-06 17:03:08823 tc->extension_app_ = extension_app_;
[email protected]98aa0b52010-05-06 17:03:08824 tc->extension_app_icon_ = extension_app_icon_;
[email protected]96d185d2009-04-24 03:28:54825 return tc;
826}
827
[email protected]4d677202009-07-19 07:37:12828void TabContents::ShowPageInfo(const GURL& url,
829 const NavigationEntry::SSLStatus& ssl,
830 bool show_history) {
831 if (!delegate_)
832 return;
833
[email protected]bb678332009-07-21 00:15:50834 delegate_->ShowPageInfo(profile(), url, ssl, show_history);
[email protected]4d677202009-07-19 07:37:12835}
836
[email protected]ef0d04c2010-12-15 23:34:21837void TabContents::SaveFavicon() {
838 NavigationEntry* entry = controller_.GetActiveEntry();
839 if (!entry || entry->url().is_empty())
840 return;
841
842 // Make sure the page is in history, otherwise adding the favicon does
843 // nothing.
844 HistoryService* history = profile()->GetOriginalProfile()->GetHistoryService(
845 Profile::IMPLICIT_ACCESS);
846 if (!history)
847 return;
848 history->AddPageNoVisitForBookmark(entry->url());
849
850 FaviconService* service = profile()->GetOriginalProfile()->GetFaviconService(
851 Profile::IMPLICIT_ACCESS);
852 if (!service)
853 return;
854 const NavigationEntry::FaviconStatus& favicon(entry->favicon());
855 if (!favicon.is_valid() || favicon.url().is_empty() ||
856 favicon.bitmap().empty()) {
857 return;
858 }
859 std::vector<unsigned char> image_data;
860 gfx::PNGCodec::EncodeBGRASkBitmap(favicon.bitmap(), false, &image_data);
861 service->SetFavicon(entry->url(), favicon.url(), image_data);
862}
863
initial.commit09911bf2008-07-26 23:55:29864ConstrainedWindow* TabContents::CreateConstrainedDialog(
[email protected]e8382172009-06-19 22:16:28865 ConstrainedWindowDelegate* delegate) {
initial.commit09911bf2008-07-26 23:55:29866 ConstrainedWindow* window =
[email protected]e8382172009-06-19 22:16:28867 ConstrainedWindow::CreateConstrainedDialog(this, delegate);
[email protected]aed59602011-02-28 22:57:33868 AddConstrainedDialog(window);
869 return window;
870}
871
872void TabContents::AddConstrainedDialog(ConstrainedWindow* window) {
initial.commit09911bf2008-07-26 23:55:29873 child_windows_.push_back(window);
[email protected]fa1cf0b82010-01-15 21:49:44874
875 if (child_windows_.size() == 1) {
876 window->ShowConstrainedWindow();
877 BlockTabContent(true);
878 }
initial.commit09911bf2008-07-26 23:55:29879}
880
[email protected]fa1cf0b82010-01-15 21:49:44881void TabContents::BlockTabContent(bool blocked) {
[email protected]8dccd7cb2010-02-25 05:19:55882 RenderWidgetHostView* rwhv = GetRenderWidgetHostView();
[email protected]1b072562010-12-13 19:37:45883 // 70% opaque grey.
884 SkColor greyish = SkColorSetARGB(178, 0, 0, 0);
[email protected]8dccd7cb2010-02-25 05:19:55885 if (rwhv)
[email protected]1b072562010-12-13 19:37:45886 rwhv->SetVisuallyDeemphasized(blocked ? &greyish : NULL, false);
[email protected]fa1cf0b82010-01-15 21:49:44887 render_view_host()->set_ignore_input_events(blocked);
888 if (delegate_)
889 delegate_->SetTabContentBlocked(this, blocked);
890}
891
initial.commit09911bf2008-07-26 23:55:29892void TabContents::AddNewContents(TabContents* new_contents,
893 WindowOpenDisposition disposition,
894 const gfx::Rect& initial_pos,
[email protected]7e7414ae2010-01-26 20:19:29895 bool user_gesture) {
[email protected]c65e2f152010-10-14 15:30:40896 if (all_contents_blocked_) {
897 if (!blocked_contents_)
898 blocked_contents_ = new BlockedContentContainer(this);
899 blocked_contents_->AddTabContents(
900 new_contents, disposition, initial_pos, user_gesture);
901 return;
902 }
903
initial.commit09911bf2008-07-26 23:55:29904 if (!delegate_)
905 return;
906
[email protected]35f7d212009-04-29 21:19:27907 if ((disposition == NEW_POPUP) && !user_gesture &&
908 !CommandLine::ForCurrentProcess()->HasSwitch(
909 switches::kDisablePopupBlocking)) {
[email protected]2c4410d2009-05-06 23:46:22910 // Unrequested popups from normal pages are constrained unless they're in
[email protected]b6c874582009-05-08 19:38:31911 // the whitelist. The popup owner will handle checking this.
[email protected]7e7414ae2010-01-26 20:19:29912 delegate_->GetConstrainingContents(this)->AddPopup(
913 new_contents, initial_pos);
[email protected]b6c874582009-05-08 19:38:31914 } else {
[email protected]0aa55312008-10-17 21:53:08915 new_contents->DisassociateFromPopupCount();
initial.commit09911bf2008-07-26 23:55:29916 delegate_->AddNewContents(this, new_contents, disposition, initial_pos,
917 user_gesture);
[email protected]7f37dd92009-09-15 16:35:49918 NotificationService::current()->Notify(
919 NotificationType::TAB_ADDED,
920 Source<TabContentsDelegate>(delegate_),
921 Details<TabContents>(this));
initial.commit09911bf2008-07-26 23:55:29922 }
initial.commit09911bf2008-07-26 23:55:29923
[email protected]8cc8d492010-02-02 10:40:49924 // TODO(pkasting): Why is this necessary?
[email protected]c65e2f152010-10-14 15:30:40925 PopupNotificationVisibilityChanged(blocked_contents_ != NULL);
initial.commit09911bf2008-07-26 23:55:29926}
927
[email protected]bcd9dc42010-01-23 16:38:18928bool TabContents::ExecuteCode(int request_id, const std::string& extension_id,
[email protected]20ad2692009-11-20 18:27:20929 bool is_js_code, const std::string& code_string,
930 bool all_frames) {
[email protected]ebd0eae72010-02-16 23:50:51931 RenderViewHost* host = render_view_host();
932 if (!host)
933 return false;
934
935 return host->Send(new ViewMsg_ExecuteCode(host->routing_id(),
[email protected]be7e5cb2010-10-04 12:53:17936 ViewMsg_ExecuteCode_Params(request_id, extension_id,
[email protected]ebd0eae72010-02-16 23:50:51937 is_js_code, code_string, all_frames)));
[email protected]912256b32009-09-18 09:47:35938}
939
[email protected]96d185d2009-04-24 03:28:54940void TabContents::PopupNotificationVisibilityChanged(bool visible) {
[email protected]8cc8d492010-02-02 10:40:49941 if (is_being_destroyed_)
942 return;
[email protected]9fb83e82010-07-02 18:24:55943 content_settings_delegate_->SetPopupsBlocked(visible);
[email protected]8cc8d492010-02-02 10:40:49944 if (!dont_notify_render_view_)
[email protected]e8345242010-05-06 03:00:40945 render_view_host()->AllowScriptToClose(!visible);
[email protected]96d185d2009-04-24 03:28:54946}
947
[email protected]5c9e97a2009-09-09 23:48:30948gfx::NativeView TabContents::GetContentNativeView() const {
[email protected]96d185d2009-04-24 03:28:54949 return view_->GetContentNativeView();
950}
951
952gfx::NativeView TabContents::GetNativeView() const {
953 return view_->GetNativeView();
954}
955
956void TabContents::GetContainerBounds(gfx::Rect *out) const {
957 view_->GetContainerBounds(out);
958}
959
960void TabContents::Focus() {
961 view_->Focus();
962}
963
[email protected]90daadb42009-06-08 21:27:28964void TabContents::FocusThroughTabTraversal(bool reverse) {
[email protected]7e383692009-06-12 19:14:54965 if (showing_interstitial_page()) {
966 render_manager_.interstitial_page()->FocusThroughTabTraversal(reverse);
967 return;
968 }
[email protected]96d185d2009-04-24 03:28:54969 render_view_host()->SetInitialFocus(reverse);
970}
971
972bool TabContents::FocusLocationBarByDefault() {
[email protected]d0980792011-02-13 19:41:40973 WebUI* web_ui = GetWebUIForCurrentState();
[email protected]7ade2732011-02-10 00:13:58974 if (web_ui)
975 return web_ui->focus_location_bar_by_default();
[email protected]3e3f0eb2009-06-22 18:33:43976 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]e0d481582009-09-15 21:06:25977 if (entry && entry->url() == GURL(chrome::kAboutBlankURL))
[email protected]3e3f0eb2009-06-22 18:33:43978 return true;
[email protected]96d185d2009-04-24 03:28:54979 return false;
980}
981
[email protected]a26dc362010-04-23 01:48:58982void TabContents::SetFocusToLocationBar(bool select_all) {
[email protected]a11aaf22010-03-30 00:03:38983 if (delegate())
[email protected]a26dc362010-04-23 01:48:58984 delegate()->SetFocusToLocationBar(select_all);
[email protected]a11aaf22010-03-30 00:03:38985}
986
[email protected]616ed5a2008-11-21 22:27:24987void TabContents::AddInfoBar(InfoBarDelegate* delegate) {
[email protected]f7019302010-03-26 19:58:32988 if (delegate_ && !delegate_->infobars_enabled()) {
989 delegate->InfoBarClosed();
990 return;
991 }
992
[email protected]616ed5a2008-11-21 22:27:24993 // Look through the existing InfoBarDelegates we have for a match. If we've
994 // already got one that matches, then we don't add the new one.
[email protected]ec8f51162011-02-08 04:20:40995 for (size_t i = 0; i < infobar_count(); ++i) {
[email protected]6d27a7b2009-12-18 23:25:45996 if (GetInfoBarDelegateAt(i)->EqualsDelegate(delegate)) {
997 // Tell the new infobar to close so that it can clean itself up.
998 delegate->InfoBarClosed();
[email protected]616ed5a2008-11-21 22:27:24999 return;
[email protected]6d27a7b2009-12-18 23:25:451000 }
[email protected]616ed5a2008-11-21 22:27:241001 }
1002
1003 infobar_delegates_.push_back(delegate);
[email protected]bfd04a62009-02-01 18:16:561004 NotificationService::current()->Notify(
[email protected]f4f50ef2011-01-21 19:01:191005 NotificationType::TAB_CONTENTS_INFOBAR_ADDED, Source<TabContents>(this),
[email protected]bfd04a62009-02-01 18:16:561006 Details<InfoBarDelegate>(delegate));
[email protected]616ed5a2008-11-21 22:27:241007
1008 // Add ourselves as an observer for navigations the first time a delegate is
1009 // added. We use this notification to expire InfoBars that need to expire on
1010 // page transitions.
1011 if (infobar_delegates_.size() == 1) {
[email protected]bfd04a62009-02-01 18:16:561012 registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED,
[email protected]ce3fa3c2009-04-20 19:55:571013 Source<NavigationController>(&controller_));
[email protected]616ed5a2008-11-21 22:27:241014 }
1015}
1016
1017void TabContents::RemoveInfoBar(InfoBarDelegate* delegate) {
[email protected]f7019302010-03-26 19:58:321018 if (delegate_ && !delegate_->infobars_enabled()) {
1019 return;
1020 }
1021
[email protected]616ed5a2008-11-21 22:27:241022 std::vector<InfoBarDelegate*>::iterator it =
1023 find(infobar_delegates_.begin(), infobar_delegates_.end(), delegate);
1024 if (it != infobar_delegates_.end()) {
1025 InfoBarDelegate* delegate = *it;
[email protected]bfd04a62009-02-01 18:16:561026 NotificationService::current()->Notify(
1027 NotificationType::TAB_CONTENTS_INFOBAR_REMOVED,
1028 Source<TabContents>(this),
1029 Details<InfoBarDelegate>(delegate));
[email protected]616ed5a2008-11-21 22:27:241030
[email protected]f4f50ef2011-01-21 19:01:191031 infobar_delegates_.erase(it);
1032 // Remove ourselves as an observer if we are tracking no more InfoBars.
1033 if (infobar_delegates_.empty()) {
1034 registrar_.Remove(this, NotificationType::NAV_ENTRY_COMMITTED,
1035 Source<NavigationController>(&controller_));
[email protected]6a02963e2009-01-06 16:58:031036 }
[email protected]616ed5a2008-11-21 22:27:241037 }
1038}
[email protected]3edd9522009-03-04 22:19:411039
[email protected]f81821c2009-08-07 01:29:471040void TabContents::ReplaceInfoBar(InfoBarDelegate* old_delegate,
1041 InfoBarDelegate* new_delegate) {
[email protected]f7019302010-03-26 19:58:321042 if (delegate_ && !delegate_->infobars_enabled()) {
1043 new_delegate->InfoBarClosed();
1044 return;
1045 }
1046
[email protected]f81821c2009-08-07 01:29:471047 std::vector<InfoBarDelegate*>::iterator it =
1048 find(infobar_delegates_.begin(), infobar_delegates_.end(), old_delegate);
1049 DCHECK(it != infobar_delegates_.end());
1050
1051 // Notify the container about the change of plans.
1052 scoped_ptr<std::pair<InfoBarDelegate*, InfoBarDelegate*> > details(
1053 new std::pair<InfoBarDelegate*, InfoBarDelegate*>(
1054 old_delegate, new_delegate));
1055 NotificationService::current()->Notify(
1056 NotificationType::TAB_CONTENTS_INFOBAR_REPLACED,
1057 Source<TabContents>(this),
1058 Details<std::pair<InfoBarDelegate*, InfoBarDelegate*> >(details.get()));
1059
[email protected]f4f50ef2011-01-21 19:01:191060 // Remove the old one.
1061 infobar_delegates_.erase(it);
[email protected]f81821c2009-08-07 01:29:471062
1063 // Add the new one.
[email protected]cdadf6f2010-08-20 16:36:081064 DCHECK(find(infobar_delegates_.begin(),
1065 infobar_delegates_.end(), new_delegate) ==
1066 infobar_delegates_.end());
[email protected]f81821c2009-08-07 01:29:471067 infobar_delegates_.push_back(new_delegate);
1068}
1069
[email protected]20ede072009-10-06 00:28:351070bool TabContents::ShouldShowBookmarkBar() {
[email protected]aa9196f2009-10-22 16:20:271071 if (showing_interstitial_page())
1072 return false;
1073
[email protected]c8f74942010-09-03 10:04:141074 // Do not show bookmarks bar if bookmarks aren't enabled.
1075 if (!browser_defaults::bookmarks_enabled)
1076 return false;
1077
[email protected]d0980792011-02-13 19:41:401078 // See GetWebUIForCurrentState() comment for more info. This case is very
[email protected]96d185d2009-04-24 03:28:541079 // similar, but for non-first loads, we want to use the committed entry. This
1080 // is so the bookmarks bar disappears at the same time the page does.
1081 if (controller_.GetLastCommittedEntry()) {
[email protected]e0112912011-02-02 22:54:351082 // Not the first load, always use the committed Web UI.
[email protected]7ade2732011-02-10 00:13:581083 return (render_manager_.web_ui() == NULL) ?
1084 false : render_manager_.web_ui()->force_bookmark_bar_visible();
[email protected]96d185d2009-04-24 03:28:541085 }
1086
1087 // When it's the first load, we know either the pending one or the committed
[email protected]d0980792011-02-13 19:41:401088 // one will have the Web UI in it (see GetWebUIForCurrentState), and only one
[email protected]96d185d2009-04-24 03:28:541089 // of them will be valid, so we can just check both.
[email protected]7ade2732011-02-10 00:13:581090 if (render_manager_.pending_web_ui())
1091 return render_manager_.pending_web_ui()->force_bookmark_bar_visible();
1092 return (render_manager_.web_ui() == NULL) ?
1093 false : render_manager_.web_ui()->force_bookmark_bar_visible();
[email protected]96d185d2009-04-24 03:28:541094}
1095
[email protected]3edd9522009-03-04 22:19:411096void TabContents::ToolbarSizeChanged(bool is_animating) {
1097 TabContentsDelegate* d = delegate();
1098 if (d)
1099 d->ToolbarSizeChanged(this, is_animating);
1100}
[email protected]616ed5a2008-11-21 22:27:241101
[email protected]fc6fb7fb2009-11-07 02:35:041102bool TabContents::CanDownload(int request_id) {
1103 TabContentsDelegate* d = delegate();
1104 if (d)
1105 return d->CanDownload(request_id);
1106 return true;
1107}
1108
[email protected]d5f942ba2008-09-26 19:30:341109void TabContents::OnStartDownload(DownloadItem* download) {
1110 DCHECK(download);
[email protected]d5f942ba2008-09-26 19:30:341111
1112 // Download in a constrained popup is shown in the tab that opened it.
[email protected]b6c874582009-05-08 19:38:311113 TabContents* tab_contents = delegate()->GetConstrainingContents(this);
[email protected]d5f942ba2008-09-26 19:30:341114
[email protected]5b928b62010-08-03 11:46:511115 if (tab_contents && tab_contents->delegate())
1116 tab_contents->delegate()->OnStartDownload(download, this);
[email protected]3edd9522009-03-04 22:19:411117}
[email protected]3edd9522009-03-04 22:19:411118
[email protected]d5f942ba2008-09-26 19:30:341119void TabContents::WillClose(ConstrainedWindow* window) {
[email protected]d6219912010-11-17 19:47:371120 ConstrainedWindowList::iterator i(
1121 std::find(child_windows_.begin(), child_windows_.end(), window));
1122 bool removed_topmost_window = i == child_windows_.begin();
1123 if (i != child_windows_.end())
1124 child_windows_.erase(i);
1125 if (child_windows_.empty()) {
[email protected]fa1cf0b82010-01-15 21:49:441126 BlockTabContent(false);
[email protected]d6219912010-11-17 19:47:371127 } else {
1128 if (removed_topmost_window)
1129 child_windows_[0]->ShowConstrainedWindow();
1130 BlockTabContent(true);
[email protected]fa1cf0b82010-01-15 21:49:441131 }
[email protected]66ba4932009-06-04 19:22:131132}
[email protected]d5f942ba2008-09-26 19:30:341133
[email protected]c65e2f152010-10-14 15:30:401134void TabContents::WillCloseBlockedContentContainer(
1135 BlockedContentContainer* container) {
1136 DCHECK(blocked_contents_ == container);
1137 blocked_contents_ = NULL;
[email protected]8cc8d492010-02-02 10:40:491138 PopupNotificationVisibilityChanged(false);
[email protected]d5f942ba2008-09-26 19:30:341139}
1140
[email protected]d5f942ba2008-09-26 19:30:341141void TabContents::DidMoveOrResize(ConstrainedWindow* window) {
[email protected]332af7732009-03-11 13:21:351142#if defined(OS_WIN)
[email protected]92edc472009-02-10 20:32:061143 UpdateWindow(GetNativeView());
[email protected]332af7732009-03-11 13:21:351144#endif
[email protected]d5f942ba2008-09-26 19:30:341145}
1146
[email protected]420ae012009-04-24 05:16:321147void TabContents::OnSavePage() {
1148 // If we can not save the page, try to download it.
1149 if (!SavePackage::IsSavableContents(contents_mime_type())) {
1150 DownloadManager* dlm = profile()->GetDownloadManager();
1151 const GURL& current_page_url = GetURL();
1152 if (dlm && current_page_url.is_valid())
[email protected]d82ed61e2009-06-16 02:46:221153 dlm->DownloadUrl(current_page_url, GURL(), "", this);
[email protected]420ae012009-04-24 05:16:321154 return;
1155 }
1156
1157 Stop();
1158
1159 // Create the save package and possibly prompt the user for the name to save
1160 // the page as. The user prompt is an asynchronous operation that runs on
1161 // another thread.
[email protected]b5a1d11c2011-02-17 03:09:421162 save_package_ = new SavePackage(this);
[email protected]420ae012009-04-24 05:16:321163 save_package_->GetSaveInfo();
1164}
1165
1166// Used in automated testing to bypass prompting the user for file names.
1167// Instead, the names and paths are hard coded rather than running them through
1168// file name sanitation and extension / mime checking.
[email protected]828cabe2009-09-26 22:47:111169bool TabContents::SavePage(const FilePath& main_file, const FilePath& dir_path,
[email protected]420ae012009-04-24 05:16:321170 SavePackage::SavePackageType save_type) {
1171 // Stop the page from navigating.
1172 Stop();
1173
[email protected]b5a1d11c2011-02-17 03:09:421174 save_package_ = new SavePackage(this, save_type, main_file, dir_path);
[email protected]cdf6cc82009-08-04 21:48:591175 return save_package_->Init();
[email protected]420ae012009-04-24 05:16:321176}
1177
[email protected]89d6e6e2009-12-03 23:06:511178void TabContents::EmailPageLocation() {
1179 std::string title = EscapeQueryParamValue(UTF16ToUTF8(GetTitle()), false);
1180 std::string page_url = EscapeQueryParamValue(GetURL().spec(), false);
1181 std::string mailto = std::string("mailto:?subject=Fwd:%20") +
1182 title + "&body=%0A%0A" + page_url;
1183 platform_util::OpenExternal(GURL(mailto));
1184}
1185
[email protected]420ae012009-04-24 05:16:321186void TabContents::PrintPreview() {
[email protected]dbeebd52010-11-16 20:34:161187 if (CommandLine::ForCurrentProcess()->HasSwitch(
1188 switches::kEnablePrintPreview)) {
1189 if (showing_interstitial_page())
1190 return;
[email protected]83a58a22011-02-05 03:44:481191
1192 printing::PrintPreviewTabController* tab_controller =
1193 printing::PrintPreviewTabController::GetInstance();
1194 if (!tab_controller)
1195 return;
1196 tab_controller->GetOrCreatePreviewTab(this, controller().window_id().id());
1197
[email protected]dbeebd52010-11-16 20:34:161198 render_view_host()->PrintPreview();
1199 } else {
1200 PrintNow();
1201 }
[email protected]420ae012009-04-24 05:16:321202}
1203
1204bool TabContents::PrintNow() {
1205 // We can't print interstitial page for now.
1206 if (showing_interstitial_page())
1207 return false;
1208
1209 return render_view_host()->PrintPages();
1210}
1211
[email protected]82270452009-06-19 15:58:011212void TabContents::PrintingDone(int document_cookie, bool success) {
1213 render_view_host()->PrintingDone(document_cookie, success);
1214}
1215
[email protected]420ae012009-04-24 05:16:321216bool TabContents::IsActiveEntry(int32 page_id) {
1217 NavigationEntry* active_entry = controller_.GetActiveEntry();
1218 return (active_entry != NULL &&
1219 active_entry->site_instance() == GetSiteInstance() &&
1220 active_entry->page_id() == page_id);
1221}
1222
[email protected]b2fe07d12010-02-09 14:38:081223void TabContents::SetOverrideEncoding(const std::string& encoding) {
[email protected]8cb5d5b2010-02-09 11:36:161224 set_encoding(encoding);
1225 render_view_host()->SetPageEncoding(encoding);
1226}
1227
[email protected]b2fe07d12010-02-09 14:38:081228void TabContents::ResetOverrideEncoding() {
[email protected]8cb5d5b2010-02-09 11:36:161229 reset_encoding();
1230 render_view_host()->ResetPageEncodingToDefault();
1231}
1232
1233void TabContents::WindowMoveOrResizeStarted() {
1234 render_view_host()->WindowMoveOrResizeStarted();
1235}
1236
[email protected]c65e2f152010-10-14 15:30:401237void TabContents::SetAllContentsBlocked(bool value) {
1238 if (all_contents_blocked_ == value)
1239 return;
1240
1241 all_contents_blocked_ = value;
1242 if (!all_contents_blocked_ && blocked_contents_) {
1243 std::vector<TabContents*> blocked;
1244 blocked_contents_->GetBlockedContents(&blocked);
1245 for (size_t i = 0; i < blocked.size(); ++i)
1246 blocked_contents_->LaunchForContents(blocked[i]);
1247 }
1248}
1249
[email protected]3f8eb7f2009-10-02 23:58:051250void TabContents::LogNewTabTime(const std::string& event_name) {
1251 // Not all new tab pages get timed. In those cases, we don't have a
1252 // new_tab_start_time_.
1253 if (new_tab_start_time_.is_null())
1254 return;
1255
1256 base::TimeDelta duration = base::TimeTicks::Now() - new_tab_start_time_;
[email protected]84abba62009-10-07 17:01:441257 MetricEventDurationDetails details(event_name,
1258 static_cast<int>(duration.InMilliseconds()));
1259
[email protected]7ab1e7d62009-10-14 23:32:011260 if (event_name == "Tab.NewTabScriptStart") {
1261 UMA_HISTOGRAM_TIMES("Tab.NewTabScriptStart", duration);
1262 } else if (event_name == "Tab.NewTabDOMContentLoaded") {
1263 UMA_HISTOGRAM_TIMES("Tab.NewTabDOMContentLoaded", duration);
1264 } else if (event_name == "Tab.NewTabOnload") {
1265 UMA_HISTOGRAM_TIMES("Tab.NewTabOnload", duration);
[email protected]3f8eb7f2009-10-02 23:58:051266 // The new tab page has finished loading; reset it.
1267 new_tab_start_time_ = base::TimeTicks();
[email protected]84abba62009-10-07 17:01:441268 } else {
1269 NOTREACHED();
[email protected]3f8eb7f2009-10-02 23:58:051270 }
[email protected]84abba62009-10-07 17:01:441271 NotificationService::current()->Notify(
1272 NotificationType::METRIC_EVENT_DURATION,
1273 Source<TabContents>(this),
1274 Details<MetricEventDurationDetails>(&details));
[email protected]3f8eb7f2009-10-02 23:58:051275}
1276
[email protected]7ab1e7d62009-10-14 23:32:011277void TabContents::OnCloseStarted() {
1278 if (tab_close_start_time_.is_null())
1279 tab_close_start_time_ = base::TimeTicks::Now();
1280}
1281
[email protected]46624bf2010-06-09 16:04:191282bool TabContents::ShouldAcceptDragAndDrop() const {
1283#if defined(OS_CHROMEOS)
1284 // ChromeOS panels (pop-ups) do not take drag-n-drop.
1285 // See http://crosbug.com/2413
[email protected]b83af492010-10-09 03:21:201286 if (delegate() && delegate()->IsPopup(this))
1287 return false;
1288 return true;
[email protected]46624bf2010-06-09 16:04:191289#else
1290 return true;
1291#endif
1292}
1293
[email protected]7813bd72011-02-05 02:19:341294void TabContents::SystemDragEnded() {
1295 if (render_view_host())
1296 render_view_host()->DragSourceSystemDragEnded();
1297 if (delegate())
1298 delegate()->DragEnded();
1299}
1300
[email protected]ec0b6c42010-08-26 03:16:581301void TabContents::UpdateHistoryForNavigation(
1302 scoped_refptr<history::HistoryAddPageArgs> add_page_args) {
1303 if (profile()->IsOffTheRecord())
1304 return;
1305
1306 // Add to history service.
1307 HistoryService* hs = profile()->GetHistoryService(Profile::IMPLICIT_ACCESS);
1308 if (hs)
1309 hs->AddPage(*add_page_args);
1310}
1311
1312void TabContents::UpdateHistoryPageTitle(const NavigationEntry& entry) {
1313 if (profile()->IsOffTheRecord())
1314 return;
1315
1316 HistoryService* hs = profile()->GetHistoryService(Profile::IMPLICIT_ACCESS);
1317 if (hs)
1318 hs->SetPageTitle(entry.virtual_url(), entry.title());
1319}
1320
[email protected]d0b8d092010-10-25 04:05:171321double TabContents::GetZoomLevel() const {
[email protected]b75b8292010-10-01 07:28:251322 HostZoomMap* zoom_map = profile()->GetHostZoomMap();
1323 if (!zoom_map)
[email protected]d0b8d092010-10-25 04:05:171324 return 0;
[email protected]b75b8292010-10-01 07:28:251325
1326 double zoom_level;
1327 if (temporary_zoom_settings_) {
1328 zoom_level = zoom_map->GetTemporaryZoomLevel(
1329 GetRenderProcessHost()->id(), render_view_host()->routing_id());
1330 } else {
1331 zoom_level = zoom_map->GetZoomLevel(GetURL());
1332 }
[email protected]d0b8d092010-10-25 04:05:171333 return zoom_level;
1334}
[email protected]b75b8292010-10-01 07:28:251335
[email protected]d0b8d092010-10-25 04:05:171336int TabContents::GetZoomPercent(bool* enable_increment,
1337 bool* enable_decrement) {
1338 *enable_decrement = *enable_increment = false;
[email protected]b75b8292010-10-01 07:28:251339 int percent = static_cast<int>(
[email protected]d0b8d092010-10-25 04:05:171340 WebKit::WebView::zoomLevelToZoomFactor(GetZoomLevel()) * 100);
[email protected]b75b8292010-10-01 07:28:251341 *enable_decrement = percent > minimum_zoom_percent_;
1342 *enable_increment = percent < maximum_zoom_percent_;
1343 return percent;
1344}
1345
[email protected]3c733bde2010-12-21 19:56:311346void TabContents::ViewSource() {
[email protected]1788e772010-12-15 16:40:501347 if (!delegate_)
1348 return;
1349
1350 NavigationEntry* active_entry = controller().GetActiveEntry();
1351 if (!active_entry)
1352 return;
1353
1354 delegate_->ViewSourceForTab(this, active_entry->url());
[email protected]77d8d622010-12-15 10:30:121355}
1356
[email protected]724159a2010-12-30 01:11:181357void TabContents::OnDidStartProvisionalLoadForFrame(int64 frame_id,
1358 bool is_main_frame,
1359 const GURL& url) {
1360 bool is_error_page = (url.spec() == chrome::kUnreachableWebDataURL);
1361 GURL validated_url(url);
1362 render_view_host()->FilterURL(ChildProcessSecurityPolicy::GetInstance(),
1363 GetRenderProcessHost()->id(), &validated_url);
1364
1365 ProvisionalLoadDetails details(
1366 is_main_frame,
1367 controller_.IsURLInPageNavigation(validated_url),
1368 validated_url, std::string(), false, is_error_page, frame_id);
1369 NotificationService::current()->Notify(
1370 NotificationType::FRAME_PROVISIONAL_LOAD_START,
1371 Source<NavigationController>(&controller_),
1372 Details<ProvisionalLoadDetails>(&details));
1373 if (is_main_frame) {
1374 // If we're displaying a network error page do not reset the content
1375 // settings delegate's cookies so the user has a chance to modify cookie
1376 // settings.
1377 if (!is_error_page)
1378 content_settings_delegate_->ClearCookieSpecificContentSettings();
1379 content_settings_delegate_->ClearGeolocationContentSettings();
[email protected]f72298c2011-02-22 19:56:141380 // TODO(cbentzel): Should error pages still show prerendered icon?
1381 set_was_prerendered(false);
[email protected]e7d50892011-01-19 21:47:381382
1383 // Check if the URL we are about to load has been prerendered by any chance,
1384 // and use it if possible.
1385 MaybeUsePreloadedPage(url);
[email protected]724159a2010-12-30 01:11:181386 }
1387}
1388
1389void TabContents::OnDidRedirectProvisionalLoad(int32 page_id,
1390 const GURL& source_url,
1391 const GURL& target_url) {
1392 NavigationEntry* entry;
1393 if (page_id == -1)
1394 entry = controller_.pending_entry();
1395 else
1396 entry = controller_.GetEntryWithPageID(GetSiteInstance(), page_id);
1397 if (!entry || entry->url() != source_url)
1398 return;
1399 entry->set_url(target_url);
[email protected]e7d50892011-01-19 21:47:381400
1401 // Check if the URL we are about to load has been prerendered by any chance,
1402 // and use it if possible.
1403 MaybeUsePreloadedPage(target_url);
[email protected]724159a2010-12-30 01:11:181404}
1405
1406void TabContents::OnDidFailProvisionalLoadWithError(
1407 int64 frame_id,
1408 bool is_main_frame,
1409 int error_code,
1410 const GURL& url,
1411 bool showing_repost_interstitial) {
1412 VLOG(1) << "Failed Provisional Load: " << url.possibly_invalid_spec()
1413 << ", error_code: " << error_code
1414 << " is_main_frame: " << is_main_frame
1415 << " showing_repost_interstitial: " << showing_repost_interstitial
1416 << " frame_id: " << frame_id;
1417 GURL validated_url(url);
1418 render_view_host()->FilterURL(ChildProcessSecurityPolicy::GetInstance(),
1419 GetRenderProcessHost()->id(), &validated_url);
1420
1421 if (net::ERR_ABORTED == error_code) {
1422 // EVIL HACK ALERT! Ignore failed loads when we're showing interstitials.
1423 // This means that the interstitial won't be torn down properly, which is
1424 // bad. But if we have an interstitial, go back to another tab type, and
1425 // then load the same interstitial again, we could end up getting the first
1426 // interstitial's "failed" message (as a result of the cancel) when we're on
1427 // the second one.
1428 //
1429 // We can't tell this apart, so we think we're tearing down the current page
1430 // which will cause a crash later one. There is also some code in
1431 // RenderViewHostManager::RendererAbortedProvisionalLoad that is commented
1432 // out because of this problem.
1433 //
1434 // http://code.google.com/p/chromium/issues/detail?id=2855
1435 // Because this will not tear down the interstitial properly, if "back" is
1436 // back to another tab type, the interstitial will still be somewhat alive
1437 // in the previous tab type. If you navigate somewhere that activates the
1438 // tab with the interstitial again, you'll see a flash before the new load
1439 // commits of the interstitial page.
1440 if (showing_interstitial_page()) {
1441 LOG(WARNING) << "Discarding message during interstitial.";
1442 return;
1443 }
1444
1445 // This will discard our pending entry if we cancelled the load (e.g., if we
1446 // decided to download the file instead of load it). Only discard the
1447 // pending entry if the URLs match, otherwise the user initiated a navigate
1448 // before the page loaded so that the discard would discard the wrong entry.
1449 NavigationEntry* pending_entry = controller_.pending_entry();
1450 if (pending_entry && pending_entry->url() == validated_url) {
1451 controller_.DiscardNonCommittedEntries();
1452 // Update the URL display.
1453 NotifyNavigationStateChanged(TabContents::INVALIDATE_URL);
1454 }
1455
1456 render_manager_.RendererAbortedProvisionalLoad(render_view_host());
1457 }
1458
1459 // Send out a notification that we failed a provisional load with an error.
1460 ProvisionalLoadDetails details(
1461 is_main_frame, controller_.IsURLInPageNavigation(validated_url),
1462 validated_url, std::string(), false, false, frame_id);
1463 details.set_error_code(error_code);
1464
1465 NotificationService::current()->Notify(
1466 NotificationType::FAIL_PROVISIONAL_LOAD_WITH_ERROR,
1467 Source<NavigationController>(&controller_),
1468 Details<ProvisionalLoadDetails>(&details));
1469}
1470
1471void TabContents::OnDidLoadResourceFromMemoryCache(
1472 const GURL& url,
[email protected]724159a2010-12-30 01:11:181473 const std::string& security_info) {
1474 static base::StatsCounter cache("WebKit.CacheHit");
1475 cache.Increment();
1476
1477 // Send out a notification that we loaded a resource from our memory cache.
1478 int cert_id = 0, cert_status = 0, security_bits = -1, connection_status = 0;
1479 SSLManager::DeserializeSecurityInfo(security_info,
1480 &cert_id, &cert_status,
1481 &security_bits,
1482 &connection_status);
[email protected]7a8c55e2011-02-15 08:21:161483 LoadFromMemoryCacheDetails details(url, GetRenderProcessHost()->id(),
1484 cert_id, cert_status);
[email protected]724159a2010-12-30 01:11:181485
1486 NotificationService::current()->Notify(
1487 NotificationType::LOAD_FROM_MEMORY_CACHE,
1488 Source<NavigationController>(&controller_),
1489 Details<LoadFromMemoryCacheDetails>(&details));
1490}
1491
1492void TabContents::OnDidDisplayInsecureContent() {
1493 displayed_insecure_content_ = true;
1494 SSLManager::NotifySSLInternalStateChanged();
1495}
1496
1497void TabContents::OnDidRunInsecureContent(
[email protected]92771112011-01-20 00:13:021498 const std::string& security_origin, const GURL& target_url) {
[email protected]9941cf52011-02-08 16:17:231499 LOG(INFO) << security_origin << " ran insecure content from "
1500 << target_url.possibly_invalid_spec();
[email protected]724159a2010-12-30 01:11:181501 controller_.ssl_manager()->DidRunInsecureContent(security_origin);
1502}
1503
1504void TabContents::OnDocumentLoadedInFrame(int64 frame_id) {
1505 controller_.DocumentLoadedInFrame();
1506 NotificationService::current()->Notify(
1507 NotificationType::FRAME_DOM_CONTENT_LOADED,
1508 Source<NavigationController>(&controller_),
1509 Details<int64>(&frame_id));
1510}
1511
1512void TabContents::OnDidFinishLoad(int64 frame_id) {
1513 NotificationService::current()->Notify(
1514 NotificationType::FRAME_DID_FINISH_LOAD,
1515 Source<NavigationController>(&controller_),
1516 Details<int64>(&frame_id));
1517}
1518
[email protected]c8f73ab2011-01-22 00:05:171519void TabContents::OnUpdateContentRestrictions(int restrictions) {
1520 content_restrictions_ = restrictions;
1521 delegate()->ContentRestrictionsChanged(this);
1522}
1523
1524void TabContents::OnPDFHasUnsupportedFeature() {
1525 PDFHasUnsupportedFeature(this);
1526}
1527
[email protected]96d185d2009-04-24 03:28:541528// Notifies the RenderWidgetHost instance about the fact that the page is
1529// loading, or done loading and calls the base implementation.
1530void TabContents::SetIsLoading(bool is_loading,
1531 LoadNotificationDetails* details) {
1532 if (is_loading == is_loading_)
1533 return;
1534
1535 if (!is_loading) {
1536 load_state_ = net::LOAD_STATE_IDLE;
1537 load_state_host_.clear();
[email protected]094e5b22009-09-25 04:23:561538 upload_size_ = 0;
1539 upload_position_ = 0;
[email protected]96d185d2009-04-24 03:28:541540 }
1541
1542 render_manager_.SetIsLoading(is_loading);
1543
1544 is_loading_ = is_loading;
1545 waiting_for_response_ = is_loading;
1546
[email protected]6ebdc9b2010-09-27 16:55:571547 if (delegate_)
1548 delegate_->LoadingStateChanged(this);
[email protected]329581b2009-04-28 06:52:351549 NotifyNavigationStateChanged(INVALIDATE_LOAD);
[email protected]96d185d2009-04-24 03:28:541550
1551 NotificationType type = is_loading ? NotificationType::LOAD_START :
1552 NotificationType::LOAD_STOP;
[email protected]ce5c4502009-05-06 16:46:111553 NotificationDetails det = NotificationService::NoDetails();
[email protected]96d185d2009-04-24 03:28:541554 if (details)
1555 det = Details<LoadNotificationDetails>(details);
1556 NotificationService::current()->Notify(type,
1557 Source<NavigationController>(&controller_),
1558 det);
1559}
1560
[email protected]b6c874582009-05-08 19:38:311561void TabContents::AddPopup(TabContents* new_contents,
[email protected]7e7414ae2010-01-26 20:19:291562 const gfx::Rect& initial_pos) {
[email protected]57eda82a2010-05-05 21:28:061563 // A page can't spawn popups (or do anything else, either) until its load
1564 // commits, so when we reach here, the popup was spawned by the
1565 // NavigationController's last committed entry, not the active entry. For
1566 // example, if a page opens a popup in an onunload() handler, then the active
1567 // entry is the page to be loaded as we navigate away from the unloading
1568 // page. For this reason, we can't use GetURL() to get the opener URL,
1569 // because it returns the active entry.
1570 NavigationEntry* entry = controller_.GetLastCommittedEntry();
1571 GURL creator = entry ? entry->virtual_url() : GURL::EmptyGURL();
1572
1573 if (creator.is_valid() &&
[email protected]8cc8d492010-02-02 10:40:491574 profile()->GetHostContentSettingsMap()->GetContentSetting(
[email protected]ca352452010-08-06 11:14:091575 creator, CONTENT_SETTINGS_TYPE_POPUPS, "") == CONTENT_SETTING_ALLOW) {
[email protected]8cc8d492010-02-02 10:40:491576 AddNewContents(new_contents, NEW_POPUP, initial_pos, true);
1577 } else {
[email protected]c65e2f152010-10-14 15:30:401578 if (!blocked_contents_)
1579 blocked_contents_ = new BlockedContentContainer(this);
1580 blocked_contents_->AddTabContents(new_contents, NEW_POPUP, initial_pos,
1581 true);
[email protected]55126132010-08-19 14:53:281582 content_settings_delegate_->OnContentBlocked(CONTENT_SETTINGS_TYPE_POPUPS,
1583 std::string());
[email protected]8cc8d492010-02-02 10:40:491584 }
[email protected]b9681312008-11-07 00:08:261585}
[email protected]616ed5a2008-11-21 22:27:241586
[email protected]616ed5a2008-11-21 22:27:241587void TabContents::ExpireInfoBars(
1588 const NavigationController::LoadCommittedDetails& details) {
1589 // Only hide InfoBars when the user has done something that makes the main
1590 // frame load. We don't want various automatic or subframe navigations making
1591 // it disappear.
1592 if (!details.is_user_initiated_main_frame_load())
1593 return;
1594
[email protected]ec8f51162011-02-08 04:20:401595 // NOTE: It is not safe to change the following code to count upwards or use
1596 // iterators, as the RemoveInfoBar() call synchronously modifies our delegate
1597 // list.
1598 for (size_t i = infobar_count(); i > 0; --i) {
1599 InfoBarDelegate* delegate = GetInfoBarDelegateAt(i - 1);
[email protected]f86a07022008-11-25 01:06:051600 if (delegate->ShouldExpire(details))
[email protected]616ed5a2008-11-21 22:27:241601 RemoveInfoBar(delegate);
[email protected]616ed5a2008-11-21 22:27:241602 }
1603}
[email protected]fdd61c62009-04-22 19:22:571604
[email protected]d0980792011-02-13 19:41:401605WebUI* TabContents::GetWebUIForCurrentState() {
[email protected]7ade2732011-02-10 00:13:581606 // When there is a pending navigation entry, we want to use the pending WebUI
[email protected]96d185d2009-04-24 03:28:541607 // that goes along with it to control the basic flags. For example, we want to
1608 // show the pending URL in the URL bar, so we want the display_url flag to
1609 // be from the pending entry.
1610 //
1611 // The confusion comes because there are multiple possibilities for the
1612 // initial load in a tab as a side effect of the way the RenderViewHostManager
1613 // works.
1614 //
[email protected]e0112912011-02-02 22:54:351615 // - For the very first tab the load looks "normal". The new tab Web UI is
[email protected]96d185d2009-04-24 03:28:541616 // the pending one, and we want it to apply here.
1617 //
1618 // - For subsequent new tabs, they'll get a new SiteInstance which will then
1619 // get switched to the one previously associated with the new tab pages.
[email protected]7ade2732011-02-10 00:13:581620 // This switching will cause the manager to commit the RVH/WebUI. So we'll
[email protected]e0112912011-02-02 22:54:351621 // have a committed Web UI in this case.
[email protected]96d185d2009-04-24 03:28:541622 //
1623 // This condition handles all of these cases:
1624 //
1625 // - First load in first tab: no committed nav entry + pending nav entry +
1626 // pending dom ui:
[email protected]e0112912011-02-02 22:54:351627 // -> Use pending Web UI if any.
[email protected]96d185d2009-04-24 03:28:541628 //
1629 // - First load in second tab: no committed nav entry + pending nav entry +
[email protected]e0112912011-02-02 22:54:351630 // no pending Web UI:
1631 // -> Use the committed Web UI if any.
[email protected]96d185d2009-04-24 03:28:541632 //
1633 // - Second navigation in any tab: committed nav entry + pending nav entry:
[email protected]e0112912011-02-02 22:54:351634 // -> Use pending Web UI if any.
[email protected]96d185d2009-04-24 03:28:541635 //
1636 // - Normal state with no load: committed nav entry + no pending nav entry:
[email protected]e0112912011-02-02 22:54:351637 // -> Use committed Web UI.
[email protected]96d185d2009-04-24 03:28:541638 if (controller_.pending_entry() &&
1639 (controller_.GetLastCommittedEntry() ||
[email protected]7ade2732011-02-10 00:13:581640 render_manager_.pending_web_ui()))
1641 return render_manager_.pending_web_ui();
1642 return render_manager_.web_ui();
[email protected]96d185d2009-04-24 03:28:541643}
[email protected]420ae012009-04-24 05:16:321644
1645void TabContents::DidNavigateMainFramePostCommit(
1646 const NavigationController::LoadCommittedDetails& details,
1647 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]80a8fad2011-01-29 04:02:381648 if (opener_web_ui_type_ != WebUIFactory::kNoWebUI) {
1649 // If this is a window.open navigation, use the same WebUI as the renderer
[email protected]c2e74fe82009-09-03 17:57:441650 // that opened the window, as long as both renderers have the same
1651 // privileges.
[email protected]80a8fad2011-01-29 04:02:381652 if (opener_web_ui_type_ ==
1653 WebUIFactory::GetWebUIType(profile(), GetURL())) {
[email protected]7ade2732011-02-10 00:13:581654 WebUI* web_ui = WebUIFactory::CreateWebUIForURL(this, GetURL());
1655 // web_ui might be NULL if the URL refers to a non-existent extension.
1656 if (web_ui) {
[email protected]d0980792011-02-13 19:41:401657 render_manager_.SetWebUIPostCommit(web_ui);
[email protected]7ade2732011-02-10 00:13:581658 web_ui->RenderViewCreated(render_view_host());
[email protected]c2e74fe82009-09-03 17:57:441659 }
1660 }
[email protected]80a8fad2011-01-29 04:02:381661 opener_web_ui_type_ = WebUIFactory::kNoWebUI;
[email protected]c2e74fe82009-09-03 17:57:441662 }
1663
[email protected]420ae012009-04-24 05:16:321664 if (details.is_user_initiated_main_frame_load()) {
1665 // Clear the status bubble. This is a workaround for a bug where WebKit
1666 // doesn't let us know that the cursor left an element during a
1667 // transition (this is also why the mouse cursor remains as a hand after
1668 // clicking on a link); see bugs 1184641 and 980803. We don't want to
1669 // clear the bubble when a user navigates to a named anchor in the same
1670 // page.
1671 UpdateTargetURL(details.entry->page_id(), GURL());
[email protected]420ae012009-04-24 05:16:321672 }
1673
[email protected]420ae012009-04-24 05:16:321674 // Allow the new page to set the title again.
1675 received_page_title_ = false;
1676
1677 // Get the favicon, either from history or request it from the net.
[email protected]b5a1d11c2011-02-17 03:09:421678 fav_icon_helper_->FetchFavIcon(details.entry->url());
[email protected]420ae012009-04-24 05:16:321679
[email protected]0de80162010-02-03 04:52:351680 // Clear all page actions, blocked content notifications and browser actions
1681 // for this tab, unless this is an in-page navigation.
[email protected]a6e82fc2010-02-24 22:28:141682 if (!details.is_in_page) {
[email protected]eaa7dd182010-12-14 11:09:001683 ExtensionService* service = profile()->GetExtensionService();
[email protected]92c6f9b92009-10-24 04:35:081684 if (service) {
1685 for (size_t i = 0; i < service->extensions()->size(); ++i) {
[email protected]5d246db22009-10-27 06:17:571686 ExtensionAction* browser_action =
[email protected]92c6f9b92009-10-24 04:35:081687 service->extensions()->at(i)->browser_action();
[email protected]56ce6e52009-10-27 00:10:521688 if (browser_action) {
1689 browser_action->ClearAllValuesForTab(controller().session_id().id());
1690 NotificationService::current()->Notify(
1691 NotificationType::EXTENSION_BROWSER_ACTION_UPDATED,
[email protected]5d246db22009-10-27 06:17:571692 Source<ExtensionAction>(browser_action),
[email protected]56ce6e52009-10-27 00:10:521693 NotificationService::NoDetails());
1694 }
[email protected]92c6f9b92009-10-24 04:35:081695
[email protected]5d246db22009-10-27 06:17:571696 ExtensionAction* page_action =
[email protected]56ce6e52009-10-27 00:10:521697 service->extensions()->at(i)->page_action();
1698 if (page_action) {
1699 page_action->ClearAllValuesForTab(controller().session_id().id());
1700 PageActionStateChanged();
1701 }
[email protected]92c6f9b92009-10-24 04:35:081702 }
[email protected]aece2c7f2009-08-27 20:43:171703 }
[email protected]0de80162010-02-03 04:52:351704
1705 // Close blocked popups.
[email protected]c65e2f152010-10-14 15:30:401706 if (blocked_contents_) {
[email protected]0fbd70332010-06-01 19:28:341707 AutoReset<bool> auto_reset(&dont_notify_render_view_, true);
[email protected]c65e2f152010-10-14 15:30:401708 blocked_contents_->Destroy();
1709 blocked_contents_ = NULL;
[email protected]0de80162010-02-03 04:52:351710 }
1711
1712 // Clear "blocked" flags.
[email protected]da4f8a12010-08-19 08:33:411713 content_settings_delegate_->ClearBlockedContentSettingsExceptForCookies();
[email protected]9fb83e82010-07-02 18:24:551714 content_settings_delegate_->GeolocationDidNavigate(details);
[email protected]f17a0ee2010-05-17 17:38:471715
1716 // Once the main frame is navigated, we're no longer considered to have
1717 // displayed insecure content.
1718 displayed_insecure_content_ = false;
[email protected]aece2c7f2009-08-27 20:43:171719 }
[email protected]ce5c4502009-05-06 16:46:111720
[email protected]8cc8d492010-02-02 10:40:491721 // Close constrained windows if necessary.
1722 if (!net::RegistryControlledDomainService::SameDomainOrHost(
1723 details.previous_url, details.entry->url()))
1724 CloseConstrainedWindows();
1725
[email protected]3c9e1872010-11-18 16:17:491726 // Notify observers about navigation.
[email protected]585b30362011-01-28 02:30:171727 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
[email protected]3c9e1872010-11-18 16:17:491728 DidNavigateMainFramePostCommit(details, params));
[email protected]420ae012009-04-24 05:16:321729}
1730
1731void TabContents::DidNavigateAnyFramePostCommit(
1732 RenderViewHost* render_view_host,
1733 const NavigationController::LoadCommittedDetails& details,
1734 const ViewHostMsg_FrameNavigate_Params& params) {
1735 // If we navigate, start showing messages again. This does nothing to prevent
1736 // a malicious script from spamming messages, since the script could just
1737 // reload the page to stop blocking.
1738 suppress_javascript_messages_ = false;
1739
[email protected]3c9e1872010-11-18 16:17:491740 // Notify observers about navigation.
[email protected]585b30362011-01-28 02:30:171741 FOR_EACH_OBSERVER(TabContentsObserver, observers_,
[email protected]3c9e1872010-11-18 16:17:491742 DidNavigateAnyFramePostCommit(details, params));
[email protected]3b1c1812010-02-12 21:53:551743
1744 // Let the LanguageState clear its state.
[email protected]80fc6112010-08-13 20:10:581745 language_state_.DidNavigate(details);
[email protected]420ae012009-04-24 05:16:321746}
1747
[email protected]8cc8d492010-02-02 10:40:491748void TabContents::CloseConstrainedWindows() {
1749 // Clear out any constrained windows since we are leaving this page entirely.
[email protected]aed59602011-02-28 22:57:331750 // To ensure that we iterate over every element in child_windows_ we
1751 // need to use a copy of child_windows_. Otherwise if
1752 // window->CloseConstrainedWindow() modifies child_windows_ we could end up
1753 // skipping some elements.
1754 ConstrainedWindowList child_windows_copy(child_windows_);
1755 for (ConstrainedWindowList::iterator it = child_windows_copy.begin();
1756 it != child_windows_copy.end(); ++it) {
1757 ConstrainedWindow* window = *it;
[email protected]fa1cf0b82010-01-15 21:49:441758 if (window) {
[email protected]420ae012009-04-24 05:16:321759 window->CloseConstrainedWindow();
[email protected]fa1cf0b82010-01-15 21:49:441760 BlockTabContent(false);
1761 }
[email protected]420ae012009-04-24 05:16:321762 }
[email protected]aed59602011-02-28 22:57:331763 DCHECK(child_windows_.empty());
[email protected]420ae012009-04-24 05:16:321764}
1765
[email protected]420ae012009-04-24 05:16:321766void TabContents::UpdateAlternateErrorPageURL() {
1767 GURL url = GetAlternateErrorPageURL();
1768 render_view_host()->SetAlternateErrorPageURL(url);
1769}
1770
1771void TabContents::UpdateWebPreferences() {
1772 render_view_host()->UpdateWebPreferences(GetWebkitPrefs());
1773}
1774
[email protected]d0b8d092010-10-25 04:05:171775void TabContents::UpdateZoomLevel() {
1776 render_view_host()->SetZoomLevel(GetZoomLevel());
1777}
1778
[email protected]420ae012009-04-24 05:16:321779void TabContents::UpdateMaxPageIDIfNecessary(SiteInstance* site_instance,
1780 RenderViewHost* rvh) {
1781 // If we are creating a RVH for a restored controller, then we might
1782 // have more page IDs than the SiteInstance's current max page ID. We must
1783 // make sure that the max page ID is larger than any restored page ID.
1784 // Note that it is ok for conflicting page IDs to exist in another tab
1785 // (i.e., NavigationController), but if any page ID is larger than the max,
1786 // the back/forward list will get confused.
1787 int max_restored_page_id = controller_.max_restored_page_id();
1788 if (max_restored_page_id > 0) {
1789 int curr_max_page_id = site_instance->max_page_id();
1790 if (max_restored_page_id > curr_max_page_id) {
1791 // Need to update the site instance immediately.
1792 site_instance->UpdateMaxPageID(max_restored_page_id);
1793
1794 // Also tell the renderer to update its internal representation. We
1795 // need to reserve enough IDs to make all restored page IDs less than
1796 // the max.
1797 if (curr_max_page_id < 0)
1798 curr_max_page_id = 0;
1799 rvh->ReservePageIDRange(max_restored_page_id - curr_max_page_id);
1800 }
1801 }
1802}
1803
[email protected]ec0b6c42010-08-26 03:16:581804scoped_refptr<history::HistoryAddPageArgs>
1805TabContents::CreateHistoryAddPageArgs(
[email protected]ebe89e062009-08-13 23:16:541806 const GURL& virtual_url,
[email protected]befd8d822009-07-01 04:51:471807 const NavigationController::LoadCommittedDetails& details,
[email protected]420ae012009-04-24 05:16:321808 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]ec0b6c42010-08-26 03:16:581809 scoped_refptr<history::HistoryAddPageArgs> add_page_args(
1810 new history::HistoryAddPageArgs(
1811 params.url, base::Time::Now(), this, params.page_id, params.referrer,
1812 params.redirects, params.transition, history::SOURCE_BROWSED,
1813 details.did_replace_entry));
1814 if (PageTransition::IsMainFrame(params.transition) &&
1815 virtual_url != params.url) {
1816 // Hack on the "virtual" URL so that it will appear in history. For some
1817 // types of URLs, we will display a magic URL that is different from where
1818 // the page is actually navigated. We want the user to see in history what
1819 // they saw in the URL bar, so we add the virtual URL as a redirect. This
1820 // only applies to the main frame, as the virtual URL doesn't apply to
1821 // sub-frames.
1822 add_page_args->url = virtual_url;
1823 if (!add_page_args->redirects.empty())
1824 add_page_args->redirects.back() = virtual_url;
[email protected]420ae012009-04-24 05:16:321825 }
[email protected]ec0b6c42010-08-26 03:16:581826 return add_page_args;
[email protected]420ae012009-04-24 05:16:321827}
1828
1829bool TabContents::UpdateTitleForEntry(NavigationEntry* entry,
1830 const std::wstring& title) {
1831 // For file URLs without a title, use the pathname instead. In the case of a
1832 // synthesized title, we don't want the update to count toward the "one set
1833 // per page of the title to history."
[email protected]e53668962010-06-23 15:35:251834 string16 final_title;
[email protected]420ae012009-04-24 05:16:321835 bool explicit_set;
1836 if (entry->url().SchemeIsFile() && title.empty()) {
[email protected]e53668962010-06-23 15:35:251837 final_title = UTF8ToUTF16(entry->url().ExtractFileName());
[email protected]420ae012009-04-24 05:16:321838 explicit_set = false; // Don't count synthetic titles toward the set limit.
1839 } else {
[email protected]e53668962010-06-23 15:35:251840 TrimWhitespace(WideToUTF16Hack(title), TRIM_ALL, &final_title);
[email protected]420ae012009-04-24 05:16:321841 explicit_set = true;
1842 }
1843
[email protected]e53668962010-06-23 15:35:251844 if (final_title == entry->title())
[email protected]420ae012009-04-24 05:16:321845 return false; // Nothing changed, don't bother.
1846
[email protected]e53668962010-06-23 15:35:251847 entry->set_title(final_title);
[email protected]420ae012009-04-24 05:16:321848
[email protected]ec0b6c42010-08-26 03:16:581849 if (!received_page_title_) {
1850 UpdateHistoryPageTitle(*entry);
[email protected]420ae012009-04-24 05:16:321851 received_page_title_ = explicit_set;
1852 }
1853
1854 // Lastly, set the title for the view.
[email protected]e53668962010-06-23 15:35:251855 view_->SetPageTitle(UTF16ToWideHack(final_title));
[email protected]420ae012009-04-24 05:16:321856
[email protected]cbc0e1b2010-04-12 18:33:041857 NotificationService::current()->Notify(
1858 NotificationType::TAB_CONTENTS_TITLE_UPDATED,
1859 Source<TabContents>(this),
1860 NotificationService::NoDetails());
1861
[email protected]420ae012009-04-24 05:16:321862 return true;
1863}
1864
1865void TabContents::NotifySwapped() {
1866 // After sending out a swap notification, we need to send a disconnect
1867 // notification so that clients that pick up a pointer to |this| can NULL the
1868 // pointer. See Bug 1230284.
1869 notify_disconnection_ = true;
1870 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341871 NotificationType::TAB_CONTENTS_SWAPPED,
[email protected]d82ed61e2009-06-16 02:46:221872 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321873 NotificationService::NoDetails());
1874}
1875
1876void TabContents::NotifyConnected() {
1877 notify_disconnection_ = true;
1878 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341879 NotificationType::TAB_CONTENTS_CONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221880 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321881 NotificationService::NoDetails());
1882}
1883
1884void TabContents::NotifyDisconnected() {
1885 if (!notify_disconnection_)
1886 return;
1887
1888 notify_disconnection_ = false;
1889 NotificationService::current()->Notify(
[email protected]57c6a652009-05-04 07:58:341890 NotificationType::TAB_CONTENTS_DISCONNECTED,
[email protected]d82ed61e2009-06-16 02:46:221891 Source<TabContents>(this),
[email protected]420ae012009-04-24 05:16:321892 NotificationService::NoDetails());
1893}
1894
[email protected]7d472472011-01-22 01:30:251895void TabContents::OnGoToEntryAtOffset(int offset) {
[email protected]876bc832010-09-07 16:29:541896 if (!delegate_ || delegate_->OnGoToEntryOffset(offset)) {
1897 NavigationEntry* entry = controller_.GetEntryAtOffset(offset);
1898 if (!entry)
1899 return;
1900 // Note that we don't call NavigationController::GotToOffset() as we don't
1901 // want to create a pending navigation entry (it might end up lingering
1902 // http://crbug.com/51680).
[email protected]784688a62010-09-13 07:06:521903 entry->set_transition_type(entry->transition_type() |
1904 PageTransition::FORWARD_BACK);
[email protected]876bc832010-09-07 16:29:541905 NavigateToEntry(*entry, NavigationController::NO_RELOAD);
1906 }
[email protected]bf5c2ff392009-07-08 16:24:331907}
1908
[email protected]38789d82010-11-17 06:03:441909void TabContents::OnDidGetApplicationInfo(int32 page_id,
1910 const WebApplicationInfo& info) {
[email protected]ed5431872009-11-17 08:39:511911 web_app_info_ = info;
[email protected]bf5c2ff392009-07-08 16:24:331912
[email protected]ed5431872009-11-17 08:39:511913 if (delegate())
1914 delegate()->OnDidGetApplicationInfo(this, page_id);
[email protected]bf5c2ff392009-07-08 16:24:331915}
1916
[email protected]bb461532010-11-26 21:50:231917void TabContents::OnInstallApplication(const WebApplicationInfo& info) {
1918 if (delegate())
1919 delegate()->OnInstallApplication(this, info);
1920}
1921
[email protected]a2cf65eb2010-01-18 08:20:381922void TabContents::OnPageContents(const GURL& url,
[email protected]a2cf65eb2010-01-18 08:20:381923 int32 page_id,
[email protected]e5106202010-06-11 21:12:361924 const string16& contents,
[email protected]1c57b7b2010-07-12 17:38:101925 const std::string& language,
1926 bool page_translatable) {
[email protected]a2cf65eb2010-01-18 08:20:381927 // Don't index any https pages. People generally don't want their bank
1928 // accounts, etc. indexed on their computer, especially since some of these
1929 // things are not marked cachable.
1930 // TODO(brettw) we may want to consider more elaborate heuristics such as
1931 // the cachability of the page. We may also want to consider subframes (this
1932 // test will still index subframes if the subframe is SSL).
[email protected]ccd40a52010-05-11 16:15:271933 // TODO(zelidrag) bug chromium-os:2808 - figure out if we want to reenable
1934 // content indexing for chromeos in some future releases.
1935#if !defined(OS_CHROMEOS)
[email protected]a2cf65eb2010-01-18 08:20:381936 if (!url.SchemeIsSecure()) {
1937 Profile* p = profile();
1938 if (p && !p->IsOffTheRecord()) {
1939 HistoryService* hs = p->GetHistoryService(Profile::IMPLICIT_ACCESS);
1940 if (hs)
1941 hs->SetPageContents(url, contents);
1942 }
1943 }
[email protected]ccd40a52010-05-11 16:15:271944#endif
[email protected]a8a81292010-01-21 00:32:451945
[email protected]1c57b7b2010-07-12 17:38:101946 language_state_.LanguageDetermined(language, page_translatable);
[email protected]a8a81292010-01-21 00:32:451947
1948 std::string lang = language;
1949 NotificationService::current()->Notify(
1950 NotificationType::TAB_LANGUAGE_DETERMINED,
[email protected]7e0831b2010-02-01 08:45:151951 Source<TabContents>(this),
[email protected]a8a81292010-01-21 00:32:451952 Details<std::string>(&lang));
[email protected]d54169e92011-01-21 09:19:521953
1954 // Generate the thumbnail here if the in-browser thumbnailing is enabled.
1955 if (CommandLine::ForCurrentProcess()->HasSwitch(
1956 switches::kEnableInBrowserThumbnailing)) {
1957 ThumbnailGenerator::UpdateThumbnailIfNecessary(this, url);
1958 }
[email protected]a2cf65eb2010-01-18 08:20:381959}
1960
[email protected]7e0831b2010-02-01 08:45:151961void TabContents::OnPageTranslated(int32 page_id,
1962 const std::string& original_lang,
[email protected]152b8ba32010-03-29 19:40:161963 const std::string& translated_lang,
1964 TranslateErrors::Type error_type) {
[email protected]3b1c1812010-02-12 21:53:551965 language_state_.set_current_language(translated_lang);
1966 language_state_.set_translation_pending(false);
[email protected]152b8ba32010-03-29 19:40:161967 PageTranslatedDetails details(original_lang, translated_lang, error_type);
[email protected]7e0831b2010-02-01 08:45:151968 NotificationService::current()->Notify(
1969 NotificationType::PAGE_TRANSLATED,
1970 Source<TabContents>(this),
[email protected]152b8ba32010-03-29 19:40:161971 Details<PageTranslatedDetails>(&details));
[email protected]7e0831b2010-02-01 08:45:151972}
1973
[email protected]ce833282010-11-04 15:48:391974void TabContents::OnSetSuggestions(
1975 int32 page_id,
1976 const std::vector<std::string>& suggestions) {
[email protected]15d9c0c2010-09-10 22:36:261977 if (delegate())
[email protected]ce833282010-11-04 15:48:391978 delegate()->OnSetSuggestions(page_id, suggestions);
1979}
1980
1981void TabContents::OnInstantSupportDetermined(int32 page_id, bool result) {
1982 if (delegate())
1983 delegate()->OnInstantSupportDetermined(page_id, result);
[email protected]15d9c0c2010-09-10 22:36:261984}
1985
[email protected]34f128d2011-01-25 19:07:441986void TabContents::OnRunFileChooser(
1987 const ViewHostMsg_RunFileChooser_Params& params) {
1988 if (file_select_helper_.get() == NULL)
1989 file_select_helper_.reset(new FileSelectHelper(profile()));
1990 file_select_helper_->RunFileChooser(render_view_host(), params);
1991}
1992
1993
[email protected]dcd5b332010-08-11 08:55:181994void TabContents::OnContentSettingsAccessed(bool content_was_blocked) {
[email protected]614c0e12010-03-25 10:12:561995 if (delegate_)
[email protected]20ad6a92010-04-21 14:07:241996 delegate_->OnContentSettingsChange(this);
[email protected]614c0e12010-03-25 10:12:561997}
1998
[email protected]8d3347f2009-07-09 22:00:211999RenderViewHostDelegate::View* TabContents::GetViewDelegate() {
[email protected]420ae012009-04-24 05:16:322000 return view_.get();
2001}
2002
[email protected]8d3347f2009-07-09 22:00:212003RenderViewHostDelegate::RendererManagement*
2004TabContents::GetRendererManagementDelegate() {
2005 return &render_manager_;
2006}
2007
[email protected]46ba9082010-06-22 10:00:582008RenderViewHostDelegate::ContentSettings*
2009TabContents::GetContentSettingsDelegate() {
[email protected]9fb83e82010-07-02 18:24:552010 return content_settings_delegate_.get();
[email protected]46ba9082010-06-22 10:00:582011}
2012
[email protected]1f18184a2010-07-21 19:34:492013RenderViewHostDelegate::SSL* TabContents::GetSSLDelegate() {
2014 return GetSSLHelper();
2015}
2016
[email protected]fd571bb2010-03-19 01:23:542017AutomationResourceRoutingDelegate*
2018TabContents::GetAutomationResourceRoutingDelegate() {
2019 return delegate();
2020}
2021
[email protected]ced90ae12010-02-20 02:06:162022RenderViewHostDelegate::BookmarkDrag* TabContents::GetBookmarkDragDelegate() {
2023 return bookmark_drag_;
2024}
2025
2026void TabContents::SetBookmarkDragDelegate(
2027 RenderViewHostDelegate::BookmarkDrag* bookmark_drag) {
2028 bookmark_drag_ = bookmark_drag;
2029}
2030
[email protected]9fb83e82010-07-02 18:24:552031TabSpecificContentSettings* TabContents::GetTabSpecificContentSettings() const {
2032 return content_settings_delegate_.get();
2033}
2034
[email protected]93623c5d2009-12-10 21:40:322035RendererPreferences TabContents::GetRendererPrefs(Profile* profile) const {
[email protected]840b1512009-07-21 16:53:462036 return renderer_preferences_;
[email protected]80d96fa2009-06-10 22:34:512037}
2038
[email protected]57c6a652009-05-04 07:58:342039TabContents* TabContents::GetAsTabContents() {
2040 return this;
2041}
2042
[email protected]7b291f92009-08-14 05:43:532043ViewType::Type TabContents::GetRenderViewType() const {
2044 return ViewType::TAB_CONTENTS;
2045}
2046
2047int TabContents::GetBrowserWindowID() const {
2048 return controller().window_id().id();
2049}
2050
[email protected]420ae012009-04-24 05:16:322051void TabContents::RenderViewCreated(RenderViewHost* render_view_host) {
[email protected]2e4633c2009-07-09 16:58:062052 NotificationService::current()->Notify(
2053 NotificationType::RENDER_VIEW_HOST_CREATED_FOR_TAB,
2054 Source<TabContents>(this),
2055 Details<RenderViewHost>(render_view_host));
[email protected]420ae012009-04-24 05:16:322056 NavigationEntry* entry = controller_.GetActiveEntry();
2057 if (!entry)
2058 return;
2059
2060 // When we're creating views, we're still doing initial setup, so we always
[email protected]e0112912011-02-02 22:54:352061 // use the pending Web UI rather than any possibly existing committed one.
[email protected]7ade2732011-02-10 00:13:582062 if (render_manager_.pending_web_ui()) {
2063 render_manager_.pending_web_ui()->RenderViewCreated(render_view_host);
[email protected]420ae012009-04-24 05:16:322064 }
2065
2066 if (entry->IsViewSourceMode()) {
2067 // Put the renderer in view source mode.
2068 render_view_host->Send(
2069 new ViewMsg_EnableViewSourceMode(render_view_host->routing_id()));
2070 }
[email protected]0666aef2009-05-13 19:48:082071
2072 view()->RenderViewCreated(render_view_host);
[email protected]420ae012009-04-24 05:16:322073}
2074
2075void TabContents::RenderViewReady(RenderViewHost* rvh) {
2076 if (rvh != render_view_host()) {
2077 // Don't notify the world, since this came from a renderer in the
2078 // background.
2079 return;
2080 }
2081
2082 NotifyConnected();
[email protected]9d8a4642009-07-29 17:25:302083 bool was_crashed = is_crashed();
[email protected]443b80e2010-12-14 00:42:232084 SetIsCrashed(base::TERMINATION_STATUS_STILL_RUNNING, 0);
[email protected]9d8a4642009-07-29 17:25:302085
2086 // Restore the focus to the tab (otherwise the focus will be on the top
2087 // window).
[email protected]484ae5912010-09-29 19:16:142088 if (was_crashed && !FocusLocationBarByDefault() &&
2089 (!delegate_ || delegate_->ShouldFocusPageAfterCrash())) {
[email protected]9d8a4642009-07-29 17:25:302090 Focus();
[email protected]484ae5912010-09-29 19:16:142091 }
[email protected]420ae012009-04-24 05:16:322092}
2093
[email protected]443b80e2010-12-14 00:42:232094void TabContents::RenderViewGone(RenderViewHost* rvh,
2095 base::TerminationStatus status,
2096 int error_code) {
[email protected]420ae012009-04-24 05:16:322097 // Ask the print preview if this renderer was valuable.
[email protected]12802702010-07-09 19:43:092098 if (!printing_->OnRenderViewGone(rvh))
[email protected]420ae012009-04-24 05:16:322099 return;
2100 if (rvh != render_view_host()) {
2101 // The pending page's RenderViewHost is gone.
2102 return;
2103 }
2104
2105 SetIsLoading(false, NULL);
2106 NotifyDisconnected();
[email protected]443b80e2010-12-14 00:42:232107 SetIsCrashed(status, error_code);
[email protected]420ae012009-04-24 05:16:322108
[email protected]ff9efc62010-02-18 22:10:542109 // Remove all infobars.
[email protected]ec8f51162011-02-08 04:20:402110 while (!infobar_delegates_.empty())
2111 RemoveInfoBar(GetInfoBarDelegateAt(infobar_count() - 1));
[email protected]ff9efc62010-02-18 22:10:542112
[email protected]8e537302009-06-10 21:19:582113 // Tell the view that we've crashed so it can prepare the sad tab page.
[email protected]cf68b092010-01-15 20:05:552114 // Only do this if we're not in browser shutdown, so that TabContents
2115 // objects that are not in a browser (e.g., HTML dialogs) and thus are
2116 // visible do not flash a sad tab page.
2117 if (browser_shutdown::GetShutdownType() == browser_shutdown::NOT_VALID)
[email protected]a8f024392011-01-13 21:50:162118 view_->OnTabCrashed(status, error_code);
[email protected]420ae012009-04-24 05:16:322119
2120 // Hide any visible hung renderer warning for this web contents' process.
[email protected]7ec6bef2011-02-26 02:22:292121 browser::HideHungRendererDialog(this);
[email protected]420ae012009-04-24 05:16:322122}
2123
[email protected]2e4633c2009-07-09 16:58:062124void TabContents::RenderViewDeleted(RenderViewHost* rvh) {
2125 NotificationService::current()->Notify(
2126 NotificationType::RENDER_VIEW_HOST_DELETED,
2127 Source<TabContents>(this),
2128 Details<RenderViewHost>(rvh));
[email protected]fb7b79f72009-11-06 18:00:482129 render_manager_.RenderViewDeleted(rvh);
[email protected]2e4633c2009-07-09 16:58:062130}
2131
[email protected]420ae012009-04-24 05:16:322132void TabContents::DidNavigate(RenderViewHost* rvh,
2133 const ViewHostMsg_FrameNavigate_Params& params) {
[email protected]8030f012009-09-25 18:09:372134 int extra_invalidate_flags = 0;
2135
2136 if (PageTransition::IsMainFrame(params.transition)) {
[email protected]e7d50892011-01-19 21:47:382137 if (MaybeUsePreloadedPage(params.url)) {
2138 return;
[email protected]33f74972010-12-08 16:40:362139 }
2140
[email protected]20ede072009-10-06 00:28:352141 bool was_bookmark_bar_visible = ShouldShowBookmarkBar();
[email protected]8030f012009-09-25 18:09:372142
[email protected]420ae012009-04-24 05:16:322143 render_manager_.DidNavigateMainFrame(rvh);
2144
[email protected]20ede072009-10-06 00:28:352145 if (was_bookmark_bar_visible != ShouldShowBookmarkBar())
[email protected]8030f012009-09-25 18:09:372146 extra_invalidate_flags |= INVALIDATE_BOOKMARK_BAR;
[email protected]8030f012009-09-25 18:09:372147 }
2148
[email protected]420ae012009-04-24 05:16:322149 // Update the site of the SiteInstance if it doesn't have one yet.
2150 if (!GetSiteInstance()->has_site())
2151 GetSiteInstance()->SetSite(params.url);
2152
2153 // Need to update MIME type here because it's referred to in
2154 // UpdateNavigationCommands() called by RendererDidNavigate() to
2155 // determine whether or not to enable the encoding menu.
2156 // It's updated only for the main frame. For a subframe,
2157 // RenderView::UpdateURL does not set params.contents_mime_type.
2158 // (see http://code.google.com/p/chromium/issues/detail?id=2929 )
2159 // TODO(jungshik): Add a test for the encoding menu to avoid
2160 // regressing it again.
2161 if (PageTransition::IsMainFrame(params.transition))
2162 contents_mime_type_ = params.contents_mime_type;
2163
2164 NavigationController::LoadCommittedDetails details;
[email protected]8030f012009-09-25 18:09:372165 bool did_navigate = controller_.RendererDidNavigate(
2166 params, extra_invalidate_flags, &details);
[email protected]3e90d4a2009-07-03 17:38:392167
[email protected]a9c0bfe2010-09-17 08:35:222168 // Send notification about committed provisional loads. This notification is
2169 // different from the NAV_ENTRY_COMMITTED notification which doesn't include
2170 // the actual URL navigated to and isn't sent for AUTO_SUBFRAME navigations.
2171 if (details.type != NavigationType::NAV_IGNORE) {
[email protected]dabb0d12010-10-05 12:50:072172 // For AUTO_SUBFRAME navigations, an event for the main frame is generated
2173 // that is not recorded in the navigation history. For the purpose of
2174 // tracking navigation events, we treat this event as a sub frame navigation
2175 // event.
2176 bool is_main_frame = did_navigate ? details.is_main_frame : false;
2177 ProvisionalLoadDetails load_details(
2178 is_main_frame, details.is_in_page, params.url, std::string(), false,
[email protected]dd0aafd2010-12-02 13:13:412179 false, params.frame_id);
[email protected]a9c0bfe2010-09-17 08:35:222180 load_details.set_transition_type(params.transition);
2181 // Whether or not a page transition was triggered by going backward or
2182 // forward in the history is only stored in the navigation controller's
2183 // entry list.
2184 if (did_navigate &&
2185 (controller_.GetActiveEntry()->transition_type() &
2186 PageTransition::FORWARD_BACK)) {
2187 load_details.set_transition_type(
2188 params.transition | PageTransition::FORWARD_BACK);
2189 }
2190 NotificationService::current()->Notify(
2191 NotificationType::FRAME_PROVISIONAL_LOAD_COMMITTED,
2192 Source<NavigationController>(&controller_),
2193 Details<ProvisionalLoadDetails>(&load_details));
[email protected]a9c0bfe2010-09-17 08:35:222194 }
2195
[email protected]3e90d4a2009-07-03 17:38:392196 // Update history. Note that this needs to happen after the entry is complete,
2197 // which WillNavigate[Main,Sub]Frame will do before this function is called.
[email protected]ec0b6c42010-08-26 03:16:582198 if (params.should_update_history) {
[email protected]3e90d4a2009-07-03 17:38:392199 // Most of the time, the displayURL matches the loaded URL, but for about:
[email protected]ec0b6c42010-08-26 03:16:582200 // URLs, we use a data: URL as the real value. We actually want to save the
2201 // about: URL to the history db and keep the data: URL hidden. This is what
2202 // the TabContents' URL getter does.
2203 scoped_refptr<history::HistoryAddPageArgs> add_page_args(
2204 CreateHistoryAddPageArgs(GetURL(), details, params));
2205 if (!delegate() ||
2206 delegate()->ShouldAddNavigationToHistory(*add_page_args,
2207 details.type)) {
2208 UpdateHistoryForNavigation(add_page_args);
2209 }
[email protected]3e90d4a2009-07-03 17:38:392210 }
2211
[email protected]76543b92009-08-31 17:27:452212 if (!did_navigate)
2213 return; // No navigation happened.
[email protected]420ae012009-04-24 05:16:322214
2215 // DO NOT ADD MORE STUFF TO THIS FUNCTION! Your component should either listen
2216 // for the appropriate notification (best) or you can add it to
2217 // DidNavigateMainFramePostCommit / DidNavigateAnyFramePostCommit (only if
2218 // necessary, please).
2219
2220 // Run post-commit tasks.
2221 if (details.is_main_frame)
2222 DidNavigateMainFramePostCommit(details, params);
2223 DidNavigateAnyFramePostCommit(rvh, details, params);
2224}
2225
2226void TabContents::UpdateState(RenderViewHost* rvh,
2227 int32 page_id,
2228 const std::string& state) {
2229 DCHECK(rvh == render_view_host());
2230
2231 // We must be prepared to handle state updates for any page, these occur
2232 // when the user is scrolling and entering form data, as well as when we're
2233 // leaving a page, in which case our state may have already been moved to
2234 // the next page. The navigation controller will look up the appropriate
2235 // NavigationEntry and update it when it is notified via the delegate.
2236
2237 int entry_index = controller_.GetEntryIndexWithPageID(
2238 GetSiteInstance(), page_id);
2239 if (entry_index < 0)
2240 return;
2241 NavigationEntry* entry = controller_.GetEntryAtIndex(entry_index);
2242
2243 if (state == entry->content_state())
2244 return; // Nothing to update.
2245 entry->set_content_state(state);
2246 controller_.NotifyEntryChanged(entry, entry_index);
2247}
2248
2249void TabContents::UpdateTitle(RenderViewHost* rvh,
2250 int32 page_id, const std::wstring& title) {
2251 // If we have a title, that's a pretty good indication that we've started
2252 // getting useful data.
2253 SetNotWaitingForResponse();
2254
2255 DCHECK(rvh == render_view_host());
[email protected]371cc8b2010-09-01 16:03:482256 NavigationEntry* entry = controller_.GetEntryWithPageID(rvh->site_instance(),
[email protected]ec0b6c42010-08-26 03:16:582257 page_id);
[email protected]420ae012009-04-24 05:16:322258 if (!entry || !UpdateTitleForEntry(entry, title))
2259 return;
2260
2261 // Broadcast notifications when the UI should be updated.
2262 if (entry == controller_.GetEntryAtOffset(0))
[email protected]f1cd5e82009-10-23 17:19:032263 NotifyNavigationStateChanged(INVALIDATE_TITLE);
[email protected]420ae012009-04-24 05:16:322264}
2265
[email protected]420ae012009-04-24 05:16:322266void TabContents::UpdateEncoding(RenderViewHost* render_view_host,
[email protected]41fc0322009-09-04 22:23:402267 const std::string& encoding) {
[email protected]420ae012009-04-24 05:16:322268 set_encoding(encoding);
2269}
2270
2271void TabContents::UpdateTargetURL(int32 page_id, const GURL& url) {
2272 if (delegate())
2273 delegate()->UpdateTargetURL(this, url);
2274}
2275
2276void TabContents::UpdateThumbnail(const GURL& url,
2277 const SkBitmap& bitmap,
2278 const ThumbnailScore& score) {
[email protected]e6baed562010-11-02 22:33:442279 if (profile()->IsOffTheRecord())
2280 return;
2281
[email protected]420ae012009-04-24 05:16:322282 // Tell History about this thumbnail
[email protected]54097492f2011-01-18 08:32:072283 history::TopSites* ts = profile()->GetTopSites();
2284 if (ts)
2285 ts->SetPageThumbnail(url, bitmap, score);
[email protected]420ae012009-04-24 05:16:322286}
2287
[email protected]c4e98902010-06-01 10:20:142288void TabContents::UpdateInspectorSetting(const std::string& key,
2289 const std::string& value) {
[email protected]24d27d72010-10-12 16:30:052290 RenderViewHostDelegateHelper::UpdateInspectorSetting(profile(), key, value);
[email protected]0df30122009-06-03 12:13:082291}
2292
[email protected]45aafc92010-06-07 14:18:532293void TabContents::ClearInspectorSettings() {
[email protected]24d27d72010-10-12 16:30:052294 RenderViewHostDelegateHelper::ClearInspectorSettings(profile());
[email protected]45aafc92010-06-07 14:18:532295}
2296
[email protected]420ae012009-04-24 05:16:322297void TabContents::Close(RenderViewHost* rvh) {
[email protected]07707302009-11-06 00:50:292298 // The UI may be in an event-tracking loop, such as between the
2299 // mouse-down and mouse-up in text selection or a button click.
2300 // Defer the close until after tracking is complete, so that we
2301 // don't free objects out from under the UI.
2302 // TODO(shess): This could probably be integrated with the
2303 // IsDoingDrag() test below. Punting for now because I need more
2304 // research to understand how this impacts platforms other than Mac.
2305 // TODO(shess): This could get more fine-grained. For instance,
2306 // closing a tab in another window while selecting text in the
2307 // current window's Omnibox should be just fine.
2308 if (view()->IsEventTracking()) {
2309 view()->CloseTabAfterEventTracking();
2310 return;
2311 }
2312
[email protected]24a4d1062009-07-10 23:10:422313 // If we close the tab while we're in the middle of a drag, we'll crash.
2314 // Instead, cancel the drag and close it as soon as the drag ends.
2315 if (view()->IsDoingDrag()) {
2316 view()->CancelDragAndCloseTab();
2317 return;
2318 }
2319
[email protected]420ae012009-04-24 05:16:322320 // Ignore this if it comes from a RenderViewHost that we aren't showing.
2321 if (delegate() && rvh == render_view_host())
2322 delegate()->CloseContents(this);
2323}
2324
2325void TabContents::RequestMove(const gfx::Rect& new_bounds) {
2326 if (delegate() && delegate()->IsPopup(this))
2327 delegate()->MoveContents(this, new_bounds);
2328}
2329
[email protected]7ab1e7d62009-10-14 23:32:012330void TabContents::DidStartLoading() {
[email protected]420ae012009-04-24 05:16:322331 SetIsLoading(true, NULL);
[email protected]3c9e1872010-11-18 16:17:492332
[email protected]de4761b2010-12-07 21:05:212333 if (delegate()) {
2334 bool is_print_preview_tab =
2335 printing::PrintPreviewTabController::IsPrintPreviewTab(this);
2336 if (content_restrictions_ || is_print_preview_tab) {
2337 content_restrictions_= is_print_preview_tab ?
2338 CONTENT_RESTRICTION_PRINT : 0;
2339 delegate()->ContentRestrictionsChanged(this);
2340 }
[email protected]9e823662010-10-13 23:36:002341 }
[email protected]3c9e1872010-11-18 16:17:492342
2343 // Notify observers about navigation.
[email protected]585b30362011-01-28 02:30:172344 FOR_EACH_OBSERVER(TabContentsObserver, observers_, DidStartLoading());
[email protected]420ae012009-04-24 05:16:322345}
2346
[email protected]7ab1e7d62009-10-14 23:32:012347void TabContents::DidStopLoading() {
[email protected]420ae012009-04-24 05:16:322348 scoped_ptr<LoadNotificationDetails> details;
2349
2350 NavigationEntry* entry = controller_.GetActiveEntry();
2351 // An entry may not exist for a stop when loading an initial blank page or
2352 // if an iframe injected by script into a blank page finishes loading.
2353 if (entry) {
[email protected]420ae012009-04-24 05:16:322354 base::TimeDelta elapsed = base::TimeTicks::Now() - current_load_start_;
2355
2356 details.reset(new LoadNotificationDetails(
[email protected]ebe89e062009-08-13 23:16:542357 entry->virtual_url(),
[email protected]420ae012009-04-24 05:16:322358 entry->transition_type(),
2359 elapsed,
2360 &controller_,
2361 controller_.GetCurrentEntryIndex()));
2362 }
2363
[email protected]420ae012009-04-24 05:16:322364 SetIsLoading(false, details.get());
[email protected]3c9e1872010-11-18 16:17:492365
2366 // Notify observers about navigation.
[email protected]585b30362011-01-28 02:30:172367 FOR_EACH_OBSERVER(TabContentsObserver, observers_, DidStopLoading());
[email protected]420ae012009-04-24 05:16:322368}
2369
[email protected]1a3c3cb2010-12-16 21:03:402370void TabContents::DidChangeLoadProgress(double progress) {
2371 if (delegate())
2372 delegate()->LoadProgressChanged(progress);
2373}
2374
[email protected]25497492010-09-11 15:15:082375void TabContents::DocumentOnLoadCompletedInMainFrame(
2376 RenderViewHost* render_view_host,
2377 int32 page_id) {
2378 NotificationService::current()->Notify(
2379 NotificationType::LOAD_COMPLETED_MAIN_FRAME,
2380 Source<TabContents>(this),
2381 Details<int>(&page_id));
2382}
2383
[email protected]420ae012009-04-24 05:16:322384void TabContents::RequestOpenURL(const GURL& url, const GURL& referrer,
2385 WindowOpenDisposition disposition) {
[email protected]7ade2732011-02-10 00:13:582386 if (render_manager_.web_ui()) {
[email protected]e0112912011-02-02 22:54:352387 // When we're a Web UI, it will provide a page transition type for us (this
[email protected]420ae012009-04-24 05:16:322388 // is so the new tab page can specify AUTO_BOOKMARK for automatically
2389 // generated suggestions).
2390 //
[email protected]e0112912011-02-02 22:54:352391 // Note also that we hide the referrer for Web UI pages. We don't really
[email protected]60e448982009-05-06 04:21:162392 // want web sites to see a referrer of "chrome://blah" (and some
2393 // chrome: URLs might have search terms or other stuff we don't want to
[email protected]420ae012009-04-24 05:16:322394 // send to the site), so we send no referrer.
2395 OpenURL(url, GURL(), disposition,
[email protected]7ade2732011-02-10 00:13:582396 render_manager_.web_ui()->link_transition_type());
[email protected]420ae012009-04-24 05:16:322397 } else {
2398 OpenURL(url, referrer, disposition, PageTransition::LINK);
2399 }
2400}
2401
[email protected]1ee614862009-07-06 20:29:182402void TabContents::DomOperationResponse(const std::string& json_string,
2403 int automation_id) {
2404}
2405
[email protected]badcca352011-02-12 01:08:082406void TabContents::ProcessWebUIMessage(
[email protected]8b8e7c92010-08-19 18:05:562407 const ViewHostMsg_DomMessage_Params& params) {
[email protected]7ade2732011-02-10 00:13:582408 if (!render_manager_.web_ui()) {
[email protected]c2e74fe82009-09-03 17:57:442409 // This can happen if someone uses window.open() to open an extension URL
2410 // from a non-extension context.
[email protected]8b8e7c92010-08-19 18:05:562411 render_view_host()->BlockExtensionRequest(params.request_id);
[email protected]420ae012009-04-24 05:16:322412 return;
2413 }
[email protected]badcca352011-02-12 01:08:082414 render_manager_.web_ui()->ProcessWebUIMessage(params);
[email protected]420ae012009-04-24 05:16:322415}
2416
2417void TabContents::ProcessExternalHostMessage(const std::string& message,
2418 const std::string& origin,
2419 const std::string& target) {
2420 if (delegate())
2421 delegate()->ForwardMessageToExternalHost(message, origin, target);
2422}
2423
[email protected]420ae012009-04-24 05:16:322424void TabContents::RunJavaScriptMessage(
2425 const std::wstring& message,
2426 const std::wstring& default_prompt,
2427 const GURL& frame_url,
2428 const int flags,
2429 IPC::Message* reply_msg,
2430 bool* did_suppress_message) {
2431 // Suppress javascript messages when requested and when inside a constrained
2432 // popup window (because that activates them and breaks them out of the
2433 // constrained window jail).
[email protected]91ed49a2009-07-07 21:29:552434 // Also suppress messages when showing an interstitial. The interstitial is
2435 // shown over the previous page, we don't want the hidden page dialogs to
2436 // interfere with the interstitial.
[email protected]8f55e802010-12-06 18:11:502437 bool suppress_this_message =
2438 suppress_javascript_messages_ ||
2439 showing_interstitial_page() ||
2440 (delegate() && delegate()->ShouldSuppressDialogs());
[email protected]420ae012009-04-24 05:16:322441 if (delegate())
2442 suppress_this_message |=
[email protected]b6c874582009-05-08 19:38:312443 (delegate()->GetConstrainingContents(this) != this);
[email protected]420ae012009-04-24 05:16:322444
2445 *did_suppress_message = suppress_this_message;
2446
2447 if (!suppress_this_message) {
2448 base::TimeDelta time_since_last_message(
2449 base::TimeTicks::Now() - last_javascript_message_dismissal_);
2450 bool show_suppress_checkbox = false;
2451 // Show a checkbox offering to suppress further messages if this message is
2452 // being displayed within kJavascriptMessageExpectedDelay of the last one.
2453 if (time_since_last_message <
2454 base::TimeDelta::FromMilliseconds(kJavascriptMessageExpectedDelay))
2455 show_suppress_checkbox = true;
2456
[email protected]d6ad7442010-09-05 19:06:522457 RunJavascriptMessageBox(profile(), this, frame_url, flags, message,
2458 default_prompt, show_suppress_checkbox, reply_msg);
[email protected]420ae012009-04-24 05:16:322459 } else {
2460 // If we are suppressing messages, just reply as is if the user immediately
2461 // pressed "Cancel".
[email protected]beb440c2009-11-06 04:08:542462 OnMessageBoxClosed(reply_msg, false, std::wstring());
[email protected]420ae012009-04-24 05:16:322463 }
2464}
2465
2466void TabContents::RunBeforeUnloadConfirm(const std::wstring& message,
2467 IPC::Message* reply_msg) {
[email protected]5aab5e22010-12-08 22:13:292468 if (delegate())
2469 delegate()->WillRunBeforeUnloadConfirm();
[email protected]8f55e802010-12-06 18:11:502470 if (delegate() && delegate()->ShouldSuppressDialogs()) {
2471 render_view_host()->JavaScriptMessageBoxClosed(reply_msg, true,
2472 std::wstring());
2473 return;
2474 }
[email protected]7ab1e7d62009-10-14 23:32:012475 is_showing_before_unload_dialog_ = true;
[email protected]d82ed61e2009-06-16 02:46:222476 RunBeforeUnloadDialog(this, message, reply_msg);
[email protected]420ae012009-04-24 05:16:322477}
2478
2479void TabContents::ShowModalHTMLDialog(const GURL& url, int width, int height,
2480 const std::string& json_arguments,
2481 IPC::Message* reply_msg) {
2482 if (delegate()) {
2483 HtmlDialogUIDelegate* dialog_delegate =
2484 new ModalHtmlDialogDelegate(url, width, height, json_arguments,
[email protected]d82ed61e2009-06-16 02:46:222485 reply_msg, this);
[email protected]420ae012009-04-24 05:16:322486 delegate()->ShowHtmlDialog(dialog_delegate, NULL);
2487 }
2488}
2489
[email protected]420ae012009-04-24 05:16:322490GURL TabContents::GetAlternateErrorPageURL() const {
2491 GURL url;
2492 // Disable alternate error pages when in OffTheRecord/Incognito mode.
2493 if (profile()->IsOffTheRecord())
2494 return url;
2495
2496 PrefService* prefs = profile()->GetPrefs();
2497 DCHECK(prefs);
2498 if (prefs->GetBoolean(prefs::kAlternateErrorPagesEnabled)) {
[email protected]a33457b82009-09-21 23:07:152499 url = google_util::AppendGoogleLocaleParam(
2500 GURL(google_util::kLinkDoctorBaseURL));
[email protected]420ae012009-04-24 05:16:322501 url = google_util::AppendGoogleTLDParam(url);
2502 }
2503 return url;
2504}
2505
2506WebPreferences TabContents::GetWebkitPrefs() {
[email protected]6c32ce72010-03-08 05:18:062507 Profile* profile = render_view_host()->process()->profile();
[email protected]7ade2732011-02-10 00:13:582508 bool is_web_ui = false;
[email protected]447021c2010-09-08 21:29:082509 WebPreferences web_prefs =
[email protected]7ade2732011-02-10 00:13:582510 RenderViewHostDelegateHelper::GetWebkitPrefs(profile, is_web_ui);
[email protected]447021c2010-09-08 21:29:082511
[email protected]9beff752010-09-22 19:35:432512 // Force accelerated compositing and 2d canvas off for chrome: and
2513 // chrome-extension: pages.
[email protected]27c790d42010-10-22 09:28:432514 if (GetURL().SchemeIs(chrome::kChromeDevToolsScheme) ||
2515 GetURL().SchemeIs(chrome::kChromeUIScheme)) {
[email protected]447021c2010-09-08 21:29:082516 web_prefs.accelerated_compositing_enabled = false;
[email protected]9beff752010-09-22 19:35:432517 web_prefs.accelerated_2d_canvas_enabled = false;
[email protected]447021c2010-09-08 21:29:082518 }
2519
[email protected]39553442010-09-09 00:01:552520#if defined(OS_MACOSX)
2521 // Disable accelerated compositing if IOSurface's are not supported,
2522 // as is the case in 10.5.
2523 if (!IOSurfaceSupport::Initialize())
2524 web_prefs.accelerated_compositing_enabled = false;
2525#endif
2526
[email protected]447021c2010-09-08 21:29:082527 return web_prefs;
[email protected]420ae012009-04-24 05:16:322528}
2529
[email protected]7d472472011-01-22 01:30:252530void TabContents::OnUserGesture() {
2531 // See comment in RenderViewHostDelegate::OnUserGesture as to why we do this.
2532 DownloadRequestLimiter* limiter =
2533 g_browser_process->download_request_limiter();
2534 if (limiter)
2535 limiter->OnUserGesture(this);
2536 ExternalProtocolHandler::PermitLaunchUrl();
2537}
2538
[email protected]fa1cf0b82010-01-15 21:49:442539void TabContents::OnIgnoredUIEvent() {
2540 if (constrained_window_count()) {
2541 ConstrainedWindow* window = *constrained_window_begin();
2542 window->FocusConstrainedWindow();
2543 }
2544}
2545
[email protected]420ae012009-04-24 05:16:322546void TabContents::OnCrossSiteResponse(int new_render_process_host_id,
2547 int new_request_id) {
[email protected]57c6a652009-05-04 07:58:342548 // Allows the TabContents to react when a cross-site response is ready to be
[email protected]420ae012009-04-24 05:16:322549 // delivered to a pending RenderViewHost. We must first run the onunload
2550 // handler of the old RenderViewHost before we can allow it to proceed.
2551 render_manager_.OnCrossSiteResponse(new_render_process_host_id,
2552 new_request_id);
2553}
2554
[email protected]420ae012009-04-24 05:16:322555void TabContents::RendererUnresponsive(RenderViewHost* rvh,
2556 bool is_during_unload) {
2557 if (is_during_unload) {
2558 // Hang occurred while firing the beforeunload/unload handler.
2559 // Pretend the handler fired so tab closing continues as if it had.
[email protected]829e7612009-04-25 01:15:112560 rvh->set_sudden_termination_allowed(true);
[email protected]420ae012009-04-24 05:16:322561
2562 if (!render_manager_.ShouldCloseTabOnUnresponsiveRenderer())
2563 return;
2564
2565 // If the tab hangs in the beforeunload/unload handler there's really
2566 // nothing we can do to recover. Pretend the unload listeners have
2567 // all fired and close the tab. If the hang is in the beforeunload handler
2568 // then the user will not have the option of cancelling the close.
2569 Close(rvh);
2570 return;
2571 }
2572
[email protected]61b50092011-02-03 18:02:192573 if (render_view_host() && render_view_host()->IsRenderViewLive() &&
2574 (!delegate() || delegate()->ShouldShowHungRendererDialog())) {
[email protected]7ec6bef2011-02-26 02:22:292575 browser::ShowHungRendererDialog(this);
[email protected]61b50092011-02-03 18:02:192576 }
[email protected]420ae012009-04-24 05:16:322577}
2578
2579void TabContents::RendererResponsive(RenderViewHost* render_view_host) {
[email protected]7ec6bef2011-02-26 02:22:292580 browser::HideHungRendererDialog(this);
[email protected]420ae012009-04-24 05:16:322581}
2582
2583void TabContents::LoadStateChanged(const GURL& url,
[email protected]094e5b22009-09-25 04:23:562584 net::LoadState load_state,
2585 uint64 upload_position,
2586 uint64 upload_size) {
[email protected]420ae012009-04-24 05:16:322587 load_state_ = load_state;
[email protected]094e5b22009-09-25 04:23:562588 upload_position_ = upload_position;
2589 upload_size_ = upload_size;
[email protected]d686e812009-06-03 19:10:292590 std::wstring languages =
[email protected]ddd231e2010-06-29 20:35:192591 UTF8ToWide(profile()->GetPrefs()->GetString(prefs::kAcceptLanguages));
[email protected]d686e812009-06-03 19:10:292592 std::string host = url.host();
[email protected]42197a22010-12-28 23:29:422593 load_state_host_ = WideToUTF16Hack(
2594 net::IDNToUnicode(host.c_str(), host.size(), languages, NULL));
[email protected]420ae012009-04-24 05:16:322595 if (load_state_ == net::LOAD_STATE_READING_RESPONSE)
2596 SetNotWaitingForResponse();
2597 if (is_loading())
[email protected]c9cd2222009-05-06 05:16:502598 NotifyNavigationStateChanged(INVALIDATE_LOAD | INVALIDATE_TAB);
[email protected]420ae012009-04-24 05:16:322599}
2600
[email protected]1ee614862009-07-06 20:29:182601bool TabContents::IsExternalTabContainer() const {
2602 if (!delegate())
2603 return false;
2604
2605 return delegate()->IsExternalTabContainer();
2606}
2607
[email protected]1ee614862009-07-06 20:29:182608void TabContents::DidInsertCSS() {
2609 // This RVHDelegate function is used for extensions and not us.
[email protected]420ae012009-04-24 05:16:322610}
2611
[email protected]546ae4e02010-12-08 14:57:192612void TabContents::FocusedNodeChanged(bool is_editable_node) {
[email protected]130efb02009-09-18 18:54:352613 NotificationService::current()->Notify(
2614 NotificationType::FOCUS_CHANGED_IN_PAGE,
[email protected]4e85c112011-01-26 22:27:212615 Source<TabContents>(this),
[email protected]546ae4e02010-12-08 14:57:192616 Details<const bool>(&is_editable_node));
[email protected]130efb02009-09-18 18:54:352617}
2618
[email protected]b75b8292010-10-01 07:28:252619void TabContents::UpdateZoomLimits(int minimum_percent,
2620 int maximum_percent,
2621 bool remember) {
2622 minimum_zoom_percent_ = minimum_percent;
2623 maximum_zoom_percent_ = maximum_percent;
2624 temporary_zoom_settings_ = !remember;
[email protected]ebf40a72010-07-22 01:46:382625}
2626
[email protected]7d472472011-01-22 01:30:252627void TabContents::WorkerCrashed() {
[email protected]0dd3a0ab2011-02-18 08:17:442628 if (delegate())
2629 delegate()->WorkerCrashed();
[email protected]7d472472011-01-22 01:30:252630}
2631
[email protected]420ae012009-04-24 05:16:322632void TabContents::BeforeUnloadFiredFromRenderManager(
2633 bool proceed,
2634 bool* proceed_to_fire_unload) {
2635 if (delegate())
2636 delegate()->BeforeUnloadFired(this, proceed, proceed_to_fire_unload);
2637}
2638
[email protected]3a3d47472010-07-15 21:03:542639void TabContents::DidStartLoadingFromRenderManager(
2640 RenderViewHost* render_view_host) {
2641 DidStartLoading();
2642}
2643
2644void TabContents::RenderViewGoneFromRenderManager(
2645 RenderViewHost* render_view_host) {
[email protected]443b80e2010-12-14 00:42:232646 DCHECK(crashed_status_ != base::TERMINATION_STATUS_STILL_RUNNING);
2647 RenderViewGone(render_view_host, crashed_status_, crashed_error_code_);
[email protected]3a3d47472010-07-15 21:03:542648}
2649
[email protected]420ae012009-04-24 05:16:322650void TabContents::UpdateRenderViewSizeForRenderManager() {
2651 // TODO(brettw) this is a hack. See TabContentsView::SizeContents.
[email protected]0323ee42010-02-17 22:03:262652 gfx::Size size = view_->GetContainerSize();
2653 // 0x0 isn't a valid window size (minimal window size is 1x1) but it may be
2654 // here during container initialization and normal window size will be set
2655 // later. In case of tab duplication this resizing to 0x0 prevents setting
2656 // normal size later so just ignore it.
2657 if (!size.IsEmpty())
2658 view_->SizeContents(size);
[email protected]420ae012009-04-24 05:16:322659}
2660
[email protected]3a3d47472010-07-15 21:03:542661void TabContents::NotifySwappedFromRenderManager() {
2662 NotifySwapped();
2663}
2664
2665NavigationController& TabContents::GetControllerForRenderManager() {
2666 return controller();
2667}
2668
[email protected]d0980792011-02-13 19:41:402669WebUI* TabContents::CreateWebUIForRenderManager(const GURL& url) {
[email protected]80a8fad2011-01-29 04:02:382670 return WebUIFactory::CreateWebUIForURL(this, url);
[email protected]420ae012009-04-24 05:16:322671}
2672
2673NavigationEntry*
2674TabContents::GetLastCommittedNavigationEntryForRenderManager() {
2675 return controller_.GetLastCommittedEntry();
2676}
2677
2678bool TabContents::CreateRenderViewForRenderManager(
2679 RenderViewHost* render_view_host) {
[email protected]420ae012009-04-24 05:16:322680 RenderWidgetHostView* rwh_view = view_->CreateViewForWidget(render_view_host);
[email protected]1a98a932009-11-17 00:12:522681
[email protected]674b3822010-08-04 04:02:512682 if (!render_view_host->CreateRenderView(string16()))
[email protected]420ae012009-04-24 05:16:322683 return false;
2684
2685 // Now that the RenderView has been created, we need to tell it its size.
2686 rwh_view->SetSize(view_->GetContainerSize());
2687
2688 UpdateMaxPageIDIfNecessary(render_view_host->site_instance(),
2689 render_view_host);
2690 return true;
2691}
2692
2693void TabContents::Observe(NotificationType type,
2694 const NotificationSource& source,
2695 const NotificationDetails& details) {
2696 switch (type.value) {
[email protected]420ae012009-04-24 05:16:322697 case NotificationType::PREF_CHANGED: {
[email protected]57ecc4b2010-08-11 03:02:512698 std::string* pref_name_in = Details<std::string>(details).ptr();
[email protected]420ae012009-04-24 05:16:322699 DCHECK(Source<PrefService>(source).ptr() == profile()->GetPrefs());
2700 if (*pref_name_in == prefs::kAlternateErrorPagesEnabled) {
2701 UpdateAlternateErrorPageURL();
[email protected]88e14382010-10-14 21:45:532702 } else if ((*pref_name_in == prefs::kDefaultCharset) ||
2703 StartsWithASCII(*pref_name_in, "webkit.webprefs.", true)) {
[email protected]420ae012009-04-24 05:16:322704 UpdateWebPreferences();
[email protected]d0b8d092010-10-25 04:05:172705 } else if (*pref_name_in == prefs::kDefaultZoomLevel) {
2706 UpdateZoomLevel();
[email protected]420ae012009-04-24 05:16:322707 } else {
2708 NOTREACHED() << "unexpected pref change notification" << *pref_name_in;
2709 }
2710 break;
2711 }
2712 case NotificationType::RENDER_WIDGET_HOST_DESTROYED:
2713 view_->RenderWidgetHostDestroyed(Source<RenderWidgetHost>(source).ptr());
2714 break;
2715
2716 case NotificationType::NAV_ENTRY_COMMITTED: {
2717 DCHECK(&controller_ == Source<NavigationController>(source).ptr());
2718
2719 NavigationController::LoadCommittedDetails& committed_details =
2720 *(Details<NavigationController::LoadCommittedDetails>(details).ptr());
2721 ExpireInfoBars(committed_details);
2722 break;
2723 }
2724
[email protected]d3ba77272009-09-03 00:06:092725#if defined(OS_LINUX)
2726 case NotificationType::BROWSER_THEME_CHANGED: {
[email protected]7ff431e2010-01-07 18:18:542727 renderer_preferences_util::UpdateFromSystemSettings(
2728 &renderer_preferences_, profile());
[email protected]d3ba77272009-09-03 00:06:092729 render_view_host()->SyncRendererPrefs();
2730 break;
2731 }
2732#endif
2733
[email protected]6c32ce72010-03-08 05:18:062734 case NotificationType::USER_STYLE_SHEET_UPDATED:
2735 UpdateWebPreferences();
2736 break;
2737
[email protected]79580c62010-02-02 02:36:252738 case NotificationType::CONTENT_SETTINGS_CHANGED: {
[email protected]5f24f212010-12-02 14:47:452739 Details<const ContentSettingsDetails> settings_details(details);
[email protected]79580c62010-02-02 02:36:252740 NavigationEntry* entry = controller_.GetActiveEntry();
[email protected]da0c8e92010-02-09 23:25:492741 GURL entry_url;
[email protected]0314ae02010-04-08 09:18:292742 if (entry)
[email protected]da0c8e92010-02-09 23:25:492743 entry_url = entry->url();
[email protected]0314ae02010-04-08 09:18:292744 if (settings_details.ptr()->update_all() ||
2745 settings_details.ptr()->pattern().Matches(entry_url)) {
2746 render_view_host()->SendContentSettings(entry_url,
[email protected]b9a4ea7f2010-06-25 07:11:232747 profile()->GetHostContentSettingsMap()->
2748 GetContentSettings(entry_url));
[email protected]79580c62010-02-02 02:36:252749 }
2750 break;
2751 }
2752
[email protected]e5adff72010-06-14 18:13:062753 case NotificationType::EXTENSION_LOADED:
[email protected]a888b29e62010-04-01 13:38:572754 break;
[email protected]a888b29e62010-04-01 13:38:572755
2756 case NotificationType::EXTENSION_UNLOADED:
[email protected]a888b29e62010-04-01 13:38:572757 break;
[email protected]a888b29e62010-04-01 13:38:572758
[email protected]88152052010-10-01 04:05:182759 case NotificationType::GOOGLE_URL_UPDATED:
2760 UpdateAlternateErrorPageURL();
2761 break;
2762
[email protected]420ae012009-04-24 05:16:322763 default:
2764 NOTREACHED();
2765 }
2766}
[email protected]aacd7cc2009-09-02 14:51:102767
[email protected]9adb9692010-10-29 23:14:022768void TabContents::UpdateExtensionAppIcon(const Extension* extension) {
[email protected]98aa0b52010-05-06 17:03:082769 extension_app_icon_.reset();
[email protected]f075fe0a22010-04-26 04:06:432770
[email protected]a888b29e62010-04-01 13:38:572771 if (extension) {
[email protected]98aa0b52010-05-06 17:03:082772 extension_app_image_loader_.reset(new ImageLoadingTracker(this));
2773 extension_app_image_loader_->LoadImage(
[email protected]a888b29e62010-04-01 13:38:572774 extension,
[email protected]807871f2010-09-16 01:04:482775 extension->GetIconResource(Extension::EXTENSION_ICON_SMALLISH,
2776 ExtensionIconSet::MATCH_EXACTLY),
[email protected]a888b29e62010-04-01 13:38:572777 gfx::Size(Extension::EXTENSION_ICON_SMALLISH,
2778 Extension::EXTENSION_ICON_SMALLISH),
2779 ImageLoadingTracker::CACHE);
2780 } else {
[email protected]98aa0b52010-05-06 17:03:082781 extension_app_image_loader_.reset(NULL);
[email protected]a888b29e62010-04-01 13:38:572782 }
2783}
2784
[email protected]9adb9692010-10-29 23:14:022785const Extension* TabContents::GetExtensionContaining(const GURL& url) {
[email protected]eaa7dd182010-12-14 11:09:002786 ExtensionService* extensions_service = profile()->GetExtensionService();
[email protected]a888b29e62010-04-01 13:38:572787 if (!extensions_service)
2788 return NULL;
2789
[email protected]9adb9692010-10-29 23:14:022790 const Extension* extension = extensions_service->GetExtensionByURL(url);
[email protected]a888b29e62010-04-01 13:38:572791 return extension ?
2792 extension : extensions_service->GetExtensionByWebExtent(url);
2793}
2794
[email protected]a888b29e62010-04-01 13:38:572795void TabContents::OnImageLoaded(SkBitmap* image, ExtensionResource resource,
2796 int index) {
2797 if (image) {
[email protected]98aa0b52010-05-06 17:03:082798 extension_app_icon_ = *image;
[email protected]a888b29e62010-04-01 13:38:572799 NotifyNavigationStateChanged(INVALIDATE_TAB);
2800 }
2801}
2802
[email protected]beb440c2009-11-06 04:08:542803gfx::NativeWindow TabContents::GetMessageBoxRootWindow() {
2804 return view_->GetTopLevelNativeWindow();
2805}
2806
2807void TabContents::OnMessageBoxClosed(IPC::Message* reply_msg,
2808 bool success,
2809 const std::wstring& prompt) {
2810 last_javascript_message_dismissal_ = base::TimeTicks::Now();
2811 if (is_showing_before_unload_dialog_ && !success) {
2812 // If a beforeunload dialog is canceled, we need to stop the throbber from
2813 // spinning, since we forced it to start spinning in Navigate.
2814 DidStopLoading();
2815
2816 tab_close_start_time_ = base::TimeTicks();
2817 }
2818 is_showing_before_unload_dialog_ = false;
2819 render_view_host()->JavaScriptMessageBoxClosed(reply_msg, success, prompt);
2820}
2821
2822void TabContents::SetSuppressMessageBoxes(bool suppress_message_boxes) {
2823 set_suppress_javascript_messages(suppress_message_boxes);
2824}
2825
[email protected]3a3d47472010-07-15 21:03:542826TabContents* TabContents::AsTabContents() {
2827 return this;
2828}
2829
2830ExtensionHost* TabContents::AsExtensionHost() {
2831 return NULL;
2832}
2833
[email protected]41fc0322009-09-04 22:23:402834void TabContents::set_encoding(const std::string& encoding) {
[email protected]aacd7cc2009-09-02 14:51:102835 encoding_ = CharacterEncoding::GetCanonicalEncodingNameByAliasName(encoding);
2836}
[email protected]f45d2a72010-03-08 23:28:352837
2838void TabContents::SetAppIcon(const SkBitmap& app_icon) {
2839 app_icon_ = app_icon;
2840 NotifyNavigationStateChanged(INVALIDATE_TITLE);
2841}
[email protected]33f74972010-12-08 16:40:362842
2843void TabContents::SwapInRenderViewHost(RenderViewHost* rvh) {
2844 render_manager_.SwapInRenderViewHost(rvh);
2845}
2846
2847void TabContents::CreateViewAndSetSizeForRVH(RenderViewHost* rvh) {
2848 RenderWidgetHostView* rwh_view = view()->CreateViewForWidget(rvh);
2849 rwh_view->SetSize(view()->GetContainerSize());
2850}
[email protected]e7d50892011-01-19 21:47:382851
2852bool TabContents::MaybeUsePreloadedPage(const GURL& url) {
[email protected]4c154ff82011-02-15 11:23:592853 prerender::PrerenderManager* pm = profile()->GetPrerenderManager();
[email protected]e7d50892011-01-19 21:47:382854 if (pm != NULL) {
[email protected]973407b2011-01-26 23:18:232855 if (pm->MaybeUsePreloadedPage(this, url))
[email protected]e7d50892011-01-19 21:47:382856 return true;
[email protected]e7d50892011-01-19 21:47:382857 }
2858 return false;
2859}