Upstream version 8.37.180.0
[platform/framework/web/crosswalk.git] / src / extensions / common / permissions / permissions_data.h
1 // Copyright (c) 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_PERMISSIONS_DATA_H_
6 #define EXTENSIONS_COMMON_PERMISSIONS_PERMISSIONS_DATA_H_
7
8 #include <map>
9 #include <string>
10 #include <vector>
11
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/strings/string16.h"
15 #include "base/synchronization/lock.h"
16 #include "extensions/common/extension.h"
17 #include "extensions/common/manifest.h"
18 #include "extensions/common/permissions/api_permission.h"
19 #include "extensions/common/permissions/permission_message.h"
20 #include "extensions/common/permissions/permission_set.h"
21
22 class GURL;
23
24 namespace extensions {
25
26 class PermissionSet;
27 class Extension;
28 class URLPatternSet;
29 class UserScript;
30
31 // A container for the active permissions of an extension.
32 // TODO(rdevlin.cronin): For the love of everything good, rename this class to
33 // ActivePermissions. We do *not* need PermissionsParser, PermissionSet,
34 // PermissionInfo, and PermissionsData. No one will be able to keep them
35 // straight.
36 class PermissionsData {
37  public:
38   // Delegate class to allow different contexts (e.g. browser vs renderer) to
39   // have control over policy decisions.
40   class PolicyDelegate {
41    public:
42     virtual ~PolicyDelegate() {}
43
44     // Returns false if script access should be blocked on this page.
45     // Otherwise, default policy should decide.
46     virtual bool CanExecuteScriptOnPage(const Extension* extension,
47                                         const GURL& document_url,
48                                         const GURL& top_document_url,
49                                         int tab_id,
50                                         int process_id,
51                                         std::string* error) = 0;
52   };
53
54   static void SetPolicyDelegate(PolicyDelegate* delegate);
55
56   PermissionsData(const Extension* extension);
57   virtual ~PermissionsData();
58
59   // Returns true if the |extension| can silently increase its permission level.
60   // Users must approve permissions for unpacked and packed extensions in the
61   // following situations:
62   //  - when installing or upgrading packed extensions
63   //  - when installing unpacked extensions that have NPAPI plugins
64   //  - when either type of extension requests optional permissions
65   static bool CanSilentlyIncreasePermissions(const Extension* extension);
66
67   // Returns true if the extension is a COMPONENT extension or is on the
68   // whitelist of extensions that can script all pages.
69   static bool CanExecuteScriptEverywhere(const Extension* extension);
70
71   // Sets the runtime permissions of the given |extension| to |permissions|.
72   void SetActivePermissions(const PermissionSet* active) const;
73
74   // Updates the tab-specific permissions of |tab_id| to include those from
75   // |permissions|.
76   void UpdateTabSpecificPermissions(
77       int tab_id,
78       scoped_refptr<const PermissionSet> permissions) const;
79
80   // Clears the tab-specific permissions of |tab_id|.
81   void ClearTabSpecificPermissions(int tab_id) const;
82
83   // Returns true if the |extension| has the given |permission|. Prefer
84   // IsExtensionWithPermissionOrSuggestInConsole when developers may be using an
85   // api that requires a permission they didn't know about, e.g. open web apis.
86   // Note this does not include APIs with no corresponding permission, like
87   // "runtime" or "browserAction".
88   // TODO(mpcomplete): drop the "API" from these names, it's confusing.
89   bool HasAPIPermission(APIPermission::ID permission) const;
90   bool HasAPIPermission(const std::string& permission_name) const;
91   bool HasAPIPermissionForTab(int tab_id, APIPermission::ID permission) const;
92   bool CheckAPIPermissionWithParam(
93       APIPermission::ID permission,
94       const APIPermission::CheckParam* param) const;
95
96   // TODO(rdevlin.cronin): GetEffectiveHostPermissions(), HasHostPermission(),
97   // and HasEffectiveAccessToAllHosts() are just forwards for the active
98   // permissions. We should either get rid of these, and have callers use
99   // active_permissions(), or should get rid of active_permissions(), and make
100   // callers use PermissionsData for everything. We should not do both.
101
102   // Returns the effective hosts associated with the active permissions.
103   const URLPatternSet& GetEffectiveHostPermissions() const;
104
105   // Whether the extension has access to the given |url|.
106   bool HasHostPermission(const GURL& url) const;
107
108   // Whether the extension has effective access to all hosts. This is true if
109   // there is a content script that matches all hosts, if there is a host
110   // permission grants access to all hosts (like <all_urls>) or an api
111   // permission that effectively grants access to all hosts (e.g. proxy,
112   // network, etc.)
113   bool HasEffectiveAccessToAllHosts() const;
114
115   // Returns the full list of permission messages that should display at
116   // install time.
117   PermissionMessages GetPermissionMessages() const;
118
119   // Returns the full list of permission messages that should display at install
120   // time as strings.
121   std::vector<base::string16> GetPermissionMessageStrings() const;
122
123   // Returns the full list of permission details for messages that should
124   // display at install time as strings.
125   std::vector<base::string16> GetPermissionMessageDetailsStrings() const;
126
127   // Returns true if the |extension| has permission to access and interact with
128   // the specified page, in order to do things like inject scripts or modify
129   // the content.
130   // If this returns false and |error| is non-NULL, |error| will be popualted
131   // with the reason the extension cannot access the page.
132   bool CanAccessPage(const Extension* extension,
133                      const GURL& document_url,
134                      const GURL& top_document_url,
135                      int tab_id,
136                      int process_id,
137                      std::string* error) const;
138
139   // Returns true if the |extension| has permission to inject a content script
140   // on the page.
141   // If this returns false and |error| is non-NULL, |error| will be popualted
142   // with the reason the extension cannot script the page.
143   // NOTE: You almost certainly want to use CanAccessPage() instead of this
144   // method.
145   bool CanRunContentScriptOnPage(const Extension* extension,
146                                  const GURL& document_url,
147                                  const GURL& top_document_url,
148                                  int tab_id,
149                                  int process_id,
150                                  std::string* error) const;
151
152   // Returns true if extension is allowed to obtain the contents of a page as
153   // an image. Since a page may contain sensitive information, this is
154   // restricted to the extension's host permissions as well as the extension
155   // page itself.
156   bool CanCaptureVisiblePage(int tab_id, std::string* error) const;
157
158   // Returns true if the user should be alerted that the |extension| is running
159   // a script. If |tab_id| and |url| are included, this also considers tab-
160   // specific permissions.
161   bool RequiresActionForScriptExecution(const Extension* extension) const;
162   bool RequiresActionForScriptExecution(const Extension* extension,
163                                         int tab_id,
164                                         const GURL& url) const;
165
166   scoped_refptr<const PermissionSet> active_permissions() const {
167     base::AutoLock auto_lock(runtime_lock_);
168     return active_permissions_unsafe_;
169   }
170
171 #if defined(UNIT_TEST)
172   scoped_refptr<const PermissionSet> GetTabSpecificPermissionsForTesting(
173       int tab_id) const {
174     return GetTabSpecificPermissions(tab_id);
175   }
176 #endif
177
178  private:
179   typedef std::map<int, scoped_refptr<const PermissionSet> > TabPermissionsMap;
180
181   // Gets the tab-specific host permissions of |tab_id|, or NULL if there
182   // aren't any.
183   scoped_refptr<const PermissionSet> GetTabSpecificPermissions(
184       int tab_id) const;
185
186   // Returns true if the |extension| has tab-specific permission to operate on
187   // the tab specified by |tab_id| with the given |url|.
188   // Note that if this returns false, it doesn't mean the extension can't run on
189   // the given tab, only that it does not have tab-specific permission to do so.
190   bool HasTabSpecificPermissionToExecuteScript(int tab_id,
191                                                const GURL& url) const;
192
193   // Returns true if the extension is permitted to run on the given page,
194   // checking against |permitted_url_patterns| in addition to blocking special
195   // sites (like the webstore or chrome:// urls).
196   bool CanRunOnPage(const Extension* extension,
197                     const GURL& document_url,
198                     const GURL& top_document_url,
199                     int tab_id,
200                     int process_id,
201                     const URLPatternSet& permitted_url_patterns,
202                     std::string* error) const;
203
204   // The associated extension's id.
205   std::string extension_id_;
206
207   // The associated extension's manifest type.
208   Manifest::Type manifest_type_;
209
210   mutable base::Lock runtime_lock_;
211
212   // The permission's which are currently active on the extension during
213   // runtime.
214   // Unsafe indicates that we must lock anytime this is directly accessed.
215   // Unless you need to change |active_permissions_unsafe_|, use the (safe)
216   // active_permissions() accessor.
217   mutable scoped_refptr<const PermissionSet> active_permissions_unsafe_;
218
219   mutable TabPermissionsMap tab_specific_permissions_;
220
221   DISALLOW_COPY_AND_ASSIGN(PermissionsData);
222 };
223
224 }  // namespace extensions
225
226 #endif  // EXTENSIONS_COMMON_PERMISSIONS_PERMISSIONS_DATA_H_