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

Side by Side Diff: remoting/host/plugin/policy_hack/json_value_serializer.cc

Issue 7599017: Framework to allow Chromoting host to respect NAT traversal policy in linux. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: silly compiler. Created 9 years, 4 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 | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 // Forked from content/common/json_value_serializer.h
6
7 #include "remoting/host/plugin/policy_hack/json_value_serializer.h"
8
9 #include "base/file_util.h"
10 #include "base/json/json_reader.h"
11 #include "base/json/json_writer.h"
12 #include "base/string_util.h"
13
14 namespace remoting {
15 namespace policy_hack {
16
17 const char* JSONFileValueSerializer::kAccessDenied = "Access denied.";
18 const char* JSONFileValueSerializer::kCannotReadFile = "Can't read file.";
19 const char* JSONFileValueSerializer::kFileLocked = "File locked.";
20 const char* JSONFileValueSerializer::kNoSuchFile = "File doesn't exist.";
21
22 JSONStringValueSerializer::~JSONStringValueSerializer() {}
23
24 bool JSONStringValueSerializer::Serialize(const Value& root) {
25 if (!json_string_ || initialized_with_const_string_)
26 return false;
27
28 base::JSONWriter::Write(&root, pretty_print_, json_string_);
29 return true;
30 }
31
32 Value* JSONStringValueSerializer::Deserialize(int* error_code,
33 std::string* error_str) {
34 if (!json_string_)
35 return NULL;
36
37 return base::JSONReader::ReadAndReturnError(*json_string_,
38 allow_trailing_comma_,
39 error_code,
40 error_str);
41 }
42
43 /******* File Serializer *******/
44
45 bool JSONFileValueSerializer::Serialize(const Value& root) {
46 std::string json_string;
47 JSONStringValueSerializer serializer(&json_string);
48 serializer.set_pretty_print(true);
49 bool result = serializer.Serialize(root);
50 if (!result)
51 return false;
52
53 int data_size = static_cast<int>(json_string.size());
54 if (file_util::WriteFile(json_file_path_,
55 json_string.data(),
56 data_size) != data_size)
57 return false;
58
59 return true;
60 }
61
62 int JSONFileValueSerializer::ReadFileToString(std::string* json_string) {
63 DCHECK(json_string);
64 if (!file_util::ReadFileToString(json_file_path_, json_string)) {
65 #if defined(OS_WIN)
66 int error = ::GetLastError();
67 if (error == ERROR_SHARING_VIOLATION || error == ERROR_LOCK_VIOLATION) {
68 return JSON_FILE_LOCKED;
69 } else if (error == ERROR_ACCESS_DENIED) {
70 return JSON_ACCESS_DENIED;
71 }
72 #endif
73 if (!file_util::PathExists(json_file_path_))
74 return JSON_NO_SUCH_FILE;
75 else
76 return JSON_CANNOT_READ_FILE;
77 }
78 return JSON_NO_ERROR;
79 }
80
81 const char* JSONFileValueSerializer::GetErrorMessageForCode(int error_code) {
82 switch (error_code) {
83 case JSON_NO_ERROR:
84 return "";
85 case JSON_ACCESS_DENIED:
86 return kAccessDenied;
87 case JSON_CANNOT_READ_FILE:
88 return kCannotReadFile;
89 case JSON_FILE_LOCKED:
90 return kFileLocked;
91 case JSON_NO_SUCH_FILE:
92 return kNoSuchFile;
93 default:
94 NOTREACHED();
95 return "";
96 }
97 }
98
99 Value* JSONFileValueSerializer::Deserialize(int* error_code,
100 std::string* error_str) {
101 std::string json_string;
102 int error = ReadFileToString(&json_string);
103 if (error != JSON_NO_ERROR) {
104 if (error_code)
105 *error_code = error;
106 if (error_str)
107 *error_str = GetErrorMessageForCode(error);
108 return NULL;
109 }
110
111 JSONStringValueSerializer serializer(json_string);
112 return serializer.Deserialize(error_code, error_str);
113 }
114
115 } // namespace policy_hack
116 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698