OLD | NEW |
| (Empty) |
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 #include "chrome/common/extensions/features/complex_feature.h" | |
6 | |
7 namespace extensions { | |
8 | |
9 ComplexFeature::ComplexFeature(scoped_ptr<FeatureList> features) { | |
10 DCHECK_GT(features->size(), 0UL); | |
11 features_.swap(*features); | |
12 | |
13 #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON) | |
14 // Verify GetContexts, IsInternal, & IsBlockedInServiceWorker are consistent | |
15 // across all features. | |
16 std::set<Feature::Context>* first_contexts = features_[0]->GetContexts(); | |
17 bool first_is_internal = features_[0]->IsInternal(); | |
18 bool first_blocked_in_service_worker = | |
19 features_[0]->IsBlockedInServiceWorker(); | |
20 for (FeatureList::const_iterator it = features_.begin() + 1; | |
21 it != features_.end(); | |
22 ++it) { | |
23 DCHECK(*first_contexts == *(*it)->GetContexts()) | |
24 << "Complex feature must have consistent values of " | |
25 "contexts across all sub features."; | |
26 DCHECK(first_is_internal == (*it)->IsInternal()) | |
27 << "Complex feature must have consistent values of " | |
28 "internal across all sub features."; | |
29 DCHECK(first_blocked_in_service_worker == (*it)->IsBlockedInServiceWorker()) | |
30 << "Complex feature must have consistent values of " | |
31 "blocked_in_service_worker across all sub features."; | |
32 } | |
33 #endif | |
34 } | |
35 | |
36 ComplexFeature::~ComplexFeature() { | |
37 } | |
38 | |
39 Feature::Availability ComplexFeature::IsAvailableToManifest( | |
40 const std::string& extension_id, Manifest::Type type, Location location, | |
41 int manifest_version, Platform platform) const { | |
42 Feature::Availability first_availability = | |
43 features_[0]->IsAvailableToManifest( | |
44 extension_id, type, location, manifest_version, platform); | |
45 if (first_availability.is_available()) | |
46 return first_availability; | |
47 | |
48 for (FeatureList::const_iterator it = features_.begin() + 1; | |
49 it != features_.end(); ++it) { | |
50 Availability availability = (*it)->IsAvailableToManifest( | |
51 extension_id, type, location, manifest_version, platform); | |
52 if (availability.is_available()) | |
53 return availability; | |
54 } | |
55 // If none of the SimpleFeatures are available, we return the availability | |
56 // info of the first SimpleFeature that was not available. | |
57 return first_availability; | |
58 } | |
59 | |
60 Feature::Availability ComplexFeature::IsAvailableToContext( | |
61 const Extension* extension, | |
62 Context context, | |
63 const GURL& url, | |
64 Platform platform) const { | |
65 Feature::Availability first_availability = | |
66 features_[0]->IsAvailableToContext(extension, context, url, platform); | |
67 if (first_availability.is_available()) | |
68 return first_availability; | |
69 | |
70 for (FeatureList::const_iterator it = features_.begin() + 1; | |
71 it != features_.end(); ++it) { | |
72 Availability availability = | |
73 (*it)->IsAvailableToContext(extension, context, url, platform); | |
74 if (availability.is_available()) | |
75 return availability; | |
76 } | |
77 // If none of the SimpleFeatures are available, we return the availability | |
78 // info of the first SimpleFeature that was not available. | |
79 return first_availability; | |
80 } | |
81 | |
82 bool ComplexFeature::IsIdInWhitelist(const std::string& extension_id) const { | |
83 for (FeatureList::const_iterator it = features_.begin(); | |
84 it != features_.end(); | |
85 ++it) { | |
86 if ((*it)->IsIdInWhitelist(extension_id)) | |
87 return true; | |
88 } | |
89 return false; | |
90 } | |
91 | |
92 bool ComplexFeature::IsBlockedInServiceWorker() const { | |
93 // Constructor verifies that composed features are consistent, thus we can | |
94 // return just the first feature's value. | |
95 return features_[0]->IsBlockedInServiceWorker(); | |
96 } | |
97 | |
98 std::set<Feature::Context>* ComplexFeature::GetContexts() { | |
99 // TODO(justinlin): Current use cases for ComplexFeatures are simple (e.g. | |
100 // allow API in dev channel for everyone but stable channel for a whitelist), | |
101 // but if they get more complicated, we need to return some meaningful context | |
102 // set. Either that or remove this method from the Feature interface. | |
103 return features_[0]->GetContexts(); | |
104 } | |
105 | |
106 bool ComplexFeature::IsInternal() const { | |
107 // TODO(justinlin): Same as the above TODO. | |
108 return features_[0]->IsInternal(); | |
109 } | |
110 | |
111 std::string ComplexFeature::GetAvailabilityMessage(AvailabilityResult result, | |
112 Manifest::Type type, | |
113 const GURL& url, | |
114 Context context) const { | |
115 if (result == IS_AVAILABLE) | |
116 return std::string(); | |
117 | |
118 // TODO(justinlin): Form some kind of combined availabilities/messages from | |
119 // SimpleFeatures. | |
120 return features_[0]->GetAvailabilityMessage(result, type, url, context); | |
121 } | |
122 | |
123 } // namespace extensions | |
OLD | NEW |