summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/api/declarative/rules_registry_service.h
blob: ee8833e3d49c85290740a4eb7b6de5484dc3de50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright (c) 2012 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.

#ifndef CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_RULES_REGISTRY_SERVICE_H__
#define CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_RULES_REGISTRY_SERVICE_H__

#include <map>
#include <string>
#include <vector>

#include "base/callback_forward.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_vector.h"
#include "chrome/browser/extensions/api/declarative/rules_registry_with_cache.h"
#include "chrome/browser/extensions/api/profile_keyed_api_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_registrar.h"

class Profile;

namespace content {
class NotificationSource;
class NotificationSource;
}

namespace extensions {
class ContentRulesRegistry;
class RulesRegistry;
class RulesRegistryStorageDelegate;
}

namespace extensions {

// This class owns all RulesRegistries implementations of an ExtensionService.
// This class lives on the UI thread.
class RulesRegistryService : public ProfileKeyedAPI,
                             public content::NotificationObserver {
 public:
  explicit RulesRegistryService(Profile* profile);
  virtual ~RulesRegistryService();

  // Unregisters refptrs to concrete RulesRegistries at other objects that were
  // created by us so that the RulesRegistries can be released.
  virtual void Shutdown() OVERRIDE;

  // ProfileKeyedAPI implementation.
  static ProfileKeyedAPIFactory<RulesRegistryService>* GetFactoryInstance();

  // Convenience method to get the RulesRegistryService for a profile.
  static RulesRegistryService* Get(Profile* profile);

  // Registers the default RulesRegistries used in Chromium.
  void RegisterDefaultRulesRegistries();

  // Registers a RulesRegistry and wraps it in an InitializingRulesRegistry.
  void RegisterRulesRegistry(scoped_refptr<RulesRegistry> rule_registry);

  // Returns the RulesRegistry for |event_name| or NULL if no such registry
  // has been registered.
  scoped_refptr<RulesRegistry> GetRulesRegistry(
      const std::string& event_name) const;

  // Accessors for each type of rules registry.
  ContentRulesRegistry* content_rules_registry() const {
    return content_rules_registry_;
  }

  // For testing.
  void SimulateExtensionUnloaded(const std::string& extension_id);
 private:
  friend class ProfileKeyedAPIFactory<RulesRegistryService>;

  // Maps event names to RuleRegistries that handle these events.
  typedef std::map<std::string, scoped_refptr<RulesRegistry> > RulesRegistryMap;

  // Notifies all RulesRegistries that |extension_id| was unloaded.
  // It is not guaranteed that this notification is processed synchronously.
  // If extensions live on another thread, the notification is posted.
  void OnExtensionUnloaded(const std::string& extension_id);

  // Implementation of content::NotificationObserver.
  virtual void Observe(int type,
                       const content::NotificationSource& source,
                       const content::NotificationDetails& details) OVERRIDE;

  // ProfileKeyedAPI implementation.
  static const char* service_name() {
    return "RulesRegistryService";
  }
  static const bool kServiceHasOwnInstanceInIncognito = true;
  static const bool kServiceIsNULLWhileTesting = true;

  RulesRegistryMap rule_registries_;

  // We own the parts of the registries which need to run on the UI thread.
  ScopedVector<RulesRegistryWithCache::RuleStorageOnUI> ui_parts_of_registries_;

  // Weak pointer into rule_registries_ to make it easier to handle content rule
  // conditions.
  ContentRulesRegistry* content_rules_registry_;

  content::NotificationRegistrar registrar_;

  Profile* profile_;

  DISALLOW_COPY_AND_ASSIGN(RulesRegistryService);
};

}  // namespace extensions

#endif  // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_RULES_REGISTRY_SERVICE_H__