[email protected] | 45f5b7d | 2014-01-22 23:47:13 | [diff] [blame] | 1 | // Copyright 2014 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 | #ifndef EXTENSIONS_BROWSER_RUNTIME_DATA_H_ |
| 6 | #define EXTENSIONS_BROWSER_RUNTIME_DATA_H_ |
| 7 | |
| 8 | #include <map> |
| 9 | #include <string> |
| 10 | |
| 11 | #include "base/compiler_specific.h" |
| 12 | #include "extensions/browser/extension_registry_observer.h" |
| 13 | |
| 14 | namespace extensions { |
| 15 | |
[email protected] | 45f5b7d | 2014-01-22 23:47:13 | [diff] [blame] | 16 | class ExtensionRegistry; |
| 17 | |
| 18 | // Contains per-extension data that can change during the life of the process, |
| 19 | // but does not persist across restarts. Shared between incognito and regular |
| 20 | // browser contexts. Lives on the UI thread. Must be destroyed before |
| 21 | // ExtensionRegistry. |
rdevlin.cronin | 2c16c60 | 2014-11-21 01:35:49 | [diff] [blame] | 22 | // If we start putting to much into this, we should expose the generic |
| 23 | // "[G|S]etRuntimeProperty(const std::string& extension_id, RuntimeFlag flag)" |
| 24 | // instead of all these. |
[email protected] | 45f5b7d | 2014-01-22 23:47:13 | [diff] [blame] | 25 | class RuntimeData : public ExtensionRegistryObserver { |
| 26 | public: |
| 27 | // Observes |registry| to clean itself up when extensions change state. |
| 28 | // |registry| must not be NULL. |
| 29 | explicit RuntimeData(ExtensionRegistry* registry); |
dcheng | 9168b2f | 2014-10-21 12:38:24 | [diff] [blame] | 30 | ~RuntimeData() override; |
[email protected] | 45f5b7d | 2014-01-22 23:47:13 | [diff] [blame] | 31 | |
| 32 | // Whether the persistent background page, if any, is ready. We don't load |
| 33 | // other components until then. If there is no background page, or if it is |
| 34 | // non-persistent (lazy), we consider it to be ready. |
| 35 | bool IsBackgroundPageReady(const Extension* extension) const; |
rdevlin.cronin | 2c16c60 | 2014-11-21 01:35:49 | [diff] [blame] | 36 | void SetBackgroundPageReady(const std::string& extension_id, bool value); |
[email protected] | 45f5b7d | 2014-01-22 23:47:13 | [diff] [blame] | 37 | |
| 38 | // Getter and setter for the flag that specifies whether the extension is |
| 39 | // being upgraded. |
rdevlin.cronin | 2c16c60 | 2014-11-21 01:35:49 | [diff] [blame] | 40 | // For these purposes, a reload counts as an upgrade. |
| 41 | bool IsBeingUpgraded(const std::string& extension_id) const; |
| 42 | void SetBeingUpgraded(const std::string& extension_id, bool value); |
[email protected] | 45f5b7d | 2014-01-22 23:47:13 | [diff] [blame] | 43 | |
[email protected] | 45f5b7d | 2014-01-22 23:47:13 | [diff] [blame] | 44 | // Returns true if the extension is being tracked. Used only for testing. |
rdevlin.cronin | 2c16c60 | 2014-11-21 01:35:49 | [diff] [blame] | 45 | bool HasExtensionForTesting(const std::string& extension_id) const; |
[email protected] | 45f5b7d | 2014-01-22 23:47:13 | [diff] [blame] | 46 | |
| 47 | // Erase runtime data for all extensions. Used only for testing. Cannot be |
| 48 | // named ClearAllForTesting due to false-positive presubmit errors. |
| 49 | void ClearAll(); |
| 50 | |
| 51 | // ExtensionRegistryObserver overrides. Public for testing. |
dcheng | 9168b2f | 2014-10-21 12:38:24 | [diff] [blame] | 52 | void OnExtensionUnloaded(content::BrowserContext* browser_context, |
| 53 | const Extension* extension, |
limasdf | 0deef204 | 2017-05-03 19:17:17 | [diff] [blame] | 54 | UnloadedExtensionReason reason) override; |
[email protected] | 45f5b7d | 2014-01-22 23:47:13 | [diff] [blame] | 55 | |
| 56 | private: |
| 57 | // Bitmasks for runtime states. |
| 58 | enum RuntimeFlag { |
| 59 | // Set if the background page is ready. |
| 60 | BACKGROUND_PAGE_READY = 1 << 0, |
| 61 | // Set while the extension is being upgraded. |
| 62 | BEING_UPGRADED = 1 << 1, |
[email protected] | 45f5b7d | 2014-01-22 23:47:13 | [diff] [blame] | 63 | }; |
| 64 | |
rdevlin.cronin | 2c16c60 | 2014-11-21 01:35:49 | [diff] [blame] | 65 | // The mask of any data that should persist across the extension being |
| 66 | // unloaded. |
| 67 | static const int kPersistAcrossUnloadMask = BEING_UPGRADED; |
| 68 | |
[email protected] | 45f5b7d | 2014-01-22 23:47:13 | [diff] [blame] | 69 | // Returns the setting for the flag or false if the extension isn't found. |
rdevlin.cronin | 2c16c60 | 2014-11-21 01:35:49 | [diff] [blame] | 70 | bool HasFlag(const std::string& extension_id, RuntimeFlag flag) const; |
[email protected] | 45f5b7d | 2014-01-22 23:47:13 | [diff] [blame] | 71 | |
| 72 | // Sets |flag| for |extension| to |value|. Adds |extension| to the list of |
| 73 | // extensions if it isn't present. |
rdevlin.cronin | 2c16c60 | 2014-11-21 01:35:49 | [diff] [blame] | 74 | void SetFlag(const std::string& extension_id, RuntimeFlag flag, bool value); |
[email protected] | 45f5b7d | 2014-01-22 23:47:13 | [diff] [blame] | 75 | |
| 76 | // Map from extension ID to the RuntimeFlags bits. |
| 77 | typedef std::map<std::string, int> ExtensionFlagsMap; |
| 78 | ExtensionFlagsMap extension_flags_; |
| 79 | |
| 80 | ExtensionRegistry* registry_; // Not owned. |
| 81 | }; |
| 82 | |
| 83 | } // namespace extensions |
| 84 | |
| 85 | #endif // EXTENSIONS_BROWSER_RUNTIME_DATA_H_ |