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,140 @@ |
+// 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_extension_util.h" |
+#include "chrome/browser/extensions/external_filesystem_extension_loader_linux.h" |
+#include "content/public/browser/browser_thread.h" |
+ |
+ |
+using content::BrowserThread; |
+ |
+namespace { |
+ |
+bool HasValidJsonExtension(const FilePath & filepath) { |
+ return filepath.Extension() == FilePath::StringType(".json"); |
+} |
+ |
+DictionaryValue* ReadJsonPrefsFile(const FilePath & json_file) { |
+ JSONFileValueSerializer serializer(json_file); |
+ DictionaryValue* |
+ json_prefs = ExternalExtensionUtil::ExtractPrefs(json_file, &serializer); |
+ |
+ return json_prefs; |
+} |
+ |
+std::set<FilePath> GetPrefsCandidatesFilesFromFolder( |
+ const FilePath& external_extension_search_path) { |
+ 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() |
+ << " (does not have a .json extension)"; |
+ } |
+ } |
+ |
+ return external_extension_paths; |
+} |
+ |
+} // namespace |
+ |
+ExternalFilesystemExtensionLoader::ExternalFilesystemExtensionLoader( |
+ int base_path_key) |
+ : base_path_key_(base_path_key) { |
+ CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
+} |
+ |
+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(base_path_key_, |
+ &extension_search_path)) { |
+ DVLOG(1) << "Could not retrieve per-extension search path"; |
+ return; |
+ } |
+ |
+ // First list the potential .json candidates |
+ 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)); |
+} |
+ |
+ |