drive_app_mapping.cc revision 6d86b77056ed63eb6871182f42a9fd5f07550f90
16d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved. 26d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 36d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)// found in the LICENSE file. 46d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) 56d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)#include "chrome/browser/apps/drive/drive_app_mapping.h" 66d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) 76d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)#include "base/prefs/pref_service.h" 86d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)#include "base/prefs/scoped_user_pref_update.h" 96d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)#include "base/values.h" 106d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)#include "chrome/common/pref_names.h" 116d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)#include "components/pref_registry/pref_registry_syncable.h" 126d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) 136d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)namespace { 146d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) 156d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)// Key for a string value that holds the mapped chrome app id. 166d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)const char kKeyChromeApp[] = "chrome_app"; 176d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) 186d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)// Key for a boolean value of whether the mapped chrome app is auto generated. 196d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)// Default is false. 206d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)const char kKeyGenerated[] = "generated"; 216d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) 226d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)scoped_ptr<base::DictionaryValue> CreateInfoDict( 236d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) const std::string& chrome_app_id, 246d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) bool generated) { 256d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue); 266d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) dict->SetStringWithoutPathExpansion(kKeyChromeApp, chrome_app_id); 276d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) 286d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) // Only writes non-default value. 296d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) if (generated) 306d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) dict->SetBooleanWithoutPathExpansion(kKeyGenerated, true); 316d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) return dict.Pass(); 326d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)} 336d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) 346d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)} // namespace 356d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) 366d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)DriveAppMapping::DriveAppMapping(PrefService* prefs) : prefs_(prefs) { 376d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)} 386d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) 396d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)DriveAppMapping::~DriveAppMapping() { 406d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)} 416d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) 426d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)// static 436d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)void DriveAppMapping::RegisterProfilePrefs( 446d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) user_prefs::PrefRegistrySyncable* registry) { 456d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) registry->RegisterDictionaryPref( 466d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) prefs::kAppLauncherDriveAppMapping, 476d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); 486d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)} 496d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) 506d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)void DriveAppMapping::Add(const std::string& drive_app_id, 516d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) const std::string& chrome_app_id, 526d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) bool generated) { 536d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) DictionaryPrefUpdate update(prefs_, prefs::kAppLauncherDriveAppMapping); 546d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) update->SetWithoutPathExpansion( 556d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) drive_app_id, CreateInfoDict(chrome_app_id, generated).release()); 566d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)} 576d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) 586d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)void DriveAppMapping::Remove(const std::string& drive_app_id) { 596d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) DictionaryPrefUpdate update(prefs_, prefs::kAppLauncherDriveAppMapping); 606d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) update->RemoveWithoutPathExpansion(drive_app_id, NULL); 616d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)} 626d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) 636d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)std::string DriveAppMapping::GetChromeApp( 646d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) const std::string& drive_app_id) const { 656d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) const base::DictionaryValue* mapping = 666d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) prefs_->GetDictionary(prefs::kAppLauncherDriveAppMapping); 676d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) const base::DictionaryValue* info_dict; 686d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) std::string chrome_app_id; 696d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) if (mapping->GetDictionaryWithoutPathExpansion(drive_app_id, &info_dict) && 706d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) info_dict->GetStringWithoutPathExpansion(kKeyChromeApp, &chrome_app_id)) { 716d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) return chrome_app_id; 726d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) } 736d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) 746d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) return std::string(); 756d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)} 766d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) 776d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)std::string DriveAppMapping::GetDriveApp( 786d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) const std::string& chrome_app_id) const { 796d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) const base::DictionaryValue* mapping = 806d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) prefs_->GetDictionary(prefs::kAppLauncherDriveAppMapping); 816d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) for (base::DictionaryValue::Iterator it(*mapping); !it.IsAtEnd(); 826d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) it.Advance()) { 836d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) const base::DictionaryValue* info_dict; 846d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) std::string value_string; 856d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) DCHECK(it.value().IsType(base::Value::TYPE_DICTIONARY)); 866d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) if (it.value().GetAsDictionary(&info_dict) && 876d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) info_dict->GetStringWithoutPathExpansion(kKeyChromeApp, 886d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) &value_string) && 896d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) value_string == chrome_app_id) { 906d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) return it.key(); 916d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) } 926d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) } 936d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) return std::string(); 946d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)} 956d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) 966d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)bool DriveAppMapping::IsChromeAppGenerated( 976d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) const std::string& chrome_app_id) const { 986d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) const base::DictionaryValue* mapping = 996d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) prefs_->GetDictionary(prefs::kAppLauncherDriveAppMapping); 1006d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) for (base::DictionaryValue::Iterator it(*mapping); !it.IsAtEnd(); 1016d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) it.Advance()) { 1026d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) const base::DictionaryValue* info_dict; 1036d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) std::string value_string; 1046d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) bool generated; 1056d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) DCHECK(it.value().IsType(base::Value::TYPE_DICTIONARY)); 1066d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) if (it.value().GetAsDictionary(&info_dict) && 1076d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) info_dict->GetStringWithoutPathExpansion(kKeyChromeApp, 1086d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) &value_string) && 1096d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) value_string == chrome_app_id && 1106d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) info_dict->GetBooleanWithoutPathExpansion(kKeyGenerated, &generated)) { 1116d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) return generated; 1126d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) } 1136d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) } 1146d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) 1156d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) return false; 1166d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)} 1176d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) 1186d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)std::set<std::string> DriveAppMapping::GetDriveAppIds() const { 1196d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) const base::DictionaryValue* mapping = 1206d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) prefs_->GetDictionary(prefs::kAppLauncherDriveAppMapping); 1216d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) std::set<std::string> keys; 1226d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) for (base::DictionaryValue::Iterator it(*mapping); !it.IsAtEnd(); 1236d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) it.Advance()) { 1246d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) keys.insert(it.key()); 1256d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) } 1266d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) return keys; 1276d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)} 128