diff options
author | avi@chromium.org <avi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-08-23 00:03:11 +0000 |
---|---|---|
committer | avi@chromium.org <avi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-08-23 00:03:11 +0000 |
commit | 36a22c4003317b933133d474f49cb5f97ed37e90 (patch) | |
tree | cd4ef16c2bbf798a73b1dda6058dde7c10d02415 | |
parent | 7cee190f9eaf5b9715057b2c66d8804fa34ed442 (diff) | |
download | chromium_src-36a22c4003317b933133d474f49cb5f97ed37e90.zip chromium_src-36a22c4003317b933133d474f49cb5f97ed37e90.tar.gz chromium_src-36a22c4003317b933133d474f49cb5f97ed37e90.tar.bz2 |
Kill PropertyBag, switch WebContents to SupportsUserData.
BUG=141177
TEST=no visible change
Review URL: https://chromiumcodereview.appspot.com/10831407
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@152882 0039d316-1c4b-4281-b951-d872f2087c98
26 files changed, 231 insertions, 511 deletions
diff --git a/base/base.gyp b/base/base.gyp index 7db29d4..f620ef0 100644 --- a/base/base.gyp +++ b/base/base.gyp @@ -453,7 +453,6 @@ 'process_util_unittest_mac.h', 'process_util_unittest_mac.mm', 'profiler/tracked_time_unittest.cc', - 'property_bag_unittest.cc', 'rand_util_unittest.cc', 'scoped_native_library_unittest.cc', 'scoped_observer.h', diff --git a/base/base.gypi b/base/base.gypi index 1ba5daa..7d25444 100644 --- a/base/base.gypi +++ b/base/base.gypi @@ -312,8 +312,6 @@ 'profiler/alternate_timer.h', 'profiler/tracked_time.cc', 'profiler/tracked_time.h', - 'property_bag.cc', - 'property_bag.h', 'rand_util.cc', 'rand_util.h', 'rand_util_nacl.cc', diff --git a/base/property_bag.cc b/base/property_bag.cc deleted file mode 100644 index 9578e64..0000000 --- a/base/property_bag.cc +++ /dev/null @@ -1,61 +0,0 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "base/property_bag.h" - -#include "base/memory/linked_ptr.h" - -namespace base { - -PropertyBag::PropertyBag() { -} - -PropertyBag::PropertyBag(const PropertyBag& other) { - operator=(other); -} - -PropertyBag::~PropertyBag() { -} - -PropertyBag& PropertyBag::operator=(const PropertyBag& other) { - props_.clear(); - - // We need to make copies of each property using the virtual copy() method. - for (PropertyMap::const_iterator i = other.props_.begin(); - i != other.props_.end(); ++i) - props_[i->first] = linked_ptr<Prop>(i->second->copy()); - return *this; -} - -void PropertyBag::SetProperty(PropID id, Prop* prop) { - props_[id] = linked_ptr<Prop>(prop); -} - -PropertyBag::Prop* PropertyBag::GetProperty(PropID id) { - PropertyMap::const_iterator found = props_.find(id); - if (found == props_.end()) - return NULL; - return found->second.get(); -} - -const PropertyBag::Prop* PropertyBag::GetProperty(PropID id) const { - PropertyMap::const_iterator found = props_.find(id); - if (found == props_.end()) - return NULL; - return found->second.get(); -} - -void PropertyBag::DeleteProperty(PropID id) { - PropertyMap::iterator found = props_.find(id); - if (found == props_.end()) - return; // Not found, nothing to do. - props_.erase(found); -} - -PropertyAccessorBase::PropertyAccessorBase() { - static PropertyBag::PropID next_id = 1; - prop_id_ = next_id++; -} - -} // namespace base diff --git a/base/property_bag.h b/base/property_bag.h deleted file mode 100644 index 16c20fa..0000000 --- a/base/property_bag.h +++ /dev/null @@ -1,179 +0,0 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef BASE_PROPERTY_BAG_H_ -#define BASE_PROPERTY_BAG_H_ - -#include <map> - -#include "base/basictypes.h" -#include "base/base_export.h" - -template <typename T> class linked_ptr; - -namespace base { - -class PropertyAccessorBase; - -// A property bag holds a generalized list of arbitrary metadata called -// properties. Each property is a class type derived from PropertyBag::Prop -// that can be set and retrieved. -// -// The property bag is not read or written directly. Instead, callers go -// through a PropertyAccessor. The Accessor generates the unique IDs that -// identify different properties. The Accessor is templatized to also provide -// typesafety to the callers. -// -// Example: -// // Note: you don't want to use Singleton for your Accessor if you're using -// // a simple type like int or string as the data, since it will enforce that -// // there is only one singleton for that type, which will conflict. If -// // you're putting in some struct that's unique to you, go ahead. -// PropertyAccessor<int>* my_accessor() const { -// static PropertyAccessor<int>* accessor = NULL; -// if (!accessor) accessor = new PropertyAccessor<int>; -// return accessor; -// } -// -// void doit(SomeObjectThatImplementsPropertyBag* object) { -// PropertyAccessor<int>* accessor = my_accessor(); -// int* property = accessor->GetProperty(object); -// if (property) -// ... use property ... -// -// accessor->SetProperty(object, 22); -// } -class BASE_EXPORT PropertyBag { - public: - // The type that uniquely identifies a property type. - typedef int PropID; - enum { NULL_PROP_ID = -1 }; // Invalid property ID. - - // Properties are all derived from this class. They must be deletable and - // copyable - class Prop { - public: - virtual ~Prop() {} - - // Copies the property and returns a pointer to the new one. The caller is - // responsible for managing the lifetime. - virtual Prop* copy() = 0; - }; - - PropertyBag(); - PropertyBag(const PropertyBag& other); - virtual ~PropertyBag(); - - PropertyBag& operator=(const PropertyBag& other); - - private: - friend class PropertyAccessorBase; - - typedef std::map<PropID, linked_ptr<Prop> > PropertyMap; - - // Used by the PropertyAccessor to set the property with the given ID. - // Ownership of the given pointer will be transferred to us. Any existing - // property matching the given ID will be deleted. - void SetProperty(PropID id, Prop* prop); - - // Used by the PropertyAccessor to retrieve the property with the given ID. - // The returned pointer will be NULL if there is no match. Ownership of the - // pointer will stay with the property bag. - Prop* GetProperty(PropID id); - const Prop* GetProperty(PropID id) const; - - // Deletes the property with the given ID from the bag if it exists. - void DeleteProperty(PropID id); - - PropertyMap props_; - - // Copy and assign is explicitly allowed for this class. -}; - -// PropertyAccessorBase ------------------------------------------------------- - -// Manages getting the unique IDs to identify a property. Callers should use -// PropertyAccessor below instead. -class BASE_EXPORT PropertyAccessorBase { - public: - PropertyAccessorBase(); - virtual ~PropertyAccessorBase() {} - - // Removes our property, if any, from the given property bag. - void DeleteProperty(PropertyBag* bag) { - bag->DeleteProperty(prop_id_); - } - - protected: - void SetPropertyInternal(PropertyBag* bag, PropertyBag::Prop* prop) { - bag->SetProperty(prop_id_, prop); - } - PropertyBag::Prop* GetPropertyInternal(PropertyBag* bag) { - return bag->GetProperty(prop_id_); - } - const PropertyBag::Prop* GetPropertyInternal(const PropertyBag* bag) const { - return bag->GetProperty(prop_id_); - } - - private: - // Identifier for this property. - PropertyBag::PropID prop_id_; - - DISALLOW_COPY_AND_ASSIGN(PropertyAccessorBase); -}; - -// PropertyAccessor ----------------------------------------------------------- - -// Provides typesafe accessor functions for a property bag, and manages the -// unique identifiers for properties via the PropertyAccessorBase. -template<class T> -class PropertyAccessor : public PropertyAccessorBase { - public: - PropertyAccessor() : PropertyAccessorBase() {} - virtual ~PropertyAccessor() {} - - // Makes a copy of the |prop| object for storage. - void SetProperty(PropertyBag* bag, const T& prop) { - SetPropertyInternal(bag, new Container(prop)); - } - - // Returns our property in the given bag or NULL if there is no match. The - // returned pointer's ownership will stay with the property bag. - T* GetProperty(PropertyBag* bag) { - PropertyBag::Prop* prop = GetPropertyInternal(bag); - if (!prop) - return NULL; - return static_cast<Container*>(prop)->get(); - } - const T* GetProperty(const PropertyBag* bag) const { - const PropertyBag::Prop* prop = GetPropertyInternal(bag); - if (!prop) - return NULL; - return static_cast<const Container*>(prop)->get(); - } - - // See also DeleteProperty on thn PropertyAccessorBase. - - private: - class Container : public PropertyBag::Prop { - public: - explicit Container(const T& data) : data_(data) {} - - T* get() { return &data_; } - const T* get() const { return &data_; } - - private: - virtual Prop* copy() { - return new Container(data_); - } - - T data_; - }; - - DISALLOW_COPY_AND_ASSIGN(PropertyAccessor); -}; - -} // namespace base - -#endif // BASE_PROPERTY_BAG_H_ diff --git a/base/property_bag_unittest.cc b/base/property_bag_unittest.cc deleted file mode 100644 index b267d04..0000000 --- a/base/property_bag_unittest.cc +++ /dev/null @@ -1,66 +0,0 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "base/property_bag.h" -#include "testing/gtest/include/gtest/gtest.h" - -namespace base { - -TEST(PropertyBagTest, AddQueryRemove) { - PropertyBag bag; - PropertyAccessor<int> adaptor; - - // Should be no match initially. - EXPECT_TRUE(adaptor.GetProperty(&bag) == NULL); - - // Add the value and make sure we get it back. - const int kFirstValue = 1; - adaptor.SetProperty(&bag, kFirstValue); - ASSERT_TRUE(adaptor.GetProperty(&bag)); - EXPECT_EQ(kFirstValue, *adaptor.GetProperty(&bag)); - - // Set it to a new value. - const int kSecondValue = 2; - adaptor.SetProperty(&bag, kSecondValue); - ASSERT_TRUE(adaptor.GetProperty(&bag)); - EXPECT_EQ(kSecondValue, *adaptor.GetProperty(&bag)); - - // Remove the value and make sure it's gone. - adaptor.DeleteProperty(&bag); - EXPECT_TRUE(adaptor.GetProperty(&bag) == NULL); -} - -TEST(PropertyBagTest, Copy) { - PropertyAccessor<int> adaptor1; - PropertyAccessor<double> adaptor2; - - // Create a bag with property type 1 in it. - PropertyBag copy; - adaptor1.SetProperty(©, 22); - - const int kType1Value = 10; - const double kType2Value = 2.7; - { - // Create a bag with property types 1 and 2 in it. - PropertyBag initial; - adaptor1.SetProperty(&initial, kType1Value); - adaptor2.SetProperty(&initial, kType2Value); - - // Assign to the original. - copy = initial; - } - - // Verify the copy got the two properties. - ASSERT_TRUE(adaptor1.GetProperty(©)); - ASSERT_TRUE(adaptor2.GetProperty(©)); - EXPECT_EQ(kType1Value, *adaptor1.GetProperty(©)); - EXPECT_EQ(kType2Value, *adaptor2.GetProperty(©)); - - // Clear it out, neither property should be left. - copy = PropertyBag(); - EXPECT_TRUE(adaptor1.GetProperty(©) == NULL); - EXPECT_TRUE(adaptor2.GetProperty(©) == NULL); -} - -} // namespace base diff --git a/chrome/browser/ui/cocoa/omnibox/omnibox_view_mac.mm b/chrome/browser/ui/cocoa/omnibox/omnibox_view_mac.mm index 8921e00..b38c503 100644 --- a/chrome/browser/ui/cocoa/omnibox/omnibox_view_mac.mm +++ b/chrome/browser/ui/cocoa/omnibox/omnibox_view_mac.mm @@ -6,7 +6,6 @@ #include <Carbon/Carbon.h> // kVK_Return -#include "base/property_bag.h" #include "base/string_util.h" #include "base/sys_string_conversions.h" #include "base/utf_string_conversions.h" @@ -85,8 +84,10 @@ NSColor* SecurityErrorSchemeColor() { return ColorWithRGBBytes(0xa2, 0x00, 0x00); } +const char kOmniboxViewMacStateKey[] = "OmniboxViewMacState"; + // Store's the model and view state across tab switches. -struct OmniboxViewMacState { +struct OmniboxViewMacState : public base::SupportsUserData::Data { OmniboxViewMacState(const OmniboxEditModel::State model_state, const bool has_focus, const NSRange& selection) @@ -94,29 +95,21 @@ struct OmniboxViewMacState { has_focus(has_focus), selection(selection) { } + virtual ~OmniboxViewMacState() {} const OmniboxEditModel::State model_state; const bool has_focus; const NSRange selection; }; -// Returns a lazily initialized property bag accessor for saving our -// state in a WebContents. When constructed |accessor| generates a -// globally-unique id used to index into the per-tab PropertyBag used -// to store the state data. -base::PropertyAccessor<OmniboxViewMacState>* GetStateAccessor() { - CR_DEFINE_STATIC_LOCAL( - base::PropertyAccessor<OmniboxViewMacState>, accessor, ()); - return &accessor; -} - // Accessors for storing and getting the state from the tab. void StoreStateToTab(WebContents* tab, - const OmniboxViewMacState& state) { - GetStateAccessor()->SetProperty(tab->GetPropertyBag(), state); + OmniboxViewMacState* state) { + tab->SetUserData(kOmniboxViewMacStateKey, state); } const OmniboxViewMacState* GetStateFromTab(const WebContents* tab) { - return GetStateAccessor()->GetProperty(tab->GetPropertyBag()); + return static_cast<OmniboxViewMacState*>( + tab->GetUserData(&kOmniboxViewMacStateKey)); } // Helper to make converting url_parse ranges to NSRange easier to @@ -187,7 +180,8 @@ void OmniboxViewMac::SaveStateToTab(WebContents* tab) { range = NSMakeRange(0, GetTextLength()); } - OmniboxViewMacState state(model()->GetStateForTabSwitch(), hasFocus, range); + OmniboxViewMacState* state = + new OmniboxViewMacState(model()->GetStateForTabSwitch(), hasFocus, range); StoreStateToTab(tab, state); } diff --git a/chrome/browser/ui/cocoa/web_dialog_window_controller.mm b/chrome/browser/ui/cocoa/web_dialog_window_controller.mm index 733154fe..805c29a 100644 --- a/chrome/browser/ui/cocoa/web_dialog_window_controller.mm +++ b/chrome/browser/ui/cocoa/web_dialog_window_controller.mm @@ -6,7 +6,6 @@ #include "base/logging.h" #include "base/memory/scoped_nsobject.h" -#include "base/property_bag.h" #include "base/sys_string_conversions.h" #import "chrome/browser/ui/browser_dialogs.h" #import "chrome/browser/ui/cocoa/browser_command_executor.h" @@ -346,8 +345,7 @@ void WebDialogWindowDelegateBridge::HandleKeyboardEvent( // This must be done before loading the page; see the comments in // WebDialogUI. - WebDialogUI::GetPropertyAccessor().SetProperty( - tabContents_->web_contents()->GetPropertyBag(), delegate_.get()); + WebDialogUI::SetDelegate(tabContents_->web_contents(), delegate_.get()); tabContents_->web_contents()->GetController().LoadURL( delegate_->GetDialogContentURL(), diff --git a/chrome/browser/ui/gtk/omnibox/omnibox_view_gtk.cc b/chrome/browser/ui/gtk/omnibox/omnibox_view_gtk.cc index 71d6ec7..c0a3256 100644 --- a/chrome/browser/ui/gtk/omnibox/omnibox_view_gtk.cc +++ b/chrome/browser/ui/gtk/omnibox/omnibox_view_gtk.cc @@ -10,7 +10,6 @@ #include <algorithm> #include "base/logging.h" -#include "base/property_bag.h" #include "base/string_util.h" #include "base/utf_string_conversion_utils.h" #include "base/utf_string_conversions.h" @@ -89,25 +88,20 @@ struct ViewState { OmniboxViewGtk::CharRange selection_range; }; -struct AutocompleteEditState { +const char kAutocompleteEditStateKey[] = "AutocompleteEditState"; + +struct AutocompleteEditState : public base::SupportsUserData::Data { AutocompleteEditState(const OmniboxEditModel::State& model_state, const ViewState& view_state) : model_state(model_state), view_state(view_state) { } + virtual ~AutocompleteEditState() {} const OmniboxEditModel::State model_state; const ViewState view_state; }; -// Returns a lazily initialized property bag accessor for saving our state in a -// WebContents. -base::PropertyAccessor<AutocompleteEditState>* GetStateAccessor() { - CR_DEFINE_STATIC_LOCAL( - base::PropertyAccessor<AutocompleteEditState>, state, ()); - return &state; -} - // Set up style properties to override the default GtkTextView; if a theme has // overridden some of these properties, an inner-line will be displayed inside // the fake GtkTextEntry. @@ -427,9 +421,9 @@ void OmniboxViewGtk::SaveStateToTab(WebContents* tab) { SavePrimarySelection(selected_text_); // NOTE: GetStateForTabSwitch may affect GetSelection, so order is important. OmniboxEditModel::State model_state = model()->GetStateForTabSwitch(); - GetStateAccessor()->SetProperty( - tab->GetPropertyBag(), - AutocompleteEditState(model_state, ViewState(GetSelection()))); + tab->SetUserData( + kAutocompleteEditStateKey, + new AutocompleteEditState(model_state, ViewState(GetSelection()))); } void OmniboxViewGtk::Update(const WebContents* contents) { @@ -445,8 +439,8 @@ void OmniboxViewGtk::Update(const WebContents* contents) { if (contents) { selected_text_.clear(); RevertAll(); - const AutocompleteEditState* state = - GetStateAccessor()->GetProperty(contents->GetPropertyBag()); + const AutocompleteEditState* state = static_cast<AutocompleteEditState*>( + contents->GetUserData(&kAutocompleteEditStateKey)); if (state) { model()->RestoreState(state->model_state); diff --git a/chrome/browser/ui/gtk/web_dialog_gtk.cc b/chrome/browser/ui/gtk/web_dialog_gtk.cc index 409c35e..cd796ac 100644 --- a/chrome/browser/ui/gtk/web_dialog_gtk.cc +++ b/chrome/browser/ui/gtk/web_dialog_gtk.cc @@ -6,7 +6,6 @@ #include <gtk/gtk.h> -#include "base/property_bag.h" #include "base/utf_string_conversions.h" #include "chrome/browser/ui/browser_dialogs.h" #include "chrome/browser/ui/gtk/gtk_util.h" @@ -211,8 +210,7 @@ gfx::NativeWindow WebDialogGtk::InitDialog() { // This must be done before loading the page; see the comments in // WebDialogUI. - WebDialogUI::GetPropertyAccessor().SetProperty( - tab_->web_contents()->GetPropertyBag(), this); + WebDialogUI::SetDelegate(tab_->web_contents(), this); tab_->web_contents()->GetController().LoadURL( GetDialogContentURL(), diff --git a/chrome/browser/ui/tab_contents/tab_contents.cc b/chrome/browser/ui/tab_contents/tab_contents.cc index d1fdb6b..b905d5f 100644 --- a/chrome/browser/ui/tab_contents/tab_contents.cc +++ b/chrome/browser/ui/tab_contents/tab_contents.cc @@ -64,8 +64,18 @@ using content::WebContents; namespace { -static base::LazyInstance<base::PropertyAccessor<TabContents*> > - g_tab_contents_property_accessor = LAZY_INSTANCE_INITIALIZER; +const char kTabContentsUserDataKey[] = "TabContentsUserData"; + +class TabContentsUserData : public base::SupportsUserData::Data { + public: + explicit TabContentsUserData(TabContents* tab_contents) + : tab_contents_(tab_contents) {} + virtual ~TabContentsUserData() {} + TabContents* tab_contents() { return tab_contents_; } + + private: + TabContents* tab_contents_; // unowned +}; } // namespace @@ -81,9 +91,9 @@ TabContents::TabContents(WebContents* contents) chrome::SetViewType(contents, chrome::VIEW_TYPE_TAB_CONTENTS); - // Stash this in the property bag so it can be retrieved without having to - // go to a Browser. - property_accessor()->SetProperty(contents->GetPropertyBag(), this); + // Stash this in the WebContents. + contents->SetUserData(&kTabContentsUserDataKey, + new TabContentsUserData(this)); // Create the tab helpers. // restore_tab_helper because it sets up the tab ID, and other helpers may @@ -205,10 +215,6 @@ TabContents::~TabContents() { infobar_tab_helper_.reset(); } -base::PropertyAccessor<TabContents*>* TabContents::property_accessor() { - return g_tab_contents_property_accessor.Pointer(); -} - TabContents* TabContents::Clone() { WebContents* new_web_contents = web_contents()->Clone(); TabContents* new_tab_contents = new TabContents(new_web_contents); @@ -222,18 +228,18 @@ TabContents* TabContents::Clone() { // static TabContents* TabContents::FromWebContents(WebContents* contents) { - TabContents** tab_contents = - property_accessor()->GetProperty(contents->GetPropertyBag()); + TabContentsUserData* user_data = static_cast<TabContentsUserData*>( + contents->GetUserData(&kTabContentsUserDataKey)); - return tab_contents ? *tab_contents : NULL; + return user_data ? user_data->tab_contents() : NULL; } // static const TabContents* TabContents::FromWebContents(const WebContents* contents) { - TabContents* const* tab_contents = - property_accessor()->GetProperty(contents->GetPropertyBag()); + TabContentsUserData* user_data = static_cast<TabContentsUserData*>( + contents->GetUserData(&kTabContentsUserDataKey)); - return tab_contents ? *tab_contents : NULL; + return user_data ? user_data->tab_contents() : NULL; } WebContents* TabContents::web_contents() const { diff --git a/chrome/browser/ui/tab_contents/tab_contents.h b/chrome/browser/ui/tab_contents/tab_contents.h index c9f4f79..0ad429c 100644 --- a/chrome/browser/ui/tab_contents/tab_contents.h +++ b/chrome/browser/ui/tab_contents/tab_contents.h @@ -9,7 +9,6 @@ #include "base/compiler_specific.h" #include "base/memory/ref_counted.h" #include "base/memory/scoped_ptr.h" -#include "base/property_bag.h" #include "content/public/browser/web_contents_observer.h" class AlternateErrorPageTabObserver; @@ -258,10 +257,6 @@ class TabContents : public content::WebContentsObserver { virtual void UserAgentOverrideSet(const std::string& user_agent) OVERRIDE; private: - // Used to retrieve this object from |web_contents_|, which is placed in - // its property bag to avoid adding additional interfaces. - static base::PropertyAccessor<TabContents*>* property_accessor(); - // Tab Helpers --------------------------------------------------------------- // (These provide API for callers and have a getter function listed in the // "Tab Helpers" section in the member functions area, above.) diff --git a/chrome/browser/ui/tabs/tab_strip_model_unittest.cc b/chrome/browser/ui/tabs/tab_strip_model_unittest.cc index 0c38f9f..8c31189 100644 --- a/chrome/browser/ui/tabs/tab_strip_model_unittest.cc +++ b/chrome/browser/ui/tabs/tab_strip_model_unittest.cc @@ -11,7 +11,6 @@ #include "base/file_util.h" #include "base/memory/scoped_ptr.h" #include "base/path_service.h" -#include "base/property_bag.h" #include "base/stl_util.h" #include "base/string_number_conversions.h" #include "base/string_split.h" @@ -117,6 +116,22 @@ class TabStripDummyDelegate : public TestTabStripModelDelegate { DISALLOW_COPY_AND_ASSIGN(TabStripDummyDelegate); }; +namespace { + +const char kTabStripModelTestIDUserDataKey[] = "TabStripModelTestIDUserData"; + +class TabStripModelTestIDUserData : public base::SupportsUserData::Data { + public: + explicit TabStripModelTestIDUserData(int id) : id_(id) {} + virtual ~TabStripModelTestIDUserData() {} + int id() { return id_; } + + private: + int id_; +}; + +} // namespace + class TabStripModelTest : public ChromeRenderViewHostTestHarness { public: TabStripModelTest() : browser_thread_(BrowserThread::UI, &message_loop_) { @@ -157,12 +172,17 @@ class TabStripModelTest : public ChromeRenderViewHostTestHarness { // Sets the id of the specified contents. void SetID(WebContents* contents, int id) { - GetIDAccessor()->SetProperty(contents->GetPropertyBag(), id); + contents->SetUserData(&kTabStripModelTestIDUserDataKey, + new TabStripModelTestIDUserData(id)); } // Returns the id of the specified contents. int GetID(WebContents* contents) { - return *GetIDAccessor()->GetProperty(contents->GetPropertyBag()); + TabStripModelTestIDUserData* user_data = + static_cast<TabStripModelTestIDUserData*>( + contents->GetUserData(&kTabStripModelTestIDUserDataKey)); + + return user_data ? user_data->id() : -1; } // Returns the state of the given tab strip as a string. The state consists @@ -226,11 +246,6 @@ class TabStripModelTest : public ChromeRenderViewHostTestHarness { } private: - base::PropertyAccessor<int>* GetIDAccessor() { - static base::PropertyAccessor<int> accessor; - return &accessor; - } - content::TestBrowserThread browser_thread_; std::wstring test_dir_; diff --git a/chrome/browser/ui/views/omnibox/omnibox_view_views.cc b/chrome/browser/ui/views/omnibox/omnibox_view_views.cc index 1700973..04e95de 100644 --- a/chrome/browser/ui/views/omnibox/omnibox_view_views.cc +++ b/chrome/browser/ui/views/omnibox/omnibox_view_views.cc @@ -5,7 +5,6 @@ #include "chrome/browser/ui/views/omnibox/omnibox_view_views.h" #include "base/logging.h" -#include "base/property_bag.h" #include "base/string_util.h" #include "base/utf_string_conversions.h" #include "chrome/app/chrome_command_ids.h" @@ -161,25 +160,20 @@ struct ViewState { gfx::SelectionModel selection_model; }; -struct AutocompleteEditState { +const char kAutocompleteEditStateKey[] = "AutocompleteEditState"; + +struct AutocompleteEditState : public base::SupportsUserData::Data { AutocompleteEditState(const OmniboxEditModel::State& model_state, const ViewState& view_state) : model_state(model_state), view_state(view_state) { } + virtual ~AutocompleteEditState() {} const OmniboxEditModel::State model_state; const ViewState view_state; }; -// Returns a lazily initialized property bag accessor for saving our state in a -// WebContents. -base::PropertyAccessor<AutocompleteEditState>* GetStateAccessor() { - CR_DEFINE_STATIC_LOCAL( - base::PropertyAccessor<AutocompleteEditState>, state, ()); - return &state; -} - // A convenience method for applying URL styles. void ApplyURLStyle(views::Textfield* textfield, size_t start, @@ -481,9 +475,9 @@ void OmniboxViewViews::SaveStateToTab(WebContents* tab) { OmniboxEditModel::State model_state = model()->GetStateForTabSwitch(); gfx::SelectionModel selection; textfield_->GetSelectionModel(&selection); - GetStateAccessor()->SetProperty( - tab->GetPropertyBag(), - AutocompleteEditState(model_state, ViewState(selection))); + tab->SetUserData( + kAutocompleteEditStateKey, + new AutocompleteEditState(model_state, ViewState(selection))); } void OmniboxViewViews::Update(const WebContents* contents) { @@ -500,8 +494,8 @@ void OmniboxViewViews::Update(const WebContents* contents) { // for views-implementation. if (contents) { RevertAll(); - const AutocompleteEditState* state = - GetStateAccessor()->GetProperty(contents->GetPropertyBag()); + const AutocompleteEditState* state = static_cast<AutocompleteEditState*>( + contents->GetUserData(&kAutocompleteEditStateKey)); if (state) { model()->RestoreState(state->model_state); diff --git a/chrome/browser/ui/views/omnibox/omnibox_view_win.cc b/chrome/browser/ui/views/omnibox/omnibox_view_win.cc index 251076f..6f1df80 100644 --- a/chrome/browser/ui/views/omnibox/omnibox_view_win.cc +++ b/chrome/browser/ui/views/omnibox/omnibox_view_win.cc @@ -16,7 +16,6 @@ #include "base/i18n/rtl.h" #include "base/lazy_instance.h" #include "base/memory/ref_counted.h" -#include "base/property_bag.h" #include "base/string_util.h" #include "base/utf_string_conversions.h" #include "base/win/iat_patch_function.h" @@ -94,16 +93,19 @@ int CopyOrLinkDragOperation(int drag_operation) { return ui::DragDropTypes::DRAG_NONE; } +const char kAutocompleteEditStateKey[] = "AutocompleteEditState"; + // The AutocompleteEditState struct contains enough information about the // OmniboxEditModel and OmniboxViewWin to save/restore a user's // typing, caret position, etc. across tab changes. We explicitly don't // preserve things like whether the popup was open as this might be weird. -struct AutocompleteEditState { +struct AutocompleteEditState : public base::SupportsUserData::Data { AutocompleteEditState(const OmniboxEditModel::State& model_state, const OmniboxViewWin::State& view_state) : model_state(model_state), view_state(view_state) { } + virtual ~AutocompleteEditState() {} const OmniboxEditModel::State model_state; const OmniboxViewWin::State view_state; @@ -376,13 +378,6 @@ BOOL WINAPI EndPaintIntercept(HWND hWnd, const PAINTSTRUCT* lpPaint) { return (edit_hwnd && (hWnd == edit_hwnd)) || ::EndPaint(hWnd, lpPaint); } -// Returns a lazily initialized property bag accessor for saving our state in a -// WebContents. -base::PropertyAccessor<AutocompleteEditState>* GetStateAccessor() { - static base::PropertyAccessor<AutocompleteEditState> state; - return &state; -} - class PaintPatcher { public: PaintPatcher(); @@ -554,8 +549,9 @@ void OmniboxViewWin::SaveStateToTab(WebContents* tab) { CHARRANGE selection; GetSelection(selection); - GetStateAccessor()->SetProperty(tab->GetPropertyBag(), - AutocompleteEditState( + tab->SetUserData( + kAutocompleteEditStateKey, + new AutocompleteEditState( model_state, State(selection, saved_selection_for_focus_change_))); } @@ -586,8 +582,8 @@ void OmniboxViewWin::Update(const WebContents* tab_for_state_restoring) { // won't overwrite all our local state. RevertAll(); - const AutocompleteEditState* state = GetStateAccessor()->GetProperty( - tab_for_state_restoring->GetPropertyBag()); + const AutocompleteEditState* state = static_cast<AutocompleteEditState*>( + tab_for_state_restoring->GetUserData(&kAutocompleteEditStateKey)); if (state) { model()->RestoreState(state->model_state); diff --git a/chrome/browser/ui/views/tabs/tab_drag_controller_interactive_uitest.cc b/chrome/browser/ui/views/tabs/tab_drag_controller_interactive_uitest.cc index a95f2cf..e267343 100644 --- a/chrome/browser/ui/views/tabs/tab_drag_controller_interactive_uitest.cc +++ b/chrome/browser/ui/views/tabs/tab_drag_controller_interactive_uitest.cc @@ -7,7 +7,6 @@ #include "base/bind.h" #include "base/callback.h" #include "base/command_line.h" -#include "base/property_bag.h" #include "base/run_loop.h" #include "base/string_number_conversions.h" #include "chrome/browser/ui/browser.h" @@ -39,6 +38,24 @@ namespace test { +namespace { + +const char kTabDragControllerInteractiveUITestUserDataKey[] = + "TabDragControllerInteractiveUITestUserData"; + +class TabDragControllerInteractiveUITestUserData + : public base::SupportsUserData::Data { + public: + explicit TabDragControllerInteractiveUITestUserData(int id) : id_(id) {} + virtual ~TabDragControllerInteractiveUITestUserData() {} + int id() { return id_; } + + private: + int id_; +}; + +} // namespace + class QuitDraggingObserver : public content::NotificationObserver { public: QuitDraggingObserver() { @@ -68,15 +85,9 @@ gfx::Point GetCenterInScreenCoordinates(const views::View* view) { return center; } -base::PropertyAccessor<int>* id_accessor() { - static base::PropertyAccessor<int>* accessor = NULL; - if (!accessor) - accessor = new base::PropertyAccessor<int>; - return accessor; -} - void SetID(content::WebContents* tab_contents, int id) { - id_accessor()->SetProperty(tab_contents->GetPropertyBag(), id); + tab_contents->SetUserData(&kTabDragControllerInteractiveUITestUserDataKey, + new TabDragControllerInteractiveUITestUserData(id)); } void ResetIDs(TabStripModel* model, int start) { @@ -89,10 +100,13 @@ std::string IDString(TabStripModel* model) { for (int i = 0; i < model->count(); ++i) { if (i != 0) result += " "; - int* id_value = id_accessor()->GetProperty( - model->GetTabContentsAt(i)->web_contents()->GetPropertyBag()); - if (id_value) - result += base::IntToString(*id_value); + content::WebContents* contents = model->GetTabContentsAt(i)->web_contents(); + TabDragControllerInteractiveUITestUserData* user_data = + static_cast<TabDragControllerInteractiveUITestUserData*>( + contents->GetUserData( + &kTabDragControllerInteractiveUITestUserDataKey)); + if (user_data) + result += base::IntToString(user_data->id()); else result += "?"; } diff --git a/chrome/browser/ui/views/tabs/tab_drag_controller_interactive_uitest_win.cc b/chrome/browser/ui/views/tabs/tab_drag_controller_interactive_uitest_win.cc index 976bc8e..faa8a23 100644 --- a/chrome/browser/ui/views/tabs/tab_drag_controller_interactive_uitest_win.cc +++ b/chrome/browser/ui/views/tabs/tab_drag_controller_interactive_uitest_win.cc @@ -7,7 +7,6 @@ #include "base/bind.h" #include "base/callback.h" #include "base/command_line.h" -#include "base/property_bag.h" #include "base/string_number_conversions.h" #include "chrome/browser/ui/browser.h" #include "chrome/browser/ui/browser_list.h" diff --git a/chrome/browser/ui/webui/constrained_web_dialog_delegate_base.cc b/chrome/browser/ui/webui/constrained_web_dialog_delegate_base.cc index e643841..bfc97d7 100644 --- a/chrome/browser/ui/webui/constrained_web_dialog_delegate_base.cc +++ b/chrome/browser/ui/webui/constrained_web_dialog_delegate_base.cc @@ -6,7 +6,6 @@ #include <string> -#include "base/property_bag.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/ui/constrained_window.h" #include "chrome/browser/ui/tab_contents/tab_contents.h" @@ -41,9 +40,8 @@ ConstrainedWebDialogDelegateBase::ConstrainedWebDialogDelegateBase( } else { web_contents->SetDelegate(this); } - // Set |this| as a property so the ConstrainedWebDialogUI can retrieve it. - ConstrainedWebDialogUI::GetPropertyAccessor().SetProperty( - web_contents->GetPropertyBag(), this); + // Set |this| as a delegate so the ConstrainedWebDialogUI can retrieve it. + ConstrainedWebDialogUI::SetConstrainedDelegate(web_contents, this); web_contents->GetController().LoadURL(delegate->GetDialogContentURL(), content::Referrer(), diff --git a/chrome/browser/view_type_utils.cc b/chrome/browser/view_type_utils.cc index 5118e0a..d24e9cf 100644 --- a/chrome/browser/view_type_utils.cc +++ b/chrome/browser/view_type_utils.cc @@ -5,31 +5,41 @@ #include "chrome/browser/view_type_utils.h" #include "base/lazy_instance.h" -#include "base/property_bag.h" #include "content/public/browser/web_contents.h" using content::WebContents; namespace chrome { -static base::LazyInstance<base::PropertyAccessor<ViewType> > - g_view_type_property_accessor = LAZY_INSTANCE_INITIALIZER; +namespace { -base::PropertyAccessor<ViewType>* GetPropertyAccessor() { - return g_view_type_property_accessor.Pointer(); -} +const char kViewTypeUserDataKey[] = "ViewTypeUserData"; + +class ViewTypeUserData : public base::SupportsUserData::Data { + public: + explicit ViewTypeUserData(ViewType type) : type_(type) {} + virtual ~ViewTypeUserData() {} + ViewType type() { return type_; } + + private: + ViewType type_; +}; + +} // namespace ViewType GetViewType(WebContents* tab) { if (!tab) return VIEW_TYPE_INVALID; - ViewType* type = GetPropertyAccessor()->GetProperty(tab->GetPropertyBag()); - if (type) - return *type; - return VIEW_TYPE_INVALID; + + ViewTypeUserData* user_data = static_cast<ViewTypeUserData*>( + tab->GetUserData(&kViewTypeUserDataKey)); + + return user_data ? user_data->type() : VIEW_TYPE_INVALID; } void SetViewType(WebContents* tab, ViewType type) { - GetPropertyAccessor()->SetProperty(tab->GetPropertyBag(), type); + tab->SetUserData(&kViewTypeUserDataKey, + new ViewTypeUserData(type)); } } // namespace chrome diff --git a/content/browser/web_contents/web_contents_impl.cc b/content/browser/web_contents/web_contents_impl.cc index 61d98d8..9ddb933 100644 --- a/content/browser/web_contents/web_contents_impl.cc +++ b/content/browser/web_contents/web_contents_impl.cc @@ -719,15 +719,6 @@ const GURL& WebContentsImpl::GetURL() const { return entry ? entry->GetVirtualURL() : GURL::EmptyGURL(); } - -const base::PropertyBag* WebContentsImpl::GetPropertyBag() const { - return &property_bag_; -} - -base::PropertyBag* WebContentsImpl::GetPropertyBag() { - return &property_bag_; -} - content::WebContentsDelegate* WebContentsImpl::GetDelegate() { return delegate_; } diff --git a/content/browser/web_contents/web_contents_impl.h b/content/browser/web_contents/web_contents_impl.h index b9320ae..bc95844 100644 --- a/content/browser/web_contents/web_contents_impl.h +++ b/content/browser/web_contents/web_contents_impl.h @@ -12,7 +12,6 @@ #include "base/gtest_prod_util.h" #include "base/memory/scoped_ptr.h" #include "base/observer_list.h" -#include "base/property_bag.h" #include "content/browser/browser_plugin/old/old_browser_plugin_host.h" #include "content/browser/renderer_host/java/java_bridge_dispatcher_host_manager.h" #include "content/browser/renderer_host/render_view_host_delegate.h" @@ -163,8 +162,6 @@ class CONTENT_EXPORT WebContentsImpl RenderViewHostManager* GetRenderManagerForTesting(); // content::WebContents ------------------------------------------------------ - virtual const base::PropertyBag* GetPropertyBag() const OVERRIDE; - virtual base::PropertyBag* GetPropertyBag() OVERRIDE; virtual content::WebContentsDelegate* GetDelegate() OVERRIDE; virtual void SetDelegate(content::WebContentsDelegate* delegate) OVERRIDE; virtual NavigationControllerImpl& GetController() OVERRIDE; @@ -642,10 +639,6 @@ class CONTENT_EXPORT WebContentsImpl std::string* embedder_channel_name, int* embedder_container_id); - // Stores random bits of data for others to associate with this object. - // WARNING: this needs to be deleted after NavigationController. - base::PropertyBag property_bag_; - // Data for core operation --------------------------------------------------- // Delegate for notifying our owner about stuff. Not owned by us. diff --git a/content/public/browser/web_contents.h b/content/public/browser/web_contents.h index d68e148..77a1ef7 100644 --- a/content/public/browser/web_contents.h +++ b/content/public/browser/web_contents.h @@ -9,6 +9,7 @@ #include "base/callback_forward.h" #include "base/process_util.h" #include "base/string16.h" +#include "base/supports_user_data.h" #include "content/common/content_export.h" #include "content/public/browser/navigation_controller.h" #include "content/public/browser/page_navigator.h" @@ -20,7 +21,6 @@ #include "webkit/glue/window_open_disposition.h" namespace base { -class PropertyBag; class TimeTicks; } @@ -46,7 +46,9 @@ class WebContentsView; struct RendererPreferences; // Describes what goes in the main content area of a tab. -class WebContents : public PageNavigator, public IPC::Sender { +class WebContents : public PageNavigator, + public IPC::Sender, + public base::SupportsUserData { public: // |base_web_contents| is used if we want to size the new WebContents's view // based on the view of an existing WebContents. This can be NULL if not @@ -83,12 +85,6 @@ class WebContents : public PageNavigator, public IPC::Sender { // Intrinsic tab state ------------------------------------------------------- - // Returns the property bag for this WebContents, where callers can add - // extra data they may wish to associate with the tab. Returns a pointer - // rather than a reference since the PropertyAccessors expect this. - virtual const base::PropertyBag* GetPropertyBag() const = 0; - virtual base::PropertyBag* GetPropertyBag() = 0; - // Gets/Sets the delegate. virtual WebContentsDelegate* GetDelegate() = 0; virtual void SetDelegate(WebContentsDelegate* delegate) = 0; diff --git a/ui/views/controls/webview/web_dialog_view.cc b/ui/views/controls/webview/web_dialog_view.cc index 6e1d24a..c3ba403 100644 --- a/ui/views/controls/webview/web_dialog_view.cc +++ b/ui/views/controls/webview/web_dialog_view.cc @@ -6,7 +6,6 @@ #include <vector> -#include "base/property_bag.h" #include "base/utf_string_conversions.h" #include "content/public/browser/browser_context.h" #include "content/public/browser/native_web_keyboard_event.h" @@ -308,8 +307,7 @@ void WebDialogView::InitDialog() { // Set the delegate. This must be done before loading the page. See // the comment above WebDialogUI in its header file for why. - WebDialogUI::GetPropertyAccessor().SetProperty( - web_contents->GetPropertyBag(), this); + WebDialogUI::SetDelegate(web_contents, this); if (delegate_) { gfx::Size out; diff --git a/ui/web_dialogs/constrained_web_dialog_ui.cc b/ui/web_dialogs/constrained_web_dialog_ui.cc index 13a40ab..b14b5ca 100644 --- a/ui/web_dialogs/constrained_web_dialog_ui.cc +++ b/ui/web_dialogs/constrained_web_dialog_ui.cc @@ -10,7 +10,6 @@ #include "base/bind.h" #include "base/bind_helpers.h" #include "base/lazy_instance.h" -#include "base/property_bag.h" #include "base/values.h" #include "content/public/browser/notification_service.h" #include "content/public/browser/render_view_host.h" @@ -23,12 +22,30 @@ using content::RenderViewHost; using content::WebContents; using content::WebUIMessageHandler; -static base::LazyInstance< - base::PropertyAccessor<ui::ConstrainedWebDialogDelegate*> > - g_constrained_web_dialog_ui_property_accessor = LAZY_INSTANCE_INITIALIZER; - namespace ui { +namespace { + +const char kConstrainedWebDialogDelegateUserDataKey[] = + "ConstrainedWebDialogDelegateUserData"; + +class ConstrainedWebDialogDelegateUserData + : public base::SupportsUserData::Data { + public: + explicit ConstrainedWebDialogDelegateUserData( + ConstrainedWebDialogDelegate* delegate) : delegate_(delegate) {} + virtual ~ConstrainedWebDialogDelegateUserData() {} + + ConstrainedWebDialogDelegate* delegate() { return delegate_; } + + private: + ConstrainedWebDialogDelegate* delegate_; // unowned + + DISALLOW_COPY_AND_ASSIGN(ConstrainedWebDialogDelegateUserData); +}; + +} // namespace + ConstrainedWebDialogUI::ConstrainedWebDialogUI(content::WebUI* web_ui) : WebUIController(web_ui) { } @@ -72,16 +89,21 @@ void ConstrainedWebDialogUI::OnDialogCloseMessage(const ListValue* args) { delegate->OnDialogCloseFromWebUI(); } -ConstrainedWebDialogDelegate* ConstrainedWebDialogUI::GetConstrainedDelegate() { - ConstrainedWebDialogDelegate** property = GetPropertyAccessor().GetProperty( - web_ui()->GetWebContents()->GetPropertyBag()); - return property ? *property : NULL; +// static +void ConstrainedWebDialogUI::SetConstrainedDelegate( + content::WebContents* web_contents, + ConstrainedWebDialogDelegate* delegate) { + web_contents->SetUserData(&kConstrainedWebDialogDelegateUserDataKey, + new ConstrainedWebDialogDelegateUserData(delegate)); } -// static -base::PropertyAccessor<ConstrainedWebDialogDelegate*>& - ConstrainedWebDialogUI::GetPropertyAccessor() { - return g_constrained_web_dialog_ui_property_accessor.Get(); +ConstrainedWebDialogDelegate* ConstrainedWebDialogUI::GetConstrainedDelegate() { + ConstrainedWebDialogDelegateUserData* user_data = + static_cast<ConstrainedWebDialogDelegateUserData*>( + web_ui()->GetWebContents()-> + GetUserData(&kConstrainedWebDialogDelegateUserDataKey)); + + return user_data ? user_data->delegate() : NULL; } } // namespace ui diff --git a/ui/web_dialogs/constrained_web_dialog_ui.h b/ui/web_dialogs/constrained_web_dialog_ui.h index 4837c5f..04e9084 100644 --- a/ui/web_dialogs/constrained_web_dialog_ui.h +++ b/ui/web_dialogs/constrained_web_dialog_ui.h @@ -13,12 +13,9 @@ class ConstrainedWindow; class Profile; class TabContents; -namespace base { -template<class T> class PropertyAccessor; -} - namespace content { class RenderViewHost; +class WebContents; } namespace ui { @@ -69,14 +66,13 @@ class WEB_DIALOGS_EXPORT ConstrainedWebDialogUI virtual void RenderViewCreated( content::RenderViewHost* render_view_host) OVERRIDE; - // Returns a property accessor that can be used to set the - // ConstrainedWebDialogDelegate property on a WebContents. - static base::PropertyAccessor<ConstrainedWebDialogDelegate*>& - GetPropertyAccessor(); + // Sets the delegate on the WebContents. + static void SetConstrainedDelegate(content::WebContents* web_contents, + ConstrainedWebDialogDelegate* delegate); protected: - // Returns the WebContents' PropertyBag's ConstrainedWebDialogDelegate. - // Returns NULL if that property is not set. + // Returns the ConstrainedWebDialogDelegate saved with the WebContents. + // Returns NULL if no such delegate is set. ConstrainedWebDialogDelegate* GetConstrainedDelegate(); private: diff --git a/ui/web_dialogs/web_dialog_ui.cc b/ui/web_dialogs/web_dialog_ui.cc index 2ef0f91..5f87096 100644 --- a/ui/web_dialogs/web_dialog_ui.cc +++ b/ui/web_dialogs/web_dialog_ui.cc @@ -7,7 +7,6 @@ #include "base/bind.h" #include "base/bind_helpers.h" #include "base/lazy_instance.h" -#include "base/property_bag.h" #include "base/values.h" #include "content/public/browser/notification_service.h" #include "content/public/browser/render_view_host.h" @@ -20,22 +19,36 @@ using content::RenderViewHost; using content::WebUIMessageHandler; -static base::LazyInstance<base::PropertyAccessor<ui::WebDialogDelegate*> > - g_web_dialog_ui_property_accessor = LAZY_INSTANCE_INITIALIZER; - namespace ui { +namespace { + +const char kWebDialogDelegateUserDataKey[] = "WebDialogDelegateUserData"; + +class WebDialogDelegateUserData : public base::SupportsUserData::Data { + public: + explicit WebDialogDelegateUserData(WebDialogDelegate* delegate) + : delegate_(delegate) {} + virtual ~WebDialogDelegateUserData() {} + WebDialogDelegate* delegate() { return delegate_; } + + private: + WebDialogDelegate* delegate_; // unowned +}; + +} // namespace + WebDialogUI::WebDialogUI(content::WebUI* web_ui) : WebUIController(web_ui) { } WebDialogUI::~WebDialogUI() { - // Don't unregister our property. During the teardown of the WebContents, + // Don't unregister our user data. During the teardown of the WebContents, // this will be deleted, but the WebContents will already be destroyed. // // This object is owned indirectly by the WebContents. WebUIs can change, so // it's scary if this WebUI is changed out and replaced with something else, - // since the property will still point to the old delegate. But the delegate + // since the user data will still point to the old delegate. But the delegate // is itself the owner of the WebContents for a dialog so will be in scope, // and the HTML dialogs won't swap WebUIs anyway since they don't navigate. } @@ -45,13 +58,25 @@ void WebDialogUI::CloseDialog(const base::ListValue* args) { } // static -base::PropertyAccessor<WebDialogDelegate*>& WebDialogUI::GetPropertyAccessor() { - return g_web_dialog_ui_property_accessor.Get(); +void WebDialogUI::SetDelegate(content::WebContents* web_contents, + WebDialogDelegate* delegate) { + web_contents->SetUserData(&kWebDialogDelegateUserDataKey, + new WebDialogDelegateUserData(delegate)); } //////////////////////////////////////////////////////////////////////////////// // Private: +WebDialogDelegate* WebDialogUI::GetDelegate( + content::WebContents* web_contents) { + WebDialogDelegateUserData* user_data = + static_cast<WebDialogDelegateUserData*>( + web_contents->GetUserData(&kWebDialogDelegateUserDataKey)); + + return user_data ? user_data->delegate() : NULL; +} + + void WebDialogUI::RenderViewCreated(RenderViewHost* render_view_host) { // Hook up the javascript function calls, also known as chrome.send("foo") // calls in the HTML, to the actual C++ functions. @@ -61,11 +86,10 @@ void WebDialogUI::RenderViewCreated(RenderViewHost* render_view_host) { // Pass the arguments to the renderer supplied by the delegate. std::string dialog_args; std::vector<WebUIMessageHandler*> handlers; - WebDialogDelegate** delegate = GetPropertyAccessor().GetProperty( - web_ui()->GetWebContents()->GetPropertyBag()); + WebDialogDelegate* delegate = GetDelegate(web_ui()->GetWebContents()); if (delegate) { - dialog_args = (*delegate)->GetDialogArgs(); - (*delegate)->GetWebUIMessageHandlers(&handlers); + dialog_args = delegate->GetDialogArgs(); + delegate->GetWebUIMessageHandlers(&handlers); } if (0 != (web_ui()->GetBindings() & content::BINDINGS_POLICY_WEB_UI)) @@ -76,18 +100,17 @@ void WebDialogUI::RenderViewCreated(RenderViewHost* render_view_host) { } if (delegate) - (*delegate)->OnDialogShown(web_ui(), render_view_host); + delegate->OnDialogShown(web_ui(), render_view_host); } void WebDialogUI::OnDialogClosed(const ListValue* args) { - WebDialogDelegate** delegate = GetPropertyAccessor().GetProperty( - web_ui()->GetWebContents()->GetPropertyBag()); + WebDialogDelegate* delegate = GetDelegate(web_ui()->GetWebContents()); if (delegate) { std::string json_retval; if (args && !args->empty() && !args->GetString(0, &json_retval)) NOTREACHED() << "Could not read JSON argument"; - (*delegate)->OnDialogClosed(json_retval); + delegate->OnDialogClosed(json_retval); } } diff --git a/ui/web_dialogs/web_dialog_ui.h b/ui/web_dialogs/web_dialog_ui.h index 562cbb4..e6e29f4 100644 --- a/ui/web_dialogs/web_dialog_ui.h +++ b/ui/web_dialogs/web_dialog_ui.h @@ -16,12 +16,6 @@ #include "ui/base/ui_base_types.h" #include "ui/web_dialogs/web_dialogs_export.h" - -namespace base { -class ListValue; -template<class T> class PropertyAccessor; -} - namespace content { class WebContents; class WebUIMessageHandler; @@ -42,10 +36,11 @@ class WebDialogDelegate; // just embed a RenderView in a dialog and be done with it. // // Before loading a URL corresponding to this WebUI, the caller should set its -// delegate as a property on the WebContents. This WebUI will pick it up from -// there and call it back. This is a bit of a hack to allow the dialog to pass -// its delegate to the Web UI without having nasty accessors on the WebContents. -// The correct design using RVH directly would avoid all of this. +// delegate as user data on the WebContents by calling SetDelegate(). This WebUI +// will pick it up from there and call it back. This is a bit of a hack to allow +// the dialog to pass its delegate to the Web UI without having nasty accessors +// on the WebContents. The correct design using RVH directly would avoid all of +// this. class WEB_DIALOGS_EXPORT WebDialogUI : public content::WebUIController { public: struct WebDialogParams { @@ -59,22 +54,26 @@ class WEB_DIALOGS_EXPORT WebDialogUI : public content::WebUIController { std::string json_input; }; - // When created, the property should already be set on the WebContents. + // When created, the delegate should already be set as user data on the + // WebContents. explicit WebDialogUI(content::WebUI* web_ui); virtual ~WebDialogUI(); // Close the dialog, passing the specified arguments to the close handler. void CloseDialog(const base::ListValue* args); - // Returns the PropertyBag accessor object used to write the delegate pointer - // into the WebContents (see class-level comment above). - static base::PropertyAccessor<WebDialogDelegate*>& GetPropertyAccessor(); + // Sets the delegate on the WebContents. + static void SetDelegate(content::WebContents* web_contents, + WebDialogDelegate* delegate); private: // WebUIController virtual void RenderViewCreated( content::RenderViewHost* render_view_host) OVERRIDE; + // Gets the delegate for the WebContent set with SetDelegate. + static WebDialogDelegate* GetDelegate(content::WebContents* web_contents); + // JS message handler. void OnDialogClosed(const base::ListValue* args); |