diff options
25 files changed, 356 insertions, 138 deletions
diff --git a/app/app.gyp b/app/app.gyp index 2c8e7f7..ce64955 100644 --- a/app/app.gyp +++ b/app/app.gyp @@ -56,6 +56,7 @@ 'text_elider_unittest.cc', 'tree_node_iterator_unittest.cc', 'tree_node_model_unittest.cc', + 'view_prop_unittest.cc', 'win_util_unittest.cc', ], 'include_dirs': [ @@ -75,6 +76,7 @@ ['OS!="win"', { 'sources!': [ 'os_exchange_data_win_unittest.cc', + 'view_prop_unittest.cc', 'win_util_unittest.cc', ], }], diff --git a/app/app_base.gypi b/app/app_base.gypi index 37e0640..a121a50 100644 --- a/app/app_base.gypi +++ b/app/app_base.gypi @@ -221,6 +221,8 @@ 'throb_animation.h', 'tween.cc', 'tween.h', + 'view_prop.cc', + 'view_prop.h', 'win/drag_source.cc', 'win/drag_source.h', 'win/drop_target.cc', @@ -315,6 +317,8 @@ 'gfx/native_theme_win.cc', 'gfx/native_theme_win.h', 'os_exchange_data.cc', + 'view_prop.cc', + 'view_prop.h', 'win/iat_patch_function.cc', 'win/iat_patch_function.h', ], diff --git a/app/view_prop.cc b/app/view_prop.cc new file mode 100644 index 0000000..778f381 --- /dev/null +++ b/app/view_prop.cc @@ -0,0 +1,106 @@ +// Copyright (c) 2010 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 "app/view_prop.h" + +#include <set> + +namespace app { +namespace win { + +// Maints the actual view, key and data. +class ViewProp::Data : public base::RefCounted<ViewProp::Data> { + public: + // Returns the Data* for the view/key pair. If |create| is false and |Get| + // has not been invoked for the view/key pair, NULL is returned. + static void Get(gfx::NativeView view, + const char* key, + bool create, + scoped_refptr<Data>* data) { + if (!data_set_) + data_set_ = new DataSet; + scoped_refptr<Data> new_data(new Data(view, key)); + DataSet::const_iterator i = data_set_->find(new_data.get()); + if (i != data_set_->end()) { + *data = *i; + return; + } + if (!create) + return; + data_set_->insert(new_data.get()); + *data = new_data.get(); + } + + // The data. + void set_data(void* data) { data_ = data; } + void* data() const { return data_; } + + const char* key() const { return key_; } + + private: + friend class base::RefCounted<Data>; + + // Used to order the Data in the map. + class DataComparator { + public: + bool operator()(const Data* d1, const Data* d2) const { + return (d1->view_ == d2->view_) ? (d1->key_ < d2->key_) : + (d1->view_ < d2->view_); + } + }; + + typedef std::set<Data*, DataComparator> DataSet; + + Data(gfx::NativeView view, const char* key) + : view_(view), + key_(key), + data_(NULL) {} + + ~Data() { + DataSet::iterator i = data_set_->find(this); + // Also check for equality using == as |Get| creates dummy values in order + // to look up a value. + if (i != data_set_->end() && *i == this) + data_set_->erase(i); + } + + // The existing set of Data is stored here. ~Data removes from the set. + static DataSet* data_set_; + + const gfx::NativeView view_; + const char* key_; + void* data_; + + DISALLOW_COPY_AND_ASSIGN(Data); +}; + +// static +ViewProp::Data::DataSet* ViewProp::Data::data_set_ = NULL; + +ViewProp::ViewProp(gfx::NativeView view, const char* key, void* data) { + Data::Get(view, key, true, &data_); + data_->set_data(data); +} + +ViewProp::~ViewProp() { + // This is done to provide similar semantics to SetProp. In particular it's + // assumed that ~ViewProp should behave as though RemoveProp was invoked. + data_->set_data(NULL); +} + +// static +void* ViewProp::GetValue(gfx::NativeView view, const char* key) { + scoped_refptr<Data> data; + Data::Get(view, key, false, &data); + return data.get() ? data->data() : NULL; +} + +// static +const char* ViewProp::Key() const { + return data_->key(); +} + +} // namespace win + +} // namespace app diff --git a/app/view_prop.h b/app/view_prop.h new file mode 100644 index 0000000..c319460 --- /dev/null +++ b/app/view_prop.h @@ -0,0 +1,47 @@ +// Copyright (c) 2010 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 APP_VIEW_PROP_H_ +#define APP_VIEW_PROP_H_ +#pragma once + +#include "base/basictypes.h" +#include "base/ref_counted.h" +#include "gfx/native_widget_types.h" + +namespace app { + +// ViewProp maintains a key/value pair for a particular view. ViewProp is +// designed as a replacement for the Win32's SetProp, but does not make use of +// window manager memory. ViewProp shares similar semantics as SetProp, the +// value for a particular view/key pair comes from the last ViewProp created. +class ViewProp { + public: + // Associates data with a view/key pair. If a ViewProp has already been + // created for the specified pair |data| replaces the current value. + // + // ViewProp does *not* make a copy of the char*, the pointer is used for + // sorting. + ViewProp(gfx::NativeView view, const char* key, void* data); + ~ViewProp(); + + // Returns the value associated with the view/key pair, or NULL if there is + // none. + static void* GetValue(gfx::NativeView view, const char* key); + + // Returns the key. + const char* Key() const; + + private: + class Data; + + // Stores the actual data. + scoped_refptr<Data> data_; + + DISALLOW_COPY_AND_ASSIGN(ViewProp); +}; + +} // namespace app + +#endif // APP_VIEW_PROP_H_ diff --git a/app/view_prop_unittest.cc b/app/view_prop_unittest.cc new file mode 100644 index 0000000..9b81cd5 --- /dev/null +++ b/app/view_prop_unittest.cc @@ -0,0 +1,70 @@ +// Copyright (c) 2010 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 "testing/gtest/include/gtest/gtest.h" + +#include "app/view_prop.h" +#include "base/scoped_ptr.h" + +typedef testing::Test ViewPropTest; + +static const char* kKey1 = "key_1"; +static const char* kKey2 = "key_2"; + +using app::ViewProp; + +// Test a handful of viewprop assertions. +TEST_F(ViewPropTest, Basic) { + gfx::NativeView nv1 = reinterpret_cast<gfx::NativeView>(1); + gfx::NativeView nv2 = reinterpret_cast<gfx::NativeView>(2); + + void* data1 = reinterpret_cast<void*>(11); + void* data2 = reinterpret_cast<void*>(12); + + // Initial value for a new view/key pair should be NULL. + EXPECT_EQ(NULL, ViewProp::GetValue(nv1, kKey1)); + + { + // Register a value for a view/key pair. + ViewProp prop(nv1, kKey1, data1); + EXPECT_EQ(data1, ViewProp::GetValue(nv1, kKey1)); + } + + // The property fell out of scope, so the value should now be NULL. + EXPECT_EQ(NULL, ViewProp::GetValue(nv1, kKey1)); + + { + // Register a value for a view/key pair. + scoped_ptr<ViewProp> v1(new ViewProp(nv1, kKey1, data1)); + EXPECT_EQ(data1, ViewProp::GetValue(nv1, kKey1)); + + // Register a value for the same view/key pair. + scoped_ptr<ViewProp> v2(new ViewProp(nv1, kKey1, data2)); + // The new value should take over. + EXPECT_EQ(data2, ViewProp::GetValue(nv1, kKey1)); + + // Null out the first ViewProp, which should NULL out the value. + v1.reset(NULL); + EXPECT_EQ(NULL, ViewProp::GetValue(nv1, kKey1)); + } + + // The property fell out of scope, so the value should now be NULL. + EXPECT_EQ(NULL, ViewProp::GetValue(nv1, kKey1)); + + { + // Register a value for a view/key pair. + scoped_ptr<ViewProp> v1(new ViewProp(nv1, kKey1, data1)); + scoped_ptr<ViewProp> v2(new ViewProp(nv2, kKey2, data2)); + EXPECT_EQ(data1, ViewProp::GetValue(nv1, kKey1)); + EXPECT_EQ(data2, ViewProp::GetValue(nv2, kKey2)); + + v1.reset(NULL); + EXPECT_EQ(NULL, ViewProp::GetValue(nv1, kKey1)); + EXPECT_EQ(data2, ViewProp::GetValue(nv2, kKey2)); + + v2.reset(NULL); + EXPECT_EQ(NULL, ViewProp::GetValue(nv1, kKey1)); + EXPECT_EQ(NULL, ViewProp::GetValue(nv2, kKey2)); + } +} diff --git a/app/win/scoped_prop.h b/app/win/scoped_prop.h index 14e6a6a..c309e52 100644 --- a/app/win/scoped_prop.h +++ b/app/win/scoped_prop.h @@ -18,6 +18,12 @@ namespace win { // cleanup. ScopedProp must be destroyed before the window is destroyed, else // you're going to leak a property, which could lead to failure to set a // property later on. +// +// *WARNING* +// SetProp is very fragile. SetProp makes use of a finite chunk of memory that +// is very easy to exhaust. Unless you need to share a property across process +// boundaries you should instead use ViewProp, which does not cause leaks at the +// window manager. class ScopedProp { public: // Registers the key value pair for the specified window. ScopedProp does not diff --git a/chrome/browser/external_tab_container_win.cc b/chrome/browser/external_tab_container_win.cc index 41b0a99..a248db2 100644 --- a/chrome/browser/external_tab_container_win.cc +++ b/chrome/browser/external_tab_container_win.cc @@ -7,7 +7,7 @@ #include <string> #include "app/l10n_util.h" -#include "app/win/scoped_prop.h" +#include "app/view_prop.h" #include "base/debug/trace_event.h" #include "base/i18n/rtl.h" #include "base/logging.h" @@ -47,7 +47,9 @@ #include "views/widget/root_view.h" #include "views/window/window.h" -static const wchar_t kWindowObjectKey[] = L"ChromeWindowObject"; +using app::ViewProp; + +static const char kWindowObjectKey[] = "ChromeWindowObject"; // This class overrides the LinkActivated function in the PageInfoBubbleView // class and routes the help center link navigation to the host browser. @@ -134,8 +136,7 @@ bool ExternalTabContainer::Init(Profile* profile, // TODO(jcampan): limit focus traversal to contents. - prop_.reset( - new app::win::ScopedProp(GetNativeView(), kWindowObjectKey, this)); + prop_.reset(new ViewProp(GetNativeView(), kWindowObjectKey, this)); if (existing_contents) { tab_contents_ = existing_contents; @@ -284,10 +285,7 @@ void ExternalTabContainer::FocusThroughTabTraversal( // static bool ExternalTabContainer::IsExternalTabContainer(HWND window) { - if (::GetProp(window, kWindowObjectKey) != NULL) - return true; - - return false; + return ViewProp::GetValue(window, kWindowObjectKey) != NULL; } // static @@ -301,7 +299,7 @@ ExternalTabContainer* ExternalTabContainer::GetContainerForTab( return NULL; } ExternalTabContainer* container = reinterpret_cast<ExternalTabContainer*>( - GetProp(parent_window, kWindowObjectKey)); + ViewProp::GetValue(parent_window, kWindowObjectKey)); return container; } @@ -311,8 +309,8 @@ ExternalTabContainer* gfx::NativeView native_window) { ExternalTabContainer* tab_container = NULL; if (native_window) { - HANDLE handle = ::GetProp(native_window, kWindowObjectKey); - tab_container = reinterpret_cast<ExternalTabContainer*>(handle); + tab_container = reinterpret_cast<ExternalTabContainer*>( + ViewProp::GetValue(native_window, kWindowObjectKey)); } return tab_container; } diff --git a/chrome/browser/external_tab_container_win.h b/chrome/browser/external_tab_container_win.h index 91ce11c..cc0e35c 100644 --- a/chrome/browser/external_tab_container_win.h +++ b/chrome/browser/external_tab_container_win.h @@ -31,9 +31,7 @@ class TabContentsContainer; class RenderViewContextMenuViews; namespace app { -namespace win { -class ScopedProp; -} +class ViewProp; } namespace IPC { @@ -339,7 +337,7 @@ class ExternalTabContainer : public TabContentsDelegate, // page without chrome frame. bool route_all_top_level_navigations_; - scoped_ptr<app::win::ScopedProp> prop_; + scoped_ptr<app::ViewProp> prop_; DISALLOW_COPY_AND_ASSIGN(ExternalTabContainer); }; diff --git a/chrome/browser/renderer_host/render_widget_host_view_win.cc b/chrome/browser/renderer_host/render_widget_host_view_win.cc index 6945b2e..3f9b9b1 100644 --- a/chrome/browser/renderer_host/render_widget_host_view_win.cc +++ b/chrome/browser/renderer_host/render_widget_host_view_win.cc @@ -7,6 +7,7 @@ #include "app/l10n_util.h" #include "app/l10n_util_win.h" #include "app/resource_bundle.h" +#include "app/view_prop.h" #include "app/win/scoped_prop.h" #include "base/command_line.h" #include "base/i18n/rtl.h" @@ -51,6 +52,7 @@ #include "webkit/glue/webaccessibility.h" #include "webkit/glue/webcursor.h" +using app::ViewProp; using base::TimeDelta; using base::TimeTicks; using WebKit::WebInputEvent; @@ -71,7 +73,7 @@ const int kMaxTooltipLength = 1024; // listening for MSAA events. const int kIdCustom = 1; -const wchar_t* kRenderWidgetHostViewKey = L"__RENDER_WIDGET_HOST_VIEW__"; +const char* const kRenderWidgetHostViewKey = "__RENDER_WIDGET_HOST_VIEW__"; // A callback function for EnumThreadWindows to enumerate and dismiss // any owned popop windows @@ -308,9 +310,10 @@ void RenderWidgetHostViewWin::CreateWnd(HWND parent) { // Add a property indicating that a particular renderer is associated with // this window. Used by the GPU process to validate window handles it - // receives from renderer processes. + // receives from renderer processes. As this is used by a separate process we + // have to use ScopedProp here instead of ViewProp. int renderer_id = render_widget_host_->process()->id(); - props_.push_back( + renderer_id_prop_.reset( new app::win::ScopedProp(m_hWnd, chrome::kChromiumRendererIdProperty, reinterpret_cast<HANDLE>(renderer_id))); @@ -798,12 +801,11 @@ LRESULT RenderWidgetHostViewWin::OnCreate(CREATESTRUCT* create_struct) { props_.push_back(views::SetWindowSupportsRerouteMouseWheel(m_hWnd)); // Save away our HWND in the parent window as a property so that the // accessibility code can find it. - props_.push_back(new app::win::ScopedProp( - GetParent(), kViewsNativeHostPropForAccessibility, - m_hWnd)); - props_.push_back(new app::win::ScopedProp( - m_hWnd, kRenderWidgetHostViewKey, - static_cast<RenderWidgetHostView*>(this))); + props_.push_back(new ViewProp(GetParent(), + kViewsNativeHostPropForAccessibility, + m_hWnd)); + props_.push_back(new ViewProp(m_hWnd, kRenderWidgetHostViewKey, + static_cast<RenderWidgetHostView*>(this))); return 0; } @@ -834,6 +836,7 @@ void RenderWidgetHostViewWin::OnDestroy() { // sequence as part of the usual cleanup when the plugin instance goes away. EnumChildWindows(m_hWnd, DetachPluginWindowsCallback, NULL); + renderer_id_prop_.reset(); props_.reset(); ResetTooltip(); @@ -1635,11 +1638,7 @@ void RenderWidgetHostViewWin::ShutdownHost() { RenderWidgetHostView* RenderWidgetHostView::GetRenderWidgetHostViewFromNativeView( gfx::NativeView native_view) { - if (::IsWindow(native_view)) { - HANDLE raw_render_host_view = ::GetProp(native_view, - kRenderWidgetHostViewKey); - if (raw_render_host_view) - return reinterpret_cast<RenderWidgetHostView*>(raw_render_host_view); - } - return NULL; + return ::IsWindow(native_view) ? + reinterpret_cast<RenderWidgetHostView*>( + ViewProp::GetValue(native_view, kRenderWidgetHostViewKey)) : NULL; } diff --git a/chrome/browser/renderer_host/render_widget_host_view_win.h b/chrome/browser/renderer_host/render_widget_host_view_win.h index d02193f..13609e6 100644 --- a/chrome/browser/renderer_host/render_widget_host_view_win.h +++ b/chrome/browser/renderer_host/render_widget_host_view_win.h @@ -25,6 +25,9 @@ #include "webkit/glue/webcursor.h" namespace app { + +class ViewProp; + namespace win { class ScopedProp; } @@ -340,7 +343,8 @@ class RenderWidgetHostViewWin // method. WebKit::WebTextInputType text_input_type_; - ScopedVector<app::win::ScopedProp> props_; + ScopedVector<app::ViewProp> props_; + scoped_ptr<app::win::ScopedProp> renderer_id_prop_; DISALLOW_COPY_AND_ASSIGN(RenderWidgetHostViewWin); }; diff --git a/chrome/browser/ui/views/frame/browser_view.cc b/chrome/browser/ui/views/frame/browser_view.cc index 4552de4..1c5c5ee 100644 --- a/chrome/browser/ui/views/frame/browser_view.cc +++ b/chrome/browser/ui/views/frame/browser_view.cc @@ -82,6 +82,7 @@ #include "views/window/window.h" #if defined(OS_WIN) +#include "app/view_prop.h" #include "app/win_util.h" #include "chrome/browser/aeropeek_manager.h" #include "chrome/browser/jumplist_win.h" @@ -99,11 +100,7 @@ using views::GridLayout; static const int kStatusBubbleHeight = 20; // The name of a key to store on the window handle so that other code can // locate this object using just the handle. -#if defined(OS_WIN) -static const wchar_t* kBrowserViewKey = L"__BROWSER_VIEW__"; -#else -static const char* kBrowserViewKey = "__BROWSER_VIEW__"; -#endif +static const char* const kBrowserViewKey = "__BROWSER_VIEW__"; // How frequently we check for hung plugin windows. static const int kDefaultHungPluginDetectFrequency = 2000; // How long do we wait before we consider a window hung (in ms). @@ -486,9 +483,8 @@ BrowserView* BrowserView::GetBrowserViewForNativeWindow( gfx::NativeWindow window) { #if defined(OS_WIN) if (IsWindow(window)) { - HANDLE data = GetProp(window, kBrowserViewKey); - if (data) - return reinterpret_cast<BrowserView*>(data); + return reinterpret_cast<BrowserView*>( + app::ViewProp::GetValue(window, kBrowserViewKey)); } #else if (window) { @@ -1889,12 +1885,7 @@ void BrowserView::Init() { SetLayoutManager(CreateLayoutManager()); // Stow a pointer to this object onto the window handle so that we can get // at it later when all we have is a native view. -#if defined(OS_WIN) GetWidget()->SetNativeWindowProperty(kBrowserViewKey, this); -#else - g_object_set_data(G_OBJECT(GetWidget()->GetNativeView()), - kBrowserViewKey, this); -#endif // Start a hung plugin window detector for this browser object (as long as // hang detection is not disabled). diff --git a/views/accessibility/view_accessibility.cc b/views/accessibility/view_accessibility.cc index c092895..5d51d814 100644 --- a/views/accessibility/view_accessibility.cc +++ b/views/accessibility/view_accessibility.cc @@ -4,11 +4,12 @@ #include "views/accessibility/view_accessibility.h" +#include "app/view_prop.h" #include "views/widget/widget.h" #include "views/widget/widget_win.h" -const wchar_t kViewsNativeHostPropForAccessibility[] = - L"Views_NativeViewHostHWNDForAccessibility"; +const char kViewsNativeHostPropForAccessibility[] = + "Views_NativeViewHostHWNDForAccessibility"; // static scoped_refptr<ViewAccessibility> ViewAccessibility::Create(views::View* view) { @@ -710,9 +711,9 @@ HRESULT ViewAccessibility::GetNativeIAccessibleInterface( if (!native_host || !accessible) return E_INVALIDARG; - HWND native_view_window = - static_cast<HWND>(GetProp(native_host->native_view(), - kViewsNativeHostPropForAccessibility)); + HWND native_view_window = static_cast<HWND>( + app::ViewProp::GetValue(native_host->native_view(), + kViewsNativeHostPropForAccessibility)); if (!IsWindow(native_view_window)) { native_view_window = native_host->native_view(); } diff --git a/views/accessibility/view_accessibility.h b/views/accessibility/view_accessibility.h index 9803cba..66df163c 100644 --- a/views/accessibility/view_accessibility.h +++ b/views/accessibility/view_accessibility.h @@ -160,6 +160,6 @@ class ATL_NO_VTABLE ViewAccessibility DISALLOW_COPY_AND_ASSIGN(ViewAccessibility); }; -extern const wchar_t kViewsNativeHostPropForAccessibility[]; +extern const char kViewsNativeHostPropForAccessibility[]; #endif // VIEWS_ACCESSIBILITY_VIEW_ACCESSIBILITY_H_ diff --git a/views/controls/native_control.cc b/views/controls/native_control.cc index ef719c9..13881bb 100644 --- a/views/controls/native_control.cc +++ b/views/controls/native_control.cc @@ -13,7 +13,7 @@ #include "app/keyboard_code_conversion_win.h" #include "app/keyboard_codes.h" #include "app/l10n_util_win.h" -#include "app/win/scoped_prop.h" +#include "app/view_prop.h" #include "base/logging.h" #include "base/scoped_ptr.h" #include "base/win_util.h" @@ -24,10 +24,12 @@ #include "views/focus/focus_manager.h" #include "views/widget/widget.h" +using app::ViewProp; + namespace views { // Maps to the NativeControl. -static const wchar_t* const kNativeControlKey = L"__NATIVE_CONTROL__"; +static const char* const kNativeControlKey = "__NATIVE_CONTROL__"; class NativeControlContainer : public CWindowImpl<NativeControlContainer, CWindow, @@ -89,8 +91,7 @@ class NativeControlContainer : public CWindowImpl<NativeControlContainer, // We subclass the control hwnd so we get the WM_KEYDOWN messages. original_handler_ = win_util::SetWindowProc( control_, &NativeControl::NativeControlWndProc); - prop_.reset( - new app::win::ScopedProp(control_, kNativeControlKey , parent_)); + prop_.reset(new ViewProp(control_, kNativeControlKey , parent_)); ::ShowWindow(control_, SW_SHOW); return 1; @@ -166,7 +167,7 @@ class NativeControlContainer : public CWindowImpl<NativeControlContainer, // Message handler that was set before we reset it. WNDPROC original_handler_; - scoped_ptr<app::win::ScopedProp> prop_; + scoped_ptr<ViewProp> prop_; DISALLOW_COPY_AND_ASSIGN(NativeControlContainer); }; @@ -363,8 +364,8 @@ LRESULT CALLBACK NativeControl::NativeControlWndProc(HWND window, UINT message, WPARAM w_param, LPARAM l_param) { - NativeControl* native_control = - static_cast<NativeControl*>(GetProp(window, kNativeControlKey)); + NativeControl* native_control = static_cast<NativeControl*>( + ViewProp::GetValue(window, kNativeControlKey)); DCHECK(native_control); WNDPROC original_handler = native_control->container_->original_handler_; DCHECK(original_handler); diff --git a/views/controls/native_control_win.cc b/views/controls/native_control_win.cc index c4f811c..a223993 100644 --- a/views/controls/native_control_win.cc +++ b/views/controls/native_control_win.cc @@ -7,14 +7,16 @@ #include <windowsx.h> #include "app/l10n_util_win.h" -#include "app/win/scoped_prop.h" +#include "app/view_prop.h" #include "base/logging.h" #include "base/win_util.h" #include "views/focus/focus_manager.h" +using app::ViewProp; + namespace views { -static const wchar_t* kNativeControlWinKey = L"__NATIVE_CONTROL_WIN__"; +static const char* const kNativeControlWinKey = "__NATIVE_CONTROL_WIN__"; //////////////////////////////////////////////////////////////////////////////// // NativeControlWin, public: @@ -130,8 +132,7 @@ void NativeControlWin::ShowContextMenu(const gfx::Point& location) { void NativeControlWin::NativeControlCreated(HWND native_control) { // Associate this object with the control's HWND so that WidgetWin can find // this object when it receives messages from it. - props_.push_back( - new app::win::ScopedProp(native_control, kNativeControlWinKey, this)); + props_.push_back(new ViewProp(native_control, kNativeControlWinKey, this)); props_.push_back(ChildWindowMessageProcessor::Register(native_control, this)); // Subclass so we get WM_KEYDOWN and WM_SETFOCUS messages. @@ -196,8 +197,8 @@ LRESULT NativeControlWin::NativeControlWndProc(HWND window, UINT message, WPARAM w_param, LPARAM l_param) { - NativeControlWin* native_control = - static_cast<NativeControlWin*>(GetProp(window, kNativeControlWinKey)); + NativeControlWin* native_control = reinterpret_cast<NativeControlWin*>( + ViewProp::GetValue(window, kNativeControlWinKey)); DCHECK(native_control); if (message == WM_KEYDOWN && diff --git a/views/controls/native_control_win.h b/views/controls/native_control_win.h index 3733a15..d27efa4 100644 --- a/views/controls/native_control_win.h +++ b/views/controls/native_control_win.h @@ -13,9 +13,7 @@ #include "views/widget/child_window_message_processor.h" namespace app { -namespace win { -class ScopedProp; -} +class ViewProp; } namespace views { @@ -75,7 +73,7 @@ class NativeControlWin : public ChildWindowMessageProcessor, DWORD GetAdditionalRTLStyle() const; private: - typedef ScopedVector<app::win::ScopedProp> ScopedProps; + typedef ScopedVector<app::ViewProp> ViewProps; // Called by the containing WidgetWin when a message of type WM_CTLCOLORBTN or // WM_CTLCOLORSTATIC is sent from the HWND created by an object dreived from @@ -91,7 +89,7 @@ class NativeControlWin : public ChildWindowMessageProcessor, // The window procedure before we subclassed. WNDPROC original_wndproc_; - ScopedProps props_; + ViewProps props_; DISALLOW_COPY_AND_ASSIGN(NativeControlWin); }; diff --git a/views/focus/focus_util_win.cc b/views/focus/focus_util_win.cc index 266a891..23172ad 100644 --- a/views/focus/focus_util_win.cc +++ b/views/focus/focus_util_win.cc @@ -6,24 +6,26 @@ #include <windowsx.h> -#include "app/win/scoped_prop.h" +#include "app/view_prop.h" #include "base/auto_reset.h" #include "base/win_util.h" +using app::ViewProp; + namespace views { // Property used to indicate the HWND supports having mouse wheel messages // rerouted to it. -static const wchar_t* const kHWNDSupportMouseWheelRerouting = - L"__HWND_MW_REROUTE_OK"; +static const char* const kHWNDSupportMouseWheelRerouting = + "__HWND_MW_REROUTE_OK"; static bool WindowSupportsRerouteMouseWheel(HWND window) { while (GetWindowLong(window, GWL_STYLE) & WS_CHILD) { if (!IsWindow(window)) break; - if (reinterpret_cast<bool>(GetProp(window, - kHWNDSupportMouseWheelRerouting))) { + if (reinterpret_cast<bool>( + ViewProp::GetValue(window, kHWNDSupportMouseWheelRerouting))) { return true; } window = GetParent(window); @@ -53,9 +55,9 @@ static bool CanRedirectMouseWheelFrom(HWND window) { return true; } -app::win::ScopedProp* SetWindowSupportsRerouteMouseWheel(HWND hwnd) { - return new app::win::ScopedProp(hwnd, kHWNDSupportMouseWheelRerouting, - reinterpret_cast<HANDLE>(true)); +ViewProp* SetWindowSupportsRerouteMouseWheel(HWND hwnd) { + return new ViewProp(hwnd, kHWNDSupportMouseWheelRerouting, + reinterpret_cast<HANDLE>(true)); } bool RerouteMouseWheel(HWND window, WPARAM w_param, LPARAM l_param) { diff --git a/views/focus/focus_util_win.h b/views/focus/focus_util_win.h index 2c53c68..6394f5d 100644 --- a/views/focus/focus_util_win.h +++ b/views/focus/focus_util_win.h @@ -9,18 +9,16 @@ #include <windows.h> namespace app { -namespace win { -class ScopedProp; -} +class ViewProp; } namespace views { // Marks the passed |hwnd| as supporting mouse-wheel message rerouting. // We reroute the mouse wheel messages to such HWND when they are under the -// mouse pointer (but are not the active window). Callers must delete the -// returned object before the window is destroyed (see ScopedProp for details). -app::win::ScopedProp* SetWindowSupportsRerouteMouseWheel(HWND hwnd); +// mouse pointer (but are not the active window). Callers own the returned +// object. +app::ViewProp* SetWindowSupportsRerouteMouseWheel(HWND hwnd); // Forwards mouse wheel messages to the window under it. // Windows sends mouse wheel messages to the currently active window. diff --git a/views/widget/child_window_message_processor.cc b/views/widget/child_window_message_processor.cc index a3b578e..79a769d 100644 --- a/views/widget/child_window_message_processor.cc +++ b/views/widget/child_window_message_processor.cc @@ -4,24 +4,27 @@ #include "views/widget/child_window_message_processor.h" -#include "app/win/scoped_prop.h" +#include "app/view_prop.h" +#include "base/logging.h" + +#include "app/view_prop.h" namespace views { -static const wchar_t* kChildWindowKey = L"__CHILD_WINDOW_MESSAGE_PROCESSOR__"; +static const char* const kChildWindowKey = "__CHILD_WINDOW_MESSAGE_PROCESSOR__"; // static -app::win::ScopedProp* ChildWindowMessageProcessor::Register( +ViewProp* ChildWindowMessageProcessor::Register( HWND hwnd, ChildWindowMessageProcessor* processor) { DCHECK(processor); - return new app::win::ScopedProp(hwnd, kChildWindowKey, processor); + return new ViewProp(hwnd, kChildWindowKey, processor); } // static ChildWindowMessageProcessor* ChildWindowMessageProcessor::Get(HWND hwnd) { return reinterpret_cast<ChildWindowMessageProcessor*>( - ::GetProp(hwnd, kChildWindowKey)); + ViewProp::GetValue(hwnd, kChildWindowKey)); } } // namespace diff --git a/views/widget/child_window_message_processor.h b/views/widget/child_window_message_processor.h index 7d4e63e..3490864 100644 --- a/views/widget/child_window_message_processor.h +++ b/views/widget/child_window_message_processor.h @@ -9,9 +9,7 @@ #include <windows.h> namespace app { -namespace win { -class ScopedProp; -} +class ViewProp; } namespace views { @@ -25,8 +23,8 @@ class ChildWindowMessageProcessor { public: // Registers |processor| for |hwnd|. The caller takes ownership of the // returned object. - static app::win::ScopedProp* Register(HWND hwnd, - ChildWindowMessageProcessor* processor); + static app::ViewProp* Register(HWND hwnd, + ChildWindowMessageProcessor* processor); // Returns the ChildWindowMessageProcessor for |hwnd|, NULL if there isn't // one. diff --git a/views/widget/widget.h b/views/widget/widget.h index 3b33c90..968ec6a 100644 --- a/views/widget/widget.h +++ b/views/widget/widget.h @@ -207,9 +207,8 @@ class Widget { // Sets/Gets a native window property on the underlying native window object. // Returns NULL if the property does not exist. Setting the property value to // NULL removes the property. - virtual void SetNativeWindowProperty(const std::wstring& name, - void* value) = 0; - virtual void* GetNativeWindowProperty(const std::wstring& name) = 0; + virtual void SetNativeWindowProperty(const char* name, void* value) = 0; + virtual void* GetNativeWindowProperty(const char* name) = 0; // Gets the theme provider. virtual ThemeProvider* GetThemeProvider() const = 0; diff --git a/views/widget/widget_gtk.cc b/views/widget/widget_gtk.cc index 5dea13c..dcb6fb3 100644 --- a/views/widget/widget_gtk.cc +++ b/views/widget/widget_gtk.cc @@ -32,7 +32,6 @@ namespace { // g_object data keys to associate a WidgetGtk object to a GtkWidget. const char* kWidgetKey = "__VIEWS_WIDGET__"; -const wchar_t* kWidgetWideKey = L"__VIEWS_WIDGET__"; // A g_object data key to associate a CompositePainter object to a GtkWidget. const char* kCompositePainterKey = "__VIEWS_COMPOSITE_PAINTER__"; // A g_object data key to associate the flag whether or not the widget @@ -803,13 +802,12 @@ const Window* WidgetGtk::GetWindow() const { return GetWindowImpl(widget_); } -void WidgetGtk::SetNativeWindowProperty(const std::wstring& name, - void* value) { - g_object_set_data(G_OBJECT(widget_), WideToUTF8(name).c_str(), value); +void WidgetGtk::SetNativeWindowProperty(const char* name, void* value) { + g_object_set_data(G_OBJECT(widget_), name, value); } -void* WidgetGtk::GetNativeWindowProperty(const std::wstring& name) { - return g_object_get_data(G_OBJECT(widget_), WideToUTF8(name).c_str()); +void* WidgetGtk::GetNativeWindowProperty(const char* name) { + return g_object_get_data(G_OBJECT(widget_), name); } ThemeProvider* WidgetGtk::GetThemeProvider() const { @@ -1490,7 +1488,7 @@ void WidgetGtk::CreateGtkWidget(GtkWidget* parent, const gfx::Rect& bounds) { } // Setting the WidgetKey property to widget_, which is used by // GetWidgetFromNativeWindow. - SetNativeWindowProperty(kWidgetWideKey, this); + SetNativeWindowProperty(kWidgetKey, this); } void WidgetGtk::ConfigureWidgetForTransparentBackground(GtkWidget* parent) { diff --git a/views/widget/widget_gtk.h b/views/widget/widget_gtk.h index 4135c86..a8a2947 100644 --- a/views/widget/widget_gtk.h +++ b/views/widget/widget_gtk.h @@ -186,9 +186,8 @@ class WidgetGtk virtual bool GetAccelerator(int cmd_id, menus::Accelerator* accelerator); virtual Window* GetWindow(); virtual const Window* GetWindow() const; - virtual void SetNativeWindowProperty(const std::wstring& name, - void* value); - virtual void* GetNativeWindowProperty(const std::wstring& name); + virtual void SetNativeWindowProperty(const char* name, void* value); + virtual void* GetNativeWindowProperty(const char* name); virtual ThemeProvider* GetThemeProvider() const; virtual ThemeProvider* GetDefaultThemeProvider() const; virtual FocusManager* GetFocusManager(); diff --git a/views/widget/widget_win.cc b/views/widget/widget_win.cc index 77cf8c1..62643b2 100644 --- a/views/widget/widget_win.cc +++ b/views/widget/widget_win.cc @@ -7,8 +7,8 @@ #include "app/keyboard_code_conversion_win.h" #include "app/l10n_util_win.h" #include "app/system_monitor.h" +#include "app/view_prop.h" #include "app/win_util.h" -#include "app/win/scoped_prop.h" #include "base/string_util.h" #include "base/win_util.h" #include "gfx/canvas_skia.h" @@ -26,11 +26,15 @@ #include "views/widget/widget_delegate.h" #include "views/window/window_win.h" +using app::ViewProp; + namespace views { // Property used to link the HWND to its RootView. -static const wchar_t* const kRootViewWindowProperty = L"__ROOT_VIEW__"; -static const wchar_t* kWidgetKey = L"__VIEWS_WIDGET__"; +static const char* const kRootViewWindowProperty = "__ROOT_VIEW__"; + +// Links the HWND to it's Widget (as a Widget, not a WidgetWin). +static const char* const kWidgetKey = "__VIEWS_WIDGET__"; bool WidgetWin::screen_reader_active_ = false; @@ -39,7 +43,8 @@ bool WidgetWin::screen_reader_active_ = false; #define OBJID_CUSTOM 1 RootView* GetRootViewForHWND(HWND hwnd) { - return reinterpret_cast<RootView*>(::GetProp(hwnd, kRootViewWindowProperty)); + return reinterpret_cast<RootView*>( + ViewProp::GetValue(hwnd, kRootViewWindowProperty)); } /////////////////////////////////////////////////////////////////////////////// @@ -414,21 +419,21 @@ const Window* WidgetWin::GetWindow() const { return GetWindowImpl(hwnd()); } -void WidgetWin::SetNativeWindowProperty(const std::wstring& name, void* value) { +void WidgetWin::SetNativeWindowProperty(const char* name, void* value) { // Remove the existing property (if any). - for (ScopedProps::iterator i = props_.begin(); i != props_.end(); ++i) { - if ((*i)->key() == name) { + for (ViewProps::iterator i = props_.begin(); i != props_.end(); ++i) { + if ((*i)->Key() == name) { props_.erase(i); break; } } if (value) - props_.push_back(new app::win::ScopedProp(hwnd(), name, value)); + props_.push_back(new ViewProp(hwnd(), name, value)); } -void* WidgetWin::GetNativeWindowProperty(const std::wstring& name) { - return GetProp(hwnd(), name.c_str()); +void* WidgetWin::GetNativeWindowProperty(const char* name) { + return ViewProp::GetValue(hwnd(), name); } ThemeProvider* WidgetWin::GetThemeProvider() const { @@ -1298,8 +1303,7 @@ Widget* Widget::CreatePopupWidget(TransparencyParam transparent, } static BOOL CALLBACK EnumChildProc(HWND hwnd, LPARAM l_param) { - RootView* root_view = - reinterpret_cast<RootView*>(GetProp(hwnd, kRootViewWindowProperty)); + RootView* root_view = GetRootViewForHWND(hwnd); if (root_view) { *reinterpret_cast<RootView**>(l_param) = root_view; return FALSE; // Stop enumerating. @@ -1309,8 +1313,7 @@ static BOOL CALLBACK EnumChildProc(HWND hwnd, LPARAM l_param) { // static RootView* Widget::FindRootView(HWND hwnd) { - RootView* root_view = - reinterpret_cast<RootView*>(GetProp(hwnd, kRootViewWindowProperty)); + RootView* root_view = GetRootViewForHWND(hwnd); if (root_view) return root_view; @@ -1323,8 +1326,7 @@ RootView* Widget::FindRootView(HWND hwnd) { // Enumerate child windows as they could have RootView distinct from // the HWND's root view. BOOL CALLBACK EnumAllRootViewsChildProc(HWND hwnd, LPARAM l_param) { - RootView* root_view = - reinterpret_cast<RootView*>(GetProp(hwnd, kRootViewWindowProperty)); + RootView* root_view = GetRootViewForHWND(hwnd); if (root_view) { std::set<RootView*>* root_views_set = reinterpret_cast<std::set<RootView*>*>(l_param); @@ -1335,8 +1337,7 @@ BOOL CALLBACK EnumAllRootViewsChildProc(HWND hwnd, LPARAM l_param) { void Widget::FindAllRootViews(HWND window, std::vector<RootView*>* root_views) { - RootView* root_view = - reinterpret_cast<RootView*>(GetProp(window, kRootViewWindowProperty)); + RootView* root_view = GetRootViewForHWND(window); std::set<RootView*> root_views_set; if (root_view) root_views_set.insert(root_view); @@ -1356,12 +1357,9 @@ void Widget::FindAllRootViews(HWND window, // static Widget* Widget::GetWidgetFromNativeView(gfx::NativeView native_view) { - if (IsWindow(native_view)) { - HANDLE raw_widget = GetProp(native_view, kWidgetKey); - if (raw_widget) - return reinterpret_cast<Widget*>(raw_widget); - } - return NULL; + return IsWindow(native_view) ? + reinterpret_cast<Widget*>(ViewProp::GetValue(native_view, kWidgetKey)) : + NULL; } // static diff --git a/views/widget/widget_win.h b/views/widget/widget_win.h index bfb9137..8b85e47 100644 --- a/views/widget/widget_win.h +++ b/views/widget/widget_win.h @@ -23,9 +23,7 @@ #include "views/widget/widget.h" namespace app { -namespace win { -class ScopedProp; -} +class ViewProp; } namespace gfx { @@ -234,9 +232,8 @@ class WidgetWin : public gfx::WindowImpl, virtual bool GetAccelerator(int cmd_id, menus::Accelerator* accelerator); virtual Window* GetWindow(); virtual const Window* GetWindow() const; - virtual void SetNativeWindowProperty(const std::wstring& name, - void* value); - virtual void* GetNativeWindowProperty(const std::wstring& name); + virtual void SetNativeWindowProperty(const char* name, void* value); + virtual void* GetNativeWindowProperty(const char* name); virtual ThemeProvider* GetThemeProvider() const; virtual ThemeProvider* GetDefaultThemeProvider() const; virtual FocusManager* GetFocusManager(); @@ -487,7 +484,7 @@ class WidgetWin : public gfx::WindowImpl, bool is_window_; private: - typedef ScopedVector<app::win::ScopedProp> ScopedProps; + typedef ScopedVector<app::ViewProp> ViewProps; // Implementation of GetWindow. Ascends the parents of |hwnd| returning the // first ancestor that is a Window. @@ -595,7 +592,7 @@ class WidgetWin : public gfx::WindowImpl, // we always mod this value with the max view events above . int accessibility_view_events_index_; - ScopedProps props_; + ViewProps props_; DISALLOW_COPY_AND_ASSIGN(WidgetWin); }; |