| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/tab_contents/tab_contents.h" | 5 #include "chrome/browser/ui/tab_contents/tab_contents.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/lazy_instance.h" | 8 #include "base/lazy_instance.h" |
| 9 #include "chrome/browser/autofill/autocomplete_history_manager.h" | 9 #include "chrome/browser/autofill/autocomplete_history_manager.h" |
| 10 #include "chrome/browser/autofill/autofill_external_delegate.h" | 10 #include "chrome/browser/autofill/autofill_external_delegate.h" |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 #include "chrome/common/chrome_notification_types.h" | 57 #include "chrome/common/chrome_notification_types.h" |
| 58 #include "chrome/common/chrome_switches.h" | 58 #include "chrome/common/chrome_switches.h" |
| 59 #include "chrome/common/thumbnail_support.h" | 59 #include "chrome/common/thumbnail_support.h" |
| 60 #include "content/public/browser/notification_service.h" | 60 #include "content/public/browser/notification_service.h" |
| 61 #include "content/public/browser/web_contents.h" | 61 #include "content/public/browser/web_contents.h" |
| 62 | 62 |
| 63 using content::WebContents; | 63 using content::WebContents; |
| 64 | 64 |
| 65 namespace { | 65 namespace { |
| 66 | 66 |
| 67 static base::LazyInstance<base::PropertyAccessor<TabContents*> > | 67 const char kTabContentsUserDataKey[] = "TabContentsUserData"; |
| 68 g_tab_contents_property_accessor = LAZY_INSTANCE_INITIALIZER; | 68 |
| 69 class TabContentsUserData : public base::SupportsUserData::Data { |
| 70 public: |
| 71 explicit TabContentsUserData(TabContents* tab_contents) |
| 72 : tab_contents_(tab_contents) {} |
| 73 virtual ~TabContentsUserData() {} |
| 74 TabContents* tab_contents() { return tab_contents_; } |
| 75 |
| 76 private: |
| 77 TabContents* tab_contents_; // unowned |
| 78 }; |
| 69 | 79 |
| 70 } // namespace | 80 } // namespace |
| 71 | 81 |
| 72 //////////////////////////////////////////////////////////////////////////////// | 82 //////////////////////////////////////////////////////////////////////////////// |
| 73 // TabContents, public: | 83 // TabContents, public: |
| 74 | 84 |
| 75 TabContents::TabContents(WebContents* contents) | 85 TabContents::TabContents(WebContents* contents) |
| 76 : content::WebContentsObserver(contents), | 86 : content::WebContentsObserver(contents), |
| 77 in_destructor_(false), | 87 in_destructor_(false), |
| 78 web_contents_(contents) { | 88 web_contents_(contents) { |
| 79 DCHECK(contents); | 89 DCHECK(contents); |
| 80 DCHECK(!FromWebContents(contents)); | 90 DCHECK(!FromWebContents(contents)); |
| 81 | 91 |
| 82 chrome::SetViewType(contents, chrome::VIEW_TYPE_TAB_CONTENTS); | 92 chrome::SetViewType(contents, chrome::VIEW_TYPE_TAB_CONTENTS); |
| 83 | 93 |
| 84 // Stash this in the property bag so it can be retrieved without having to | 94 // Stash this in the WebContents. |
| 85 // go to a Browser. | 95 contents->SetUserData(&kTabContentsUserDataKey, |
| 86 property_accessor()->SetProperty(contents->GetPropertyBag(), this); | 96 new TabContentsUserData(this)); |
| 87 | 97 |
| 88 // Create the tab helpers. | 98 // Create the tab helpers. |
| 89 // restore_tab_helper because it sets up the tab ID, and other helpers may | 99 // restore_tab_helper because it sets up the tab ID, and other helpers may |
| 90 // rely on that. | 100 // rely on that. |
| 91 restore_tab_helper_.reset(new RestoreTabHelper(contents)); | 101 restore_tab_helper_.reset(new RestoreTabHelper(contents)); |
| 92 | 102 |
| 93 autocomplete_history_manager_.reset(new AutocompleteHistoryManager(contents)); | 103 autocomplete_history_manager_.reset(new AutocompleteHistoryManager(contents)); |
| 94 autofill_manager_ = new AutofillManager(this); | 104 autofill_manager_ = new AutofillManager(this); |
| 95 if (CommandLine::ForCurrentProcess()->HasSwitch( | 105 if (CommandLine::ForCurrentProcess()->HasSwitch( |
| 96 switches::kExternalAutofillPopup)) { | 106 switches::kExternalAutofillPopup)) { |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 198 content::NotificationService::current()->Notify( | 208 content::NotificationService::current()->Notify( |
| 199 chrome::NOTIFICATION_TAB_CONTENTS_DESTROYED, | 209 chrome::NOTIFICATION_TAB_CONTENTS_DESTROYED, |
| 200 content::Source<TabContents>(this), | 210 content::Source<TabContents>(this), |
| 201 content::NotificationService::NoDetails()); | 211 content::NotificationService::NoDetails()); |
| 202 | 212 |
| 203 // Need to tear down infobars before the WebContents goes away. | 213 // Need to tear down infobars before the WebContents goes away. |
| 204 // TODO(avi): Can we get this handled by the tab helper itself? | 214 // TODO(avi): Can we get this handled by the tab helper itself? |
| 205 infobar_tab_helper_.reset(); | 215 infobar_tab_helper_.reset(); |
| 206 } | 216 } |
| 207 | 217 |
| 208 base::PropertyAccessor<TabContents*>* TabContents::property_accessor() { | |
| 209 return g_tab_contents_property_accessor.Pointer(); | |
| 210 } | |
| 211 | |
| 212 TabContents* TabContents::Clone() { | 218 TabContents* TabContents::Clone() { |
| 213 WebContents* new_web_contents = web_contents()->Clone(); | 219 WebContents* new_web_contents = web_contents()->Clone(); |
| 214 TabContents* new_tab_contents = new TabContents(new_web_contents); | 220 TabContents* new_tab_contents = new TabContents(new_web_contents); |
| 215 | 221 |
| 216 // TODO(avi): Can we generalize this so that knowledge of the functionings of | 222 // TODO(avi): Can we generalize this so that knowledge of the functionings of |
| 217 // the tab helpers isn't required here? | 223 // the tab helpers isn't required here? |
| 218 new_tab_contents->extension_tab_helper()->CopyStateFrom( | 224 new_tab_contents->extension_tab_helper()->CopyStateFrom( |
| 219 *extension_tab_helper_.get()); | 225 *extension_tab_helper_.get()); |
| 220 return new_tab_contents; | 226 return new_tab_contents; |
| 221 } | 227 } |
| 222 | 228 |
| 223 // static | 229 // static |
| 224 TabContents* TabContents::FromWebContents(WebContents* contents) { | 230 TabContents* TabContents::FromWebContents(WebContents* contents) { |
| 225 TabContents** tab_contents = | 231 TabContentsUserData* user_data = static_cast<TabContentsUserData*>( |
| 226 property_accessor()->GetProperty(contents->GetPropertyBag()); | 232 contents->GetUserData(&kTabContentsUserDataKey)); |
| 227 | 233 |
| 228 return tab_contents ? *tab_contents : NULL; | 234 return user_data ? user_data->tab_contents() : NULL; |
| 229 } | 235 } |
| 230 | 236 |
| 231 // static | 237 // static |
| 232 const TabContents* TabContents::FromWebContents(const WebContents* contents) { | 238 const TabContents* TabContents::FromWebContents(const WebContents* contents) { |
| 233 TabContents* const* tab_contents = | 239 TabContentsUserData* user_data = static_cast<TabContentsUserData*>( |
| 234 property_accessor()->GetProperty(contents->GetPropertyBag()); | 240 contents->GetUserData(&kTabContentsUserDataKey)); |
| 235 | 241 |
| 236 return tab_contents ? *tab_contents : NULL; | 242 return user_data ? user_data->tab_contents() : NULL; |
| 237 } | 243 } |
| 238 | 244 |
| 239 WebContents* TabContents::web_contents() const { | 245 WebContents* TabContents::web_contents() const { |
| 240 return web_contents_.get(); | 246 return web_contents_.get(); |
| 241 } | 247 } |
| 242 | 248 |
| 243 Profile* TabContents::profile() const { | 249 Profile* TabContents::profile() const { |
| 244 return Profile::FromBrowserContext(web_contents()->GetBrowserContext()); | 250 return Profile::FromBrowserContext(web_contents()->GetBrowserContext()); |
| 245 } | 251 } |
| 246 | 252 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 258 #if defined(ENABLE_SESSION_SERVICE) | 264 #if defined(ENABLE_SESSION_SERVICE) |
| 259 SessionService* session = | 265 SessionService* session = |
| 260 SessionServiceFactory::GetForProfile(profile()); | 266 SessionServiceFactory::GetForProfile(profile()); |
| 261 if (session) { | 267 if (session) { |
| 262 session->SetTabUserAgentOverride(restore_tab_helper()->window_id(), | 268 session->SetTabUserAgentOverride(restore_tab_helper()->window_id(), |
| 263 restore_tab_helper()->session_id(), | 269 restore_tab_helper()->session_id(), |
| 264 user_agent); | 270 user_agent); |
| 265 } | 271 } |
| 266 #endif | 272 #endif |
| 267 } | 273 } |
| OLD | NEW |