blob: f220ac21fbd163ec9e4b651c040ba5e605381bb5 [file] [log] [blame]
[email protected]45f5b7d2014-01-22 23:47:131// 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
14namespace extensions {
15
[email protected]45f5b7d2014-01-22 23:47:1316class 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.cronin2c16c602014-11-21 01:35:4922// 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]45f5b7d2014-01-22 23:47:1325class 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);
dcheng9168b2f2014-10-21 12:38:2430 ~RuntimeData() override;
[email protected]45f5b7d2014-01-22 23:47:1331
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.cronin2c16c602014-11-21 01:35:4936 void SetBackgroundPageReady(const std::string& extension_id, bool value);
[email protected]45f5b7d2014-01-22 23:47:1337
38 // Getter and setter for the flag that specifies whether the extension is
39 // being upgraded.
rdevlin.cronin2c16c602014-11-21 01:35:4940 // 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]45f5b7d2014-01-22 23:47:1343
[email protected]45f5b7d2014-01-22 23:47:1344 // Returns true if the extension is being tracked. Used only for testing.
rdevlin.cronin2c16c602014-11-21 01:35:4945 bool HasExtensionForTesting(const std::string& extension_id) const;
[email protected]45f5b7d2014-01-22 23:47:1346
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.
dcheng9168b2f2014-10-21 12:38:2452 void OnExtensionUnloaded(content::BrowserContext* browser_context,
53 const Extension* extension,
limasdf0deef2042017-05-03 19:17:1754 UnloadedExtensionReason reason) override;
[email protected]45f5b7d2014-01-22 23:47:1355
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]45f5b7d2014-01-22 23:47:1363 };
64
rdevlin.cronin2c16c602014-11-21 01:35:4965 // The mask of any data that should persist across the extension being
66 // unloaded.
67 static const int kPersistAcrossUnloadMask = BEING_UPGRADED;
68
[email protected]45f5b7d2014-01-22 23:47:1369 // Returns the setting for the flag or false if the extension isn't found.
rdevlin.cronin2c16c602014-11-21 01:35:4970 bool HasFlag(const std::string& extension_id, RuntimeFlag flag) const;
[email protected]45f5b7d2014-01-22 23:47:1371
72 // Sets |flag| for |extension| to |value|. Adds |extension| to the list of
73 // extensions if it isn't present.
rdevlin.cronin2c16c602014-11-21 01:35:4974 void SetFlag(const std::string& extension_id, RuntimeFlag flag, bool value);
[email protected]45f5b7d2014-01-22 23:47:1375
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_