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