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_
10 #include "base/memory/linked_ptr.h"
11 #include "extensions/common/permissions/api_permission_set.h"
12 #include "extensions/common/permissions/coalesced_permission_message.h"
14 namespace extensions
{
16 // A formatter which generates a single permission message for all permissions
17 // that match a given rule. All remaining permissions that match the given rule
18 // are bucketed together, then GetPermissionMessage() is called with the
19 // resultant permission set.
20 // Note: since a ChromePermissionMessageFormatter can only produce a single
21 // CoalescedPermissionMessage and applies to all permissions with a given ID,
22 // this maintains the property that one permission ID can still only produce one
23 // message (that is, multiple permissions with the same ID cannot appear in
24 // multiple messages).
25 class ChromePermissionMessageFormatter
{
27 ChromePermissionMessageFormatter() {}
28 virtual ~ChromePermissionMessageFormatter() {}
30 // Returns the permission message for the given set of |permissions|.
31 // |permissions| is guaranteed to have the IDs specified by the
32 // required/optional permissions for the rule. The set will never be empty.
33 virtual CoalescedPermissionMessage
GetPermissionMessage(
34 const PermissionIDSet
& permissions
) const = 0;
37 DISALLOW_COPY_AND_ASSIGN(ChromePermissionMessageFormatter
);
40 // A simple rule to generate a coalesced permission message that stores the
42 // message ID for a given set of permission IDs. This rule generates the message
43 // with the given |message_id| if all the |required| permissions are present. If
44 // any |optional| permissions are present, they are also 'absorbed' by the rule
45 // to generate the final coalesced message.
46 // An optional |formatter| can be provided, which decides how the final
47 // CoalescedPermissionMessage appears. The default formatter simply displays the
48 // message with the ID |message_id|.
49 // Once a permission is used in a rule, it cannot apply to any future rules.
50 // TODO(sashab): Move all ChromePermissionMessageFormatters to their own
51 // provider class and remove ownership from ChromePermissionMessageRule.
52 class ChromePermissionMessageRule
{
54 // Returns all the rules used to generate permission messages for Chrome apps
56 static std::vector
<ChromePermissionMessageRule
> GetAllRules();
58 // Convenience constructors to allow inline initialization of the permission
60 class PermissionIDSetInitializer
: public std::set
<APIPermission::ID
> {
62 // Don't make the constructor explicit to make the usage convenient.
63 PermissionIDSetInitializer();
64 PermissionIDSetInitializer(
65 APIPermission::ID permission_one
); // NOLINT(runtime/explicit)
66 PermissionIDSetInitializer(APIPermission::ID permission_one
,
67 APIPermission::ID permission_two
);
68 PermissionIDSetInitializer(APIPermission::ID permission_one
,
69 APIPermission::ID permission_two
,
70 APIPermission::ID permission_three
);
71 PermissionIDSetInitializer(APIPermission::ID permission_one
,
72 APIPermission::ID permission_two
,
73 APIPermission::ID permission_three
,
74 APIPermission::ID permission_four
);
75 PermissionIDSetInitializer(APIPermission::ID permission_one
,
76 APIPermission::ID permission_two
,
77 APIPermission::ID permission_three
,
78 APIPermission::ID permission_four
,
79 APIPermission::ID permission_five
);
80 PermissionIDSetInitializer(APIPermission::ID permission_one
,
81 APIPermission::ID permission_two
,
82 APIPermission::ID permission_three
,
83 APIPermission::ID permission_four
,
84 APIPermission::ID permission_five
,
85 APIPermission::ID permission_six
);
86 virtual ~PermissionIDSetInitializer();
89 // Create a rule using the default formatter (display the message with ID
91 ChromePermissionMessageRule(int message_id
,
92 PermissionIDSetInitializer required
,
93 PermissionIDSetInitializer optional
);
94 // Create a rule with a custom formatter. Takes ownership of |formatter|.
95 ChromePermissionMessageRule(ChromePermissionMessageFormatter
* formatter
,
96 PermissionIDSetInitializer required
,
97 PermissionIDSetInitializer optional
);
98 virtual ~ChromePermissionMessageRule();
100 std::set
<APIPermission::ID
> required_permissions() const;
101 std::set
<APIPermission::ID
> optional_permissions() const;
102 std::set
<APIPermission::ID
> all_permissions() const;
104 CoalescedPermissionMessage
GetPermissionMessage(
105 const PermissionIDSet
& permissions
) const;
108 std::set
<APIPermission::ID
> required_permissions_
;
109 std::set
<APIPermission::ID
> optional_permissions_
;
111 // Owned by this class. linked_ptr is needed because this object is copyable
112 // and stored in a returned vector.
113 linked_ptr
<ChromePermissionMessageFormatter
> formatter_
;
115 // DISALLOW_COPY_AND_ASSIGN(ChromePermissionMessageRule);
118 } // namespace extensions
120 #endif // CHROME_COMMON_EXTENSIONS_PERMISSIONS_CHROME_PERMISSION_MESSAGE_RULES_H_