1// Copyright 2013 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#ifndef EXTENSIONS_COMMON_EXTENSION_BUILDER_H_ 6#define EXTENSIONS_COMMON_EXTENSION_BUILDER_H_ 7 8#include <string> 9 10#include "base/files/file_path.h" 11#include "base/memory/ref_counted.h" 12#include "base/memory/scoped_ptr.h" 13#include "extensions/common/manifest.h" 14#include "extensions/common/value_builder.h" 15 16namespace extensions { 17class Extension; 18 19// An easier way to create extensions than Extension::Create. The 20// constructor sets up some defaults which are customized using the 21// methods. The only method that must be called is SetManifest(). 22class ExtensionBuilder { 23 public: 24 ExtensionBuilder(); 25 ~ExtensionBuilder(); 26 27 // Can only be called once, after which it's invalid to use the builder. 28 // CHECKs that the extension was created successfully. 29 scoped_refptr<Extension> Build(); 30 31 // Workaround to allow you to pass rvalue ExtensionBuilders by reference to 32 // other functions, e.g. UseBuilder(ExtensionBuilder().Pass()) 33 ExtensionBuilder& Pass() { return *this; } 34 35 // Defaults to FilePath(). 36 ExtensionBuilder& SetPath(const base::FilePath& path); 37 38 // Defaults to Manifest::UNPACKED. 39 ExtensionBuilder& SetLocation(Manifest::Location location); 40 41 ExtensionBuilder& SetManifest(scoped_ptr<base::DictionaryValue> manifest); 42 ExtensionBuilder& SetManifest(DictionaryBuilder& manifest_builder) { 43 return SetManifest(manifest_builder.Build()); 44 } 45 46 // Adds the keys from the DictionaryBuilder to the manifest, with new keys 47 // taking precedence. 48 ExtensionBuilder& MergeManifest(DictionaryBuilder& builder); 49 50 ExtensionBuilder& AddFlags(int init_from_value_flags); 51 52 // Defaults to the default extension ID created in Extension::Create. 53 ExtensionBuilder& SetID(const std::string& id); 54 55 private: 56 base::FilePath path_; 57 Manifest::Location location_; 58 scoped_ptr<base::DictionaryValue> manifest_; 59 int flags_; 60 std::string id_; 61}; 62 63} // namespace extensions 64 65#endif // EXTENSIONS_COMMON_EXTENSION_BUILDER_H_ 66