Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2157)

Side by Side Diff: chrome/common/extensions/features/complex_feature.cc

Issue 217163003: Enable _api_features.json to block APIs from service worker contexts. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/common/extensions/features/complex_feature.h" 5 #include "chrome/common/extensions/features/complex_feature.h"
6 6
7 namespace extensions { 7 namespace extensions {
8 8
9 ComplexFeature::ComplexFeature(scoped_ptr<FeatureList> features) { 9 ComplexFeature::ComplexFeature(scoped_ptr<FeatureList> features) {
10 DCHECK_GT(features->size(), 0UL); 10 DCHECK_GT(features->size(), 0UL);
11 features_.swap(*features); 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
12 } 34 }
13 35
14 ComplexFeature::~ComplexFeature() { 36 ComplexFeature::~ComplexFeature() {
15 } 37 }
16 38
17 Feature::Availability ComplexFeature::IsAvailableToManifest( 39 Feature::Availability ComplexFeature::IsAvailableToManifest(
18 const std::string& extension_id, Manifest::Type type, Location location, 40 const std::string& extension_id, Manifest::Type type, Location location,
19 int manifest_version, Platform platform) const { 41 int manifest_version, Platform platform) const {
20 Feature::Availability first_availability = 42 Feature::Availability first_availability =
21 features_[0]->IsAvailableToManifest( 43 features_[0]->IsAvailableToManifest(
(...skipping 28 matching lines...) Expand all
50 Availability availability = 72 Availability availability =
51 (*it)->IsAvailableToContext(extension, context, url, platform); 73 (*it)->IsAvailableToContext(extension, context, url, platform);
52 if (availability.is_available()) 74 if (availability.is_available())
53 return availability; 75 return availability;
54 } 76 }
55 // If none of the SimpleFeatures are available, we return the availability 77 // If none of the SimpleFeatures are available, we return the availability
56 // info of the first SimpleFeature that was not available. 78 // info of the first SimpleFeature that was not available.
57 return first_availability; 79 return first_availability;
58 } 80 }
59 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
60 std::set<Feature::Context>* ComplexFeature::GetContexts() { 98 std::set<Feature::Context>* ComplexFeature::GetContexts() {
61 // TODO(justinlin): Current use cases for ComplexFeatures are simple (e.g. 99 // TODO(justinlin): Current use cases for ComplexFeatures are simple (e.g.
62 // allow API in dev channel for everyone but stable channel for a whitelist), 100 // allow API in dev channel for everyone but stable channel for a whitelist),
63 // but if they get more complicated, we need to return some meaningful context 101 // but if they get more complicated, we need to return some meaningful context
64 // set. Either that or remove this method from the Feature interface. 102 // set. Either that or remove this method from the Feature interface.
65 return features_[0]->GetContexts(); 103 return features_[0]->GetContexts();
66 } 104 }
67 105
68 bool ComplexFeature::IsInternal() const { 106 bool ComplexFeature::IsInternal() const {
69 // TODO(justinlin): Same as the above TODO. 107 // TODO(justinlin): Same as the above TODO.
70 return features_[0]->IsInternal(); 108 return features_[0]->IsInternal();
71 } 109 }
72 110
73 std::string ComplexFeature::GetAvailabilityMessage(AvailabilityResult result, 111 std::string ComplexFeature::GetAvailabilityMessage(AvailabilityResult result,
74 Manifest::Type type, 112 Manifest::Type type,
75 const GURL& url, 113 const GURL& url,
76 Context context) const { 114 Context context) const {
77 if (result == IS_AVAILABLE) 115 if (result == IS_AVAILABLE)
78 return std::string(); 116 return std::string();
79 117
80 // TODO(justinlin): Form some kind of combined availabilities/messages from 118 // TODO(justinlin): Form some kind of combined availabilities/messages from
81 // SimpleFeatures. 119 // SimpleFeatures.
82 return features_[0]->GetAvailabilityMessage(result, type, url, context); 120 return features_[0]->GetAvailabilityMessage(result, type, url, context);
83 } 121 }
84 122
85 bool ComplexFeature::IsIdInWhitelist(const std::string& extension_id) const {
86 for (FeatureList::const_iterator it = features_.begin();
87 it != features_.end(); ++it) {
88 if ((*it)->IsIdInWhitelist(extension_id))
89 return true;
90 }
91 return false;
92 }
93
94 } // namespace extensions 123 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/common/extensions/features/complex_feature.h ('k') | chrome/common/extensions/features/complex_feature_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698