1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "chrome/browser/ui/views/chrome_views_delegate.h" 6 7 #include "base/memory/scoped_ptr.h" 8 #include "base/string_util.h" 9 #include "base/utf_string_conversions.h" 10 #include "chrome/browser/browser_process.h" 11 #include "chrome/browser/prefs/pref_service.h" 12 #include "chrome/browser/prefs/scoped_user_pref_update.h" 13 #include "chrome/browser/profiles/profile_manager.h" 14 #include "chrome/browser/ui/views/accessibility_event_router_views.h" 15 #include "chrome/browser/ui/window_sizer.h" 16 #include "chrome/common/pref_names.h" 17 #include "ui/base/clipboard/clipboard.h" 18 #include "ui/gfx/rect.h" 19 #include "views/widget/native_widget.h" 20 #include "views/widget/widget.h" 21 #include "views/window/window.h" 22 23 #if defined(OS_WIN) 24 #include "chrome/browser/app_icon_win.h" 25 #endif 26 27 namespace { 28 29 // If the given window has a profile associated with it, use that profile's 30 // preference service. Otherwise, store and retrieve the data from Local State. 31 // This function may return NULL if the necessary pref service has not yet 32 // been initialized. 33 // TODO(mirandac): This function will also separate windows by profile in a 34 // multi-profile environment. 35 PrefService* GetPrefsForWindow(views::Window* window) { 36 Profile* profile = reinterpret_cast<Profile*>( 37 window->AsWidget()->native_widget()->GetNativeWindowProperty( 38 Profile::kProfileKey)); 39 if (!profile) { 40 // Use local state for windows that have no explicit profile. 41 return g_browser_process->local_state(); 42 } 43 return profile->GetPrefs(); 44 } 45 46 } // namespace 47 48 /////////////////////////////////////////////////////////////////////////////// 49 // ChromeViewsDelegate, views::ViewsDelegate implementation: 50 51 ui::Clipboard* ChromeViewsDelegate::GetClipboard() const { 52 return g_browser_process->clipboard(); 53 } 54 55 void ChromeViewsDelegate::SaveWindowPlacement(views::Window* window, 56 const std::wstring& window_name, 57 const gfx::Rect& bounds, 58 bool maximized) { 59 PrefService* prefs = GetPrefsForWindow(window); 60 if (!prefs) 61 return; 62 63 DCHECK(prefs->FindPreference(WideToUTF8(window_name).c_str())); 64 DictionaryPrefUpdate update(prefs, WideToUTF8(window_name).c_str()); 65 DictionaryValue* window_preferences = update.Get(); 66 window_preferences->SetInteger("left", bounds.x()); 67 window_preferences->SetInteger("top", bounds.y()); 68 window_preferences->SetInteger("right", bounds.right()); 69 window_preferences->SetInteger("bottom", bounds.bottom()); 70 window_preferences->SetBoolean("maximized", maximized); 71 72 scoped_ptr<WindowSizer::MonitorInfoProvider> monitor_info_provider( 73 WindowSizer::CreateDefaultMonitorInfoProvider()); 74 gfx::Rect work_area( 75 monitor_info_provider->GetMonitorWorkAreaMatching(bounds)); 76 window_preferences->SetInteger("work_area_left", work_area.x()); 77 window_preferences->SetInteger("work_area_top", work_area.y()); 78 window_preferences->SetInteger("work_area_right", work_area.right()); 79 window_preferences->SetInteger("work_area_bottom", work_area.bottom()); 80 } 81 82 bool ChromeViewsDelegate::GetSavedWindowBounds(views::Window* window, 83 const std::wstring& window_name, 84 gfx::Rect* bounds) const { 85 PrefService* prefs = GetPrefsForWindow(window); 86 if (!prefs) 87 return false; 88 89 DCHECK(prefs->FindPreference(WideToUTF8(window_name).c_str())); 90 const DictionaryValue* dictionary = 91 prefs->GetDictionary(WideToUTF8(window_name).c_str()); 92 int left, top, right, bottom; 93 if (!dictionary || !dictionary->GetInteger("left", &left) || 94 !dictionary->GetInteger("top", &top) || 95 !dictionary->GetInteger("right", &right) || 96 !dictionary->GetInteger("bottom", &bottom)) 97 return false; 98 99 bounds->SetRect(left, top, right - left, bottom - top); 100 return true; 101 } 102 103 bool ChromeViewsDelegate::GetSavedMaximizedState( 104 views::Window* window, 105 const std::wstring& window_name, 106 bool* maximized) const { 107 PrefService* prefs = GetPrefsForWindow(window); 108 if (!prefs) 109 return false; 110 111 DCHECK(prefs->FindPreference(WideToUTF8(window_name).c_str())); 112 const DictionaryValue* dictionary = 113 prefs->GetDictionary(WideToUTF8(window_name).c_str()); 114 115 return dictionary && dictionary->GetBoolean("maximized", maximized) && 116 maximized; 117 } 118 119 void ChromeViewsDelegate::NotifyAccessibilityEvent( 120 views::View* view, ui::AccessibilityTypes::Event event_type) { 121 AccessibilityEventRouterViews::GetInstance()->HandleAccessibilityEvent( 122 view, event_type); 123 } 124 125 void ChromeViewsDelegate::NotifyMenuItemFocused( 126 const std::wstring& menu_name, 127 const std::wstring& menu_item_name, 128 int item_index, 129 int item_count, 130 bool has_submenu) { 131 AccessibilityEventRouterViews::GetInstance()->HandleMenuItemFocused( 132 menu_name, menu_item_name, item_index, item_count, has_submenu); 133 } 134 135 #if defined(OS_WIN) 136 HICON ChromeViewsDelegate::GetDefaultWindowIcon() const { 137 return GetAppIcon(); 138 } 139 #endif 140 141 void ChromeViewsDelegate::AddRef() { 142 g_browser_process->AddRefModule(); 143 } 144 145 void ChromeViewsDelegate::ReleaseRef() { 146 g_browser_process->ReleaseModule(); 147 } 148