OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "remoting/test/refresh_token_store.h" | 5 #include "remoting/test/refresh_token_store.h" |
6 | 6 |
7 #include "base/files/file_util.h" | 7 #include "base/files/file_util.h" |
8 #include "base/files/important_file_writer.h" | 8 #include "base/files/important_file_writer.h" |
9 #include "base/json/json_reader.h" | 9 #include "base/json/json_reader.h" |
10 #include "base/json/json_writer.h" | 10 #include "base/json/json_writer.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/memory/ptr_util.h" |
13 #include "base/values.h" | 14 #include "base/values.h" |
14 | 15 |
15 namespace { | 16 namespace { |
16 const base::FilePath::CharType kTokenFileName[] = | 17 const base::FilePath::CharType kTokenFileName[] = |
17 FILE_PATH_LITERAL("refresh_tokens.json"); | 18 FILE_PATH_LITERAL("refresh_tokens.json"); |
18 const base::FilePath::CharType kRemotingFolder[] = | 19 const base::FilePath::CharType kRemotingFolder[] = |
19 FILE_PATH_LITERAL("remoting"); | 20 FILE_PATH_LITERAL("remoting"); |
20 const base::FilePath::CharType kRefreshTokenStoreFolder[] = | 21 const base::FilePath::CharType kRefreshTokenStoreFolder[] = |
21 FILE_PATH_LITERAL("token_store"); | 22 FILE_PATH_LITERAL("token_store"); |
22 } // namespace | 23 } // namespace |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 base::FilePath refresh_token_file_path(GetPathForRefreshTokenFile()); | 65 base::FilePath refresh_token_file_path(GetPathForRefreshTokenFile()); |
65 DCHECK(!refresh_token_file_path.empty()); | 66 DCHECK(!refresh_token_file_path.empty()); |
66 VLOG(1) << "Reading token from: " << refresh_token_file_path.value(); | 67 VLOG(1) << "Reading token from: " << refresh_token_file_path.value(); |
67 | 68 |
68 std::string file_contents; | 69 std::string file_contents; |
69 if (!base::ReadFileToString(refresh_token_file_path, &file_contents)) { | 70 if (!base::ReadFileToString(refresh_token_file_path, &file_contents)) { |
70 VLOG(1) << "Couldn't read token file: " << refresh_token_file_path.value(); | 71 VLOG(1) << "Couldn't read token file: " << refresh_token_file_path.value(); |
71 return std::string(); | 72 return std::string(); |
72 } | 73 } |
73 | 74 |
74 scoped_ptr<base::Value> token_data(base::JSONReader::Read(file_contents)); | 75 std::unique_ptr<base::Value> token_data( |
| 76 base::JSONReader::Read(file_contents)); |
75 base::DictionaryValue* tokens = nullptr; | 77 base::DictionaryValue* tokens = nullptr; |
76 if (!token_data || !token_data->GetAsDictionary(&tokens)) { | 78 if (!token_data || !token_data->GetAsDictionary(&tokens)) { |
77 LOG(ERROR) << "Refresh token file contents were not valid JSON, " | 79 LOG(ERROR) << "Refresh token file contents were not valid JSON, " |
78 << "could not retrieve token."; | 80 << "could not retrieve token."; |
79 return std::string(); | 81 return std::string(); |
80 } | 82 } |
81 | 83 |
82 std::string refresh_token; | 84 std::string refresh_token; |
83 if (!tokens->GetStringWithoutPathExpansion(user_name_, &refresh_token)) { | 85 if (!tokens->GetStringWithoutPathExpansion(user_name_, &refresh_token)) { |
84 // This may not be an error as the file could exist but contain refresh | 86 // This may not be an error as the file could exist but contain refresh |
(...skipping 21 matching lines...) Expand all Loading... |
106 } | 108 } |
107 | 109 |
108 std::string file_contents("{}"); | 110 std::string file_contents("{}"); |
109 if (base::PathExists(file_path)) { | 111 if (base::PathExists(file_path)) { |
110 if (!base::ReadFileToString(file_path, &file_contents)) { | 112 if (!base::ReadFileToString(file_path, &file_contents)) { |
111 LOG(ERROR) << "Invalid token file: " << file_path.value(); | 113 LOG(ERROR) << "Invalid token file: " << file_path.value(); |
112 return false; | 114 return false; |
113 } | 115 } |
114 } | 116 } |
115 | 117 |
116 scoped_ptr<base::Value> token_data(base::JSONReader::Read(file_contents)); | 118 std::unique_ptr<base::Value> token_data( |
| 119 base::JSONReader::Read(file_contents)); |
117 base::DictionaryValue* tokens = nullptr; | 120 base::DictionaryValue* tokens = nullptr; |
118 if (!token_data || !token_data->GetAsDictionary(&tokens)) { | 121 if (!token_data || !token_data->GetAsDictionary(&tokens)) { |
119 LOG(ERROR) << "Invalid refresh token file format, could not store token."; | 122 LOG(ERROR) << "Invalid refresh token file format, could not store token."; |
120 return false; | 123 return false; |
121 } | 124 } |
122 | 125 |
123 std::string json_string; | 126 std::string json_string; |
124 tokens->SetStringWithoutPathExpansion(user_name_, refresh_token); | 127 tokens->SetStringWithoutPathExpansion(user_name_, refresh_token); |
125 if (!base::JSONWriter::Write(*token_data, &json_string)) { | 128 if (!base::JSONWriter::Write(*token_data, &json_string)) { |
126 LOG(ERROR) << "Couldn't convert JSON data to string"; | 129 LOG(ERROR) << "Couldn't convert JSON data to string"; |
(...skipping 24 matching lines...) Expand all Loading... |
151 } | 154 } |
152 | 155 |
153 // If no file has been specified, then we will use a default file name. | 156 // If no file has been specified, then we will use a default file name. |
154 if (refresh_token_file_path.Extension().empty()) { | 157 if (refresh_token_file_path.Extension().empty()) { |
155 refresh_token_file_path = refresh_token_file_path.Append(kTokenFileName); | 158 refresh_token_file_path = refresh_token_file_path.Append(kTokenFileName); |
156 } | 159 } |
157 | 160 |
158 return refresh_token_file_path; | 161 return refresh_token_file_path; |
159 } | 162 } |
160 | 163 |
161 scoped_ptr<RefreshTokenStore> RefreshTokenStore::OnDisk( | 164 std::unique_ptr<RefreshTokenStore> RefreshTokenStore::OnDisk( |
162 const std::string& user_name, | 165 const std::string& user_name, |
163 const base::FilePath& refresh_token_file_path) { | 166 const base::FilePath& refresh_token_file_path) { |
164 return make_scoped_ptr<RefreshTokenStore>( | 167 return base::WrapUnique<RefreshTokenStore>( |
165 new RefreshTokenStoreOnDisk(user_name, refresh_token_file_path)); | 168 new RefreshTokenStoreOnDisk(user_name, refresh_token_file_path)); |
166 } | 169 } |
167 | 170 |
168 } // namespace test | 171 } // namespace test |
169 } // namespace remoting | 172 } // namespace remoting |
OLD | NEW |