OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/browser_window_state.h" | 5 #include "chrome/browser/ui/browser_window_state.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/prefs/pref_service.h" | 8 #include "base/prefs/pref_service.h" |
| 9 #include "base/prefs/scoped_user_pref_update.h" |
9 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
10 #include "chrome/browser/defaults.h" | 11 #include "chrome/browser/defaults.h" |
11 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
12 #include "chrome/browser/sessions/session_service.h" | 13 #include "chrome/browser/sessions/session_service.h" |
13 #include "chrome/browser/sessions/session_service_factory.h" | 14 #include "chrome/browser/sessions/session_service_factory.h" |
14 #include "chrome/browser/ui/browser.h" | 15 #include "chrome/browser/ui/browser.h" |
15 #include "chrome/browser/ui/window_sizer/window_sizer.h" | 16 #include "chrome/browser/ui/window_sizer/window_sizer.h" |
16 #include "chrome/common/chrome_switches.h" | 17 #include "chrome/common/chrome_switches.h" |
17 #include "chrome/common/pref_names.h" | 18 #include "chrome/common/pref_names.h" |
18 | 19 |
(...skipping 14 matching lines...) Expand all Loading... |
33 int num2 = 0; | 34 int num2 = 0; |
34 if (!base::StringToInt(str.substr(0, num1_size), &num1) || | 35 if (!base::StringToInt(str.substr(0, num1_size), &num1) || |
35 !base::StringToInt(str.substr(num2_pos, num2_size), &num2)) | 36 !base::StringToInt(str.substr(num2_pos, num2_size), &num2)) |
36 return false; | 37 return false; |
37 | 38 |
38 *ret_num1 = num1; | 39 *ret_num1 = num1; |
39 *ret_num2 = num2; | 40 *ret_num2 = num2; |
40 return true; | 41 return true; |
41 } | 42 } |
42 | 43 |
| 44 class WindowPlacementPrefUpdate : public DictionaryPrefUpdate { |
| 45 public: |
| 46 WindowPlacementPrefUpdate(PrefService* service, |
| 47 const std::string& window_name) |
| 48 : DictionaryPrefUpdate(service, prefs::kAppWindowPlacement), |
| 49 window_name_(window_name) {} |
| 50 |
| 51 virtual ~WindowPlacementPrefUpdate() {} |
| 52 |
| 53 virtual base::DictionaryValue* Get() OVERRIDE { |
| 54 base::DictionaryValue* all_apps_dict = DictionaryPrefUpdate::Get(); |
| 55 base::DictionaryValue* this_app_dict = NULL; |
| 56 if (!all_apps_dict->GetDictionary(window_name_, &this_app_dict)) { |
| 57 this_app_dict = new base::DictionaryValue; |
| 58 all_apps_dict->Set(window_name_, this_app_dict); |
| 59 } |
| 60 return this_app_dict; |
| 61 } |
| 62 |
| 63 private: |
| 64 const std::string window_name_; |
| 65 |
| 66 DISALLOW_COPY_AND_ASSIGN(WindowPlacementPrefUpdate); |
| 67 }; |
| 68 |
43 } // namespace | 69 } // namespace |
44 | 70 |
45 std::string GetWindowPlacementKey(const Browser* browser) { | 71 std::string GetWindowName(const Browser* browser) { |
46 if (browser->app_name().empty()) { | 72 if (browser->app_name().empty()) { |
47 return browser->is_type_popup() ? | 73 return browser->is_type_popup() ? |
48 prefs::kBrowserWindowPlacementPopup : prefs::kBrowserWindowPlacement; | 74 prefs::kBrowserWindowPlacementPopup : prefs::kBrowserWindowPlacement; |
49 } | 75 } |
50 return std::string(prefs::kBrowserWindowPlacement) + "_" + | 76 return browser->app_name(); |
51 browser->app_name(); | 77 } |
| 78 |
| 79 scoped_ptr<DictionaryPrefUpdate> GetWindowPlacementDictionaryReadWrite( |
| 80 const std::string& window_name, |
| 81 PrefService* prefs) { |
| 82 DCHECK(!window_name.empty()); |
| 83 // A normal DictionaryPrefUpdate will suffice for non-app windows. |
| 84 if (prefs->FindPreference(window_name.c_str())) { |
| 85 return make_scoped_ptr( |
| 86 new DictionaryPrefUpdate(prefs, window_name.c_str())); |
| 87 } |
| 88 return scoped_ptr<DictionaryPrefUpdate>( |
| 89 new WindowPlacementPrefUpdate(prefs, window_name)); |
| 90 } |
| 91 |
| 92 const base::DictionaryValue* GetWindowPlacementDictionaryReadOnly( |
| 93 const std::string& window_name, |
| 94 PrefService* prefs) { |
| 95 DCHECK(!window_name.empty()); |
| 96 if (prefs->FindPreference(window_name.c_str())) |
| 97 return prefs->GetDictionary(window_name.c_str()); |
| 98 |
| 99 const base::DictionaryValue* app_windows = |
| 100 prefs->GetDictionary(prefs::kAppWindowPlacement); |
| 101 if (!app_windows) |
| 102 return NULL; |
| 103 const base::DictionaryValue* to_return = NULL; |
| 104 app_windows->GetDictionary(window_name, &to_return); |
| 105 return to_return; |
52 } | 106 } |
53 | 107 |
54 bool ShouldSaveWindowPlacement(const Browser* browser) { | 108 bool ShouldSaveWindowPlacement(const Browser* browser) { |
55 // Only save the window placement of popups if the window is from a trusted | 109 // Only save the window placement of popups if the window is from a trusted |
56 // source (v1 app, devtools, or system window). | 110 // source (v1 app, devtools, or system window). |
57 return (browser->type() == Browser::TYPE_TABBED) || | 111 return (browser->type() == Browser::TYPE_TABBED) || |
58 ((browser->type() == Browser::TYPE_POPUP) && browser->is_trusted_source()); | 112 ((browser->type() == Browser::TYPE_POPUP) && browser->is_trusted_source()); |
59 } | 113 } |
60 | 114 |
61 void SaveWindowPlacement(const Browser* browser, | 115 void SaveWindowPlacement(const Browser* browser, |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 if (parsed_command_line.HasSwitch(switches::kWindowPosition)) { | 161 if (parsed_command_line.HasSwitch(switches::kWindowPosition)) { |
108 std::string str = | 162 std::string str = |
109 parsed_command_line.GetSwitchValueASCII(switches::kWindowPosition); | 163 parsed_command_line.GetSwitchValueASCII(switches::kWindowPosition); |
110 int x, y; | 164 int x, y; |
111 if (ParseCommaSeparatedIntegers(str, &x, &y)) | 165 if (ParseCommaSeparatedIntegers(str, &x, &y)) |
112 bounds->set_origin(gfx::Point(x, y)); | 166 bounds->set_origin(gfx::Point(x, y)); |
113 } | 167 } |
114 } | 168 } |
115 | 169 |
116 } // namespace chrome | 170 } // namespace chrome |
OLD | NEW |