summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/extension_info_map.h
blob: 6aaf6b06d4b1096b3607247bed50c8577d567842 (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
// Copyright (c) 2011 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_EXTENSION_INFO_MAP_H_
#define CHROME_BROWSER_EXTENSIONS_EXTENSION_INFO_MAP_H_
#pragma once

#include <map>
#include <string>

#include "base/basictypes.h"
#include "base/time.h"
#include "base/memory/ref_counted.h"
#include "chrome/browser/extensions/extensions_quota_service.h"
#include "chrome/common/extensions/extension_constants.h"
#include "chrome/common/extensions/extension_set.h"

class Extension;

// Contains extension data that needs to be accessed on the IO thread. It can
// be created/destroyed on any thread, but all other methods must be called on
// the IO thread.
class ExtensionInfoMap : public base::RefCountedThreadSafe<ExtensionInfoMap> {
 public:
  ExtensionInfoMap();
  ~ExtensionInfoMap();

  const ExtensionSet& extensions() const { return extensions_; }
  const ExtensionSet& disabled_extensions() const {
    return disabled_extensions_;
  }

  // Callback for when new extensions are loaded.
  void AddExtension(const Extension* extension,
                    base::Time install_time,
                    bool incognito_enabled);

  // Callback for when an extension is unloaded.
  void RemoveExtension(const std::string& extension_id,
                       const extension_misc::UnloadedExtensionReason reason);

  // Returns the time the extension was installed, or base::Time() if not found.
  base::Time GetInstallTime(const std::string& extension_id) const;

  // Returns true if the user has allowed this extension to run in incognito
  // mode.
  bool IsIncognitoEnabled(const std::string& extension_id) const;

  // Returns true if the given extension can see events and data from another
  // sub-profile (incognito to original profile, or vice versa).
  bool CanCrossIncognito(const Extension* extension);

  // Record that |extension_id| is running in |process_id|. We normally have
  // this information in  ExtensionProcessManager on the UI thread, but we also
  // sometimes need it on the IO thread. Note that this can be any of
  // (extension, packaged app, hosted app).
  void RegisterExtensionProcess(const std::string& extension_id,
                                int process_id);

  // Remove any record of |extension_id| created with RegisterExtensionProcess.
  // If |extension_id| is unknown, we ignore it.
  void UnregisterExtensionProcess(const std::string& extension_id,
                                  int process_id);

  // Returns true if |extension_id| is running in |process_id|..
  bool IsExtensionInProcess(const std::string& extension_id,
                            int process_id) const;

  // Returns true if there is exists an extension with the same origin as
  // |origin| in |process_id| with |permission|.
  bool SecurityOriginHasAPIPermission(
      const GURL& origin, int process_id,
      ExtensionAPIPermission::ID permission) const;

  ExtensionsQuotaService* quota_service() { return &quota_service_; }

 private:
  // Extra dynamic data related to an extension.
  struct ExtraData;
  // Map of extension_id to ExtraData.
  typedef std::map<std::string, ExtraData> ExtraDataMap;

  ExtensionSet extensions_;
  ExtensionSet disabled_extensions_;

  // Extra data associated with enabled extensions.
  ExtraDataMap extra_data_;

  typedef std::multimap<std::string, int> ExtensionProcessIDMap;
  ExtensionProcessIDMap extension_process_ids_;

  // Used by dispatchers to limit API quota for individual extensions.
  ExtensionsQuotaService quota_service_;
};

#endif  // CHROME_BROWSER_EXTENSIONS_EXTENSION_INFO_MAP_H_