From 905cae8e3a0e78d359ad1af3e0ef65f3a1e787c8 Mon Sep 17 00:00:00 2001 From: "bartfab@chromium.org" Date: Wed, 17 Jul 2013 14:27:37 +0000 Subject: [PATCH] Decode 'dict' cloud policies This CL implements the decoding of cloud policies with type 'dict' from JSON strings. The decoding is a prerequisite for policies referencing external data, which will be encoded as JSON strings. BUG=108997,256635 TEST=Manual Review URL: https://chromiumcodereview.appspot.com/19257003 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@212039 0039d316-1c4b-4281-b951-d872f2087c98 --- chrome/tools/build/generate_policy_source.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/chrome/tools/build/generate_policy_source.py b/chrome/tools/build/generate_policy_source.py index fb307d162e5e..3ad6d57fc0d1 100755 --- a/chrome/tools/build/generate_policy_source.py +++ b/chrome/tools/build/generate_policy_source.py @@ -28,8 +28,6 @@ class PolicyDetails: # - the equivalent base::Value::Type # - the equivalent Protobuf field type # - the name of one of the protobufs for shared policy types - # TODO(joaodasilva): introduce a message to represent dictionary values. - # Mapping 'dict' to 'string' for now. http://crbug.com/108997 TYPE_MAP = { 'dict': ('TYPE_DICTIONARY', 'string', 'String'), 'int': ('TYPE_INTEGER', 'int64', 'Integer'), @@ -412,8 +410,11 @@ CPP_HEAD = ''' #include #include +#include "base/basictypes.h" #include "base/callback.h" +#include "base/json/json_reader.h" #include "base/logging.h" +#include "base/memory/scoped_ptr.h" #include "base/values.h" #include "chrome/browser/policy/external_data_fetcher.h" #include "chrome/browser/policy/policy_map.h" @@ -447,6 +448,20 @@ base::ListValue* DecodeStringList(const em::StringList& string_list) { return list_value; } +base::DictionaryValue* DecodeDictionaryValue(const std::string& json) { + scoped_ptr root( + base::JSONReader::Read(json, base::JSON_ALLOW_TRAILING_COMMAS)); + base::DictionaryValue* dict = NULL; + if (!root || !root->GetAsDictionary(&dict) || !dict) { + LOG(WARNING) << "Invalid JSON string, ignoring: " << json; + // TODO(bartfab): Figure out a way to show errors in chrome://policy. + return new base::DictionaryValue; + } + + ignore_result(root.release()); + return dict; +} + void DecodePolicy(const em::CloudPolicySettings& policy, PolicyMap* map) { ''' @@ -467,8 +482,7 @@ def _CreateValue(type, arg): elif type == 'TYPE_LIST': return 'DecodeStringList(%s)' % arg elif type == 'TYPE_DICTIONARY': - # TODO(joaodasilva): decode 'dict' types. http://crbug.com/108997 - return 'new base::DictionaryValue()' + return 'DecodeDictionaryValue(%s)' % arg else: raise NotImplementedError('Unknown type %s' % type) -- 2.11.4.GIT