1 // Copyright 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.
5 #include "chrome/browser/apps/drive/drive_app_mapping.h"
7 #include "base/prefs/pref_service.h"
8 #include "base/prefs/scoped_user_pref_update.h"
9 #include "base/values.h"
10 #include "chrome/common/pref_names.h"
11 #include "components/pref_registry/pref_registry_syncable.h"
15 // Key for a string value that holds the mapped chrome app id.
16 const char kKeyChromeApp[] = "chrome_app";
18 // Key for a boolean value of whether the mapped chrome app is auto generated.
20 const char kKeyGenerated[] = "generated";
22 scoped_ptr<base::DictionaryValue> CreateInfoDict(
23 const std::string& chrome_app_id,
25 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue);
26 dict->SetStringWithoutPathExpansion(kKeyChromeApp, chrome_app_id);
28 // Only writes non-default value.
30 dict->SetBooleanWithoutPathExpansion(kKeyGenerated, true);
36 DriveAppMapping::DriveAppMapping(PrefService* prefs) : prefs_(prefs) {
39 DriveAppMapping::~DriveAppMapping() {
43 void DriveAppMapping::RegisterProfilePrefs(
44 user_prefs::PrefRegistrySyncable* registry) {
45 registry->RegisterDictionaryPref(
46 prefs::kAppLauncherDriveAppMapping,
47 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
50 void DriveAppMapping::Add(const std::string& drive_app_id,
51 const std::string& chrome_app_id,
53 DictionaryPrefUpdate update(prefs_, prefs::kAppLauncherDriveAppMapping);
54 update->SetWithoutPathExpansion(
55 drive_app_id, CreateInfoDict(chrome_app_id, generated).release());
58 void DriveAppMapping::Remove(const std::string& drive_app_id) {
59 DictionaryPrefUpdate update(prefs_, prefs::kAppLauncherDriveAppMapping);
60 update->RemoveWithoutPathExpansion(drive_app_id, NULL);
63 std::string DriveAppMapping::GetChromeApp(
64 const std::string& drive_app_id) const {
65 const base::DictionaryValue* mapping =
66 prefs_->GetDictionary(prefs::kAppLauncherDriveAppMapping);
67 const base::DictionaryValue* info_dict;
68 std::string chrome_app_id;
69 if (mapping->GetDictionaryWithoutPathExpansion(drive_app_id, &info_dict) &&
70 info_dict->GetStringWithoutPathExpansion(kKeyChromeApp, &chrome_app_id)) {
77 std::string DriveAppMapping::GetDriveApp(
78 const std::string& chrome_app_id) const {
79 const base::DictionaryValue* mapping =
80 prefs_->GetDictionary(prefs::kAppLauncherDriveAppMapping);
81 for (base::DictionaryValue::Iterator it(*mapping); !it.IsAtEnd();
83 const base::DictionaryValue* info_dict;
84 std::string value_string;
85 DCHECK(it.value().IsType(base::Value::TYPE_DICTIONARY));
86 if (it.value().GetAsDictionary(&info_dict) &&
87 info_dict->GetStringWithoutPathExpansion(kKeyChromeApp,
89 value_string == chrome_app_id) {
96 bool DriveAppMapping::IsChromeAppGenerated(
97 const std::string& chrome_app_id) const {
98 const base::DictionaryValue* mapping =
99 prefs_->GetDictionary(prefs::kAppLauncherDriveAppMapping);
100 for (base::DictionaryValue::Iterator it(*mapping); !it.IsAtEnd();
102 const base::DictionaryValue* info_dict;
103 std::string value_string;
104 bool generated = false;
105 DCHECK(it.value().IsType(base::Value::TYPE_DICTIONARY));
106 if (it.value().GetAsDictionary(&info_dict) &&
107 info_dict->GetStringWithoutPathExpansion(kKeyChromeApp,
109 value_string == chrome_app_id &&
110 info_dict->GetBooleanWithoutPathExpansion(kKeyGenerated, &generated)) {
118 std::set<std::string> DriveAppMapping::GetDriveAppIds() const {
119 const base::DictionaryValue* mapping =
120 prefs_->GetDictionary(prefs::kAppLauncherDriveAppMapping);
121 std::set<std::string> keys;
122 for (base::DictionaryValue::Iterator it(*mapping); !it.IsAtEnd();
124 keys.insert(it.key());