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_extension_util.h" |
| 19 #include "chrome/browser/extensions/external_filesystem_extension_loader_linux.h
" |
| 20 #include "content/public/browser/browser_thread.h" |
| 21 |
| 22 |
| 23 using content::BrowserThread; |
| 24 |
| 25 namespace { |
| 26 |
| 27 bool HasValidJsonExtension(const FilePath & filepath) { |
| 28 return filepath.Extension() == FilePath::StringType(".json"); |
| 29 } |
| 30 |
| 31 DictionaryValue* ReadJsonPrefsFile(const FilePath & json_file) { |
| 32 JSONFileValueSerializer serializer(json_file); |
| 33 DictionaryValue* |
| 34 json_prefs = ExternalExtensionUtil::ExtractPrefs(json_file, &serializer); |
| 35 |
| 36 return json_prefs; |
| 37 } |
| 38 |
| 39 std::set<FilePath> GetPrefsCandidatesFilesFromFolder( |
| 40 const FilePath& external_extension_search_path) { |
| 41 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 42 |
| 43 std::set<FilePath> external_extension_paths; |
| 44 |
| 45 if (!file_util::PathExists(external_extension_search_path)) { |
| 46 // Does not have to exist. |
| 47 DVLOG(1) << "Extension search path not found"; |
| 48 return external_extension_paths; |
| 49 } |
| 50 |
| 51 base::DirReaderPosix |
| 52 reader(external_extension_search_path.value().c_str()); |
| 53 |
| 54 if (!reader.IsValid()) { |
| 55 LOG(ERROR) << "Can not read external extensions path '" |
| 56 << external_extension_search_path.LossyDisplayName() |
| 57 << "' although it appears to exist."; |
| 58 return external_extension_paths; |
| 59 } |
| 60 |
| 61 while (reader.Next()) { |
| 62 const FilePath filename(reader.name()); |
| 63 |
| 64 DVLOG(1) << "Checking for file: " << reader.name(); |
| 65 |
| 66 if (HasValidJsonExtension(filename)) { |
| 67 external_extension_paths.insert(filename); |
| 68 } else { |
| 69 DVLOG(1) << "Not considering: " << reader.name() |
| 70 << " (does not have a .json extension)"; |
| 71 } |
| 72 } |
| 73 |
| 74 return external_extension_paths; |
| 75 } |
| 76 |
| 77 } // namespace |
| 78 |
| 79 ExternalFilesystemExtensionLoader::ExternalFilesystemExtensionLoader( |
| 80 int base_path_key) |
| 81 : base_path_key_(base_path_key) { |
| 82 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 83 } |
| 84 |
| 85 void ExternalFilesystemExtensionLoader::StartLoading() { |
| 86 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 87 BrowserThread::PostTask( |
| 88 BrowserThread::FILE, FROM_HERE, |
| 89 base::Bind(&ExternalFilesystemExtensionLoader::LoadOnFileThread, this)); |
| 90 } |
| 91 |
| 92 void ExternalFilesystemExtensionLoader::LoadOnFileThread() { |
| 93 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 94 scoped_ptr<DictionaryValue> prefs(new DictionaryValue); |
| 95 |
| 96 FilePath extension_search_path; |
| 97 if (!PathService::Get(base_path_key_, |
| 98 &extension_search_path)) { |
| 99 DVLOG(1) << "Could not retrieve per-extension search path"; |
| 100 return; |
| 101 } |
| 102 |
| 103 // First list the potential .json candidates |
| 104 std::set<FilePath> |
| 105 candidates = GetPrefsCandidatesFilesFromFolder(extension_search_path); |
| 106 if (candidates.empty()) { |
| 107 DVLOG(1) << "Extension candidates list empty"; |
| 108 return; |
| 109 } |
| 110 |
| 111 // For each file read the json description & build the proper |
| 112 // associated prefs |
| 113 for (std::set<FilePath>::const_iterator it = candidates.begin(); |
| 114 it != candidates.end(); |
| 115 ++it) { |
| 116 FilePath |
| 117 extension_candidate_path = extension_search_path.Append(*it); |
| 118 |
| 119 FilePath::StringType |
| 120 id = extension_candidate_path.RemoveExtension().BaseName().value(); |
| 121 |
| 122 DictionaryValue* |
| 123 json_file_content = ReadJsonPrefsFile(extension_candidate_path); |
| 124 |
| 125 DVLOG(1) << "Reading json file: " << extension_candidate_path.value(); |
| 126 |
| 127 if (NULL != json_file_content) { |
| 128 DVLOG(1) << "Adding extension with id: " << id; |
| 129 |
| 130 prefs->Set(id, json_file_content); |
| 131 } |
| 132 } |
| 133 |
| 134 prefs_.reset(prefs.release()); |
| 135 BrowserThread::PostTask( |
| 136 BrowserThread::UI, FROM_HERE, |
| 137 base::Bind(&ExternalFilesystemExtensionLoader::LoadFinished, this)); |
| 138 } |
| 139 |
| 140 |
OLD | NEW |