JSONValueConverter changes for custom base::Value filed parsing.

chanegs approved in http://codereview.chromium.org/10006023/

BUG=chromium-os:29052
TEST=JSONValueConverterTest.*
TBR=willchan

Review URL: https://chromiumcodereview.appspot.com/10066031

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@132101 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/base/json/json_value_converter.h b/base/json/json_value_converter.h
index 7764632..d296b66 100644
--- a/base/json/json_value_converter.h
+++ b/base/json/json_value_converter.h
@@ -205,6 +205,25 @@
 };
 
 template <typename FieldType>
+class ValueFieldConverter : public ValueConverter<FieldType> {
+ public:
+  typedef bool(*ConvertFunc)(const base::Value* value, FieldType* field);
+
+  ValueFieldConverter(ConvertFunc convert_func)
+      : convert_func_(convert_func) {}
+
+  virtual bool Convert(const base::Value& value,
+                       FieldType* field) const OVERRIDE {
+    return convert_func_(&value, field);
+  }
+
+ private:
+  ConvertFunc convert_func_;
+
+  DISALLOW_COPY_AND_ASSIGN(ValueFieldConverter);
+};
+
+template <typename FieldType>
 class CustomFieldConverter : public ValueConverter<FieldType> {
  public:
   typedef bool(*ConvertFunc)(const StringPiece& value, FieldType* field);
@@ -368,6 +387,17 @@
         new internal::CustomFieldConverter<FieldType>(convert_func)));
   }
 
+  template <typename FieldType>
+  void RegisterCustomValueField(
+      const std::string& field_name,
+      FieldType StructType::* field,
+      bool (*convert_func)(const base::Value*, FieldType*)) {
+    fields_.push_back(new internal::FieldConverter<StructType, FieldType>(
+        field_name,
+        field,
+        new internal::ValueFieldConverter<FieldType>(convert_func)));
+  }
+
   void RegisterRepeatedInt(const std::string& field_name,
                            ScopedVector<int> StructType::* field) {
     fields_.push_back(