Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / common / extensions / permissions / chrome_permission_message_rules.h
blob99641e804c83531312c90bd2ddaa747bbc9aaa06
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.
5 #ifndef CHROME_COMMON_EXTENSIONS_PERMISSIONS_CHROME_PERMISSION_MESSAGE_RULES_H_
6 #define CHROME_COMMON_EXTENSIONS_PERMISSIONS_CHROME_PERMISSION_MESSAGE_RULES_H_
8 #include <set>
9 #include <vector>
11 #include "base/memory/linked_ptr.h"
12 #include "extensions/common/permissions/api_permission.h"
13 #include "extensions/common/permissions/api_permission_set.h"
14 #include "extensions/common/permissions/permission_message.h"
16 namespace extensions {
18 // A formatter which generates a single permission message for all permissions
19 // that match a given rule. All remaining permissions that match the given rule
20 // are bucketed together, then GetPermissionMessage() is called with the
21 // resultant permission set.
22 // Note: since a ChromePermissionMessageFormatter can only produce a single
23 // PermissionMessage and applies to all permissions with a given ID, this
24 // maintains the property that one permission ID can still only produce one
25 // message (that is, multiple permissions with the same ID cannot appear in
26 // multiple messages).
27 class ChromePermissionMessageFormatter {
28 public:
29 ChromePermissionMessageFormatter() {}
30 virtual ~ChromePermissionMessageFormatter() {}
32 // Returns the permission message for the given set of |permissions|.
33 // |permissions| is guaranteed to have the IDs specified by the
34 // required/optional permissions for the rule. The set will never be empty.
35 virtual PermissionMessage GetPermissionMessage(
36 const PermissionIDSet& permissions) const = 0;
38 private:
39 DISALLOW_COPY_AND_ASSIGN(ChromePermissionMessageFormatter);
42 // A simple rule to generate a coalesced permission message that stores the
43 // corresponding
44 // message ID for a given set of permission IDs. This rule generates the message
45 // with the given |message_id| if all the |required| permissions are present. If
46 // any |optional| permissions are present, they are also 'absorbed' by the rule
47 // to generate the final coalesced message.
48 // An optional |formatter| can be provided, which decides how the final
49 // PermissionMessage appears. The default formatter simply displays the message
50 // with the ID |message_id|.
51 // Once a permission is used in a rule, it cannot apply to any future rules.
52 // TODO(sashab): Move all ChromePermissionMessageFormatters to their own
53 // provider class and remove ownership from ChromePermissionMessageRule.
54 class ChromePermissionMessageRule {
55 public:
56 virtual ~ChromePermissionMessageRule();
58 // Returns all the rules used to generate permission messages for Chrome apps
59 // and extensions.
60 static std::vector<ChromePermissionMessageRule> GetAllRules();
62 std::set<APIPermission::ID> required_permissions() const;
63 std::set<APIPermission::ID> optional_permissions() const;
64 std::set<APIPermission::ID> all_permissions() const;
66 PermissionMessage GetPermissionMessage(
67 const PermissionIDSet& permissions) const;
69 private:
70 class PermissionIDSetInitializer;
72 // Create a rule using the default formatter (display the message with ID
73 // |message_id|).
74 ChromePermissionMessageRule(int message_id,
75 const PermissionIDSetInitializer& required,
76 const PermissionIDSetInitializer& optional);
77 // Create a rule with a custom formatter. Takes ownership of |formatter|.
78 ChromePermissionMessageRule(ChromePermissionMessageFormatter* formatter,
79 const PermissionIDSetInitializer& required,
80 const PermissionIDSetInitializer& optional);
82 std::set<APIPermission::ID> required_permissions_;
83 std::set<APIPermission::ID> optional_permissions_;
85 // Owned by this class. linked_ptr is needed because this object is copyable
86 // and stored in a returned vector.
87 linked_ptr<ChromePermissionMessageFormatter> formatter_;
90 } // namespace extensions
92 #endif // CHROME_COMMON_EXTENSIONS_PERMISSIONS_CHROME_PERMISSION_MESSAGE_RULES_H_