• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2012 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_PERMISSIONS_UPDATER_H__
6 #define CHROME_BROWSER_EXTENSIONS_PERMISSIONS_UPDATER_H__
7 
8 #include <string>
9 
10 #include "base/memory/ref_counted.h"
11 
12 namespace base {
13 class DictionaryValue;
14 }
15 
16 namespace content {
17 class BrowserContext;
18 }
19 
20 namespace extensions {
21 
22 class Extension;
23 class ExtensionPrefs;
24 class PermissionSet;
25 
26 // Updates an Extension's active and granted permissions in persistent storage
27 // and notifies interested parties of the changes.
28 class PermissionsUpdater {
29  public:
30   enum InitFlag {
31     INIT_FLAG_NONE = 0,
32     INIT_FLAG_TRANSIENT = 1 << 0,
33   };
34 
35   explicit PermissionsUpdater(content::BrowserContext* browser_context);
36   PermissionsUpdater(content::BrowserContext* browser_context,
37                      InitFlag init_flag);
38   ~PermissionsUpdater();
39 
40   // Adds the set of |permissions| to the |extension|'s active permission set
41   // and sends the relevant messages and notifications. This method assumes the
42   // user has already been prompted, if necessary, for the extra permissions.
43   void AddPermissions(const Extension* extension,
44                       const PermissionSet* permissions);
45 
46   // Removes the set of |permissions| from the |extension|'s active permission
47   // set and sends the relevant messages and notifications.
48   void RemovePermissions(const Extension* extension,
49                          const PermissionSet* permissions);
50 
51   // Adds all permissions in the |extension|'s active permissions to its
52   // granted permission set.
53   void GrantActivePermissions(const Extension* extension);
54 
55   // Initializes the |extension|'s active permission set to include only
56   // permissions currently requested by the extension and all the permissions
57   // required by the extension.
58   void InitializePermissions(const Extension* extension);
59 
60   // Grants any withheld all-hosts (or all-hosts-like) permissions.
61   void GrantWithheldImpliedAllHosts(const Extension* extension);
62 
63   // Revokes any requests all-hosts (or all-hosts-like) permissions.
64   void WithholdImpliedAllHosts(const Extension* extension);
65 
66  private:
67   enum EventType {
68     ADDED,
69     REMOVED,
70   };
71 
72   // Sets the |extension|'s active permissions to |active| and records the
73   // change in the prefs. If |withheld| is non-null, also sets the extension's
74   // withheld permissions to |withheld|. Otherwise, |withheld| permissions are
75   // not changed.
76   void SetPermissions(const Extension* extension,
77                       const scoped_refptr<const PermissionSet>& active,
78                       scoped_refptr<const PermissionSet> withheld);
79 
80   // Dispatches specified event to the extension.
81   void DispatchEvent(const std::string& extension_id,
82                      const char* event_name,
83                      const PermissionSet* changed_permissions);
84 
85   // Issues the relevant events, messages and notifications when the
86   // |extension|'s permissions have |changed| (|changed| is the delta).
87   // Specifically, this sends the EXTENSION_PERMISSIONS_UPDATED notification,
88   // the ExtensionMsg_UpdatePermissions IPC message, and fires the
89   // onAdded/onRemoved events in the extension.
90   void NotifyPermissionsUpdated(EventType event_type,
91                                 const Extension* extension,
92                                 const PermissionSet* changed);
93 
94   // The associated BrowserContext.
95   content::BrowserContext* browser_context_;
96 
97   // Initialization flag that determines whether prefs is consulted about the
98   // extension. Transient extensions should not have entries in prefs.
99   InitFlag init_flag_;
100 };
101 
102 }  // namespace extensions
103 
104 #endif  // CHROME_BROWSER_EXTENSIONS_PERMISSIONS_UPDATER_H__
105