OLD | NEW |
---|---|
(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/file_path.h" | |
8 #include "base/file_util.h" | |
9 #include "base/json/json_file_value_serializer.h" | |
10 #include "base/json/json_string_value_serializer.h" | |
11 #include "base/memory/scoped_handle.h" | |
12 #include "base/path_service.h" | |
13 #include "base/string_util.h" | |
14 #include "base/utf_string_conversions.h" | |
15 #include "base/values.h" | |
16 #include "base/version.h" | |
17 #include "chrome/browser/extensions/external_extension_provider_impl.h" | |
18 #include "chrome/browser/extensions/external_filesystem_extension_loader_linux.h " | |
19 #include "chrome/common/chrome_paths.h" | |
20 #include "content/public/browser/browser_thread.h" | |
21 | |
22 | |
23 using content::BrowserThread; | |
24 | |
25 namespace { | |
26 | |
27 // Caller takes ownership of the returned dictionary. | |
28 DictionaryValue* ExtractPrefs(const FilePath& path, | |
Sam Kerner (Chrome)
2012/04/13 19:35:13
The utility method makes this obsolete?
Alexandre Abreu
2012/04/13 21:52:38
Done.
| |
29 base::ValueSerializer* serializer) { | |
30 std::string error_msg; | |
31 Value* extensions = serializer->Deserialize(NULL, &error_msg); | |
32 if (!extensions) { | |
33 LOG(WARNING) << "Unable to deserialize json data: " << error_msg | |
34 << " In file " << path.value() << " ."; | |
35 } else { | |
36 if (!extensions->IsType(Value::TYPE_DICTIONARY)) { | |
37 LOG(WARNING) << "Expected a JSON dictionary in file " | |
38 << path.value() << " ."; | |
39 } else { | |
40 return static_cast<DictionaryValue*>(extensions); | |
41 } | |
42 } | |
43 return new DictionaryValue; | |
44 } | |
45 | |
46 bool HasValidJsonExtension(const FilePath & filepath) { | |
47 return filepath.Extension() == FilePath::StringType(".json"); | |
48 } | |
49 | |
50 DictionaryValue* ReadJsonPrefsFile(const FilePath & json_file) { | |
51 JSONFileValueSerializer serializer(json_file); | |
52 DictionaryValue* parsed_json_prefs = ExtractPrefs(json_file, &serializer); | |
53 | |
54 return parsed_json_prefs; | |
55 } | |
56 | |
57 std::set<FilePath> GetPrefsCandidatesFilesFromFolder( | |
58 const FilePath & external_extension_search_path) { | |
Sam Kerner (Chrome)
2012/04/13 19:35:13
Style nit: No space between "FilePath" and "&".
Alexandre Abreu
2012/04/13 21:52:38
Done.
| |
59 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
60 | |
61 std::set<FilePath> external_extension_paths; | |
62 | |
63 if (!file_util::PathExists(external_extension_search_path)) { | |
64 // Does not have to exist. | |
65 DVLOG(1) << "Extension search path not found"; | |
66 return external_extension_paths; | |
67 } | |
68 | |
69 base::DirReaderPosix | |
70 reader(external_extension_search_path.value().c_str()); | |
71 | |
72 if (!reader.IsValid()) { | |
73 LOG(ERROR) << "Can not read external extensions path '" | |
74 << external_extension_search_path.LossyDisplayName() | |
75 << "' although it appears to exist."; | |
76 return external_extension_paths; | |
77 } | |
78 | |
79 while (reader.Next()) { | |
80 const FilePath filename(reader.name()); | |
81 | |
82 DVLOG(1) << "Checking for file: " << reader.name(); | |
83 | |
84 if (HasValidJsonExtension(filename)) { | |
85 external_extension_paths.insert(filename); | |
86 } else { | |
87 DVLOG(1) << "Not considering: " << reader.name(); | |
Sam Kerner (Chrome)
2012/04/13 19:35:13
Say why you are not considering a file.
Alexandre Abreu
2012/04/13 21:52:38
Done.
| |
88 } | |
89 } | |
90 | |
91 return external_extension_paths; | |
92 } | |
93 | |
94 } // namespace | |
95 | |
96 void ExternalFilesystemExtensionLoader::StartLoading() { | |
97 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
98 BrowserThread::PostTask( | |
99 BrowserThread::FILE, FROM_HERE, | |
100 base::Bind(&ExternalFilesystemExtensionLoader::LoadOnFileThread, this)); | |
101 } | |
102 | |
103 void ExternalFilesystemExtensionLoader::LoadOnFileThread() { | |
104 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
105 scoped_ptr<DictionaryValue> prefs(new DictionaryValue); | |
106 | |
107 FilePath extension_search_path; | |
108 if (!PathService::Get(chrome::DIR_SINGLE_EXTERNAL_EXTENSIONS | |
Sam Kerner (Chrome)
2012/04/13 19:35:13
Style nit: Coma should be on previous line.
Alexandre Abreu
2012/04/13 21:52:38
Done.
| |
109 , &extension_search_path)) { | |
110 DVLOG(1) << "Could not retrieve per-extension search path"; | |
111 return; | |
112 } | |
113 | |
114 // First list the potential .json candidates (excluding some like | |
Sam Kerner (Chrome)
2012/04/13 19:35:13
I think the last part of this comment is out of da
Alexandre Abreu
2012/04/13 21:52:38
Done.
| |
115 // reserved names like "external_extension.json" ... | |
116 std::set<FilePath> | |
117 candidates = GetPrefsCandidatesFilesFromFolder(extension_search_path); | |
118 if (candidates.empty()) { | |
119 DVLOG(1) << "Extension candidates list empty"; | |
120 return; | |
121 } | |
122 | |
123 // For each file read the json description & build the proper | |
124 // associated prefs | |
125 for (std::set<FilePath>::const_iterator it = candidates.begin(); | |
126 it != candidates.end(); | |
127 ++it) { | |
128 FilePath | |
129 extension_candidate_path = extension_search_path.Append(*it); | |
130 | |
131 FilePath::StringType | |
132 id = extension_candidate_path.RemoveExtension().BaseName().value(); | |
133 | |
134 DictionaryValue* | |
135 json_file_content = ReadJsonPrefsFile(extension_candidate_path); | |
136 | |
137 DVLOG(1) << "Reading json file: " << extension_candidate_path.value(); | |
138 | |
139 if (NULL != json_file_content) { | |
140 DVLOG(1) << "Adding extension with id: " << id; | |
141 | |
142 prefs->Set(id, json_file_content); | |
143 } | |
144 } | |
145 | |
146 prefs_.reset(prefs.release()); | |
147 BrowserThread::PostTask( | |
148 BrowserThread::UI, FROM_HERE, | |
149 base::Bind(&ExternalFilesystemExtensionLoader::LoadFinished, this)); | |
150 } | |
151 | |
152 | |
OLD | NEW |