diff options
author | benwells@chromium.org <benwells@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-11-05 22:52:42 +0000 |
---|---|---|
committer | benwells@chromium.org <benwells@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-11-05 22:52:42 +0000 |
commit | 22b7b2cede575642c81042aab307a5afc6a88568 (patch) | |
tree | 0c2709ce63902f6250664e7ed2d856565c9b4fee /extensions/common | |
parent | ae9fafd5dc78e603e6111774cb23fed69ce548b2 (diff) | |
download | chromium_src-22b7b2cede575642c81042aab307a5afc6a88568.zip chromium_src-22b7b2cede575642c81042aab307a5afc6a88568.tar.gz chromium_src-22b7b2cede575642c81042aab307a5afc6a88568.tar.bz2 |
Moved extension and value builder code to extensions component.
This is only used by tests but it has no chrome dependencies, and test
in extensions use it, so it should move down to.
TBR=sky@chromium.org
BUG=162530
Review URL: https://codereview.chromium.org/48643003
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@233119 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'extensions/common')
-rw-r--r-- | extensions/common/extension_builder.cc | 61 | ||||
-rw-r--r-- | extensions/common/extension_builder.h | 63 | ||||
-rw-r--r-- | extensions/common/value_builder.cc | 102 | ||||
-rw-r--r-- | extensions/common/value_builder.h | 104 |
4 files changed, 330 insertions, 0 deletions
diff --git a/extensions/common/extension_builder.cc b/extensions/common/extension_builder.cc new file mode 100644 index 0000000..ef5a14f --- /dev/null +++ b/extensions/common/extension_builder.cc @@ -0,0 +1,61 @@ +// Copyright 2013 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 "extensions/common/extension_builder.h" + +#include "chrome/common/extensions/extension.h" + +namespace extensions { + +ExtensionBuilder::ExtensionBuilder() + : location_(Manifest::UNPACKED), + flags_(Extension::NO_FLAGS) { +} +ExtensionBuilder::~ExtensionBuilder() {} + +scoped_refptr<Extension> ExtensionBuilder::Build() { + std::string error; + scoped_refptr<Extension> extension = Extension::Create( + path_, + location_, + *manifest_, + flags_, + id_, + &error); + CHECK_EQ("", error); + return extension; +} + +ExtensionBuilder& ExtensionBuilder::SetPath(const base::FilePath& path) { + path_ = path; + return *this; +} + +ExtensionBuilder& ExtensionBuilder::SetLocation(Manifest::Location location) { + location_ = location; + return *this; +} + +ExtensionBuilder& ExtensionBuilder::SetManifest( + scoped_ptr<base::DictionaryValue> manifest) { + manifest_ = manifest.Pass(); + return *this; +} + +ExtensionBuilder& ExtensionBuilder::MergeManifest(DictionaryBuilder& builder) { + manifest_->MergeDictionary(builder.Build().get()); + return *this; +} + +ExtensionBuilder& ExtensionBuilder::AddFlags(int init_from_value_flags) { + flags_ |= init_from_value_flags; + return *this; +} + +ExtensionBuilder& ExtensionBuilder::SetID(const std::string& id) { + id_ = id; + return *this; +} + +} // namespace extensions diff --git a/extensions/common/extension_builder.h b/extensions/common/extension_builder.h new file mode 100644 index 0000000..afb7f88 --- /dev/null +++ b/extensions/common/extension_builder.h @@ -0,0 +1,63 @@ +// Copyright 2013 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. + +#ifndef EXTENSIONS_COMMON_EXTENSION_BUILDER_H_ +#define EXTENSIONS_COMMON_EXTENSION_BUILDER_H_ + +#include "base/files/file_path.h" +#include "base/memory/ref_counted.h" +#include "base/memory/scoped_ptr.h" +#include "extensions/common/manifest.h" +#include "extensions/common/value_builder.h" + +namespace extensions { +class Extension; + +// An easier way to create extensions than Extension::Create. The +// constructor sets up some defaults which are customized using the +// methods. The only method that must be called is SetManifest(). +class ExtensionBuilder { + public: + ExtensionBuilder(); + ~ExtensionBuilder(); + + // Can only be called once, after which it's invalid to use the builder. + // CHECKs that the extension was created successfully. + scoped_refptr<Extension> Build(); + + // Workaround to allow you to pass rvalue ExtensionBuilders by reference to + // other functions, e.g. UseBuilder(ExtensionBuilder().Pass()) + ExtensionBuilder& Pass() { return *this; } + + // Defaults to FilePath(). + ExtensionBuilder& SetPath(const base::FilePath& path); + + // Defaults to Manifest::UNPACKED. + ExtensionBuilder& SetLocation(Manifest::Location location); + + ExtensionBuilder& SetManifest(scoped_ptr<base::DictionaryValue> manifest); + ExtensionBuilder& SetManifest(DictionaryBuilder& manifest_builder) { + return SetManifest(manifest_builder.Build()); + } + + // Adds the keys from the DictionaryBuilder to the manifest, with new keys + // taking precedence. + ExtensionBuilder& MergeManifest(DictionaryBuilder& builder); + + ExtensionBuilder& AddFlags(int init_from_value_flags); + + // Defaults to the default extension ID created in Extension::Create. + ExtensionBuilder& SetID(const std::string& id); + + private: + base::FilePath path_; + Manifest::Location location_; + scoped_ptr<base::DictionaryValue> manifest_; + int flags_; + std::string id_; +}; + +} // namespace extensions + +#endif // EXTENSIONS_COMMON_EXTENSION_BUILDER_H_ diff --git a/extensions/common/value_builder.cc b/extensions/common/value_builder.cc new file mode 100644 index 0000000..c32679e --- /dev/null +++ b/extensions/common/value_builder.cc @@ -0,0 +1,102 @@ +// Copyright 2013 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 "extensions/common/value_builder.h" + +namespace extensions { + +// DictionaryBuilder + +DictionaryBuilder::DictionaryBuilder() : dict_(new base::DictionaryValue) {} + +DictionaryBuilder::DictionaryBuilder(const base::DictionaryValue& init) + : dict_(init.DeepCopy()) {} + +DictionaryBuilder::~DictionaryBuilder() {} + +DictionaryBuilder& DictionaryBuilder::Set(const std::string& path, + int in_value) { + dict_->SetWithoutPathExpansion(path, new base::FundamentalValue(in_value)); + return *this; +} + +DictionaryBuilder& DictionaryBuilder::Set(const std::string& path, + double in_value) { + dict_->SetWithoutPathExpansion(path, new base::FundamentalValue(in_value)); + return *this; +} + +DictionaryBuilder& DictionaryBuilder::Set(const std::string& path, + const std::string& in_value) { + dict_->SetWithoutPathExpansion(path, new base::StringValue(in_value)); + return *this; +} + +DictionaryBuilder& DictionaryBuilder::Set(const std::string& path, + const string16& in_value) { + dict_->SetWithoutPathExpansion(path, new base::StringValue(in_value)); + return *this; +} + +DictionaryBuilder& DictionaryBuilder::Set(const std::string& path, + DictionaryBuilder& in_value) { + dict_->SetWithoutPathExpansion(path, in_value.Build().release()); + return *this; +} + +DictionaryBuilder& DictionaryBuilder::Set(const std::string& path, + ListBuilder& in_value) { + dict_->SetWithoutPathExpansion(path, in_value.Build().release()); + return *this; +} + +DictionaryBuilder& DictionaryBuilder::SetBoolean( + const std::string& path, bool in_value) { + dict_->SetWithoutPathExpansion(path, new base::FundamentalValue(in_value)); + return *this; +} + +// ListBuilder + +ListBuilder::ListBuilder() : list_(new base::ListValue) {} +ListBuilder::ListBuilder(const base::ListValue& init) : list_(init.DeepCopy()) { +} +ListBuilder::~ListBuilder() {} + +ListBuilder& ListBuilder::Append(int in_value) { + list_->Append(new base::FundamentalValue(in_value)); + return *this; +} + +ListBuilder& ListBuilder::Append(double in_value) { + list_->Append(new base::FundamentalValue(in_value)); + return *this; +} + +ListBuilder& ListBuilder::Append(const std::string& in_value) { + list_->Append(new base::StringValue(in_value)); + return *this; +} + +ListBuilder& ListBuilder::Append(const string16& in_value) { + list_->Append(new base::StringValue(in_value)); + return *this; +} + +ListBuilder& ListBuilder::Append(DictionaryBuilder& in_value) { + list_->Append(in_value.Build().release()); + return *this; +} + +ListBuilder& ListBuilder::Append(ListBuilder& in_value) { + list_->Append(in_value.Build().release()); + return *this; +} + +ListBuilder& ListBuilder::AppendBoolean(bool in_value) { + list_->Append(new base::FundamentalValue(in_value)); + return *this; +} + +} // namespace extensions diff --git a/extensions/common/value_builder.h b/extensions/common/value_builder.h new file mode 100644 index 0000000..77b0d73 --- /dev/null +++ b/extensions/common/value_builder.h @@ -0,0 +1,104 @@ +// Copyright 2013 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. + +// This file provides a builders for DictionaryValue and ListValue. These +// aren't specific to extensions and could move up to base/ if there's interest +// from other sub-projects. +// +// The general pattern is to write: +// +// scoped_ptr<BuiltType> result(FooBuilder() +// .Set(args) +// .Set(args) +// .Build()); +// +// For methods that take other built types, you can pass the builder directly +// to the setter without calling Build(): +// +// DictionaryBuilder().Set("key", ListBuilder() +// .Append("foo").Append("bar") /* No .Build() */); +// +// Because of limitations in C++03, and to avoid extra copies, you can't pass a +// just-constructed Builder into another Builder's method directly. Use the +// Pass() method. +// +// The Build() method invalidates its builder, and returns ownership of the +// built value. +// +// These objects are intended to be used as temporaries rather than stored +// anywhere, so the use of non-const reference parameters is likely to cause +// less confusion than usual. + +#ifndef EXTENSIONS_COMMON_VALUE_BUILDER_H_ +#define EXTENSIONS_COMMON_VALUE_BUILDER_H_ + +#include <string> + +#include "base/memory/scoped_ptr.h" +#include "base/strings/string16.h" +#include "base/values.h" + +namespace extensions { + +class ListBuilder; + +class DictionaryBuilder { + public: + DictionaryBuilder(); + explicit DictionaryBuilder(const base::DictionaryValue& init); + ~DictionaryBuilder(); + + // Workaround to allow you to pass rvalue ExtensionBuilders by reference to + // other functions. + DictionaryBuilder& Pass() { return *this; } + + // Can only be called once, after which it's invalid to use the builder. + scoped_ptr<base::DictionaryValue> Build() { return dict_.Pass(); } + + DictionaryBuilder& Set(const std::string& path, int in_value); + DictionaryBuilder& Set(const std::string& path, double in_value); + DictionaryBuilder& Set(const std::string& path, const std::string& in_value); + DictionaryBuilder& Set(const std::string& path, const string16& in_value); + DictionaryBuilder& Set(const std::string& path, DictionaryBuilder& in_value); + DictionaryBuilder& Set(const std::string& path, ListBuilder& in_value); + + // Named differently because overload resolution is too eager to + // convert implicitly to bool. + DictionaryBuilder& SetBoolean(const std::string& path, bool in_value); + + private: + scoped_ptr<base::DictionaryValue> dict_; +}; + +class ListBuilder { + public: + ListBuilder(); + explicit ListBuilder(const base::ListValue& init); + ~ListBuilder(); + + // Workaround to allow you to pass rvalue ExtensionBuilders by reference to + // other functions. + ListBuilder& Pass() { return *this; } + + // Can only be called once, after which it's invalid to use the builder. + scoped_ptr<base::ListValue> Build() { return list_.Pass(); } + + ListBuilder& Append(int in_value); + ListBuilder& Append(double in_value); + ListBuilder& Append(const std::string& in_value); + ListBuilder& Append(const string16& in_value); + ListBuilder& Append(DictionaryBuilder& in_value); + ListBuilder& Append(ListBuilder& in_value); + + // Named differently because overload resolution is too eager to + // convert implicitly to bool. + ListBuilder& AppendBoolean(bool in_value); + + private: + scoped_ptr<base::ListValue> list_; +}; + +} // namespace extensions + +#endif // EXTENSIONS_COMMON_VALUE_BUILDER_H_ |