/extensions/browser/api/declarative/rules_registry_service.h

https://gitlab.com/jonnialva90/iridium-browser · C Header · 153 lines · 86 code · 34 blank · 33 comment · 1 complexity · aed94c7412d2d8e933f8d6e3906f9002 MD5 · raw file

  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
  3. // found in the LICENSE file.
  4. #ifndef EXTENSIONS_BROWSER_API_DECLARATIVE_RULES_REGISTRY_SERVICE_H__
  5. #define EXTENSIONS_BROWSER_API_DECLARATIVE_RULES_REGISTRY_SERVICE_H__
  6. #include <map>
  7. #include <string>
  8. #include <vector>
  9. #include "base/callback_forward.h"
  10. #include "base/memory/ref_counted.h"
  11. #include "base/memory/scoped_vector.h"
  12. #include "base/scoped_observer.h"
  13. #include "extensions/browser/api/declarative/rules_registry.h"
  14. #include "extensions/browser/browser_context_keyed_api_factory.h"
  15. #include "extensions/browser/extension_registry_observer.h"
  16. namespace content {
  17. class BrowserContext;
  18. }
  19. namespace extensions {
  20. class ContentRulesRegistry;
  21. class ExtensionRegistry;
  22. class RulesRegistryStorageDelegate;
  23. }
  24. namespace extensions {
  25. // This class owns all RulesRegistries implementations of an ExtensionService.
  26. // This class lives on the UI thread.
  27. class RulesRegistryService : public BrowserContextKeyedAPI,
  28. public ExtensionRegistryObserver {
  29. public:
  30. static const int kDefaultRulesRegistryID;
  31. static const int kInvalidRulesRegistryID;
  32. struct RulesRegistryKey {
  33. std::string event_name;
  34. int rules_registry_id;
  35. RulesRegistryKey(const std::string& event_name, int rules_registry_id)
  36. : event_name(event_name), rules_registry_id(rules_registry_id) {}
  37. bool operator<(const RulesRegistryKey& other) const {
  38. return (event_name < other.event_name) ||
  39. ((event_name == other.event_name) &&
  40. (rules_registry_id < other.rules_registry_id));
  41. }
  42. };
  43. explicit RulesRegistryService(content::BrowserContext* context);
  44. ~RulesRegistryService() override;
  45. // Unregisters refptrs to concrete RulesRegistries at other objects that were
  46. // created by us so that the RulesRegistries can be released.
  47. void Shutdown() override;
  48. // BrowserContextKeyedAPI implementation.
  49. static BrowserContextKeyedAPIFactory<RulesRegistryService>*
  50. GetFactoryInstance();
  51. // Convenience method to get the RulesRegistryService for a context. If a
  52. // RulesRegistryService does not already exist for |context|, one will be
  53. // created and returned.
  54. static RulesRegistryService* Get(content::BrowserContext* context);
  55. // The same as Get(), except that if a RulesRegistryService does not already
  56. // exist for |context|, nullptr is returned.
  57. static RulesRegistryService* GetIfExists(content::BrowserContext* context);
  58. int GetNextRulesRegistryID();
  59. // Registers the default RulesRegistries used in Chromium.
  60. void EnsureDefaultRulesRegistriesRegistered(int rules_registry_id);
  61. // Registers a RulesRegistry and wraps it in an InitializingRulesRegistry.
  62. void RegisterRulesRegistry(scoped_refptr<RulesRegistry> rule_registry);
  63. // Returns the RulesRegistry for |event_name| and |rules_registry_id| or
  64. // NULL if no such registry has been registered. Default rules registries
  65. // (such as the WebRequest rules registry) will be created on first access.
  66. scoped_refptr<RulesRegistry> GetRulesRegistry(int rules_registry_id,
  67. const std::string& event_name);
  68. // Remove all rules registries of the given rules_registry_id.
  69. void RemoveRulesRegistriesByID(int rules_registry_id);
  70. // Accessors for each type of rules registry.
  71. ContentRulesRegistry* content_rules_registry() const {
  72. CHECK(content_rules_registry_);
  73. return content_rules_registry_;
  74. }
  75. // For testing.
  76. void SimulateExtensionUninstalled(const Extension* extension);
  77. private:
  78. friend class BrowserContextKeyedAPIFactory<RulesRegistryService>;
  79. // Maps <event name, rules registry ID> to RuleRegistries that handle these
  80. // events.
  81. typedef std::map<RulesRegistryKey, scoped_refptr<RulesRegistry> >
  82. RulesRegistryMap;
  83. // ExtensionRegistryObserver implementation.
  84. void OnExtensionLoaded(content::BrowserContext* browser_context,
  85. const Extension* extension) override;
  86. void OnExtensionUnloaded(content::BrowserContext* browser_context,
  87. const Extension* extension,
  88. UnloadedExtensionInfo::Reason reason) override;
  89. void OnExtensionUninstalled(content::BrowserContext* browser_context,
  90. const Extension* extension,
  91. extensions::UninstallReason reason) override;
  92. // Iterates over all registries, and calls |notification_callback| on them
  93. // with |extension| as the argument. If a registry lives on a different
  94. // thread, the call is posted to that thread, so no guarantee of synchronous
  95. // processing.
  96. void NotifyRegistriesHelper(
  97. void (RulesRegistry::*notification_callback)(const Extension*),
  98. const Extension* extension);
  99. // BrowserContextKeyedAPI implementation.
  100. static const char* service_name() {
  101. return "RulesRegistryService";
  102. }
  103. static const bool kServiceHasOwnInstanceInIncognito = true;
  104. static const bool kServiceIsNULLWhileTesting = true;
  105. int current_rules_registry_id_;
  106. RulesRegistryMap rule_registries_;
  107. // We own the parts of the registries which need to run on the UI thread.
  108. ScopedVector<RulesCacheDelegate> cache_delegates_;
  109. // Weak pointer into rule_registries_ to make it easier to handle content rule
  110. // conditions.
  111. ContentRulesRegistry* content_rules_registry_;
  112. // Listen to extension load, unloaded notification.
  113. ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver>
  114. extension_registry_observer_;
  115. content::BrowserContext* browser_context_;
  116. DISALLOW_COPY_AND_ASSIGN(RulesRegistryService);
  117. };
  118. } // namespace extensions
  119. #endif // EXTENSIONS_BROWSER_API_DECLARATIVE_RULES_REGISTRY_SERVICE_H__