Commit 7a2f097a authored by vabr@chromium.org's avatar vabr@chromium.org

RulesRegistryWithCache::ProcessChangedRules postponed

The main change here is that ProcessChangedRules (includes converting the rules
and writing them to disk) is now postponed to when the rules registry becomes
ready. After start-up, this will just mean that ProcessChangedRules will be
added to the message loop queue.

A minor change is a new way to manage ProcessChangedRules tasks:
If there is not the UI part (which does the writing), ProcessChangedRules tasks
are not ever posted.
If there is the UI part, then at any time at most one ProcessChangedRules task
is ever scheduled. This avoids unnecessary work duplication.

BUG=236368

Review URL: https://chromiumcodereview.appspot.com/15911005

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@203993 0039d316-1c4b-4281-b951-d872f2087c98
parent a6695f1d
......@@ -44,6 +44,7 @@ std::vector<linked_ptr<extensions::RulesRegistry::Rule> > RulesFromValue(
if (!value || !value->GetAsList(&list))
return rules;
rules.reserve(list->GetSize());
for (size_t i = 0; i < list->GetSize(); ++i) {
const base::DictionaryValue* dict = NULL;
if (!list->GetDictionary(i, &dict))
......@@ -80,7 +81,7 @@ RulesRegistryWithCache::RulesRegistryWithCache(
bool log_storage_init_delay,
scoped_ptr<RuleStorageOnUI>* ui_part)
: RulesRegistry(owner_thread, event_name),
weak_ptr_factory_((profile) ? this : NULL),
weak_ptr_factory_(profile ? this : NULL),
storage_on_ui_((profile
? (new RuleStorageOnUI(profile,
GetDeclarativeRuleStorageKey(
......@@ -90,7 +91,9 @@ RulesRegistryWithCache::RulesRegistryWithCache(
weak_ptr_factory_.GetWeakPtr(),
log_storage_init_delay))
->GetWeakPtr()
: base::WeakPtr<RuleStorageOnUI>())) {
: base::WeakPtr<RuleStorageOnUI>())),
process_changed_rules_requested_(profile ? NOT_SCHEDULED_FOR_PROCESSING
: NEVER_PROCESS) {
if (!profile) {
CHECK(!ui_part);
return;
......@@ -128,7 +131,7 @@ std::string RulesRegistryWithCache::AddRules(
rules_[key] = *i;
}
ProcessChangedRules(extension_id);
MaybeProcessChangedRules(extension_id);
return kSuccess;
}
......@@ -149,7 +152,7 @@ std::string RulesRegistryWithCache::RemoveRules(
rules_.erase(lookup_key);
}
ProcessChangedRules(extension_id);
MaybeProcessChangedRules(extension_id);
return kSuccess;
}
......@@ -171,7 +174,7 @@ std::string RulesRegistryWithCache::RemoveAllRules(
rules_.erase(key);
}
ProcessChangedRules(extension_id);
MaybeProcessChangedRules(extension_id);
return kSuccess;
}
......@@ -239,6 +242,8 @@ void RulesRegistryWithCache::ProcessChangedRules(
const std::string& extension_id) {
DCHECK(content::BrowserThread::CurrentlyOn(owner_thread()));
process_changed_rules_requested_ = NOT_SCHEDULED_FOR_PROCESSING;
std::vector<linked_ptr<RulesRegistry::Rule> > new_rules;
std::string error = GetAllRules(extension_id, &new_rules);
DCHECK_EQ(std::string(), error);
......@@ -251,6 +256,18 @@ void RulesRegistryWithCache::ProcessChangedRules(
base::Passed(RulesToValue(new_rules))));
}
void RulesRegistryWithCache::MaybeProcessChangedRules(
const std::string& extension_id) {
if (process_changed_rules_requested_ != NOT_SCHEDULED_FOR_PROCESSING)
return;
process_changed_rules_requested_ = SCHEDULED_FOR_PROCESSING;
ready_.Post(FROM_HERE,
base::Bind(&RulesRegistryWithCache::ProcessChangedRules,
weak_ptr_factory_.GetWeakPtr(),
extension_id));
}
// RulesRegistryWithCache::RuleStorageOnUI
const char RulesRegistryWithCache::RuleStorageOnUI::kRulesStoredKey[] =
......
......@@ -176,10 +176,23 @@ class RulesRegistryWithCache : public RulesRegistry {
typedef std::pair<ExtensionId, RuleId> RulesDictionaryKey;
typedef std::map<RulesDictionaryKey, linked_ptr<RulesRegistry::Rule> >
RulesDictionary;
enum ProcessChangedRulesState {
// ProcessChangedRules can never be called, |storage_on_ui_| is NULL.
NEVER_PROCESS,
// A task to call ProcessChangedRules is scheduled for future execution.
SCHEDULED_FOR_PROCESSING,
// No task to call ProcessChangedRules is scheduled yet, but it is possible
// to schedule one.
NOT_SCHEDULED_FOR_PROCESSING
};
// Common processing after extension's rules have changed.
void ProcessChangedRules(const std::string& extension_id);
// Calls ProcessChangedRules if |process_changed_rules_requested_| ==
// NOT_SCHEDULED_FOR_PROCESSING.
void MaybeProcessChangedRules(const std::string& extension_id);
// Process the callbacks once the registry gets ready.
void MarkReady(base::Time storage_init_time);
......@@ -207,6 +220,8 @@ class RulesRegistryWithCache : public RulesRegistry {
// safe. The registry only ever associates with one RuleStorageOnUI instance.
const base::WeakPtr<RuleStorageOnUI> storage_on_ui_;
ProcessChangedRulesState process_changed_rules_requested_;
DISALLOW_COPY_AND_ASSIGN(RulesRegistryWithCache);
};
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment