Chromium Code Reviews| Index: chrome/browser/extensions/external_filesystem_extension_loader_linux.cc |
| =================================================================== |
| --- chrome/browser/extensions/external_filesystem_extension_loader_linux.cc (revision 0) |
| +++ chrome/browser/extensions/external_filesystem_extension_loader_linux.cc (revision 0) |
| @@ -0,0 +1,152 @@ |
| +// Copyright (c) 2012 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 "base/bind.h" |
| +#include "base/dir_reader_posix.h" |
| +#include "base/file_path.h" |
| +#include "base/file_util.h" |
| +#include "base/json/json_file_value_serializer.h" |
| +#include "base/json/json_string_value_serializer.h" |
| +#include "base/memory/scoped_handle.h" |
| +#include "base/path_service.h" |
| +#include "base/string_util.h" |
| +#include "base/utf_string_conversions.h" |
| +#include "base/values.h" |
| +#include "base/version.h" |
| +#include "chrome/browser/extensions/external_extension_provider_impl.h" |
| +#include "chrome/browser/extensions/external_filesystem_extension_loader_linux.h" |
| +#include "chrome/common/chrome_paths.h" |
| +#include "content/public/browser/browser_thread.h" |
| + |
| + |
| +using content::BrowserThread; |
| + |
| +namespace { |
| + |
| +// Caller takes ownership of the returned dictionary. |
| +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.
|
| + base::ValueSerializer* serializer) { |
| + std::string error_msg; |
| + Value* extensions = serializer->Deserialize(NULL, &error_msg); |
| + if (!extensions) { |
| + LOG(WARNING) << "Unable to deserialize json data: " << error_msg |
| + << " In file " << path.value() << " ."; |
| + } else { |
| + if (!extensions->IsType(Value::TYPE_DICTIONARY)) { |
| + LOG(WARNING) << "Expected a JSON dictionary in file " |
| + << path.value() << " ."; |
| + } else { |
| + return static_cast<DictionaryValue*>(extensions); |
| + } |
| + } |
| + return new DictionaryValue; |
| +} |
| + |
| +bool HasValidJsonExtension(const FilePath & filepath) { |
| + return filepath.Extension() == FilePath::StringType(".json"); |
| +} |
| + |
| +DictionaryValue* ReadJsonPrefsFile(const FilePath & json_file) { |
| + JSONFileValueSerializer serializer(json_file); |
| + DictionaryValue* parsed_json_prefs = ExtractPrefs(json_file, &serializer); |
| + |
| + return parsed_json_prefs; |
| +} |
| + |
| +std::set<FilePath> GetPrefsCandidatesFilesFromFolder( |
| + 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.
|
| + CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| + |
| + std::set<FilePath> external_extension_paths; |
| + |
| + if (!file_util::PathExists(external_extension_search_path)) { |
| + // Does not have to exist. |
| + DVLOG(1) << "Extension search path not found"; |
| + return external_extension_paths; |
| + } |
| + |
| + base::DirReaderPosix |
| + reader(external_extension_search_path.value().c_str()); |
| + |
| + if (!reader.IsValid()) { |
| + LOG(ERROR) << "Can not read external extensions path '" |
| + << external_extension_search_path.LossyDisplayName() |
| + << "' although it appears to exist."; |
| + return external_extension_paths; |
| + } |
| + |
| + while (reader.Next()) { |
| + const FilePath filename(reader.name()); |
| + |
| + DVLOG(1) << "Checking for file: " << reader.name(); |
| + |
| + if (HasValidJsonExtension(filename)) { |
| + external_extension_paths.insert(filename); |
| + } else { |
| + 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.
|
| + } |
| + } |
| + |
| + return external_extension_paths; |
| +} |
| + |
| +} // namespace |
| + |
| +void ExternalFilesystemExtensionLoader::StartLoading() { |
| + CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + BrowserThread::PostTask( |
| + BrowserThread::FILE, FROM_HERE, |
| + base::Bind(&ExternalFilesystemExtensionLoader::LoadOnFileThread, this)); |
| +} |
| + |
| +void ExternalFilesystemExtensionLoader::LoadOnFileThread() { |
| + CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| + scoped_ptr<DictionaryValue> prefs(new DictionaryValue); |
| + |
| + FilePath extension_search_path; |
| + 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.
|
| + , &extension_search_path)) { |
| + DVLOG(1) << "Could not retrieve per-extension search path"; |
| + return; |
| + } |
| + |
| + // 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.
|
| + // reserved names like "external_extension.json" ... |
| + std::set<FilePath> |
| + candidates = GetPrefsCandidatesFilesFromFolder(extension_search_path); |
| + if (candidates.empty()) { |
| + DVLOG(1) << "Extension candidates list empty"; |
| + return; |
| + } |
| + |
| + // For each file read the json description & build the proper |
| + // associated prefs |
| + for (std::set<FilePath>::const_iterator it = candidates.begin(); |
| + it != candidates.end(); |
| + ++it) { |
| + FilePath |
| + extension_candidate_path = extension_search_path.Append(*it); |
| + |
| + FilePath::StringType |
| + id = extension_candidate_path.RemoveExtension().BaseName().value(); |
| + |
| + DictionaryValue* |
| + json_file_content = ReadJsonPrefsFile(extension_candidate_path); |
| + |
| + DVLOG(1) << "Reading json file: " << extension_candidate_path.value(); |
| + |
| + if (NULL != json_file_content) { |
| + DVLOG(1) << "Adding extension with id: " << id; |
| + |
| + prefs->Set(id, json_file_content); |
| + } |
| + } |
| + |
| + prefs_.reset(prefs.release()); |
| + BrowserThread::PostTask( |
| + BrowserThread::UI, FROM_HERE, |
| + base::Bind(&ExternalFilesystemExtensionLoader::LoadFinished, this)); |
| +} |
| + |
| + |