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 14 ComplexFeature::~ComplexFeature() { 15 } 16 17 Feature::Availability ComplexFeature::IsAvailableToManifest( 18 const std::string& extension_id, Manifest::Type type, Location location, 19 int manifest_version, Platform platform) const { 20 Feature::Availability first_availability = 21 features_[0]->IsAvailableToManifest( 22 extension_id, type, location, manifest_version, platform); 23 if (first_availability.is_available()) 24 return first_availability; 25 26 for (FeatureList::const_iterator it = features_.begin() + 1; 27 it != features_.end(); ++it) { 28 Availability availability = (*it)->IsAvailableToManifest( 29 extension_id, type, location, manifest_version, platform); 30 if (availability.is_available()) 31 return availability; 32 } 33 // If none of the SimpleFeatures are available, we return the availability 34 // info of the first SimpleFeature that was not available. 35 return first_availability; 36 } 37 38 Feature::Availability ComplexFeature::IsAvailableToContext( 39 const Extension* extension, 40 Context context, 41 const GURL& url, 42 Platform platform) const { 43 Feature::Availability first_availability = 44 features_[0]->IsAvailableToContext(extension, context, url, 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 = 51 (*it)->IsAvailableToContext(extension, context, url, 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 std::set<Feature::Context>* ComplexFeature::GetContexts() { 61 // 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), 63 // 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. 65 return features_[0]->GetContexts(); 66 } 67 68 bool ComplexFeature::IsInternal() const { 69 // TODO(justinlin): Same as the above TODO. 70 return features_[0]->IsInternal(); 71 } 72 73 std::string ComplexFeature::GetAvailabilityMessage(AvailabilityResult result, 74 Manifest::Type type, 75 const GURL& url, 76 Context context) const { 77 if (result == IS_AVAILABLE) 78 return std::string(); 79 80 // TODO(justinlin): Form some kind of combined availabilities/messages from 81 // SimpleFeatures. 82 return features_[0]->GetAvailabilityMessage(result, type, url, context); 83 } 84 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 95