Home | History | Annotate | Download | only in extensions
      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   explicit PermissionsUpdater(content::BrowserContext* browser_context);
     31   ~PermissionsUpdater();
     32 
     33   // Adds the set of |permissions| to the |extension|'s active permission set
     34   // and sends the relevant messages and notifications. This method assumes the
     35   // user has already been prompted, if necessary, for the extra permissions.
     36   void AddPermissions(const Extension* extension,
     37                       const PermissionSet* permissions);
     38 
     39   // Removes the set of |permissions| from the |extension|'s active permission
     40   // set and sends the relevant messages and notifications.
     41   void RemovePermissions(const Extension* extension,
     42                          const PermissionSet* permissions);
     43 
     44   // Adds all permissions in the |extension|'s active permissions to its
     45   // granted permission set.
     46   void GrantActivePermissions(const Extension* extension);
     47 
     48   // Initializes the |extension|'s active permission set to include only
     49   // permissions currently requested by the extension and all the permissions
     50   // required by the extension.
     51   void InitializeActivePermissions(const Extension* extension);
     52 
     53  private:
     54   enum EventType {
     55     ADDED,
     56     REMOVED,
     57   };
     58 
     59   // Sets the |extension|'s active permissions to |permissions| and records the
     60   // change in the prefs.
     61   void SetActivePermissions(const Extension* extension,
     62                             const PermissionSet* permisssions);
     63 
     64   // Dispatches specified event to the extension.
     65   void DispatchEvent(const std::string& extension_id,
     66                      const char* event_name,
     67                      const PermissionSet* changed_permissions);
     68 
     69   // Issues the relevant events, messages and notifications when the
     70   // |extension|'s permissions have |changed| (|changed| is the delta).
     71   // Specifically, this sends the EXTENSION_PERMISSIONS_UPDATED notification,
     72   // the ExtensionMsg_UpdatePermissions IPC message, and fires the
     73   // onAdded/onRemoved events in the extension.
     74   void NotifyPermissionsUpdated(EventType event_type,
     75                                 const Extension* extension,
     76                                 const PermissionSet* changed);
     77 
     78   // The associated BrowserContext.
     79   content::BrowserContext* browser_context_;
     80 };
     81 
     82 }  // namespace extensions
     83 
     84 #endif  // CHROME_BROWSER_EXTENSIONS_PERMISSIONS_UPDATER_H__
     85