#include "base/command_line.h"
#include "base/prefs/pref_service.h"
+#include "base/prefs/scoped_user_pref_update.h"
#include "base/strings/string_number_conversions.h"
#include "chrome/browser/defaults.h"
#include "chrome/browser/profiles/profile.h"
size_t num2_pos = num1_size + 1;
size_t num2_size = str.size() - num2_pos;
- int num1, num2;
+ int num1 = 0;
+ int num2 = 0;
if (!base::StringToInt(str.substr(0, num1_size), &num1) ||
!base::StringToInt(str.substr(num2_pos, num2_size), &num2))
return false;
return true;
}
+class WindowPlacementPrefUpdate : public DictionaryPrefUpdate {
+ public:
+ WindowPlacementPrefUpdate(PrefService* service,
+ const std::string& window_name)
+ : DictionaryPrefUpdate(service, prefs::kAppWindowPlacement),
+ window_name_(window_name) {}
+
+ ~WindowPlacementPrefUpdate() override {}
+
+ base::DictionaryValue* Get() override {
+ base::DictionaryValue* all_apps_dict = DictionaryPrefUpdate::Get();
+ base::DictionaryValue* this_app_dict = NULL;
+ if (!all_apps_dict->GetDictionary(window_name_, &this_app_dict)) {
+ this_app_dict = new base::DictionaryValue;
+ all_apps_dict->Set(window_name_, this_app_dict);
+ }
+ return this_app_dict;
+ }
+
+ private:
+ const std::string window_name_;
+
+ DISALLOW_COPY_AND_ASSIGN(WindowPlacementPrefUpdate);
+};
+
} // namespace
-std::string GetWindowPlacementKey(const Browser* browser) {
- std::string name(prefs::kBrowserWindowPlacement);
- if (!browser->app_name().empty()) {
- name.append("_");
- name.append(browser->app_name());
+std::string GetWindowName(const Browser* browser) {
+ if (browser->app_name().empty()) {
+ return browser->is_type_popup() ?
+ prefs::kBrowserWindowPlacementPopup : prefs::kBrowserWindowPlacement;
}
- return name;
+ return browser->app_name();
}
-bool ShouldSaveWindowPlacement(const Browser* browser) {
- switch (browser->type()) {
- case Browser::TYPE_TABBED:
- return true;
- case Browser::TYPE_POPUP:
- // Only save the window placement of popups if they are restored,
- // or the window belongs to DevTools or an App.
- return browser_defaults::kRestorePopups || browser->is_devtools() ||
- browser->is_app();
- default:
- return false;
+scoped_ptr<DictionaryPrefUpdate> GetWindowPlacementDictionaryReadWrite(
+ const std::string& window_name,
+ PrefService* prefs) {
+ DCHECK(!window_name.empty());
+ // A normal DictionaryPrefUpdate will suffice for non-app windows.
+ if (prefs->FindPreference(window_name.c_str())) {
+ return make_scoped_ptr(
+ new DictionaryPrefUpdate(prefs, window_name.c_str()));
}
+ return scoped_ptr<DictionaryPrefUpdate>(
+ new WindowPlacementPrefUpdate(prefs, window_name));
+}
+
+const base::DictionaryValue* GetWindowPlacementDictionaryReadOnly(
+ const std::string& window_name,
+ PrefService* prefs) {
+ DCHECK(!window_name.empty());
+ if (prefs->FindPreference(window_name.c_str()))
+ return prefs->GetDictionary(window_name.c_str());
+
+ const base::DictionaryValue* app_windows =
+ prefs->GetDictionary(prefs::kAppWindowPlacement);
+ if (!app_windows)
+ return NULL;
+ const base::DictionaryValue* to_return = NULL;
+ app_windows->GetDictionary(window_name, &to_return);
+ return to_return;
+}
+
+bool ShouldSaveWindowPlacement(const Browser* browser) {
+ // Only save the window placement of popups if the window is from a trusted
+ // source (v1 app, devtools, or system window).
+ return (browser->type() == Browser::TYPE_TABBED) ||
+ ((browser->type() == Browser::TYPE_POPUP) && browser->is_trusted_source());
}
void SaveWindowPlacement(const Browser* browser,