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

Side by Side Diff: chrome/browser/extensions/external_filesystem_extension_loader_linux.cc

Issue 9963120: Introduces an additional extension loader that load extra extensions based on per-extension json fi… (Closed) Base URL: https://src.chromium.org/svn/trunk/src/
Patch Set: Created 8 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
OLDNEW
(Empty)
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 "base/bind.h"
6 #include "base/dir_reader_posix.h"
7 #include "base/json/json_file_value_serializer.h"
8 #include "base/json/json_string_value_serializer.h"
9 #include "base/file_path.h"
10 #include "base/file_util.h"
11 #include "base/memory/scoped_handle.h"
12 #include "base/string_util.h"
13 #include "base/utf_string_conversions.h"
14 #include "base/values.h"
15 #include "base/version.h"
16 #include "chrome/browser/extensions/external_extension_provider_impl.h"
17 #include "chrome/browser/extensions/external_filesystem_extension_loader_linux.h "
18 #include "content/public/browser/browser_thread.h"
19
20
21 using content::BrowserThread;
22
23 namespace {
24
25 // The search path that contains information about external extensions.
26 #if defined(GOOGLE_CHROME_BUILD)
27 const char kFilepathPrefExtensions[] = "/usr/share/google-chrome/extensions";
Sam Kerner (Chrome) 2012/04/13 01:35:27 Take a look at src/chrome/common/chrome_paths.h .
28 #else
29 const char kFilepathPrefExtensions[] = "/usr/share/chromium/extensions";
30 #endif
31
32
33 // Caller takes ownership of the returned dictionary.
34 DictionaryValue* ExtractPrefs(const FilePath& path,
Sam Kerner (Chrome) 2012/04/13 01:35:27 This code looks a lot like the code that reads ext
35 base::ValueSerializer* serializer) {
36 std::string error_msg;
37 Value* extensions = serializer->Deserialize(NULL, &error_msg);
38 if (!extensions) {
39 LOG(WARNING) << "Unable to deserialize json data: " << error_msg
40 << " In file " << path.value() << " .";
41 } else {
42 if (!extensions->IsType(Value::TYPE_DICTIONARY)) {
43 LOG(WARNING) << "Expected a JSON dictionary in file "
44 << path.value() << " .";
45 } else {
46 return static_cast<DictionaryValue*>(extensions);
47 }
48 }
49 return new DictionaryValue;
50 }
51
52 bool HasValidJsonExtension(const FilePath & filepath) {
53 return filepath.Extension() == FilePath::StringType(".json");
54 }
55
56 bool IsExcludedFromJsonPrefsCandidates(const FilePath & filepath) {
Sam Kerner (Chrome) 2012/04/13 01:35:27 Why is this needed? I think you are looking in a
57 return filepath.BaseName()
58 .value() == FilePath::StringType("external_extension.json");
59 }
60
61 DictionaryValue* ReadJsonPrefsFile(const FilePath & json_file) {
62 JSONFileValueSerializer serializer(json_file);
63 DictionaryValue* parsed_json_prefs = ExtractPrefs(json_file, &serializer);
64
65 return parsed_json_prefs;
66 }
67
68 std::set<FilePath> GetPrefsCandidatesFilesFromFolder(
69 const FilePath & external_extension_search_path) {
70 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
71
72 std::set<FilePath> external_extension_paths;
73
74 if (!file_util::PathExists(external_extension_search_path)) {
75 // Does not have to exist.
76 DVLOG(1) << "Extension search path not found";
77 return external_extension_paths;
78 }
79
80 base::DirReaderPosix
81 reader(external_extension_search_path.value().c_str());
Sam Kerner (Chrome) 2012/04/13 01:35:27 Chrome style is four spaces when continuing from t
82
83 if (!reader.IsValid()) {
84 LOG(ERROR) << "Can not read external extensions path '"
85 << external_extension_search_path.LossyDisplayName()
86 << "' although it appears to exist.";
87 return external_extension_paths;
88 }
89
90 while (reader.Next()) {
91 const FilePath filename(reader.name());
92
93 DVLOG(1) << "Checking for file: " << reader.name();
94
95 if ( HasValidJsonExtension(filename)
Sam Kerner (Chrome) 2012/04/13 01:35:27 Style: No space between "(" and "Has".
96 && !IsExcludedFromJsonPrefsCandidates(filename)) {
97 external_extension_paths.insert(filename);
98 } else {
99 DVLOG(1) << "Not considering: " << reader.name();
100 }
101 }
102
103 return external_extension_paths;
104 }
105
106 } // namespace
107
108
Sam Kerner (Chrome) 2012/04/13 01:35:27 Use one blank line between functions.
109
110
111 void ExternalFilesystemExtensionLoader::StartLoading() {
112 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
113 BrowserThread::PostTask(
114 BrowserThread::FILE, FROM_HERE,
115 base::Bind(&ExternalFilesystemExtensionLoader::LoadOnFileThread, this));
116 }
117
118
119 void ExternalFilesystemExtensionLoader::LoadOnFileThread() {
120 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
121 scoped_ptr<DictionaryValue> prefs(new DictionaryValue);
122
123 const FilePath
124 EXTENSION_SEARCH_PATH = FilePath(kFilepathPrefExtensions);
Sam Kerner (Chrome) 2012/04/13 01:35:27 4 spaces.
125
126 // First list the potential .json candidates (excluding some like
127 // reserved names like "external_extension.json" ...
128 std::set<FilePath>
129 candidates = GetPrefsCandidatesFilesFromFolder(EXTENSION_SEARCH_PATH);
Sam Kerner (Chrome) 2012/04/13 01:35:27 4 spaces.
130
131 if (candidates.empty()) {
132 DVLOG(1) << "Extension candidates list empty";
Sam Kerner (Chrome) 2012/04/13 01:35:27 Indent is 2 spaces.
133 return;
134 }
135
136 // For each file read the json description & build the proper
137 // associated prefs
138 for (std::set<FilePath>::const_iterator it = candidates.begin()
Sam Kerner (Chrome) 2012/04/13 01:35:27 semicolons belong on the same line as the statemen
139 ; it != candidates.end()
140 ; ++it) {
141 FilePath
142 extension_candidate_path = EXTENSION_SEARCH_PATH.Append(*it);
143
144 FilePath::StringType
145 id = extension_candidate_path.RemoveExtension().BaseName().value();
146
147 DictionaryValue*
148 json_file_content = ReadJsonPrefsFile(extension_candidate_path);
149
150 DVLOG(1) << "Reading json file: " << extension_candidate_path.value();
151
152 if (NULL != json_file_content) {
153 DVLOG(1) << "Adding extension with id: " << id;
154
155 prefs->Set(id, json_file_content);
156 }
157 }
158
Sam Kerner (Chrome) 2012/04/13 01:35:27 What happens if: * The id is not valid (Extension:
159 prefs_.reset(prefs.release());
160 BrowserThread::PostTask(
161 BrowserThread::UI, FROM_HERE,
162 base::Bind(&ExternalFilesystemExtensionLoader::LoadFinished, this));
163 }
164
165
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698