summaryrefslogtreecommitdiffstats
path: root/chrome/browser/apps/drive/drive_app_mapping.cc
blob: 7b50d46605426a0fd4b3a136d0d04cd04ba83479 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/apps/drive/drive_app_mapping.h"

#include <stddef.h>

#include "base/prefs/pref_service.h"
#include "base/prefs/scoped_user_pref_update.h"
#include "base/values.h"
#include "chrome/common/pref_names.h"
#include "components/pref_registry/pref_registry_syncable.h"

namespace {

// Key for a string value that holds the mapped chrome app id.
const char kKeyChromeApp[] = "chrome_app";

// Key for a boolean value of whether the mapped chrome app is auto generated.
// Default is false.
const char kKeyGenerated[] = "generated";

scoped_ptr<base::DictionaryValue> CreateInfoDict(
    const std::string& chrome_app_id,
    bool generated) {
  scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue);
  dict->SetStringWithoutPathExpansion(kKeyChromeApp, chrome_app_id);

  // Only writes non-default value.
  if (generated)
    dict->SetBooleanWithoutPathExpansion(kKeyGenerated, true);
  return dict;
}

}  // namespace

DriveAppMapping::DriveAppMapping(PrefService* prefs) : prefs_(prefs) {
  GetUninstalledIdsFromPref();
}

DriveAppMapping::~DriveAppMapping() {
}

// static
void DriveAppMapping::RegisterProfilePrefs(
    user_prefs::PrefRegistrySyncable* registry) {
  registry->RegisterDictionaryPref(prefs::kAppLauncherDriveAppMapping);
  registry->RegisterListPref(prefs::kAppLauncherUninstalledDriveApps);
}

void DriveAppMapping::Add(const std::string& drive_app_id,
                          const std::string& chrome_app_id,
                          bool generated) {
  DictionaryPrefUpdate update(prefs_, prefs::kAppLauncherDriveAppMapping);
  update->SetWithoutPathExpansion(
      drive_app_id, CreateInfoDict(chrome_app_id, generated).release());
}

void DriveAppMapping::Remove(const std::string& drive_app_id) {
  DictionaryPrefUpdate update(prefs_, prefs::kAppLauncherDriveAppMapping);
  update->RemoveWithoutPathExpansion(drive_app_id, NULL);
}

std::string DriveAppMapping::GetChromeApp(
    const std::string& drive_app_id) const {
  const base::DictionaryValue* mapping =
      prefs_->GetDictionary(prefs::kAppLauncherDriveAppMapping);
  const base::DictionaryValue* info_dict;
  std::string chrome_app_id;
  if (mapping->GetDictionaryWithoutPathExpansion(drive_app_id, &info_dict) &&
      info_dict->GetStringWithoutPathExpansion(kKeyChromeApp, &chrome_app_id)) {
    return chrome_app_id;
  }

  return std::string();
}

std::string DriveAppMapping::GetDriveApp(
    const std::string& chrome_app_id) const {
  const base::DictionaryValue* mapping =
      prefs_->GetDictionary(prefs::kAppLauncherDriveAppMapping);
  for (base::DictionaryValue::Iterator it(*mapping); !it.IsAtEnd();
       it.Advance()) {
    const base::DictionaryValue* info_dict;
    std::string value_string;
    DCHECK(it.value().IsType(base::Value::TYPE_DICTIONARY));
    if (it.value().GetAsDictionary(&info_dict) &&
        info_dict->GetStringWithoutPathExpansion(kKeyChromeApp,
                                                 &value_string) &&
        value_string == chrome_app_id) {
      return it.key();
    }
  }
  return std::string();
}

bool DriveAppMapping::IsChromeAppGenerated(
    const std::string& chrome_app_id) const {
  const base::DictionaryValue* mapping =
      prefs_->GetDictionary(prefs::kAppLauncherDriveAppMapping);
  for (base::DictionaryValue::Iterator it(*mapping); !it.IsAtEnd();
       it.Advance()) {
    const base::DictionaryValue* info_dict;
    std::string value_string;
    bool generated = false;
    DCHECK(it.value().IsType(base::Value::TYPE_DICTIONARY));
    if (it.value().GetAsDictionary(&info_dict) &&
        info_dict->GetStringWithoutPathExpansion(kKeyChromeApp,
                                                 &value_string) &&
        value_string == chrome_app_id &&
        info_dict->GetBooleanWithoutPathExpansion(kKeyGenerated, &generated)) {
      return generated;
    }
  }

  return false;
}

std::set<std::string> DriveAppMapping::GetDriveAppIds() const {
  const base::DictionaryValue* mapping =
      prefs_->GetDictionary(prefs::kAppLauncherDriveAppMapping);
  std::set<std::string> keys;
  for (base::DictionaryValue::Iterator it(*mapping); !it.IsAtEnd();
       it.Advance()) {
    keys.insert(it.key());
  }
  return keys;
}

void DriveAppMapping::AddUninstalledDriveApp(const std::string& drive_app_id) {
  if (IsUninstalledDriveApp(drive_app_id))
    return;
  uninstalled_app_ids_.insert(drive_app_id);
  UpdateUninstalledList();
}

void DriveAppMapping::RemoveUninstalledDriveApp(
    const std::string& drive_app_id) {
  auto it = uninstalled_app_ids_.find(drive_app_id);
  if (it == uninstalled_app_ids_.end())
    return;
  uninstalled_app_ids_.erase(it);
  UpdateUninstalledList();
}

bool DriveAppMapping::IsUninstalledDriveApp(
    const std::string& drive_app_id) const {
  return uninstalled_app_ids_.find(drive_app_id) != uninstalled_app_ids_.end();
}

void DriveAppMapping::GetUninstalledIdsFromPref() {
  uninstalled_app_ids_.clear();
  const base::ListValue* list =
      prefs_->GetList(prefs::kAppLauncherUninstalledDriveApps);
  for (size_t i = 0; i < list->GetSize(); ++i) {
    std::string app_id;
    if (!list->GetString(i, &app_id))
      continue;
    uninstalled_app_ids_.insert(app_id);
  }
}

void DriveAppMapping::UpdateUninstalledList() {
  ListPrefUpdate update(prefs_, prefs::kAppLauncherUninstalledDriveApps);
  update->Clear();
  for (const auto& app_id : uninstalled_app_ids_)
    update->AppendString(app_id);
}