Permalink
Browse files

This is based on a patch from Alexandre Abreu (http://codereview.chro…

…mium.org/9963120/) that I've modified slightly to work on Windows.

Modifies the external extension preferences loader to load not only external_extensions.json but also extensions based on standalone per-extension json files.

The new json files should be named <extension-id>.json and formatted as such:

{
  "external_crx": "<relative path from the .json file>",
  "external_version": "x.x"
}

Also, for Linux, added a new search path: /usr/share/[chromium|google-chrome]/extensions.

And finally, modifies the documentation to account for that change and to deprecate the external_extension.json files uses.

BUG=75174
TEST=None
Review URL: https://chromiumcodereview.appspot.com/10260010

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@134892 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information...
1 parent 67c28a6 commit 34950274996196a68e02f23c9d05ec29dd82ad53 finnur@chromium.org committed May 2, 2012
View
@@ -174,5 +174,6 @@ Arun Mankuzhi <arun.m@samsung.com>
Taylor Price <trprice@gmail.com>
Alexandru Chiculita <achicu@adobe.com>
Eric Rescorla <ekr@rtfm.com>
+Alexandre Abreu <wiss1976@gmail.com>
Erik Sjölund <erik.sjolund@gmail.com>
Simon Arlott <simon.arlott@gmail.com>
@@ -345,6 +345,20 @@ void ExternalExtensionProviderImpl::CreateExternalProviders(
Extension::INVALID,
Extension::NO_FLAGS)));
#endif
+
+#if defined(OS_LINUX) && !defined(OS_CHROMEOS)
+ provider_list->push_back(
+ linked_ptr<ExternalExtensionProviderInterface>(
+ new ExternalExtensionProviderImpl(
+ service,
+ new ExternalPrefExtensionLoader(
+ chrome::DIR_STANDALONE_EXTERNAL_EXTENSIONS,
+ ExternalPrefExtensionLoader::NONE),
+ Extension::EXTERNAL_PREF,
+ Extension::EXTERNAL_PREF_DOWNLOAD,
+ Extension::NO_FLAGS)));
+#endif
+
provider_list->push_back(
linked_ptr<ExternalExtensionProviderInterface>(
new ExternalExtensionProviderImpl(
@@ -5,45 +5,93 @@
#include "chrome/browser/extensions/external_pref_extension_loader.h"
#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/logging.h"
#include "base/metrics/histogram.h"
#include "base/path_service.h"
+#include "base/string_util.h"
+#include "base/utf_string_conversions.h"
#include "chrome/common/chrome_paths.h"
#include "content/public/browser/browser_thread.h"
using content::BrowserThread;
namespace {
+FilePath::CharType kExternalExtensionJson[] =
+ FILE_PATH_LITERAL("external_extensions.json");
+
+std::set<FilePath> GetPrefsCandidateFilesFromFolder(
+ 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.
+ return external_extension_paths;
+ }
+
+ file_util::FileEnumerator json_files(
+ external_extension_search_path,
+ false, // Recursive.
+ file_util::FileEnumerator::FILES);
+#if defined(OS_WIN)
+ FilePath::StringType extension = UTF8ToWide(std::string(".json"));
+#elif defined(OS_POSIX)
+ FilePath::StringType extension(".json");
+#endif
+ do {
+ FilePath file = json_files.Next();
+ if (file.BaseName().value() == kExternalExtensionJson)
+ continue; // Already taken care of elsewhere.
+ if (file.empty())
+ break;
+ if (file.MatchesExtension(extension)) {
+ external_extension_paths.insert(file.BaseName());
+ } else {
+ DVLOG(1) << "Not considering: " << file.LossyDisplayName()
+ << " (does not have a .json extension)";
+ }
+ } while (true);
+
+ return external_extension_paths;
+}
+
+// Extracts extension information from a json file serialized by |serializer|.
+// |path| is only used for informational purposes (outputted when an error
+// occurs). An empty dictionary is returned in case of failure (e.g. invalid
+// path or json content).
// Caller takes ownership of the returned dictionary.
-DictionaryValue* ExtractPrefs(const FilePath& path,
- base::ValueSerializer* serializer) {
+DictionaryValue* ExtractExtensionPrefs(base::ValueSerializer* serializer,
+ const FilePath& path) {
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);
- }
+ << " in file " << path.value() << ".";
+ return new DictionaryValue;
}
+
+ DictionaryValue* ext_dictionary = NULL;
+ if (extensions->GetAsDictionary(&ext_dictionary))
+ return ext_dictionary;
+
+ LOG(WARNING) << "Expected a JSON dictionary in file "
+ << path.value() << ".";
return new DictionaryValue;
}
} // namespace
-ExternalPrefExtensionLoader::ExternalPrefExtensionLoader(int base_path_key,
+ExternalPrefExtensionLoader::ExternalPrefExtensionLoader(int base_path_id,
Options options)
- : base_path_key_(base_path_key),
- options_(options){
+ : base_path_id_(base_path_id),
+ options_(options) {
CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
}
@@ -61,23 +109,59 @@ void ExternalPrefExtensionLoader::StartLoading() {
base::Bind(&ExternalPrefExtensionLoader::LoadOnFileThread, this));
}
-DictionaryValue* ExternalPrefExtensionLoader::ReadJsonPrefsFile() {
- // TODO(skerner): Some values of base_path_key_ will cause
+void ExternalPrefExtensionLoader::LoadOnFileThread() {
+ CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
+
+ // TODO(skerner): Some values of base_path_id_ will cause
// PathService::Get() to return false, because the path does
// not exist. Find and fix the build/install scripts so that
// this can become a CHECK(). Known examples include chrome
// OS developer builds and linux install packages.
// Tracked as crbug.com/70402 .
- if (!PathService::Get(base_path_key_, &base_path_)) {
- return NULL;
+ if (!PathService::Get(base_path_id_, &base_path_))
+ return;
+
+ scoped_ptr<DictionaryValue> prefs(new DictionaryValue);
+
+ ReadExternalExtensionPrefFile(prefs.get());
+ if (!prefs->empty())
+ LOG(WARNING) << "You are using an old-style extension deployment method "
+ "(external_extensions.json), which will soon be "
+ "deprecated. (see http://code.google.com/chrome/"
+ "extensions/external_extensions.html )";
+
+ ReadStandaloneExtensionPrefFiles(prefs.get());
+
+ prefs_.reset(prefs.release());
+ if (!prefs_.get())
+ prefs_.reset(new DictionaryValue());
+
+ if (base_path_id_ == chrome::DIR_EXTERNAL_EXTENSIONS) {
+ UMA_HISTOGRAM_COUNTS_100("Extensions.ExternalJsonCount",
+ prefs_->size());
}
- FilePath json_file = base_path_.Append(
- FILE_PATH_LITERAL("external_extensions.json"));
+ // If we have any records to process, then we must have
+ // read at least one .json file. If so, then we should have
+ // set |base_path_|.
+ if (!prefs_->empty())
+ CHECK(!base_path_.empty());
+
+ BrowserThread::PostTask(
+ BrowserThread::UI, FROM_HERE,
+ base::Bind(&ExternalPrefExtensionLoader::LoadFinished, this));
+}
+
+void ExternalPrefExtensionLoader::ReadExternalExtensionPrefFile(
+ DictionaryValue* prefs) {
+ CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
+ CHECK(NULL != prefs);
+
+ FilePath json_file = base_path_.Append(kExternalExtensionJson);
if (!file_util::PathExists(json_file)) {
// This is not an error. The file does not exist by default.
- return NULL;
+ return;
}
if (IsOptionSet(ENSURE_PATH_CONTROLLED_BY_ADMIN)) {
@@ -89,7 +173,7 @@ DictionaryValue* ExternalPrefExtensionLoader::ReadJsonPrefsFile() {
<< "writable by all users. These restrictions prevent "
<< "unprivleged users from making chrome install extensions "
<< "on other users' accounts.";
- return NULL;
+ return;
}
#else
// The only platform that uses this check is Mac OS. If you add one,
@@ -100,31 +184,51 @@ DictionaryValue* ExternalPrefExtensionLoader::ReadJsonPrefsFile() {
}
JSONFileValueSerializer serializer(json_file);
- DictionaryValue* parsed_json_prefs = ExtractPrefs(json_file, &serializer);
- return parsed_json_prefs;
+ DictionaryValue * ext_prefs = ExtractExtensionPrefs(&serializer, json_file);
+ if (ext_prefs)
+ prefs->MergeDictionary(ext_prefs);
}
-void ExternalPrefExtensionLoader::LoadOnFileThread() {
+void ExternalPrefExtensionLoader::ReadStandaloneExtensionPrefFiles(
+ DictionaryValue* prefs) {
CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
-
- prefs_.reset(ReadJsonPrefsFile());
- if (!prefs_.get())
- prefs_.reset(new DictionaryValue());
-
- if (base_path_key_ == chrome::DIR_EXTERNAL_EXTENSIONS) {
- UMA_HISTOGRAM_COUNTS_100("Extensions.ExternalJsonCount",
- prefs_->size());
+ CHECK(NULL != prefs);
+
+ // First list the potential .json candidates.
+ std::set<FilePath>
+ candidates = GetPrefsCandidateFilesFromFolder(base_path_);
+ if (candidates.empty()) {
+ DVLOG(1) << "Extension candidates list empty";
+ return;
}
- // If we have any records to process, then we must have
- // read the .json file. If we read the .json file, then
- // we were should have set |base_path_|.
- if (!prefs_->empty())
- CHECK(!base_path_.empty());
-
- BrowserThread::PostTask(
- BrowserThread::UI, FROM_HERE,
- base::Bind(&ExternalPrefExtensionLoader::LoadFinished, this));
+ // 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 = base_path_.Append(*it);
+
+ std::string id =
+#if defined(OS_WIN)
+ WideToASCII(
+ extension_candidate_path.RemoveExtension().BaseName().value());
+#elif defined(OS_POSIX)
+ extension_candidate_path.RemoveExtension().BaseName().value().c_str();
+#endif
+
+ DVLOG(1) << "Reading json file: "
+ << extension_candidate_path.LossyDisplayName().c_str();
+
+ JSONFileValueSerializer serializer(extension_candidate_path);
+ DictionaryValue* ext_prefs =
+ ExtractExtensionPrefs(&serializer, extension_candidate_path);
+
+ if (ext_prefs) {
+ DVLOG(1) << "Adding extension with id: " << id;
+ prefs->Set(id, ext_prefs);
+ }
+ }
}
ExternalTestingExtensionLoader::ExternalTestingExtensionLoader(
@@ -134,7 +238,7 @@ ExternalTestingExtensionLoader::ExternalTestingExtensionLoader(
CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
JSONStringValueSerializer serializer(json_data);
FilePath fake_json_path = fake_base_path.AppendASCII("fake.json");
- testing_prefs_.reset(ExtractPrefs(fake_json_path, &serializer));
+ testing_prefs_.reset(ExtractExtensionPrefs(&serializer, fake_json_path));
}
void ExternalTestingExtensionLoader::StartLoading() {
@@ -1,4 +1,4 @@
-// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// 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.
@@ -29,10 +29,10 @@ class ExternalPrefExtensionLoader : public ExternalExtensionLoader {
ENSURE_PATH_CONTROLLED_BY_ADMIN = 1 << 0
};
- // |base_path_key| is the directory containing the external_extensions.json
- // file. Relative file paths to extension files are resolved relative
- // to this path.
- explicit ExternalPrefExtensionLoader(int base_path_key, Options options);
+ // |base_path_id| is the directory containing the external_extensions.json
+ // file or the standalone extension manifest files. Relative file paths to
+ // extension files are resolved relative to this path.
+ explicit ExternalPrefExtensionLoader(int base_path_id, Options options);
virtual const FilePath GetBaseCrxFilePath() OVERRIDE;
@@ -47,11 +47,32 @@ class ExternalPrefExtensionLoader : public ExternalExtensionLoader {
virtual ~ExternalPrefExtensionLoader() {}
- DictionaryValue* ReadJsonPrefsFile();
+ // Actually searches for and loads candidate standalone extension preference
+ // files in the path corresponding to |base_path_id|.
+ // Must be called on the file thread.
void LoadOnFileThread();
- int base_path_key_;
+ // Extracts the information contained in an external_extension.json file
+ // regarding which extensions to install. |prefs| will be modified to
+ // receive the extracted extension information.
+ // Must be called from the File thread.
+ void ReadExternalExtensionPrefFile(DictionaryValue * prefs);
+
+ // Extracts the information contained in standalone external extension
+ // json files (<extension id>.json) regarding what external extensions
+ // to install. |prefs| will be modified to receive the extracted extension
+ // information.
+ // Must be called from the File thread.
+ void ReadStandaloneExtensionPrefFiles(DictionaryValue * prefs);
+
+ // The resource id of the base path with the information about the json
+ // file containing which extensions to load.
+ int base_path_id_;
+
Options options_;
+
+ // The path (coresponding to |base_path_id_| containing the json files
+ // describing which extensions to load.
FilePath base_path_;
DISALLOW_COPY_AND_ASSIGN(ExternalPrefExtensionLoader);
@@ -71,6 +71,18 @@ const FilePath::CharType kGTalkPluginFileName[] =
FILE_PATH_LITERAL("pepper/libppgoogletalk.so");
#endif // defined(OS_POSIX) && !defined(OS_MACOSX)
+
+#if defined(OS_LINUX) && !defined(OS_CHROMEOS)
+// The path to the external extension <id>.json files.
+// /usr/share seems like a good choice, see: http://www.pathname.com/fhs/
+const char kFilepathSinglePrefExtensions[] =
+#if defined(GOOGLE_CHROME_BUILD)
+ FILE_PATH_LITERAL("/usr/share/google-chrome/extensions");
+#else
+ FILE_PATH_LITERAL("/usr/share/chromium/extensions");
+#endif // defined(GOOGLE_CHROME_BUILD)
+#endif // defined(OS_LINUX) && !defined(OS_CHROMEOS)
+
} // namespace
namespace chrome {
@@ -361,6 +373,12 @@ bool PathProvider(int key, FilePath* result) {
break;
}
#endif
+#if defined(OS_LINUX) && !defined(OS_CHROMEOS)
+ case chrome::DIR_STANDALONE_EXTERNAL_EXTENSIONS: {
+ cur = FilePath(FILE_PATH_LITERAL(kFilepathSinglePrefExtensions));
+ break;
+ }
+#endif
case chrome::DIR_EXTERNAL_EXTENSIONS:
#if defined(OS_MACOSX)
if (!chrome::GetGlobalApplicationSupportDirectory(&cur))
@@ -55,6 +55,12 @@ enum {
// Getting this path does not create it.
#endif
+#if defined(OS_LINUX) && !defined(OS_CHROMEOS)
+ DIR_STANDALONE_EXTERNAL_EXTENSIONS, // Directory for 'per-extension'
+ // definition manifest files that
+ // describe extensions which are to be
+ // installed when chrome is run.
+#endif
DIR_EXTERNAL_EXTENSIONS, // Directory where installer places .crx files.
DIR_DEFAULT_APPS, // Directory where installer places .crx files
Oops, something went wrong.

0 comments on commit 3495027

Please sign in to comment.