buffet: Moved LoadJsonDict() function into buffet/utils.h Moved LoadJsonDict from CommandManager class into a separate header file as a stand-alone function so that it can be used by itself. This function will come in handy when implementing device state management. BUG=chromium:415364 TEST=FEATURES=test emerge-link buffet Change-Id: Ie818a811989d82e4b092e399ca136e15276ab453 Reviewed-on: https://chromium-review.googlesource.com/219134 Tested-by: Alex Vakulenko <avakulenko@chromium.org> Reviewed-by: Christopher Wiley <wiley@chromium.org> Commit-Queue: Alex Vakulenko <avakulenko@chromium.org>
diff --git a/buffet/buffet.gyp b/buffet/buffet.gyp index 002fd5c..ca2ddc7 100644 --- a/buffet/buffet.gyp +++ b/buffet/buffet.gyp
@@ -40,6 +40,7 @@ 'device_registration_info.cc', 'manager.cc', 'storage_impls.cc', + 'utils.cc', ], }, {
diff --git a/buffet/commands/command_manager.cc b/buffet/commands/command_manager.cc index f43fc66..7a1701a 100644 --- a/buffet/commands/command_manager.cc +++ b/buffet/commands/command_manager.cc
@@ -5,24 +5,15 @@ #include "buffet/commands/command_manager.h" #include <base/files/file_enumerator.h> -#include <base/files/file_util.h> -#include <base/json/json_reader.h> #include <base/values.h> #include <chromeos/dbus/exported_object_manager.h> #include <chromeos/errors/error.h> -#include <chromeos/errors/error_codes.h> #include "buffet/commands/schema_constants.h" +#include "buffet/utils.h" using chromeos::dbus_utils::ExportedObjectManager; -namespace { - -const char kCommandManagerErrorDomain[] = "Buffet_CommandManager"; -const char kCommandManagerFileReadError[] = "file_read_error"; - -} // namespace - namespace buffet { CommandManager::CommandManager() { @@ -91,40 +82,6 @@ } } -std::unique_ptr<const base::DictionaryValue> CommandManager::LoadJsonDict( - const base::FilePath& json_file_path, chromeos::ErrorPtr* error) { - std::string json_string; - if (!base::ReadFileToString(json_file_path, &json_string)) { - chromeos::errors::system::AddSystemError(error, errno); - chromeos::Error::AddToPrintf(error, kCommandManagerErrorDomain, - kCommandManagerFileReadError, - "Failed to read file '%s'", - json_file_path.value().c_str()); - return std::unique_ptr<const base::DictionaryValue>(); - } - std::string error_message; - base::Value* value = base::JSONReader::ReadAndReturnError( - json_string, base::JSON_PARSE_RFC, nullptr, &error_message); - if (!value) { - chromeos::Error::AddToPrintf(error, chromeos::errors::json::kDomain, - chromeos::errors::json::kParseError, - "Error parsing content of JSON file '%s': %s", - json_file_path.value().c_str(), - error_message.c_str()); - return std::unique_ptr<const base::DictionaryValue>(); - } - const base::DictionaryValue* dict_value = nullptr; - if (!value->GetAsDictionary(&dict_value)) { - delete value; - chromeos::Error::AddToPrintf(error, chromeos::errors::json::kDomain, - chromeos::errors::json::kObjectExpected, - "Content of file '%s' is not a JSON object", - json_file_path.value().c_str()); - return std::unique_ptr<const base::DictionaryValue>(); - } - return std::unique_ptr<const base::DictionaryValue>(dict_value); -} - std::string CommandManager::AddCommand( std::unique_ptr<CommandInstance> command_instance) { return command_queue_.Add(std::move(command_instance));
diff --git a/buffet/commands/command_manager.h b/buffet/commands/command_manager.h index 601ae9a..05326b3 100644 --- a/buffet/commands/command_manager.h +++ b/buffet/commands/command_manager.h
@@ -79,12 +79,6 @@ std::string AddCommand(std::unique_ptr<CommandInstance> command_instance); private: - // Helper function to load a JSON file that is expected to be - // an object/dictionary. In case of error, returns empty unique ptr and fills - // in error details in |error|. - std::unique_ptr<const base::DictionaryValue> LoadJsonDict( - const base::FilePath& json_file_path, chromeos::ErrorPtr* error); - CommandDictionary base_dictionary_; // Base/std command definitions/schemas. CommandDictionary dictionary_; // Command definitions/schemas. CommandQueue command_queue_;
diff --git a/buffet/device_registration_info.cc b/buffet/device_registration_info.cc index 0368c83..2f2ddac 100644 --- a/buffet/device_registration_info.cc +++ b/buffet/device_registration_info.cc
@@ -20,11 +20,11 @@ #include "buffet/commands/command_manager.h" #include "buffet/device_registration_storage_keys.h" #include "buffet/storage_impls.h" +#include "buffet/utils.h" const char buffet::kErrorDomainOAuth2[] = "oauth2"; const char buffet::kErrorDomainGCD[] = "gcd"; const char buffet::kErrorDomainGCDServer[] = "gcd_server"; -const char buffet::kErrorDomainBuffet[] = "buffet"; namespace buffet { namespace storage_keys {
diff --git a/buffet/device_registration_info.h b/buffet/device_registration_info.h index ec2f00b..8cef223 100644 --- a/buffet/device_registration_info.h +++ b/buffet/device_registration_info.h
@@ -29,7 +29,6 @@ extern const char kErrorDomainOAuth2[]; extern const char kErrorDomainGCD[]; extern const char kErrorDomainGCDServer[]; -extern const char kErrorDomainBuffet[]; // The DeviceRegistrationInfo class represents device registration information. class DeviceRegistrationInfo {
diff --git a/buffet/utils.cc b/buffet/utils.cc new file mode 100644 index 0000000..71e9a56 --- /dev/null +++ b/buffet/utils.cc
@@ -0,0 +1,55 @@ +// Copyright 2014 The Chromium OS 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 "buffet/utils.h" + +#include <map> +#include <string> + +#include <base/files/file_util.h> +#include <base/json/json_reader.h> +#include <chromeos/errors/error_codes.h> + +namespace buffet { + +const char kErrorDomainBuffet[] = "buffet"; +const char kFileReadError[] = "file_read_error"; + +std::unique_ptr<const base::DictionaryValue> LoadJsonDict( + const base::FilePath& json_file_path, chromeos::ErrorPtr* error) { + std::unique_ptr<const base::DictionaryValue> result; + std::string json_string; + if (!base::ReadFileToString(json_file_path, &json_string)) { + chromeos::errors::system::AddSystemError(error, errno); + chromeos::Error::AddToPrintf(error, kErrorDomainBuffet, + kFileReadError, + "Failed to read file '%s'", + json_file_path.value().c_str()); + return result; + } + std::string error_message; + base::Value* value = base::JSONReader::ReadAndReturnError( + json_string, base::JSON_PARSE_RFC, nullptr, &error_message); + if (!value) { + chromeos::Error::AddToPrintf(error, chromeos::errors::json::kDomain, + chromeos::errors::json::kParseError, + "Error parsing content of JSON file '%s': %s", + json_file_path.value().c_str(), + error_message.c_str()); + return result; + } + const base::DictionaryValue* dict_value = nullptr; + if (!value->GetAsDictionary(&dict_value)) { + delete value; + chromeos::Error::AddToPrintf(error, chromeos::errors::json::kDomain, + chromeos::errors::json::kObjectExpected, + "Content of file '%s' is not a JSON object", + json_file_path.value().c_str()); + return result; + } + result.reset(dict_value); + return result; +} + +} // namespace buffet
diff --git a/buffet/utils.h b/buffet/utils.h new file mode 100644 index 0000000..e71560c --- /dev/null +++ b/buffet/utils.h
@@ -0,0 +1,27 @@ +// Copyright 2014 The Chromium OS 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 BUFFET_UTILS_H_ +#define BUFFET_UTILS_H_ + +#include <memory> + +#include <base/values.h> +#include <base/files/file_path.h> +#include <chromeos/errors/error.h> + +namespace buffet { + +extern const char kErrorDomainBuffet[]; +extern const char kFileReadError[]; + +// Helper function to load a JSON file that is expected to be +// an object/dictionary. In case of error, returns empty unique ptr and fills +// in error details in |error|. +std::unique_ptr<const base::DictionaryValue> LoadJsonDict( + const base::FilePath& json_file_path, chromeos::ErrorPtr* error); + +} // namespace buffet + +#endif // BUFFET_UTILS_H_