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

Side by Side Diff: chrome/common/extensions/manifest.cc

Issue 12093036: Move Extension Location and Type enums to Manifest, and move InstallWarning to its own file. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 10 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/manifest.h" 5 #include "chrome/common/extensions/manifest.h"
6 6
7 #include "base/basictypes.h" 7 #include "base/basictypes.h"
8 #include "base/lazy_instance.h" 8 #include "base/lazy_instance.h"
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/string_split.h" 10 #include "base/string_split.h"
11 #include "base/stringprintf.h" 11 #include "base/stringprintf.h"
12 #include "base/utf_string_conversions.h" 12 #include "base/utf_string_conversions.h"
13 #include "chrome/common/extensions/extension_manifest_constants.h" 13 #include "chrome/common/extensions/extension_manifest_constants.h"
14 #include "chrome/common/extensions/features/base_feature_provider.h" 14 #include "chrome/common/extensions/features/base_feature_provider.h"
15 #include "extensions/common/error_utils.h" 15 #include "extensions/common/error_utils.h"
16 16
17 namespace errors = extension_manifest_errors; 17 namespace errors = extension_manifest_errors;
18 namespace keys = extension_manifest_keys; 18 namespace keys = extension_manifest_keys;
19 19
20 namespace extensions { 20 namespace extensions {
21 21
22 Manifest::Manifest(Extension::Location location, 22 namespace {
23
24 // Rank extension locations in a way that allows
25 // Manifest::GetHigherPriorityLocation() to compare locations.
26 // An extension installed from two locations will have the location
27 // with the higher rank, as returned by this function. The actual
28 // integer values may change, and should never be persisted.
29 int GetLocationRank(Manifest::Location location) {
30 const int kInvalidRank = -1;
31 int rank = kInvalidRank; // Will CHECK that rank is not kInvalidRank.
32
33 switch (location) {
34 // Component extensions can not be overriden by any other type.
35 case Manifest::COMPONENT:
36 rank = 6;
37 break;
38
39 // Policy controlled extensions may not be overridden by any type
40 // that is not part of chrome.
41 case Manifest::EXTERNAL_POLICY_DOWNLOAD:
42 rank = 5;
43 break;
44
45 // A developer-loaded extension should override any installed type
46 // that a user can disable.
47 case Manifest::LOAD:
48 rank = 4;
49 break;
50
51 // The relative priority of various external sources is not important,
52 // but having some order ensures deterministic behavior.
53 case Manifest::EXTERNAL_REGISTRY:
54 rank = 3;
55 break;
56
57 case Manifest::EXTERNAL_PREF:
58 rank = 2;
59 break;
60
61 case Manifest::EXTERNAL_PREF_DOWNLOAD:
62 rank = 1;
63 break;
64
65 // User installed extensions are overridden by any external type.
66 case Manifest::INTERNAL:
67 rank = 0;
68 break;
69
70 default:
71 NOTREACHED() << "Need to add new extension locaton " << location;
72 }
73
74 CHECK(rank != kInvalidRank);
75 return rank;
76 }
77
78 } // namespace
79
80 // static
81 Manifest::Location Manifest::GetHigherPriorityLocation(
82 Manifest::Location loc1, Manifest::Location loc2) {
83 if (loc1 == loc2)
84 return loc1;
85
86 int loc1_rank = GetLocationRank(loc1);
87 int loc2_rank = GetLocationRank(loc2);
88
89 // If two different locations have the same rank, then we can not
90 // deterministicly choose a location.
91 CHECK(loc1_rank != loc2_rank);
92
93 // Highest rank has highest priority.
94 return (loc1_rank > loc2_rank ? loc1 : loc2 );
95 }
96
97 // TODO(yoz): remove extraneous Manifest::
Jeffrey Yasskin 2013/01/29 22:54:07 Why can't you do it now?
Yoyo Zhou 2013/01/30 01:16:01 I left this here so I would remember to actually d
98 Manifest::Manifest(Manifest::Location location,
23 scoped_ptr<DictionaryValue> value) 99 scoped_ptr<DictionaryValue> value)
24 : location_(location), 100 : location_(location),
25 value_(value.Pass()), 101 value_(value.Pass()),
26 type_(Extension::TYPE_UNKNOWN) { 102 type_(Manifest::TYPE_UNKNOWN) {
27 if (value_->HasKey(keys::kTheme)) { 103 if (value_->HasKey(keys::kTheme)) {
28 type_ = Extension::TYPE_THEME; 104 type_ = Manifest::TYPE_THEME;
29 } else if (value_->HasKey(keys::kApp)) { 105 } else if (value_->HasKey(keys::kApp)) {
30 if (value_->Get(keys::kWebURLs, NULL) || 106 if (value_->Get(keys::kWebURLs, NULL) ||
31 value_->Get(keys::kLaunchWebURL, NULL)) { 107 value_->Get(keys::kLaunchWebURL, NULL)) {
32 type_ = Extension::TYPE_HOSTED_APP; 108 type_ = Manifest::TYPE_HOSTED_APP;
33 } else if (value_->Get(keys::kPlatformAppBackground, NULL)) { 109 } else if (value_->Get(keys::kPlatformAppBackground, NULL)) {
34 type_ = Extension::TYPE_PLATFORM_APP; 110 type_ = Manifest::TYPE_PLATFORM_APP;
35 } else { 111 } else {
36 type_ = Extension::TYPE_LEGACY_PACKAGED_APP; 112 type_ = Manifest::TYPE_LEGACY_PACKAGED_APP;
37 } 113 }
38 } else { 114 } else {
39 type_ = Extension::TYPE_EXTENSION; 115 type_ = Manifest::TYPE_EXTENSION;
40 } 116 }
41 CHECK_NE(type_, Extension::TYPE_UNKNOWN); 117 CHECK_NE(type_, Manifest::TYPE_UNKNOWN);
42 } 118 }
43 119
44 Manifest::~Manifest() { 120 Manifest::~Manifest() {
45 } 121 }
46 122
47 void Manifest::ValidateManifest( 123 void Manifest::ValidateManifest(
48 std::string* error, 124 std::string* error,
49 Extension::InstallWarningVector* warnings) const { 125 InstallWarning::Vector* warnings) const {
50 *error = ""; 126 *error = "";
51 if (type_ == Extension::TYPE_PLATFORM_APP && GetManifestVersion() < 2) { 127 if (type_ == Manifest::TYPE_PLATFORM_APP && GetManifestVersion() < 2) {
52 *error = errors::kPlatformAppNeedsManifestVersion2; 128 *error = errors::kPlatformAppNeedsManifestVersion2;
53 return; 129 return;
54 } 130 }
55 131
56 // Check every feature to see if its in the manifest. Note that this means 132 // Check every feature to see if its in the manifest. Note that this means
57 // we will ignore keys that are not features; we do this for forward 133 // we will ignore keys that are not features; we do this for forward
58 // compatibility. 134 // compatibility.
59 // TODO(aa): Consider having an error here in the case of strict error 135 // TODO(aa): Consider having an error here in the case of strict error
60 // checking to let developers know when they screw up. 136 // checking to let developers know when they screw up.
61 137
62 std::set<std::string> feature_names = 138 std::set<std::string> feature_names =
63 BaseFeatureProvider::GetManifestFeatures()->GetAllFeatureNames(); 139 BaseFeatureProvider::GetManifestFeatures()->GetAllFeatureNames();
64 for (std::set<std::string>::iterator feature_name = feature_names.begin(); 140 for (std::set<std::string>::iterator feature_name = feature_names.begin();
65 feature_name != feature_names.end(); ++feature_name) { 141 feature_name != feature_names.end(); ++feature_name) {
66 // Use Get instead of HasKey because the former uses path expansion. 142 // Use Get instead of HasKey because the former uses path expansion.
67 if (!value_->Get(*feature_name, NULL)) 143 if (!value_->Get(*feature_name, NULL))
68 continue; 144 continue;
69 145
70 Feature* feature = 146 Feature* feature =
71 BaseFeatureProvider::GetManifestFeatures()->GetFeature(*feature_name); 147 BaseFeatureProvider::GetManifestFeatures()->GetFeature(*feature_name);
72 Feature::Availability result = feature->IsAvailableToManifest( 148 Feature::Availability result = feature->IsAvailableToManifest(
73 extension_id_, type_, Feature::ConvertLocation(location_), 149 extension_id_, type_, Feature::ConvertLocation(location_),
74 GetManifestVersion()); 150 GetManifestVersion());
75 if (!result.is_available()) 151 if (!result.is_available())
76 warnings->push_back(Extension::InstallWarning( 152 warnings->push_back(InstallWarning(
77 Extension::InstallWarning::FORMAT_TEXT, result.message())); 153 InstallWarning::FORMAT_TEXT, result.message()));
78 } 154 }
79 155
80 // Also generate warnings for keys that are not features. 156 // Also generate warnings for keys that are not features.
81 for (DictionaryValue::key_iterator key = value_->begin_keys(); 157 for (DictionaryValue::key_iterator key = value_->begin_keys();
82 key != value_->end_keys(); ++key) { 158 key != value_->end_keys(); ++key) {
83 if (!BaseFeatureProvider::GetManifestFeatures()->GetFeature(*key)) { 159 if (!BaseFeatureProvider::GetManifestFeatures()->GetFeature(*key)) {
84 warnings->push_back(Extension::InstallWarning( 160 warnings->push_back(InstallWarning(
85 Extension::InstallWarning::FORMAT_TEXT, 161 InstallWarning::FORMAT_TEXT,
86 base::StringPrintf("Unrecognized manifest key '%s'.", 162 base::StringPrintf("Unrecognized manifest key '%s'.",
87 (*key).c_str()))); 163 (*key).c_str())));
88 } 164 }
89 } 165 }
90 } 166 }
91 167
92 bool Manifest::HasKey(const std::string& key) const { 168 bool Manifest::HasKey(const std::string& key) const {
93 return CanAccessKey(key) && value_->HasKey(key); 169 return CanAccessKey(key) && value_->HasKey(key);
94 } 170 }
95 171
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
140 return manifest; 216 return manifest;
141 } 217 }
142 218
143 bool Manifest::Equals(const Manifest* other) const { 219 bool Manifest::Equals(const Manifest* other) const {
144 return other && value_->Equals(other->value()); 220 return other && value_->Equals(other->value());
145 } 221 }
146 222
147 int Manifest::GetManifestVersion() const { 223 int Manifest::GetManifestVersion() const {
148 // Platform apps were launched after manifest version 2 was the preferred 224 // Platform apps were launched after manifest version 2 was the preferred
149 // version, so they default to that. 225 // version, so they default to that.
150 int manifest_version = type_ == Extension::TYPE_PLATFORM_APP ? 2 : 1; 226 int manifest_version = type_ == Manifest::TYPE_PLATFORM_APP ? 2 : 1;
151 value_->GetInteger(keys::kManifestVersion, &manifest_version); 227 value_->GetInteger(keys::kManifestVersion, &manifest_version);
152 return manifest_version; 228 return manifest_version;
153 } 229 }
154 230
155 bool Manifest::CanAccessPath(const std::string& path) const { 231 bool Manifest::CanAccessPath(const std::string& path) const {
156 std::vector<std::string> components; 232 std::vector<std::string> components;
157 base::SplitString(path, '.', &components); 233 base::SplitString(path, '.', &components);
158 std::string key; 234 std::string key;
159 for (size_t i = 0; i < components.size(); ++i) { 235 for (size_t i = 0; i < components.size(); ++i) {
160 key += components[i]; 236 key += components[i];
161 if (!CanAccessKey(key)) 237 if (!CanAccessKey(key))
162 return false; 238 return false;
163 key += '.'; 239 key += '.';
164 } 240 }
165 return true; 241 return true;
166 } 242 }
167 243
168 bool Manifest::CanAccessKey(const std::string& key) const { 244 bool Manifest::CanAccessKey(const std::string& key) const {
169 Feature* feature = 245 Feature* feature =
170 BaseFeatureProvider::GetManifestFeatures()->GetFeature(key); 246 BaseFeatureProvider::GetManifestFeatures()->GetFeature(key);
171 if (!feature) 247 if (!feature)
172 return true; 248 return true;
173 249
174 return feature->IsAvailableToManifest( 250 return feature->IsAvailableToManifest(
175 extension_id_, type_, Feature::ConvertLocation(location_), 251 extension_id_, type_, Feature::ConvertLocation(location_),
176 GetManifestVersion()).is_available(); 252 GetManifestVersion()).is_available();
177 } 253 }
178 254
179 } // namespace extensions 255 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698