Home | History | Annotate | Download | only in permissions
      1 // Copyright 2013 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_COMMON_PERMISSIONS_PERMISSION_SET_H_
      6 #define EXTENSIONS_COMMON_PERMISSIONS_PERMISSION_SET_H_
      7 
      8 #include <map>
      9 #include <set>
     10 #include <string>
     11 #include <vector>
     12 
     13 #include "base/gtest_prod_util.h"
     14 #include "base/memory/ref_counted.h"
     15 #include "base/memory/singleton.h"
     16 #include "base/strings/string16.h"
     17 #include "extensions/common/manifest.h"
     18 #include "extensions/common/permissions/api_permission.h"
     19 #include "extensions/common/permissions/api_permission_set.h"
     20 #include "extensions/common/permissions/manifest_permission.h"
     21 #include "extensions/common/permissions/manifest_permission_set.h"
     22 #include "extensions/common/url_pattern_set.h"
     23 
     24 namespace extensions {
     25 class Extension;
     26 
     27 // The PermissionSet is an immutable class that encapsulates an
     28 // extension's permissions. The class exposes set operations for combining and
     29 // manipulating the permissions.
     30 class PermissionSet
     31     : public base::RefCountedThreadSafe<PermissionSet> {
     32  public:
     33   // Creates an empty permission set (e.g. default permissions).
     34   PermissionSet();
     35 
     36   // Creates a new permission set based on the specified data: the API
     37   // permissions, manifest key permissions, host permissions, and scriptable
     38   // hosts. The effective hosts of the newly created permission set will be
     39   // inferred from the given host permissions.
     40   PermissionSet(const APIPermissionSet& apis,
     41                 const ManifestPermissionSet& manifest_permissions,
     42                 const URLPatternSet& explicit_hosts,
     43                 const URLPatternSet& scriptable_hosts);
     44 
     45   // Creates a new permission set equal to |set1| - |set2|, passing ownership of
     46   // the new set to the caller.
     47   static PermissionSet* CreateDifference(
     48       const PermissionSet* set1, const PermissionSet* set2);
     49 
     50   // Creates a new permission set equal to the intersection of |set1| and
     51   // |set2|, passing ownership of the new set to the caller.
     52   static PermissionSet* CreateIntersection(
     53       const PermissionSet* set1, const PermissionSet* set2);
     54 
     55   // Creates a new permission set equal to the union of |set1| and |set2|.
     56   // Passes ownership of the new set to the caller.
     57   static PermissionSet* CreateUnion(
     58       const PermissionSet* set1, const PermissionSet* set2);
     59 
     60   bool operator==(const PermissionSet& rhs) const;
     61 
     62   // Returns true if every API or host permission available to |set| is also
     63   // available to this. In other words, if the API permissions of |set| are a
     64   // subset of this, and the host permissions in this encompass those in |set|.
     65   bool Contains(const PermissionSet& set) const;
     66 
     67   // Gets the API permissions in this set as a set of strings.
     68   std::set<std::string> GetAPIsAsStrings() const;
     69 
     70   // Returns true if this is an empty set (e.g., the default permission set).
     71   bool IsEmpty() const;
     72 
     73   // Returns true if the set has the specified API permission.
     74   bool HasAPIPermission(APIPermission::ID permission) const;
     75 
     76   // Returns true if the |extension| explicitly requests access to the given
     77   // |permission_name|. Note this does not include APIs without no corresponding
     78   // permission, like "runtime" or "browserAction".
     79   bool HasAPIPermission(const std::string& permission_name) const;
     80 
     81   // Returns true if the set allows the given permission with the default
     82   // permission detal.
     83   bool CheckAPIPermission(APIPermission::ID permission) const;
     84 
     85   // Returns true if the set allows the given permission and permission param.
     86   bool CheckAPIPermissionWithParam(APIPermission::ID permission,
     87       const APIPermission::CheckParam* param) const;
     88 
     89   // Returns true if this includes permission to access |origin|.
     90   bool HasExplicitAccessToOrigin(const GURL& origin) const;
     91 
     92   // Returns true if this permission set includes access to script |url|.
     93   bool HasScriptableAccessToURL(const GURL& url) const;
     94 
     95   // Returns true if this permission set includes effective access to all
     96   // origins.
     97   bool HasEffectiveAccessToAllHosts() const;
     98 
     99   // Returns true if this permission set includes effective access to |url|.
    100   bool HasEffectiveAccessToURL(const GURL& url) const;
    101 
    102   // Returns true if this permission set effectively represents full access
    103   // (e.g. native code).
    104   bool HasEffectiveFullAccess() const;
    105 
    106   const APIPermissionSet& apis() const { return apis_; }
    107 
    108   const ManifestPermissionSet& manifest_permissions() const {
    109       return manifest_permissions_;
    110   }
    111 
    112   const URLPatternSet& effective_hosts() const { return effective_hosts_; }
    113 
    114   const URLPatternSet& explicit_hosts() const { return explicit_hosts_; }
    115 
    116   const URLPatternSet& scriptable_hosts() const { return scriptable_hosts_; }
    117 
    118  private:
    119   FRIEND_TEST_ALL_PREFIXES(PermissionsTest, GetWarningMessages_AudioVideo);
    120   friend class base::RefCountedThreadSafe<PermissionSet>;
    121 
    122   ~PermissionSet();
    123 
    124   void AddAPIPermission(APIPermission::ID id);
    125 
    126   // Adds permissions implied independently of other context.
    127   void InitImplicitPermissions();
    128 
    129   // Initializes the effective host permission based on the data in this set.
    130   void InitEffectiveHosts();
    131 
    132   // The api list is used when deciding if an extension can access certain
    133   // extension APIs and features.
    134   APIPermissionSet apis_;
    135 
    136   // The manifest key permission list is used when deciding if an extension
    137   // can access certain extension APIs and features.
    138   ManifestPermissionSet manifest_permissions_;
    139 
    140   // The list of hosts that can be accessed directly from the extension.
    141   // TODO(jstritar): Rename to "hosts_"?
    142   URLPatternSet explicit_hosts_;
    143 
    144   // The list of hosts that can be scripted by content scripts.
    145   // TODO(jstritar): Rename to "user_script_hosts_"?
    146   URLPatternSet scriptable_hosts_;
    147 
    148   // The list of hosts this effectively grants access to.
    149   URLPatternSet effective_hosts_;
    150 };
    151 
    152 }  // namespace extensions
    153 
    154 #endif  // EXTENSIONS_COMMON_PERMISSIONS_PERMISSION_SET_H_
    155