Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / declarative / rules_cache_delegate.cc
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/browser/extensions/api/declarative/rules_cache_delegate.h"
6
7 #include "chrome/browser/chrome_notification_types.h"
8 #include "chrome/browser/extensions/api/declarative/rules_registry.h"
9 #include "chrome/browser/extensions/extension_service.h"
10 #include "chrome/browser/extensions/extension_util.h"
11 #include "chrome/browser/extensions/state_store.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "content/public/browser/notification_details.h"
14 #include "content/public/browser/notification_source.h"
15 #include "extensions/browser/extension_system.h"
16 #include "extensions/browser/info_map.h"
17
18 namespace {
19
20 // Returns the key to use for storing declarative rules in the state store.
21 std::string GetDeclarativeRuleStorageKey(const std::string& event_name,
22                                          bool incognito) {
23   if (incognito)
24     return "declarative_rules.incognito." + event_name;
25   else
26     return "declarative_rules." + event_name;
27 }
28
29
30 }  // namespace
31
32 namespace extensions {
33
34 // RulesCacheDelegate
35
36 const char RulesCacheDelegate::kRulesStoredKey[] =
37     "has_declarative_rules";
38
39 RulesCacheDelegate::RulesCacheDelegate(bool log_storage_init_delay)
40     : profile_(NULL),
41       log_storage_init_delay_(log_storage_init_delay),
42       notified_registry_(false),
43       weak_ptr_factory_(this) {
44 }
45
46 RulesCacheDelegate::~RulesCacheDelegate() {}
47
48 // Returns the key to use for storing whether the rules have been stored.
49 // static
50 std::string RulesCacheDelegate::GetRulesStoredKey(const std::string& event_name,
51                                                   bool incognito) {
52   std::string result(kRulesStoredKey);
53   result += incognito ? ".incognito." : ".";
54   return result + event_name;
55 }
56
57 // This is called from the constructor of RulesRegistry, so it is
58 // important that it both
59 // 1. calls no (in particular virtual) methods of the rules registry, and
60 // 2. does not create scoped_refptr holding the registry. (A short-lived
61 // scoped_refptr might delete the rules registry before it is constructed.)
62 void RulesCacheDelegate::Init(RulesRegistry* registry) {
63   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
64
65   // WARNING: The first use of |registry_| will bind it to the calling thread
66   // so don't use this here.
67   registry_ = registry->GetWeakPtr();
68
69   profile_ = registry->profile();
70   storage_key_ =
71       GetDeclarativeRuleStorageKey(registry->event_name(),
72                                    profile_->IsOffTheRecord());
73   rules_stored_key_ = GetRulesStoredKey(registry->event_name(),
74                                         profile_->IsOffTheRecord());
75   rules_registry_thread_ = registry->owner_thread();
76
77   ExtensionSystem& system = *ExtensionSystem::Get(profile_);
78   extensions::StateStore* store = system.rules_store();
79   if (store)
80     store->RegisterKey(storage_key_);
81
82   if (profile_->IsOffTheRecord())
83     log_storage_init_delay_ = false;
84
85   system.ready().Post(
86       FROM_HERE,
87       base::Bind(&RulesCacheDelegate::ReadRulesForInstalledExtensions,
88                  weak_ptr_factory_.GetWeakPtr()));
89   system.ready().Post(FROM_HERE,
90                       base::Bind(&RulesCacheDelegate::CheckIfReady,
91                                  weak_ptr_factory_.GetWeakPtr()));
92 }
93
94 void RulesCacheDelegate::WriteToStorage(const std::string& extension_id,
95                                      scoped_ptr<base::Value> value) {
96   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
97   if (!profile_)
98     return;
99
100   const base::ListValue* rules = NULL;
101   CHECK(value->GetAsList(&rules));
102   bool rules_stored_previously = GetDeclarativeRulesStored(extension_id);
103   bool store_rules = !rules->empty();
104   SetDeclarativeRulesStored(extension_id, store_rules);
105   if (!rules_stored_previously && !store_rules)
106     return;
107
108   StateStore* store = ExtensionSystem::Get(profile_)->rules_store();
109   if (store)
110     store->SetExtensionValue(extension_id, storage_key_, value.Pass());
111 }
112
113 void RulesCacheDelegate::CheckIfReady() {
114   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
115   if (notified_registry_ || !waiting_for_extensions_.empty())
116     return;
117
118   content::BrowserThread::PostTask(
119       rules_registry_thread_,
120       FROM_HERE,
121       base::Bind(
122           &RulesRegistry::MarkReady, registry_, storage_init_time_));
123   notified_registry_ = true;
124 }
125
126 void RulesCacheDelegate::ReadRulesForInstalledExtensions() {
127   ExtensionSystem& system = *ExtensionSystem::Get(profile_);
128   ExtensionService* extension_service = system.extension_service();
129   DCHECK(extension_service);
130   // In an OTR profile, we start on top of a normal profile already, so the
131   // extension service should be ready.
132   DCHECK(!profile_->IsOffTheRecord() || extension_service->is_ready());
133   if (extension_service->is_ready()) {
134     const ExtensionSet* extensions = extension_service->extensions();
135     for (ExtensionSet::const_iterator i = extensions->begin();
136          i != extensions->end();
137          ++i) {
138       bool needs_apis_storing_rules =
139           (*i)->HasAPIPermission(APIPermission::kDeclarativeContent) ||
140           (*i)->HasAPIPermission(APIPermission::kDeclarativeWebRequest);
141       bool respects_off_the_record =
142           !(profile_->IsOffTheRecord()) ||
143           util::IsIncognitoEnabled((*i)->id(), profile_);
144       if (needs_apis_storing_rules && respects_off_the_record)
145         ReadFromStorage((*i)->id());
146     }
147   }
148 }
149
150 void RulesCacheDelegate::ReadFromStorage(const std::string& extension_id) {
151   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
152   if (!profile_)
153     return;
154
155   if (log_storage_init_delay_ && storage_init_time_.is_null())
156     storage_init_time_ = base::Time::Now();
157
158   if (!GetDeclarativeRulesStored(extension_id)) {
159     ExtensionSystem::Get(profile_)->ready().Post(
160         FROM_HERE, base::Bind(&RulesCacheDelegate::CheckIfReady,
161                               weak_ptr_factory_.GetWeakPtr()));
162     return;
163   }
164
165   extensions::StateStore* store = ExtensionSystem::Get(profile_)->rules_store();
166   if (!store)
167     return;
168   waiting_for_extensions_.insert(extension_id);
169   store->GetExtensionValue(
170       extension_id,
171       storage_key_,
172       base::Bind(&RulesCacheDelegate::ReadFromStorageCallback,
173                  weak_ptr_factory_.GetWeakPtr(),
174                  extension_id));
175 }
176
177 void RulesCacheDelegate::ReadFromStorageCallback(
178     const std::string& extension_id,
179     scoped_ptr<base::Value> value) {
180   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
181   content::BrowserThread::PostTask(
182       rules_registry_thread_,
183       FROM_HERE,
184       base::Bind(&RulesRegistry::DeserializeAndAddRules,
185                  registry_,
186                  extension_id,
187                  base::Passed(&value)));
188
189   waiting_for_extensions_.erase(extension_id);
190
191   if (waiting_for_extensions_.empty())
192     ExtensionSystem::Get(profile_)->ready().Post(
193         FROM_HERE, base::Bind(&RulesCacheDelegate::CheckIfReady,
194                               weak_ptr_factory_.GetWeakPtr()));
195 }
196
197 bool RulesCacheDelegate::GetDeclarativeRulesStored(
198     const std::string& extension_id) const {
199   CHECK(profile_);
200   const ExtensionScopedPrefs* extension_prefs = ExtensionPrefs::Get(profile_);
201
202   bool rules_stored = true;
203   if (extension_prefs->ReadPrefAsBoolean(
204           extension_id, rules_stored_key_, &rules_stored))
205     return rules_stored;
206
207   // Safe default -- if we don't know that the rules are not stored, we force
208   // a read by returning true.
209   return true;
210 }
211
212 void RulesCacheDelegate::SetDeclarativeRulesStored(
213     const std::string& extension_id,
214     bool rules_stored) {
215   CHECK(profile_);
216   ExtensionSystem& system = *ExtensionSystem::Get(profile_);
217   ExtensionService* extension_service = system.extension_service();
218   DCHECK(extension_service);
219   DCHECK(extension_service->GetInstalledExtension(extension_id));
220   ExtensionScopedPrefs* extension_prefs = ExtensionPrefs::Get(profile_);
221   extension_prefs->UpdateExtensionPref(
222       extension_id,
223       rules_stored_key_,
224       new base::FundamentalValue(rules_stored));
225 }
226
227 }  // namespace extensions