1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/extensions/external_pref_extension_provider.h"
#include "app/app_paths.h"
#include "base/file_path.h"
#include "base/file_util.h"
#include "base/path_service.h"
#include "base/string_util.h"
#include "base/utf_string_conversions.h"
#include "base/version.h"
#include "chrome/common/json_value_serializer.h"
// Constants for keeping track of extension preferences.
const char kLocation[] = "location";
const char kState[] = "state";
const char kExternalCrx[] = "external_crx";
const char kExternalVersion[] = "external_version";
const char kExternalUpdateUrl[] = "external_update_url";
ExternalPrefExtensionProvider::ExternalPrefExtensionProvider() {
FilePath json_file;
PathService::Get(app::DIR_EXTERNAL_EXTENSIONS, &json_file);
json_file = json_file.Append(FILE_PATH_LITERAL("external_extensions.json"));
if (file_util::PathExists(json_file)) {
JSONFileValueSerializer serializer(json_file);
SetPreferences(&serializer);
} else {
prefs_.reset(new DictionaryValue());
}
}
ExternalPrefExtensionProvider::~ExternalPrefExtensionProvider() {
}
void ExternalPrefExtensionProvider::SetPreferencesForTesting(
const std::string& json_data_for_testing) {
JSONStringValueSerializer serializer(json_data_for_testing);
SetPreferences(&serializer);
}
void ExternalPrefExtensionProvider::VisitRegisteredExtension(
Visitor* visitor, const std::set<std::string>& ids_to_ignore) const {
for (DictionaryValue::key_iterator i = prefs_->begin_keys();
i != prefs_->end_keys(); ++i) {
const std::string& extension_id = *i;
if (ids_to_ignore.find(extension_id) != ids_to_ignore.end())
continue;
DictionaryValue* extension;
if (!prefs_->GetDictionaryWithoutPathExpansion(extension_id, &extension))
continue;
FilePath::StringType external_crx;
std::string external_version;
std::string external_update_url;
bool has_external_crx = extension->GetString(kExternalCrx, &external_crx);
bool has_external_version = extension->GetString(kExternalVersion,
&external_version);
bool has_external_update_url = extension->GetString(kExternalUpdateUrl,
&external_update_url);
if (has_external_crx != has_external_version) {
LOG(WARNING) << "Malformed extension dictionary for extension: "
<< extension_id.c_str() << ". " << kExternalCrx
<< " and " << kExternalVersion << " must be used together.";
continue;
}
if (has_external_crx == has_external_update_url) {
LOG(WARNING) << "Malformed extension dictionary for extension: "
<< extension_id.c_str() << ". Exactly one of the "
<< "followng keys should be used: " << kExternalCrx
<< ", " << kExternalUpdateUrl << ".";
continue;
}
if (has_external_crx) {
if (external_crx.find(FilePath::kParentDirectory) !=
base::StringPiece::npos) {
LOG(WARNING) << "Path traversal not allowed in path: "
<< external_crx.c_str();
continue;
}
// See if it's an absolute path...
FilePath path(external_crx);
if (!path.IsAbsolute()) {
// Try path as relative path from external extension dir.
FilePath base_path;
PathService::Get(app::DIR_EXTERNAL_EXTENSIONS, &base_path);
path = base_path.Append(external_crx);
}
scoped_ptr<Version> version;
version.reset(Version::GetVersionFromString(external_version));
visitor->OnExternalExtensionFileFound(extension_id, version.get(), path,
Extension::EXTERNAL_PREF);
continue;
}
DCHECK(has_external_update_url); // Checking of keys above ensures this.
GURL update_url(external_update_url);
if (!update_url.is_valid()) {
LOG(WARNING) << "Malformed extension dictionary for extension: "
<< extension_id.c_str() << ". " << kExternalUpdateUrl
<< " must be a valid URL: " << external_update_url;
continue;
}
visitor->OnExternalExtensionUpdateUrlFound(extension_id, update_url);
}
}
Version* ExternalPrefExtensionProvider::RegisteredVersion(
const std::string& id, Extension::Location* location) const {
DictionaryValue* extension = NULL;
if (!prefs_->GetDictionary(id, &extension))
return NULL;
std::string external_version;
if (!extension->GetString(kExternalVersion, &external_version))
return NULL;
if (location)
*location = Extension::EXTERNAL_PREF;
return Version::GetVersionFromString(external_version);
}
void ExternalPrefExtensionProvider::SetPreferences(
ValueSerializer* serializer) {
std::string error_msg;
Value* extensions = serializer->Deserialize(NULL, &error_msg);
scoped_ptr<DictionaryValue> dictionary(new DictionaryValue());
if (!extensions) {
LOG(WARNING) << "Unable to deserialize json data: " << error_msg;
} else {
if (!extensions->IsType(Value::TYPE_DICTIONARY)) {
NOTREACHED() << "Invalid json data";
} else {
dictionary.reset(static_cast<DictionaryValue*>(extensions));
}
}
prefs_.reset(dictionary.release());
}
|