Chromium Code Reviews| Index: chrome/browser/extensions/extension_pref_value_map.cc |
| diff --git a/chrome/browser/extensions/extension_pref_value_map.cc b/chrome/browser/extensions/extension_pref_value_map.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..6f8ff568a57883fa60c99e3c2d1bc2c81fc33d91 |
| --- /dev/null |
| +++ b/chrome/browser/extensions/extension_pref_value_map.cc |
| @@ -0,0 +1,182 @@ |
| +// Copyright (c) 2011 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_pref_value_map.h" |
| + |
| +#include "base/scoped_ptr.h" |
| +#include "base/stl_util-inl.h" |
| +#include "base/values.h" |
| +#include "chrome/browser/prefs/pref_value_map.h" |
| + |
| +ExtensionPrefValueMap::ExtensionPrefValueMap() |
| + : initialization_complete_(false) { |
| +} |
| + |
| +ExtensionPrefValueMap::~ExtensionPrefValueMap() { |
| + STLDeleteValues(&entries_); |
|
Mattias Nissler (ping if slow)
2011/01/05 12:08:07
Hm, let's put an entries_.clear() here just to be
battre
2011/01/05 20:23:08
Done.
|
| +} |
| + |
| +void ExtensionPrefValueMap::SetExtensionPref(const std::string& ext_id, |
| + const std::string& key, |
| + bool incognito, |
| + Value* value) { |
| + scoped_ptr<Value> scoped_value(value); |
| + PrefValueMap* prefs = GetExtensionPrefValueMap(ext_id, incognito); |
| + |
| + Value* oldValue = NULL; |
| + prefs->GetValue(key, &oldValue); |
| + |
| + bool modified = !Value::Equals(oldValue, scoped_value.get()); |
| + if (!modified) |
|
Mattias Nissler (ping if slow)
2011/01/05 12:08:07
no need for the modified local, just inline the co
battre
2011/01/05 20:23:08
Gone. (due to following comment)
|
| + return; |
| + |
| + prefs->SetValue(key, scoped_value.release()); |
|
Mattias Nissler (ping if slow)
2011/01/05 12:08:07
You can just do the following:
if (prefs->SetValu
battre
2011/01/05 20:23:08
Done.
|
| + NotifyPrefValueChanged(key); |
| +} |
| + |
| +void ExtensionPrefValueMap::RemoveExtensionPref(const std::string& ext_id, |
| + const std::string& key, |
| + bool incognito) { |
| + PrefValueMap* prefs = GetExtensionPrefValueMap(ext_id, incognito); |
| + prefs->RemoveValue(key); |
|
Mattias Nissler (ping if slow)
2011/01/05 12:08:07
RemoveValue returns a bool indicating whether ther
battre
2011/01/05 20:23:08
Done.
|
| + NotifyPrefValueChanged(key); |
| +} |
| + |
| +void ExtensionPrefValueMap::OnInitializationCompleted() { |
| + DCHECK(!initialization_complete_); |
|
Mattias Nissler (ping if slow)
2011/01/05 12:08:07
It seems overkill to have that flag only for the s
battre
2011/01/05 20:23:08
Done.
|
| + initialization_complete_ = true; |
| + NotifyInitializationCompleted(); |
|
Mattias Nissler (ping if slow)
2011/01/05 12:08:07
Why not just inline the NotifyInitializationComple
battre
2011/01/05 20:23:08
Done.
|
| +} |
| + |
| +void ExtensionPrefValueMap::RegisterExtension(const std::string& ext_id, |
| + const base::Time& install_time, |
| + bool is_enabled) { |
| + if (entries_.find(ext_id) != entries_.end()) { |
| + LOG(WARNING) << "Extension " << ext_id << " registered multiple times."; |
|
Mattias Nissler (ping if slow)
2011/01/05 12:08:07
Make this a NOTREACHED()?
battre
2011/01/05 20:23:08
Cannot be NOTREACHED. An extension can be reinstal
|
| + UnregisterExtension(ext_id); |
| + } |
| + entries_[ext_id] = new ExtensionEntry; |
| + entries_[ext_id]->install_time = install_time; |
| + entries_[ext_id]->enabled = is_enabled; |
| +} |
| + |
| +void ExtensionPrefValueMap::UnregisterExtension(const std::string& ext_id) { |
| + if (entries_.find(ext_id) == entries_.end()) { |
| + LOG(WARNING) << "Extension " << ext_id << " unregistered without" |
| + << " being registered."; |
|
Mattias Nissler (ping if slow)
2011/01/05 12:08:07
NOTREACHED() again?
battre
2011/01/05 20:23:08
Everytime the extension black list is updated, all
|
| + return; |
| + } |
| + std::set<std::string> keys; // keys set by this extension |
| + GetExtensionControlledKeys(ext_id, &keys); |
|
Mattias Nissler (ping if slow)
2011/01/05 12:08:07
It seems this function should really operate on Ex
battre
2011/01/05 20:23:08
Done.
|
| + |
| + delete entries_[ext_id]; |
|
Mattias Nissler (ping if slow)
2011/01/05 12:08:07
If you do the find() call above, can we use that i
battre
2011/01/05 20:23:08
Done.
|
| + entries_.erase(ext_id); |
| + |
| + NotifyPrefValueChanged(keys); |
| +} |
| + |
| +void ExtensionPrefValueMap::UpdateExtensionsState(const std::string& ext_id, |
| + bool is_enabled) { |
| + CHECK(entries_.find(ext_id) != entries_.end()); |
| + if (entries_[ext_id]->enabled == is_enabled) |
| + return; |
| + std::set<std::string> keys; // keys set by this extension |
| + GetExtensionControlledKeys(ext_id, &keys); |
| + entries_[ext_id]->enabled = is_enabled; |
| + NotifyPrefValueChanged(keys); |
| +} |
| + |
| +PrefValueMap* ExtensionPrefValueMap::GetExtensionPrefValueMap( |
| + const std::string& ext_id, |
| + bool incognito) { |
| + CHECK(entries_[ext_id]); |
| + PrefValueMap* prefs = incognito ? &(entries_[ext_id]->inc_preferences) |
| + : &(entries_[ext_id]->reg_preferences); |
|
Mattias Nissler (ping if slow)
2011/01/05 12:08:07
Again, three lookups where one would suffice. Why
battre
2011/01/05 20:23:08
I prefer the visual clarity of a [] lookup over so
|
| + return prefs; |
| +} |
| + |
| +const PrefValueMap* ExtensionPrefValueMap::GetExtensionPrefValueMap( |
| + const std::string& ext_id, |
| + bool incognito) const { |
| + ExtensionEntryMap::const_iterator i = entries_.find(ext_id); |
| + CHECK(i != entries_.end()); |
| + if (incognito) |
| + return &(i->second->inc_preferences); |
| + else |
| + return &(i->second->reg_preferences); |
| +} |
| + |
|
Mattias Nissler (ping if slow)
2011/01/05 12:08:07
excess newline
battre
2011/01/05 20:23:08
Done.
|
| + |
| +void ExtensionPrefValueMap::GetExtensionControlledKeys( |
| + const std::string& ext_id, std::set<std::string>* out) const { |
|
Mattias Nissler (ping if slow)
2011/01/05 12:08:07
I think you should break the line after the comma.
Mattias Nissler (ping if slow)
2011/01/05 12:08:07
Do you actually require ordering here? If not, I'd
battre
2011/01/05 20:23:08
Done.
battre
2011/01/05 20:23:08
I don't require ordering but uniqueness. An extens
|
| + PrefValueMap::const_iterator i; |
| + |
| + const PrefValueMap* reg_prefs = GetExtensionPrefValueMap(ext_id, false); |
| + for (i = reg_prefs->begin(); i != reg_prefs->end(); ++i) |
| + out->insert(i->first); |
| + |
| + const PrefValueMap* inc_prefs = GetExtensionPrefValueMap(ext_id, true); |
| + for (i = inc_prefs->begin(); i != inc_prefs->end(); ++i) |
| + out->insert(i->first); |
| +} |
| + |
| +const Value* ExtensionPrefValueMap::GetEffectivePrefValue( |
| + const std::string& key, |
| + bool incognito) const { |
| + Value *winner = NULL; |
| + base::Time winners_install_time = base::Time(); |
|
Mattias Nissler (ping if slow)
2011/01/05 12:08:07
no need for the assignment here, the default const
battre
2011/01/05 20:23:08
right... one a Java programmer by heart you never
|
| + |
| + ExtensionEntryMap::const_iterator i; |
| + for (i = entries_.begin(); i != entries_.end(); ++i) { |
| + const std::string& ext_id = i->first; |
| + const base::Time& install_time = i->second->install_time; |
| + const bool enabled = i->second->enabled; |
| + |
| + if (!enabled) |
|
Mattias Nissler (ping if slow)
2011/01/05 12:08:07
inline i->second->enabled?
battre
2011/01/05 20:23:08
I prefer my variant: The first three lines extract
|
| + continue; |
| + if (install_time < winners_install_time) |
| + continue; |
| + |
| + Value* value = NULL; |
| + const PrefValueMap* prefs = GetExtensionPrefValueMap(ext_id, false); |
| + if (prefs->GetValue(key, &value)) { |
| + winner = value; |
| + winners_install_time = install_time; |
| + } |
| + |
| + if (!incognito) |
| + continue; |
| + |
| + prefs = GetExtensionPrefValueMap(ext_id, true); |
| + if (prefs->GetValue(key, &value)) { |
| + winner = value; |
| + winners_install_time = install_time; |
| + } |
| + } |
| + return winner; |
| +} |
| + |
| +void ExtensionPrefValueMap::AddObserver(PrefStore::Observer* observer) { |
| + observers_.AddObserver(observer); |
| +} |
| + |
| +void ExtensionPrefValueMap::RemoveObserver(PrefStore::Observer* observer) { |
| + observers_.RemoveObserver(observer); |
| +} |
| + |
| +void ExtensionPrefValueMap::NotifyInitializationCompleted() { |
| + FOR_EACH_OBSERVER(PrefStore::Observer, observers_, |
| + OnInitializationCompleted()); |
| +} |
| + |
| +void ExtensionPrefValueMap::NotifyPrefValueChanged( |
| + const std::set<std::string>& keys) { |
| + std::set<std::string>::const_iterator i; |
| + for (i = keys.begin(); i != keys.end(); ++i) |
| + NotifyPrefValueChanged(*i); |
| +} |
| + |
| +void ExtensionPrefValueMap::NotifyPrefValueChanged(const std::string& key) { |
| + FOR_EACH_OBSERVER(PrefStore::Observer, observers_, OnPrefValueChanged(key)); |
| +} |