Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(535)

Side by Side Diff: chrome/browser/extensions/api/storage/managed_value_store_cache.cc

Issue 299393002: Use ExtensionRegistryObserver instead of deprecated extension notification from c/b/extension (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/extensions/api/storage/managed_value_store_cache.h" 5 #include "chrome/browser/extensions/api/storage/managed_value_store_cache.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/bind_helpers.h" 8 #include "base/bind_helpers.h"
9 #include "base/callback.h" 9 #include "base/callback.h"
10 #include "base/file_util.h" 10 #include "base/file_util.h"
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/memory/weak_ptr.h" 12 #include "base/memory/weak_ptr.h"
13 #include "chrome/browser/chrome_notification_types.h" 13 #include "base/scoped_observer.h"
14 #include "chrome/browser/extensions/api/storage/policy_value_store.h" 14 #include "chrome/browser/extensions/api/storage/policy_value_store.h"
15 #include "chrome/browser/policy/profile_policy_connector.h" 15 #include "chrome/browser/policy/profile_policy_connector.h"
16 #include "chrome/browser/policy/profile_policy_connector_factory.h" 16 #include "chrome/browser/policy/profile_policy_connector_factory.h"
17 #include "chrome/browser/policy/schema_registry_service.h" 17 #include "chrome/browser/policy/schema_registry_service.h"
18 #include "chrome/browser/policy/schema_registry_service_factory.h" 18 #include "chrome/browser/policy/schema_registry_service_factory.h"
19 #include "chrome/browser/profiles/profile.h" 19 #include "chrome/browser/profiles/profile.h"
20 #include "chrome/common/extensions/api/storage/storage_schema_manifest_handler.h " 20 #include "chrome/common/extensions/api/storage/storage_schema_manifest_handler.h "
21 #include "components/policy/core/common/policy_namespace.h" 21 #include "components/policy/core/common/policy_namespace.h"
22 #include "components/policy/core/common/schema.h" 22 #include "components/policy/core/common/schema.h"
23 #include "components/policy/core/common/schema_map.h" 23 #include "components/policy/core/common/schema_map.h"
24 #include "components/policy/core/common/schema_registry.h" 24 #include "components/policy/core/common/schema_registry.h"
25 #include "content/public/browser/browser_thread.h" 25 #include "content/public/browser/browser_thread.h"
26 #include "content/public/browser/notification_details.h"
27 #include "content/public/browser/notification_observer.h"
28 #include "content/public/browser/notification_registrar.h"
29 #include "content/public/browser/notification_source.h"
30 #include "extensions/browser/api/storage/settings_storage_factory.h" 26 #include "extensions/browser/api/storage/settings_storage_factory.h"
31 #include "extensions/browser/extension_prefs.h" 27 #include "extensions/browser/extension_prefs.h"
32 #include "extensions/browser/extension_registry.h" 28 #include "extensions/browser/extension_registry.h"
29 #include "extensions/browser/extension_registry_observer.h"
33 #include "extensions/browser/extension_system.h" 30 #include "extensions/browser/extension_system.h"
34 #include "extensions/browser/value_store/value_store_change.h" 31 #include "extensions/browser/value_store/value_store_change.h"
35 #include "extensions/common/api/storage.h" 32 #include "extensions/common/api/storage.h"
36 #include "extensions/common/constants.h" 33 #include "extensions/common/constants.h"
37 #include "extensions/common/extension.h" 34 #include "extensions/common/extension.h"
38 #include "extensions/common/extension_set.h" 35 #include "extensions/common/extension_set.h"
39 #include "extensions/common/manifest.h" 36 #include "extensions/common/manifest.h"
40 #include "extensions/common/manifest_constants.h" 37 #include "extensions/common/manifest_constants.h"
41 #include "extensions/common/one_shot_event.h" 38 #include "extensions/common/one_shot_event.h"
42 39
43 using content::BrowserContext; 40 using content::BrowserContext;
44 using content::BrowserThread; 41 using content::BrowserThread;
45 42
46 namespace extensions { 43 namespace extensions {
44 class ExtensionRegistry;
47 45
48 namespace storage = core_api::storage; 46 namespace storage = core_api::storage;
49 47
50 namespace { 48 namespace {
51 49
52 const char kLoadSchemasBackgroundTaskTokenName[] = 50 const char kLoadSchemasBackgroundTaskTokenName[] =
53 "load_managed_storage_schemas_token"; 51 "load_managed_storage_schemas_token";
54 52
55 // The Legacy Browser Support was the first user of the policy-for-extensions 53 // The Legacy Browser Support was the first user of the policy-for-extensions
56 // API, and relied on behavior that will be phased out. If this extension is 54 // API, and relied on behavior that will be phased out. If this extension is
57 // present then its policies will be loaded in a special way. 55 // present then its policies will be loaded in a special way.
58 // TODO(joaodasilva): remove this for M35. http://crbug.com/325349 56 // TODO(joaodasilva): remove this for M35. http://crbug.com/325349
59 const char kLegacyBrowserSupportExtensionId[] = 57 const char kLegacyBrowserSupportExtensionId[] =
60 "heildphpnddilhkemkielfhnkaagiabh"; 58 "heildphpnddilhkemkielfhnkaagiabh";
61 59
62 } // namespace 60 } // namespace
63 61
64 // This helper observes initialization of all the installed extensions and 62 // This helper observes initialization of all the installed extensions and
65 // subsequent loads and unloads, and keeps the SchemaRegistry of the Profile 63 // subsequent loads and unloads, and keeps the SchemaRegistry of the Profile
66 // in sync with the current list of extensions. This allows the PolicyService 64 // in sync with the current list of extensions. This allows the PolicyService
67 // to fetch cloud policy for those extensions, and allows its providers to 65 // to fetch cloud policy for those extensions, and allows its providers to
68 // selectively load only extension policy that has users. 66 // selectively load only extension policy that has users.
69 class ManagedValueStoreCache::ExtensionTracker 67 class ManagedValueStoreCache::ExtensionTracker
70 : public content::NotificationObserver { 68 : public ExtensionRegistryObserver {
71 public: 69 public:
72 explicit ExtensionTracker(Profile* profile); 70 explicit ExtensionTracker(Profile* profile);
73 virtual ~ExtensionTracker() {} 71 virtual ~ExtensionTracker() {}
74 72
75 // NotificationObserver implementation: 73 private:
76 virtual void Observe(int type, 74 // ExtensionRegistryObserver implementation.
77 const content::NotificationSource& source, 75 virtual void OnExtensionWillBeInstalled(
78 const content::NotificationDetails& details) OVERRIDE; 76 content::BrowserContext* browser_context,
77 const Extension* extension,
78 bool is_update,
79 bool from_ephemeral,
80 const std::string& old_name) OVERRIDE;
81 virtual void OnExtensionUninstalled(content::BrowserContext* browser_context,
82 const Extension* extension) OVERRIDE;
79 83
80 private:
81 // Handler for the signal from ExtensionSystem::ready(). 84 // Handler for the signal from ExtensionSystem::ready().
82 void OnExtensionsReady(); 85 void OnExtensionsReady();
83 86
84 // Starts a schema load for all extensions that use managed storage. 87 // Starts a schema load for all extensions that use managed storage.
85 void LoadSchemas(scoped_ptr<ExtensionSet> added); 88 void LoadSchemas(scoped_ptr<ExtensionSet> added);
86 89
87 bool UsesManagedStorage(const Extension* extension) const; 90 bool UsesManagedStorage(const Extension* extension) const;
88 91
89 // Loads the schemas of the |extensions| and passes a ComponentMap to 92 // Loads the schemas of the |extensions| and passes a ComponentMap to
90 // Register(). 93 // Register().
91 static void LoadSchemasOnBlockingPool(scoped_ptr<ExtensionSet> extensions, 94 static void LoadSchemasOnBlockingPool(scoped_ptr<ExtensionSet> extensions,
92 base::WeakPtr<ExtensionTracker> self); 95 base::WeakPtr<ExtensionTracker> self);
93 void Register(const policy::ComponentMap* components); 96 void Register(const policy::ComponentMap* components);
94 97
95 Profile* profile_; 98 Profile* profile_;
96 content::NotificationRegistrar registrar_; 99 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver>
100 extension_registry_observer_;
97 policy::SchemaRegistry* schema_registry_; 101 policy::SchemaRegistry* schema_registry_;
98 base::WeakPtrFactory<ExtensionTracker> weak_factory_; 102 base::WeakPtrFactory<ExtensionTracker> weak_factory_;
99 103
100 DISALLOW_COPY_AND_ASSIGN(ExtensionTracker); 104 DISALLOW_COPY_AND_ASSIGN(ExtensionTracker);
101 }; 105 };
102 106
103 ManagedValueStoreCache::ExtensionTracker::ExtensionTracker(Profile* profile) 107 ManagedValueStoreCache::ExtensionTracker::ExtensionTracker(Profile* profile)
104 : profile_(profile), 108 : profile_(profile),
109 extension_registry_observer_(this),
105 schema_registry_( 110 schema_registry_(
106 policy::SchemaRegistryServiceFactory::GetForContext(profile)), 111 policy::SchemaRegistryServiceFactory::GetForContext(profile)),
107 weak_factory_(this) { 112 weak_factory_(this) {
108 registrar_.Add(this, 113 extension_registry_observer_.Add(ExtensionRegistry::Get(profile_));
109 chrome::NOTIFICATION_EXTENSION_INSTALLED_DEPRECATED,
110 content::Source<Profile>(profile_));
111 registrar_.Add(this,
112 chrome::NOTIFICATION_EXTENSION_UNINSTALLED_DEPRECATED,
113 content::Source<Profile>(profile_));
114
115 // Load schemas when the extension system is ready. It might be ready now. 114 // Load schemas when the extension system is ready. It might be ready now.
116 ExtensionSystem::Get(profile_)->ready().Post( 115 ExtensionSystem::Get(profile_)->ready().Post(
117 FROM_HERE, 116 FROM_HERE,
118 base::Bind(&ExtensionTracker::OnExtensionsReady, 117 base::Bind(&ExtensionTracker::OnExtensionsReady,
119 weak_factory_.GetWeakPtr())); 118 weak_factory_.GetWeakPtr()));
120 } 119 }
121 120
122 void ManagedValueStoreCache::ExtensionTracker::Observe( 121 void ManagedValueStoreCache::ExtensionTracker::OnExtensionWillBeInstalled(
123 int type, 122 content::BrowserContext* browser_context,
124 const content::NotificationSource& source, 123 const Extension* extension,
125 const content::NotificationDetails& details) { 124 bool is_update,
125 bool from_ephemeral,
126 const std::string& old_name) {
126 // Some extensions are installed on the first run before the ExtensionSystem 127 // Some extensions are installed on the first run before the ExtensionSystem
127 // becomes ready. Wait until all of them are ready before registering the 128 // becomes ready. Wait until all of them are ready before registering the
128 // schemas of managed extensions, so that the policy loaders are reloaded at 129 // schemas of managed extensions, so that the policy loaders are reloaded at
129 // most once. 130 // most once.
130 if (!ExtensionSystem::Get(profile_)->ready().is_signaled()) 131 if (!ExtensionSystem::Get(profile_)->ready().is_signaled())
131 return; 132 return;
133 scoped_ptr<ExtensionSet> added(new ExtensionSet);
134 added->Insert(extension);
135 LoadSchemas(added.Pass());
136 }
132 137
133 switch (type) { 138 void ManagedValueStoreCache::ExtensionTracker::OnExtensionUninstalled(
134 case chrome::NOTIFICATION_EXTENSION_INSTALLED_DEPRECATED: { 139 content::BrowserContext* browser_context,
135 scoped_ptr<ExtensionSet> added(new ExtensionSet); 140 const Extension* extension) {
136 added->Insert( 141 if (!ExtensionSystem::Get(profile_)->ready().is_signaled())
137 content::Details<InstalledExtensionInfo>(details)->extension); 142 return;
138 LoadSchemas(added.Pass()); 143 if (extension && UsesManagedStorage(extension)) {
139 break; 144 schema_registry_->UnregisterComponent(policy::PolicyNamespace(
140 } 145 policy::POLICY_DOMAIN_EXTENSIONS, extension->id()));
141 case chrome::NOTIFICATION_EXTENSION_UNINSTALLED_DEPRECATED: {
142 const Extension* removed =
143 content::Details<const Extension>(details).ptr();
144 if (removed && UsesManagedStorage(removed)) {
145 schema_registry_->UnregisterComponent(policy::PolicyNamespace(
146 policy::POLICY_DOMAIN_EXTENSIONS, removed->id()));
147 }
148 break;
149 }
150 default:
151 NOTREACHED();
152 return;
153 } 146 }
154 } 147 }
155 148
156 void ManagedValueStoreCache::ExtensionTracker::OnExtensionsReady() { 149 void ManagedValueStoreCache::ExtensionTracker::OnExtensionsReady() {
157 // Load schemas for all installed extensions. 150 // Load schemas for all installed extensions.
158 LoadSchemas( 151 LoadSchemas(
159 ExtensionRegistry::Get(profile_)->GenerateInstalledExtensionsSet()); 152 ExtensionRegistry::Get(profile_)->GenerateInstalledExtensionsSet());
160 } 153 }
161 154
162 void ManagedValueStoreCache::ExtensionTracker::LoadSchemas( 155 void ManagedValueStoreCache::ExtensionTracker::LoadSchemas(
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after
369 362
370 return store; 363 return store;
371 } 364 }
372 365
373 bool ManagedValueStoreCache::HasStore(const std::string& extension_id) const { 366 bool ManagedValueStoreCache::HasStore(const std::string& extension_id) const {
374 // TODO(joaodasilva): move this check to a ValueStore method. 367 // TODO(joaodasilva): move this check to a ValueStore method.
375 return base::DirectoryExists(base_path_.AppendASCII(extension_id)); 368 return base::DirectoryExists(base_path_.AppendASCII(extension_id));
376 } 369 }
377 370
378 } // namespace extensions 371 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698