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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
|
// Copyright (c) 2010 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/extensions/extension_prefs.h"
#include "base/string_util.h"
#include "base/string_number_conversions.h"
#include "base/utf_string_conversions.h"
#include "chrome/common/extensions/extension.h"
#include "chrome/common/pref_names.h"
using base::Time;
namespace {
// Additional preferences keys
// Where an extension was installed from. (see Extension::Location)
const char kPrefLocation[] = "location";
// Enabled, disabled, killed, etc. (see Extension::State)
const char kPrefState[] = "state";
// The path to the current version's manifest file.
const char kPrefPath[] = "path";
// The dictionary containing the extension's manifest.
const char kPrefManifest[] = "manifest";
// The version number.
const char kPrefVersion[] = "manifest.version";
// Indicates if an extension is blacklisted:
const char kPrefBlacklist[] = "blacklist";
// Indicates whether to show an install warning when the user enables.
const char kExtensionDidEscalatePermissions[] = "install_warning_on_enable";
// A preference that tracks browser action toolbar configuration. This is a list
// object stored in the Preferences file. The extensions are stored by ID.
const char kExtensionToolbar[] = "extensions.toolbar";
// The key for a serialized Time value indicating the start of the day (from the
// server's perspective) an extension last included a "ping" parameter during
// its update check.
const char kLastPingDay[] = "lastpingday";
// Path for settings specific to blacklist update.
const char kExtensionsBlacklistUpdate[] = "extensions.blacklistupdate";
// Path and sub-keys for the idle install info dictionary preference.
const char kIdleInstallInfo[] = "idle_install_info";
const char kIdleInstallInfoCrxPath[] = "crx_path";
const char kIdleInstallInfoVersion[] = "version";
const char kIdleInstallInfoFetchTime[] = "fetch_time";
// A preference that, if true, will allow this extension to run in incognito
// mode.
const char kPrefIncognitoEnabled[] = "incognito";
// A preference to control whether an extension is allowed to inject script in
// pages with file URLs.
const char kPrefAllowFileAccess[] = "allowFileAccess";
// A preference set by the web store to indicate login information for
// purchased apps.
const char kWebStoreLogin[] = "extensions.webstore_login";
// A preference set by the the NTP to persist the desired launch container type
// used for apps.
const char kPrefLaunchType[] = "launchType";
// A preference determining the order of which the apps appear on the NTP.
const char kPrefAppLaunchIndex[] = "app_launcher_index";
} // namespace
////////////////////////////////////////////////////////////////////////////////
namespace {
// TODO(asargent) - This is cleanup code for a key that was introduced into
// the extensions.settings sub-dictionary which wasn't a valid extension
// id. We can remove this in a couple of months. (See http://crbug.com/40017
// and http://crbug.com/39745 for more details).
static void CleanupBadExtensionKeys(PrefService* prefs) {
DictionaryValue* dictionary =
prefs->GetMutableDictionary(ExtensionPrefs::kExtensionsPref);
std::set<std::string> bad_keys;
for (DictionaryValue::key_iterator i = dictionary->begin_keys();
i != dictionary->end_keys(); ++i) {
const std::string& key_name(*i);
if (!Extension::IdIsValid(key_name)) {
bad_keys.insert(key_name);
}
}
bool dirty = false;
for (std::set<std::string>::iterator i = bad_keys.begin();
i != bad_keys.end(); ++i) {
dirty = true;
dictionary->Remove(*i, NULL);
}
if (dirty)
prefs->ScheduleSavePersistentPrefs();
}
} // namespace
ExtensionPrefs::ExtensionPrefs(PrefService* prefs, const FilePath& root_dir)
: prefs_(prefs),
install_directory_(root_dir) {
// TODO(asargent) - Remove this in a couple of months. (See comment above
// CleanupBadExtensionKeys).
CleanupBadExtensionKeys(prefs);
MakePathsRelative();
}
ExtensionPrefs::~ExtensionPrefs() {}
// static
const char ExtensionPrefs::kExtensionsPref[] = "extensions.settings";
static FilePath::StringType MakePathRelative(const FilePath& parent,
const FilePath& child,
bool *dirty) {
if (!parent.IsParent(child))
return child.value();
if (dirty)
*dirty = true;
FilePath::StringType retval = child.value().substr(
parent.value().length());
if (FilePath::IsSeparator(retval[0]))
return retval.substr(1);
else
return retval;
}
void ExtensionPrefs::MakePathsRelative() {
bool dirty = false;
const DictionaryValue* dict = prefs_->GetMutableDictionary(kExtensionsPref);
if (!dict || dict->empty())
return;
for (DictionaryValue::key_iterator i = dict->begin_keys();
i != dict->end_keys(); ++i) {
DictionaryValue* extension_dict;
if (!dict->GetDictionaryWithoutPathExpansion(*i, &extension_dict))
continue;
int location_value;
if (extension_dict->GetInteger(kPrefLocation, &location_value) &&
location_value == Extension::LOAD) {
// Unpacked extensions can have absolute paths.
continue;
}
FilePath::StringType path_string;
if (!extension_dict->GetString(kPrefPath, &path_string))
continue;
FilePath path(path_string);
if (path.IsAbsolute()) {
extension_dict->SetString(kPrefPath,
MakePathRelative(install_directory_, path, &dirty));
}
}
if (dirty)
SavePrefsAndNotify();
}
void ExtensionPrefs::MakePathsAbsolute(DictionaryValue* dict) {
if (!dict || dict->empty())
return;
for (DictionaryValue::key_iterator i = dict->begin_keys();
i != dict->end_keys(); ++i) {
DictionaryValue* extension_dict;
if (!dict->GetDictionaryWithoutPathExpansion(*i, &extension_dict)) {
NOTREACHED();
continue;
}
int location_value;
if (extension_dict->GetInteger(kPrefLocation, &location_value) &&
location_value == Extension::LOAD) {
// Unpacked extensions will already have absolute paths.
continue;
}
FilePath::StringType path_string;
if (!extension_dict->GetString(kPrefPath, &path_string))
continue;
DCHECK(!FilePath(path_string).IsAbsolute());
extension_dict->SetString(
kPrefPath, install_directory_.Append(path_string).value());
}
}
DictionaryValue* ExtensionPrefs::CopyCurrentExtensions() {
const DictionaryValue* extensions = prefs_->GetDictionary(kExtensionsPref);
if (extensions) {
DictionaryValue* copy =
static_cast<DictionaryValue*>(extensions->DeepCopy());
MakePathsAbsolute(copy);
return copy;
}
return new DictionaryValue;
}
bool ExtensionPrefs::ReadBooleanFromPref(
DictionaryValue* ext, const std::string& pref_key) {
if (!ext->HasKey(pref_key)) return false;
bool bool_value = false;
if (!ext->GetBoolean(pref_key, &bool_value)) {
NOTREACHED() << "Failed to fetch " << pref_key << " flag.";
// In case we could not fetch the flag, we treat it as false.
return false;
}
return bool_value;
}
bool ExtensionPrefs::ReadExtensionPrefBoolean(
const std::string& extension_id, const std::string& pref_key) {
const DictionaryValue* extensions = prefs_->GetDictionary(kExtensionsPref);
if (!extensions)
return false;
DictionaryValue* ext = NULL;
if (!extensions->GetDictionary(extension_id, &ext)) {
// No such extension yet.
return false;
}
return ReadBooleanFromPref(ext, pref_key);
}
bool ExtensionPrefs::ReadIntegerFromPref(
DictionaryValue* ext, const std::string& pref_key, int* out_value) {
if (!ext->HasKey(pref_key)) return false;
if (!ext->GetInteger(pref_key, out_value)) {
NOTREACHED() << "Failed to fetch " << pref_key << " flag.";
// In case we could not fetch the flag, we treat it as false.
return false;
}
return out_value != NULL;
}
bool ExtensionPrefs::ReadExtensionPrefInteger(
const std::string& extension_id, const std::string& pref_key,
int* out_value) {
const DictionaryValue* extensions = prefs_->GetDictionary(kExtensionsPref);
if (!extensions)
return false;
DictionaryValue* ext = NULL;
if (!extensions->GetDictionary(extension_id, &ext)) {
// No such extension yet.
return false;
}
return ReadIntegerFromPref(ext, pref_key, out_value);
}
void ExtensionPrefs::SavePrefsAndNotify() {
prefs_->ScheduleSavePersistentPrefs();
prefs_->pref_notifier()->OnUserPreferenceSet(kExtensionsPref);
}
bool ExtensionPrefs::IsBlacklistBitSet(DictionaryValue* ext) {
return ReadBooleanFromPref(ext, kPrefBlacklist);
}
bool ExtensionPrefs::IsExtensionBlacklisted(const std::string& extension_id) {
return ReadExtensionPrefBoolean(extension_id, kPrefBlacklist);
}
bool ExtensionPrefs::IsExtensionAllowedByPolicy(
const std::string& extension_id) {
std::string string_value;
const ListValue* blacklist =
prefs_->GetList(prefs::kExtensionInstallDenyList);
if (!blacklist || blacklist->empty())
return true;
// Check the whitelist first.
const ListValue* whitelist =
prefs_->GetList(prefs::kExtensionInstallAllowList);
if (whitelist) {
for (ListValue::const_iterator it = whitelist->begin();
it != whitelist->end(); ++it) {
if (!(*it)->GetAsString(&string_value))
LOG(WARNING) << "Failed to read whitelist string.";
else if (string_value == extension_id)
return true;
}
}
// Then check the blacklist (the admin blacklist, not the Google blacklist).
if (blacklist) {
for (ListValue::const_iterator it = blacklist->begin();
it != blacklist->end(); ++it) {
if (!(*it)->GetAsString(&string_value)) {
LOG(WARNING) << "Failed to read blacklist string.";
} else {
if (string_value == "*")
return false; // Only whitelisted extensions are allowed.
if (string_value == extension_id)
return false;
}
}
}
return true;
}
bool ExtensionPrefs::DidExtensionEscalatePermissions(
const std::string& extension_id) {
return ReadExtensionPrefBoolean(extension_id,
kExtensionDidEscalatePermissions);
}
void ExtensionPrefs::SetDidExtensionEscalatePermissions(
Extension* extension, bool did_escalate) {
UpdateExtensionPref(extension->id(), kExtensionDidEscalatePermissions,
Value::CreateBooleanValue(did_escalate));
prefs_->ScheduleSavePersistentPrefs();
}
void ExtensionPrefs::UpdateBlacklist(
const std::set<std::string>& blacklist_set) {
std::vector<std::string> remove_pref_ids;
std::set<std::string> used_id_set;
const DictionaryValue* extensions = prefs_->GetDictionary(kExtensionsPref);
if (extensions) {
for (DictionaryValue::key_iterator extension_id = extensions->begin_keys();
extension_id != extensions->end_keys(); ++extension_id) {
DictionaryValue* ext;
if (!extensions->GetDictionaryWithoutPathExpansion(*extension_id, &ext)) {
NOTREACHED() << "Invalid pref for extension " << *extension_id;
continue;
}
const std::string& id(*extension_id);
if (blacklist_set.find(id) == blacklist_set.end()) {
if (!IsBlacklistBitSet(ext)) {
// This extension is not in blacklist. And it was not blacklisted
// before.
continue;
} else {
if (ext->size() == 1) {
// We should remove the entry if the only flag here is blacklist.
remove_pref_ids.push_back(id);
} else {
// Remove the blacklist bit.
ext->Remove(kPrefBlacklist, NULL);
}
}
} else {
if (!IsBlacklistBitSet(ext)) {
// Only set the blacklist if it was not set.
ext->SetBoolean(kPrefBlacklist, true);
}
// Keep the record if this extension is already processed.
used_id_set.insert(id);
}
}
}
// Iterate the leftovers to set blacklist in pref
std::set<std::string>::const_iterator set_itr = blacklist_set.begin();
for (; set_itr != blacklist_set.end(); ++set_itr) {
if (used_id_set.find(*set_itr) == used_id_set.end()) {
UpdateExtensionPref(*set_itr, kPrefBlacklist,
Value::CreateBooleanValue(true));
}
}
for (unsigned int i = 0; i < remove_pref_ids.size(); ++i) {
DeleteExtensionPrefs(remove_pref_ids[i]);
}
SavePrefsAndNotify();
return;
}
Time ExtensionPrefs::LastPingDayImpl(const DictionaryValue* dictionary) const {
if (dictionary && dictionary->HasKey(kLastPingDay)) {
std::string string_value;
int64 value;
dictionary->GetString(kLastPingDay, &string_value);
if (base::StringToInt64(string_value, &value)) {
return Time::FromInternalValue(value);
}
}
return Time();
}
void ExtensionPrefs::SetLastPingDayImpl(const Time& time,
DictionaryValue* dictionary) {
if (!dictionary) {
NOTREACHED();
return;
}
std::string value = base::Int64ToString(time.ToInternalValue());
dictionary->SetString(kLastPingDay, value);
SavePrefsAndNotify();
}
Time ExtensionPrefs::LastPingDay(const std::string& extension_id) const {
DCHECK(Extension::IdIsValid(extension_id));
return LastPingDayImpl(GetExtensionPref(extension_id));
}
Time ExtensionPrefs::BlacklistLastPingDay() const {
return LastPingDayImpl(prefs_->GetDictionary(kExtensionsBlacklistUpdate));
}
void ExtensionPrefs::SetLastPingDay(const std::string& extension_id,
const Time& time) {
DCHECK(Extension::IdIsValid(extension_id));
SetLastPingDayImpl(time, GetExtensionPref(extension_id));
}
void ExtensionPrefs::SetBlacklistLastPingDay(const Time& time) {
SetLastPingDayImpl(time,
prefs_->GetMutableDictionary(kExtensionsBlacklistUpdate));
}
bool ExtensionPrefs::IsIncognitoEnabled(const std::string& extension_id) {
return ReadExtensionPrefBoolean(extension_id, kPrefIncognitoEnabled);
}
void ExtensionPrefs::SetIsIncognitoEnabled(const std::string& extension_id,
bool enabled) {
UpdateExtensionPref(extension_id, kPrefIncognitoEnabled,
Value::CreateBooleanValue(enabled));
SavePrefsAndNotify();
}
bool ExtensionPrefs::AllowFileAccess(const std::string& extension_id) {
return ReadExtensionPrefBoolean(extension_id, kPrefAllowFileAccess);
}
void ExtensionPrefs::SetAllowFileAccess(const std::string& extension_id,
bool allow) {
UpdateExtensionPref(extension_id, kPrefAllowFileAccess,
Value::CreateBooleanValue(allow));
SavePrefsAndNotify();
}
ExtensionPrefs::LaunchType ExtensionPrefs::GetLaunchType(
const std::string& extension_id) {
int value;
if (ReadExtensionPrefInteger(extension_id, kPrefLaunchType, &value) && (
value == LAUNCH_PINNED ||
value == LAUNCH_REGULAR ||
value == LAUNCH_FULLSCREEN)) {
return static_cast<LaunchType>(value);
}
return LAUNCH_REGULAR;
}
void ExtensionPrefs::SetLaunchType(const std::string& extension_id,
LaunchType launch_type) {
UpdateExtensionPref(extension_id, kPrefLaunchType,
Value::CreateIntegerValue(static_cast<int>(launch_type)));
SavePrefsAndNotify();
}
void ExtensionPrefs::GetKilledExtensionIds(std::set<std::string>* killed_ids) {
const DictionaryValue* dict = prefs_->GetDictionary(kExtensionsPref);
if (!dict || dict->empty())
return;
for (DictionaryValue::key_iterator i = dict->begin_keys();
i != dict->end_keys(); ++i) {
const std::string& key_name(*i);
if (!Extension::IdIsValid(key_name)) {
LOG(WARNING) << "Invalid external extension ID encountered: " << key_name;
continue;
}
DictionaryValue* extension;
if (!dict->GetDictionary(key_name, &extension)) {
NOTREACHED();
continue;
}
// Check to see if the extension has been killed.
int state;
if (extension->GetInteger(kPrefState, &state) &&
state == static_cast<int>(Extension::KILLBIT)) {
killed_ids->insert(StringToLowerASCII(key_name));
}
}
}
std::vector<std::string> ExtensionPrefs::GetToolbarOrder() {
std::vector<std::string> extension_ids;
const ListValue* toolbar_order = prefs_->GetList(kExtensionToolbar);
if (toolbar_order) {
for (size_t i = 0; i < toolbar_order->GetSize(); ++i) {
std::string extension_id;
if (toolbar_order->GetString(i, &extension_id))
extension_ids.push_back(extension_id);
}
}
return extension_ids;
}
void ExtensionPrefs::SetToolbarOrder(
const std::vector<std::string>& extension_ids) {
ListValue* toolbar_order = prefs_->GetMutableList(kExtensionToolbar);
toolbar_order->Clear();
for (std::vector<std::string>::const_iterator iter = extension_ids.begin();
iter != extension_ids.end(); ++iter) {
toolbar_order->Append(new StringValue(*iter));
}
SavePrefsAndNotify();
}
void ExtensionPrefs::OnExtensionInstalled(
Extension* extension, Extension::State initial_state,
bool initial_incognito_enabled) {
const std::string& id = extension->id();
UpdateExtensionPref(id, kPrefState,
Value::CreateIntegerValue(initial_state));
UpdateExtensionPref(id, kPrefIncognitoEnabled,
Value::CreateBooleanValue(initial_incognito_enabled));
UpdateExtensionPref(id, kPrefLocation,
Value::CreateIntegerValue(extension->location()));
FilePath::StringType path = MakePathRelative(install_directory_,
extension->path(), NULL);
UpdateExtensionPref(id, kPrefPath, Value::CreateStringValue(path));
// We store prefs about LOAD extensions, but don't cache their manifest
// since it may change on disk.
if (extension->location() != Extension::LOAD) {
UpdateExtensionPref(id, kPrefManifest,
extension->manifest_value()->DeepCopy());
}
UpdateExtensionPref(id, kPrefAppLaunchIndex,
Value::CreateIntegerValue(GetNextAppLaunchIndex()));
SavePrefsAndNotify();
}
void ExtensionPrefs::OnExtensionUninstalled(const std::string& extension_id,
const Extension::Location& location,
bool external_uninstall) {
// For external extensions, we save a preference reminding ourself not to try
// and install the extension anymore (except when |external_uninstall| is
// true, which signifies that the registry key was deleted or the pref file
// no longer lists the extension).
if (!external_uninstall && Extension::IsExternalLocation(location)) {
UpdateExtensionPref(extension_id, kPrefState,
Value::CreateIntegerValue(Extension::KILLBIT));
SavePrefsAndNotify();
} else {
DeleteExtensionPrefs(extension_id);
}
}
Extension::State ExtensionPrefs::GetExtensionState(
const std::string& extension_id) {
DictionaryValue* extension = GetExtensionPref(extension_id);
// If the extension doesn't have a pref, it's a --load-extension.
if (!extension)
return Extension::ENABLED;
int state = -1;
if (!extension->GetInteger(kPrefState, &state) ||
state < 0 || state >= Extension::NUM_STATES) {
LOG(ERROR) << "Bad or missing pref 'state' for extension '"
<< extension_id << "'";
return Extension::ENABLED;
}
return static_cast<Extension::State>(state);
}
void ExtensionPrefs::SetExtensionState(Extension* extension,
Extension::State state) {
UpdateExtensionPref(extension->id(), kPrefState,
Value::CreateIntegerValue(state));
SavePrefsAndNotify();
}
std::string ExtensionPrefs::GetVersionString(const std::string& extension_id) {
DictionaryValue* extension = GetExtensionPref(extension_id);
if (!extension)
return std::string();
std::string version;
if (!extension->GetString(kPrefVersion, &version)) {
LOG(ERROR) << "Bad or missing pref 'version' for extension '"
<< extension_id << "'";
}
return version;
}
void ExtensionPrefs::UpdateManifest(Extension* extension) {
if (extension->location() != Extension::LOAD) {
UpdateExtensionPref(extension->id(), kPrefManifest,
extension->manifest_value()->DeepCopy());
SavePrefsAndNotify();
}
}
FilePath ExtensionPrefs::GetExtensionPath(const std::string& extension_id) {
const DictionaryValue* dict = prefs_->GetDictionary(kExtensionsPref);
if (!dict || dict->empty())
return FilePath();
std::string path;
if (!dict->GetString(extension_id + "." + kPrefPath, &path))
return FilePath();
return install_directory_.Append(FilePath::FromWStringHack(UTF8ToWide(path)));
}
void ExtensionPrefs::UpdateExtensionPref(const std::string& extension_id,
const std::string& key,
Value* data_value) {
if (!Extension::IdIsValid(extension_id)) {
NOTREACHED() << "Invalid extension_id " << extension_id;
return;
}
DictionaryValue* extension = GetOrCreateExtensionPref(extension_id);
extension->Set(key, data_value);
}
void ExtensionPrefs::DeleteExtensionPrefs(const std::string& extension_id) {
DictionaryValue* dict = prefs_->GetMutableDictionary(kExtensionsPref);
if (dict->HasKey(extension_id)) {
dict->Remove(extension_id, NULL);
SavePrefsAndNotify();
}
}
DictionaryValue* ExtensionPrefs::GetOrCreateExtensionPref(
const std::string& extension_id) {
DCHECK(Extension::IdIsValid(extension_id));
DictionaryValue* dict = prefs_->GetMutableDictionary(kExtensionsPref);
DictionaryValue* extension = NULL;
if (!dict->GetDictionary(extension_id, &extension)) {
// Extension pref does not exist, create it.
extension = new DictionaryValue();
dict->Set(extension_id, extension);
}
return extension;
}
DictionaryValue* ExtensionPrefs::GetExtensionPref(
const std::string& extension_id) const {
const DictionaryValue* dict = prefs_->GetDictionary(kExtensionsPref);
if (!dict)
return NULL;
DictionaryValue* extension = NULL;
dict->GetDictionary(extension_id, &extension);
return extension;
}
// Helper function for GetInstalledExtensionsInfo.
static ExtensionInfo* GetInstalledExtensionInfoImpl(
DictionaryValue* extension_data,
DictionaryValue::key_iterator extension_id) {
DictionaryValue* ext;
if (!extension_data->GetDictionaryWithoutPathExpansion(*extension_id, &ext)) {
LOG(WARNING) << "Invalid pref for extension " << *extension_id;
NOTREACHED();
return NULL;
}
if (ext->HasKey(kPrefBlacklist)) {
bool is_blacklisted = false;
if (!ext->GetBoolean(kPrefBlacklist, &is_blacklisted)) {
NOTREACHED() << "Invalid blacklist pref:" << *extension_id;
return NULL;
}
if (is_blacklisted) {
return NULL;
}
}
int state_value;
if (!ext->GetInteger(kPrefState, &state_value)) {
// This can legitimately happen if we store preferences for component
// extensions.
return NULL;
}
if (state_value == Extension::KILLBIT) {
LOG(WARNING) << "External extension has been uninstalled by the user "
<< *extension_id;
return NULL;
}
FilePath::StringType path;
if (!ext->GetString(kPrefPath, &path)) {
return NULL;
}
int location_value;
if (!ext->GetInteger(kPrefLocation, &location_value)) {
return NULL;
}
// Only the following extension types can be installed permanently in the
// preferences.
Extension::Location location =
static_cast<Extension::Location>(location_value);
if (location != Extension::INTERNAL &&
location != Extension::LOAD &&
!Extension::IsExternalLocation(location)) {
NOTREACHED();
return NULL;
}
DictionaryValue* manifest = NULL;
if (location != Extension::LOAD &&
!ext->GetDictionary(kPrefManifest, &manifest)) {
LOG(WARNING) << "Missing manifest for extension " << *extension_id;
// Just a warning for now.
}
return new ExtensionInfo(manifest, *extension_id, FilePath(path), location);
}
ExtensionPrefs::ExtensionsInfo* ExtensionPrefs::GetInstalledExtensionsInfo() {
scoped_ptr<DictionaryValue> extension_data(CopyCurrentExtensions());
ExtensionsInfo* extensions_info = new ExtensionsInfo;
for (DictionaryValue::key_iterator extension_id(
extension_data->begin_keys());
extension_id != extension_data->end_keys(); ++extension_id) {
if (!Extension::IdIsValid(*extension_id))
continue;
ExtensionInfo* info = GetInstalledExtensionInfoImpl(extension_data.get(),
extension_id);
if (info)
extensions_info->push_back(linked_ptr<ExtensionInfo>(info));
}
return extensions_info;
}
ExtensionInfo* ExtensionPrefs::GetInstalledExtensionInfo(
const std::string& extension_id) {
scoped_ptr<DictionaryValue> extension_data(CopyCurrentExtensions());
for (DictionaryValue::key_iterator extension_iter(
extension_data->begin_keys());
extension_iter != extension_data->end_keys(); ++extension_iter) {
if (*extension_iter == extension_id) {
return GetInstalledExtensionInfoImpl(extension_data.get(),
extension_iter);
}
}
return NULL;
}
void ExtensionPrefs::SetIdleInstallInfo(const std::string& extension_id,
const FilePath& crx_path,
const std::string& version,
const base::Time& fetch_time) {
DictionaryValue* extension_prefs = GetExtensionPref(extension_id);
if (!extension_prefs) {
NOTREACHED();
return;
}
extension_prefs->Remove(kIdleInstallInfo, NULL);
DictionaryValue* info = new DictionaryValue();
info->SetString(kIdleInstallInfoCrxPath, crx_path.value());
info->SetString(kIdleInstallInfoVersion, version);
info->SetString(kIdleInstallInfoFetchTime,
base::Int64ToString(fetch_time.ToInternalValue()));
extension_prefs->Set(kIdleInstallInfo, info);
SavePrefsAndNotify();
}
bool ExtensionPrefs::RemoveIdleInstallInfo(const std::string& extension_id) {
DictionaryValue* extension_prefs = GetExtensionPref(extension_id);
if (!extension_prefs)
return false;
bool result = extension_prefs->Remove(kIdleInstallInfo, NULL);
SavePrefsAndNotify();
return result;
}
bool ExtensionPrefs::GetIdleInstallInfo(const std::string& extension_id,
FilePath* crx_path,
std::string* version,
base::Time* fetch_time) {
DictionaryValue* extension_prefs = GetExtensionPref(extension_id);
if (!extension_prefs)
return false;
// Do all the reads from the prefs together, and don't do any assignment
// to the out parameters unless all the reads succeed.
DictionaryValue* info = NULL;
if (!extension_prefs->GetDictionary(kIdleInstallInfo, &info))
return false;
FilePath::StringType path_string;
if (!info->GetString(kIdleInstallInfoCrxPath, &path_string))
return false;
std::string tmp_version;
if (!info->GetString(kIdleInstallInfoVersion, &tmp_version))
return false;
std::string fetch_time_string;
if (!info->GetString(kIdleInstallInfoFetchTime, &fetch_time_string))
return false;
int64 fetch_time_value;
if (!base::StringToInt64(fetch_time_string, &fetch_time_value))
return false;
if (crx_path)
*crx_path = FilePath(path_string);
if (version)
*version = tmp_version;
if (fetch_time)
*fetch_time = base::Time::FromInternalValue(fetch_time_value);
return true;
}
std::set<std::string> ExtensionPrefs::GetIdleInstallInfoIds() {
std::set<std::string> result;
const DictionaryValue* extensions = prefs_->GetDictionary(kExtensionsPref);
if (!extensions)
return result;
for (DictionaryValue::key_iterator iter = extensions->begin_keys();
iter != extensions->end_keys(); ++iter) {
const std::string& id(*iter);
if (!Extension::IdIsValid(id)) {
NOTREACHED();
continue;
}
DictionaryValue* extension_prefs = GetExtensionPref(id);
if (!extension_prefs)
continue;
DictionaryValue* info = NULL;
if (extension_prefs->GetDictionary(kIdleInstallInfo, &info))
result.insert(id);
}
return result;
}
bool ExtensionPrefs::GetWebStoreLogin(std::string* result) {
if (prefs_->HasPrefPath(kWebStoreLogin)) {
*result = prefs_->GetString(kWebStoreLogin);
return true;
}
return false;
}
void ExtensionPrefs::SetWebStoreLogin(const std::string& login) {
prefs_->SetString(kWebStoreLogin, login);
SavePrefsAndNotify();
}
int ExtensionPrefs::GetAppLaunchIndex(const std::string& extension_id) {
int value;
if (ReadExtensionPrefInteger(extension_id, kPrefAppLaunchIndex, &value))
return value;
return -1;
}
void ExtensionPrefs::SetAppLaunchIndex(const std::string& extension_id,
int index) {
DCHECK_GE(index, 0);
UpdateExtensionPref(extension_id, kPrefAppLaunchIndex,
Value::CreateIntegerValue(index));
SavePrefsAndNotify();
}
int ExtensionPrefs::GetNextAppLaunchIndex() {
const DictionaryValue* extensions = prefs_->GetDictionary(kExtensionsPref);
if (!extensions)
return 0;
int max_value = -1;
for (DictionaryValue::key_iterator extension_id = extensions->begin_keys();
extension_id != extensions->end_keys(); ++extension_id) {
int value = GetAppLaunchIndex(*extension_id);
if (value > max_value)
max_value = value;
}
return max_value + 1;
}
// static
void ExtensionPrefs::RegisterUserPrefs(PrefService* prefs) {
prefs->RegisterDictionaryPref(kExtensionsPref);
prefs->RegisterListPref(kExtensionToolbar);
prefs->RegisterIntegerPref(prefs::kExtensionToolbarSize, -1);
prefs->RegisterDictionaryPref(kExtensionsBlacklistUpdate);
prefs->RegisterListPref(prefs::kExtensionInstallAllowList);
prefs->RegisterListPref(prefs::kExtensionInstallDenyList);
prefs->RegisterStringPref(kWebStoreLogin, std::string() /* default_value */);
}
|