• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 CHROME_BROWSER_EXTENSIONS_PENDING_EXTENSION_MANAGER_H_
6 #define CHROME_BROWSER_EXTENSIONS_PENDING_EXTENSION_MANAGER_H_
7 
8 #include <list>
9 #include <string>
10 
11 #include "chrome/browser/extensions/pending_extension_info.h"
12 #include "extensions/common/manifest.h"
13 
14 class GURL;
15 
16 namespace base {
17 class Version;
18 }
19 
20 namespace content {
21 class BrowserContext;
22 }
23 
24 FORWARD_DECLARE_TEST(ExtensionServiceTest,
25                      UpdatePendingExtensionAlreadyInstalled);
26 
27 namespace extensions {
28 class Extension;
29 class PendingExtensionManager;
30 
31 class ExtensionUpdaterTest;
32 void SetupPendingExtensionManagerForTest(
33     int count, const GURL& update_url,
34     PendingExtensionManager* pending_extension_manager);
35 
36 // Class PendingExtensionManager manages the set of extensions which are
37 // being installed or updated. In general, installation and updates take
38 // time, because they involve downloading, unpacking, and installing.
39 // This class allows us to avoid race cases where multiple sources install
40 // the same extension.
41 // The ExtensionService creates an instance of this class, and manages its
42 // lifetime. This class should only be used from the UI thread.
43 class PendingExtensionManager {
44  public:
45   explicit PendingExtensionManager(content::BrowserContext* context);
46   ~PendingExtensionManager();
47 
48   // TODO(skerner): Many of these methods can be private once code in
49   // ExtensionService is moved into methods of this class.
50 
51   // Remove extension with id |id| from the set of pending extensions. Returns
52   // true if such an extension was found and removed, false otherwise.
53   bool Remove(const std::string& id);
54 
55   // Get the  information for a pending extension.  Returns a pointer to the
56   // pending extension with id |id|, or NULL if there is no such extension.
57   const PendingExtensionInfo* GetById(const std::string& id) const;
58 
59   // Is |id| in the set of pending extensions?
60   bool IsIdPending(const std::string& id) const;
61 
62   // Returns true if there are any extensions pending.
63   bool HasPendingExtensions() const;
64 
65   // Whether there is pending extension install from sync.
66   bool HasPendingExtensionFromSync() const;
67 
68   // Adds an extension in a pending state; the extension with the
69   // given info will be installed on the next auto-update cycle.
70   // Return true if the extension was added.  Will return false
71   // if the extension is pending from another source which overrides
72   // sync installs (such as a policy extension) or if the extension
73   // is already installed.
74   //
75   // TODO(akalin): Replace |install_silently| with a list of
76   // pre-enabled permissions.
77   bool AddFromSync(
78       const std::string& id,
79       const GURL& update_url,
80       PendingExtensionInfo::ShouldAllowInstallPredicate should_allow_install,
81       bool install_silently,
82       bool remote_install);
83 
84   // Adds an extension that was depended on by another extension.
85   bool AddFromExtensionImport(
86       const std::string& id,
87       const GURL& update_url,
88       PendingExtensionInfo::ShouldAllowInstallPredicate should_allow_install);
89 
90   // Given an extension id and an update URL, schedule the extension
91   // to be fetched, installed, and activated.
92   bool AddFromExternalUpdateUrl(const std::string& id,
93                                 const std::string& install_parameter,
94                                 const GURL& update_url,
95                                 Manifest::Location location,
96                                 int creation_flags,
97                                 bool mark_acknowledged);
98 
99   // Add a pending extension record for an external CRX file.
100   // Return true if the CRX should be installed, false if an existing
101   // pending record overrides it.
102   bool AddFromExternalFile(
103       const std::string& id,
104       Manifest::Location location,
105       const base::Version& version,
106       int creation_flags,
107       bool mark_acknowledged);
108 
109   // Get the list of pending IDs that should be installed from an update URL.
110   // Pending extensions that will be installed from local files will not be
111   // included in the set.
112   void GetPendingIdsForUpdateCheck(
113       std::list<std::string>* out_ids_for_update_check) const;
114 
115  private:
116   typedef std::list<PendingExtensionInfo> PendingExtensionList;
117 
118   // Assumes an extension with id |id| is not already installed.
119   // Return true if the extension was added.
120   bool AddExtensionImpl(
121       const std::string& id,
122       const std::string& install_parameter,
123       const GURL& update_url,
124       const base::Version& version,
125       PendingExtensionInfo::ShouldAllowInstallPredicate should_allow_install,
126       bool is_from_sync,
127       bool install_silently,
128       Manifest::Location install_source,
129       int creation_flags,
130       bool mark_acknowledged,
131       bool remote_install);
132 
133   // Add a pending extension record directly.  Used for unit tests that need
134   // to set an inital state. Use friendship to allow the tests to call this
135   // method.
136   void AddForTesting(const PendingExtensionInfo& pending_extension_info);
137 
138   // The BrowserContext with which the manager is associated.
139   content::BrowserContext* context_;
140 
141   PendingExtensionList pending_extension_list_;
142 
143   FRIEND_TEST_ALL_PREFIXES(::ExtensionServiceTest,
144                            UpdatePendingExtensionAlreadyInstalled);
145   friend class ExtensionUpdaterTest;
146   friend void SetupPendingExtensionManagerForTest(
147       int count, const GURL& update_url,
148       PendingExtensionManager* pending_extension_manager);
149 
150   DISALLOW_COPY_AND_ASSIGN(PendingExtensionManager);
151 };
152 
153 }  // namespace extensions
154 
155 #endif  // CHROME_BROWSER_EXTENSIONS_PENDING_EXTENSION_MANAGER_H_
156