lazy_event_dispatch_util.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright 2017 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_EVENTS_LAZY_EVENT_DISPATCH_UTIL_H_
  5. #define EXTENSIONS_BROWSER_EVENTS_LAZY_EVENT_DISPATCH_UTIL_H_
  6. #include "base/memory/raw_ptr.h"
  7. #include "base/observer_list.h"
  8. #include "base/scoped_observation.h"
  9. #include "extensions/browser/extension_registry.h"
  10. #include "extensions/browser/extension_registry_observer.h"
  11. #include "extensions/browser/uninstall_reason.h"
  12. namespace base {
  13. class Version;
  14. }
  15. namespace extensions {
  16. // Utility class to observe extension installation and loading related events
  17. // from lazy contexts.
  18. //
  19. // This class observes ExtensionRegistry and uses ExtensionPrefs to detect
  20. // whether an extension is loaded after (first time) installation or after an
  21. // update.
  22. class LazyEventDispatchUtil : public ExtensionRegistryObserver {
  23. public:
  24. // Helps observer with events for lazy event dispatching.
  25. class Observer {
  26. public:
  27. // Called when an extension is loaded after installation, for one of the
  28. // following scenarios:
  29. // 1. New extension is installed.
  30. // 2. An extension is updated and loaded.
  31. // 3. An extension is enabled after it was disabled during an update.
  32. virtual void OnExtensionInstalledAndLoaded(
  33. content::BrowserContext* browser_context,
  34. const Extension* extension,
  35. const base::Version& old_version) {}
  36. };
  37. explicit LazyEventDispatchUtil(content::BrowserContext* browser_context);
  38. LazyEventDispatchUtil(const LazyEventDispatchUtil&) = delete;
  39. LazyEventDispatchUtil& operator=(const LazyEventDispatchUtil&) = delete;
  40. ~LazyEventDispatchUtil() override;
  41. void AddObserver(Observer* observer);
  42. void RemoveObserver(Observer* observer);
  43. // ExtensionRegistryObserver:
  44. void OnExtensionLoaded(content::BrowserContext* browser_context,
  45. const Extension* extension) override;
  46. void OnExtensionWillBeInstalled(content::BrowserContext* browser_context,
  47. const Extension* extension,
  48. bool is_update,
  49. const std::string& old_name) override;
  50. void OnExtensionUninstalled(content::BrowserContext* browser_context,
  51. const Extension* extension,
  52. UninstallReason reason) override;
  53. private:
  54. bool ReadPendingOnInstallInfoFromPref(const ExtensionId& extension_id,
  55. base::Version* previous_version);
  56. void RemovePendingOnInstallInfoFromPref(const ExtensionId& extension_id);
  57. void StorePendingOnInstallInfoToPref(const Extension* extension);
  58. raw_ptr<content::BrowserContext> browser_context_;
  59. base::ObserverList<Observer>::Unchecked observers_;
  60. base::ScopedObservation<ExtensionRegistry, ExtensionRegistryObserver>
  61. extension_registry_observation_{this};
  62. };
  63. } // namespace extensions
  64. #endif // EXTENSIONS_BROWSER_EVENTS_LAZY_EVENT_DISPATCH_UTIL_H_