• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2014 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/prefs/browser_ui_prefs_migrator.h"
6 
7 #include <set>
8 
9 #include "base/memory/scoped_ptr.h"
10 #include "base/strings/string_util.h"
11 #include "base/values.h"
12 #include "chrome/common/pref_names.h"
13 
BrowserUIPrefsMigrator(WriteablePrefStore * pref_store)14 BrowserUIPrefsMigrator::BrowserUIPrefsMigrator(WriteablePrefStore* pref_store)
15     : pref_store_(pref_store) {
16 }
17 
~BrowserUIPrefsMigrator()18 BrowserUIPrefsMigrator::~BrowserUIPrefsMigrator() {
19 }
20 
OnInitializationCompleted(bool succeeded)21 void BrowserUIPrefsMigrator::OnInitializationCompleted(
22     bool succeeded) {
23   pref_store_->RemoveObserver(this);
24   scoped_ptr<BrowserUIPrefsMigrator> self_deleter(this);
25   if (!succeeded)
26     return;
27 
28   base::Value* browser_value = NULL;
29   if (!pref_store_->GetMutableValue("browser", &browser_value))
30     return;
31 
32   base::DictionaryValue* browser_dict = NULL;
33   if (!browser_value->GetAsDictionary(&browser_dict))
34     return;
35 
36   // Don't bother scanning "browser" if the migration already occurred.
37   if (browser_dict->HasKey("app_window_placement"))
38     return;
39 
40   // Get a set of keys in the dictionary. This must be done separately from the
41   // migration because the migration modifies the dictionary being iterated.
42   std::set<std::string> keys_to_check;
43   for (base::DictionaryValue::Iterator it(*browser_dict); !it.IsAtEnd();
44        it.Advance()) {
45     keys_to_check.insert(it.key());
46   }
47 
48   scoped_ptr<base::DictionaryValue> app_window_placement;
49   // Apps used to have their window placement preferences registered as
50   // "browser.window_placement_$APPNAME".
51   const std::string search_for =
52       std::string(prefs::kBrowserWindowPlacement) + "_";
53   for (std::set<std::string>::const_iterator it = keys_to_check.begin();
54        it != keys_to_check.end();
55        ++it) {
56     std::string full_key("browser." + *it);
57     if (StartsWithASCII(full_key, search_for, true /* case_sensitive */)) {
58       if (full_key == prefs::kBrowserWindowPlacementPopup)
59         continue;
60       scoped_ptr<base::Value> single_app_placement_dict;
61       bool found = browser_dict->Remove(*it, &single_app_placement_dict);
62       DCHECK(found);
63       std::string new_key(full_key.substr(search_for.length()));
64       if (!app_window_placement)
65         app_window_placement.reset(new base::DictionaryValue);
66       app_window_placement->Set(new_key, single_app_placement_dict.release());
67     }
68   }
69   if (app_window_placement) {
70     pref_store_->SetValue(prefs::kAppWindowPlacement,
71                           app_window_placement.release());
72   }
73 }
74