15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This file provides a builders for DictionaryValue and ListValue.  These
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// aren't specific to extensions and could move up to base/ if there's interest
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// from other sub-projects.
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The general pattern is to write:
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//  scoped_ptr<BuiltType> result(FooBuilder()
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//                               .Set(args)
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//                               .Set(args)
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//                               .Build());
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// For methods that take other built types, you can pass the builder directly
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// to the setter without calling Build():
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// DictionaryBuilder().Set("key", ListBuilder()
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//                         .Append("foo").Append("bar") /* No .Build() */);
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Because of limitations in C++03, and to avoid extra copies, you can't pass a
23a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)// just-constructed Builder into another Builder's method directly. Use the
24a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)// Pass() method.
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The Build() method invalidates its builder, and returns ownership of the
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// built value.
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// These objects are intended to be used as temporaries rather than stored
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// anywhere, so the use of non-const reference parameters is likely to cause
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// less confusion than usual.
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef CHROME_COMMON_EXTENSIONS_VALUE_BUILDER_H_
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CHROME_COMMON_EXTENSIONS_VALUE_BUILDER_H_
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string>
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/scoped_ptr.h"
39868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/string16.h"
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/values.h"
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace extensions {
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class ListBuilder;
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class DictionaryBuilder {
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DictionaryBuilder();
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  explicit DictionaryBuilder(const base::DictionaryValue& init);
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ~DictionaryBuilder();
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
52a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // Workaround to allow you to pass rvalue ExtensionBuilders by reference to
53a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // other functions.
54a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  DictionaryBuilder& Pass() { return *this; }
55a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Can only be called once, after which it's invalid to use the builder.
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<base::DictionaryValue> Build() { return dict_.Pass(); }
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DictionaryBuilder& Set(const std::string& path, int in_value);
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DictionaryBuilder& Set(const std::string& path, double in_value);
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DictionaryBuilder& Set(const std::string& path, const std::string& in_value);
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DictionaryBuilder& Set(const std::string& path, const string16& in_value);
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DictionaryBuilder& Set(const std::string& path, DictionaryBuilder& in_value);
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DictionaryBuilder& Set(const std::string& path, ListBuilder& in_value);
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Named differently because overload resolution is too eager to
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // convert implicitly to bool.
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DictionaryBuilder& SetBoolean(const std::string& path, bool in_value);
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<base::DictionaryValue> dict_;
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class ListBuilder {
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ListBuilder();
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  explicit ListBuilder(const base::ListValue& init);
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ~ListBuilder();
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
80a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // Workaround to allow you to pass rvalue ExtensionBuilders by reference to
81a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // other functions.
82a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  ListBuilder& Pass() { return *this; }
83a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Can only be called once, after which it's invalid to use the builder.
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<base::ListValue> Build() { return list_.Pass(); }
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ListBuilder& Append(int in_value);
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ListBuilder& Append(double in_value);
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ListBuilder& Append(const std::string& in_value);
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ListBuilder& Append(const string16& in_value);
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ListBuilder& Append(DictionaryBuilder& in_value);
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ListBuilder& Append(ListBuilder& in_value);
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Named differently because overload resolution is too eager to
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // convert implicitly to bool.
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ListBuilder& AppendBoolean(bool in_value);
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<base::ListValue> list_;
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // namespace extensions
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // CHROME_COMMON_EXTENSIONS_VALUE_BUILDER_H_
105