Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(62)

Side by Side Diff: chrome/common/json_value_serializer.cc

Issue 1120006: detect preferences errors (Closed)
Patch Set: changes from review Created 10 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/common/json_value_serializer.h ('k') | chrome/common/json_value_serializer_perftest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/common/json_value_serializer.h" 5 #include "chrome/common/json_value_serializer.h"
6 6
7 #include "base/file_util.h" 7 #include "base/file_util.h"
8 #include "base/json/json_reader.h" 8 #include "base/json/json_reader.h"
9 #include "base/json/json_writer.h" 9 #include "base/json/json_writer.h"
10 #include "base/string_util.h" 10 #include "base/string_util.h"
11 11
12 const char* JSONFileValueSerializer::kAccessDenied = "Access denied.";
13 const char* JSONFileValueSerializer::kCannotReadFile = "Can't read file.";
14 const char* JSONFileValueSerializer::kFileLocked = "File locked.";
15 const char* JSONFileValueSerializer::kNoSuchFile = "File doesn't exist.";
16
12 JSONStringValueSerializer::~JSONStringValueSerializer() {} 17 JSONStringValueSerializer::~JSONStringValueSerializer() {}
13 18
14 bool JSONStringValueSerializer::Serialize(const Value& root) { 19 bool JSONStringValueSerializer::Serialize(const Value& root) {
15 if (!json_string_ || initialized_with_const_string_) 20 if (!json_string_ || initialized_with_const_string_)
16 return false; 21 return false;
17 22
18 base::JSONWriter::Write(&root, pretty_print_, json_string_); 23 base::JSONWriter::Write(&root, pretty_print_, json_string_);
19 return true; 24 return true;
20 } 25 }
21 26
22 Value* JSONStringValueSerializer::Deserialize(std::string* error_message) { 27 Value* JSONStringValueSerializer::Deserialize(int* error_code,
28 std::string* error_str) {
23 if (!json_string_) 29 if (!json_string_)
24 return NULL; 30 return NULL;
25 31
26 return base::JSONReader::ReadAndReturnError(*json_string_, 32 return base::JSONReader::ReadAndReturnError(*json_string_,
27 allow_trailing_comma_, 33 allow_trailing_comma_,
28 error_message); 34 error_code,
35 error_str);
29 } 36 }
30 37
31 /******* File Serializer *******/ 38 /******* File Serializer *******/
32 39
33 bool JSONFileValueSerializer::Serialize(const Value& root) { 40 bool JSONFileValueSerializer::Serialize(const Value& root) {
34 std::string json_string; 41 std::string json_string;
35 JSONStringValueSerializer serializer(&json_string); 42 JSONStringValueSerializer serializer(&json_string);
36 serializer.set_pretty_print(true); 43 serializer.set_pretty_print(true);
37 bool result = serializer.Serialize(root); 44 bool result = serializer.Serialize(root);
38 if (!result) 45 if (!result)
39 return false; 46 return false;
40 47
41 int data_size = static_cast<int>(json_string.size()); 48 int data_size = static_cast<int>(json_string.size());
42 if (file_util::WriteFile(json_file_path_, 49 if (file_util::WriteFile(json_file_path_,
43 json_string.data(), 50 json_string.data(),
44 data_size) != data_size) 51 data_size) != data_size)
45 return false; 52 return false;
46 53
47 return true; 54 return true;
48 } 55 }
49 56
50 Value* JSONFileValueSerializer::Deserialize(std::string* error_message) { 57 int JSONFileValueSerializer::ReadFileToString(std::string* json_string) {
58 DCHECK(json_string);
59 if (!file_util::ReadFileToString(json_file_path_, json_string)) {
60 #if defined(OS_WIN)
61 int error = ::GetLastError();
62 if (error == ERROR_SHARING_VIOLATION || error == ERROR_LOCK_VIOLATION) {
63 return JSON_FILE_LOCKED;
64 } else if (error == ERROR_ACCESS_DENIED) {
65 return JSON_ACCESS_DENIED;
66 }
67 #endif
68 if (!file_util::PathExists(json_file_path_))
69 return JSON_NO_SUCH_FILE;
70 else
71 return JSON_CANNOT_READ_FILE;
72 }
73 return JSON_NO_ERROR;
74 }
75
76 const char* JSONFileValueSerializer::GetErrorMessageForCode(int error_code) {
77 switch (error_code) {
78 case JSON_NO_ERROR:
79 return "";
80 case JSON_ACCESS_DENIED:
81 return kAccessDenied;
82 case JSON_CANNOT_READ_FILE:
83 return kCannotReadFile;
84 case JSON_FILE_LOCKED:
85 return kFileLocked;
86 case JSON_NO_SUCH_FILE:
87 return kNoSuchFile;
88 default:
89 NOTREACHED();
90 return "";
91 }
92 }
93
94 Value* JSONFileValueSerializer::Deserialize(int* error_code,
95 std::string* error_str) {
51 std::string json_string; 96 std::string json_string;
52 if (!file_util::ReadFileToString(json_file_path_, &json_string)) { 97 int error = ReadFileToString(&json_string);
98 if (error != JSON_NO_ERROR) {
99 if (error_code)
100 *error_code = error;
101 if (error_str)
102 *error_str = GetErrorMessageForCode(error);
53 return NULL; 103 return NULL;
54 } 104 }
105
55 JSONStringValueSerializer serializer(json_string); 106 JSONStringValueSerializer serializer(json_string);
56 return serializer.Deserialize(error_message); 107 return serializer.Deserialize(error_code, error_str);
57 } 108 }
OLDNEW
« no previous file with comments | « chrome/common/json_value_serializer.h ('k') | chrome/common/json_value_serializer_perftest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698