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

Side by Side Diff: net/tools/tld_cleanup/tld_cleanup_util.cc

Issue 266243004: Clang format slam. Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 7 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
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 "net/tools/tld_cleanup/tld_cleanup_util.h" 5 #include "net/tools/tld_cleanup/tld_cleanup_util.h"
6 6
7 #include "base/file_util.h" 7 #include "base/file_util.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/strings/string_number_conversions.h" 9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/string_util.h" 10 #include "base/strings/string_util.h"
(...skipping 11 matching lines...) Expand all
22 } 22 }
23 23
24 namespace net { 24 namespace net {
25 namespace tld_cleanup { 25 namespace tld_cleanup {
26 26
27 // Writes the list of domain rules contained in the 'rules' set to the 27 // Writes the list of domain rules contained in the 'rules' set to the
28 // 'outfile', with each rule terminated by a LF. The file must already have 28 // 'outfile', with each rule terminated by a LF. The file must already have
29 // been created with write access. 29 // been created with write access.
30 bool WriteRules(const RuleMap& rules, const base::FilePath& outfile) { 30 bool WriteRules(const RuleMap& rules, const base::FilePath& outfile) {
31 std::string data; 31 std::string data;
32 data.append("%{\n" 32 data.append(
33 "// Copyright 2012 The Chromium Authors. All rights reserved.\n" 33 "%{\n"
34 "// Use of this source code is governed by a BSD-style license " 34 "// Copyright 2012 The Chromium Authors. All rights reserved.\n"
35 "that can be\n" 35 "// Use of this source code is governed by a BSD-style license "
36 "// found in the LICENSE file.\n\n" 36 "that can be\n"
37 "// This file is generated by net/tools/tld_cleanup/.\n" 37 "// found in the LICENSE file.\n\n"
38 "// DO NOT MANUALLY EDIT!\n" 38 "// This file is generated by net/tools/tld_cleanup/.\n"
39 "%}\n" 39 "// DO NOT MANUALLY EDIT!\n"
40 "struct DomainRule {\n" 40 "%}\n"
41 " int name_offset;\n" 41 "struct DomainRule {\n"
42 " int type; // flags: 1: exception, 2: wildcard, 4: private\n" 42 " int name_offset;\n"
43 "};\n" 43 " int type; // flags: 1: exception, 2: wildcard, 4: private\n"
44 "%%\n"); 44 "};\n"
45 "%%\n");
45 46
46 for (RuleMap::const_iterator i = rules.begin(); i != rules.end(); ++i) { 47 for (RuleMap::const_iterator i = rules.begin(); i != rules.end(); ++i) {
47 data.append(i->first); 48 data.append(i->first);
48 data.append(", "); 49 data.append(", ");
49 int type = 0; 50 int type = 0;
50 if (i->second.exception) { 51 if (i->second.exception) {
51 type = kExceptionRule; 52 type = kExceptionRule;
52 } else if (i->second.wildcard) { 53 } else if (i->second.wildcard) {
53 type = kWildcardRule; 54 type = kWildcardRule;
54 } 55 }
55 if (i->second.is_private) { 56 if (i->second.is_private) {
56 type += kPrivateRule; 57 type += kPrivateRule;
57 } 58 }
58 data.append(base::IntToString(type)); 59 data.append(base::IntToString(type));
59 data.append("\n"); 60 data.append("\n");
60 } 61 }
61 62
62 data.append("%%\n"); 63 data.append("%%\n");
63 64
64 int written = base::WriteFile(outfile, 65 int written =
65 data.data(), 66 base::WriteFile(outfile, data.data(), static_cast<int>(data.size()));
66 static_cast<int>(data.size()));
67 67
68 return written == static_cast<int>(data.size()); 68 return written == static_cast<int>(data.size());
69 } 69 }
70 70
71 // Adjusts the rule to a standard form: removes single extraneous dots and 71 // Adjusts the rule to a standard form: removes single extraneous dots and
72 // canonicalizes it using GURL. Returns kSuccess if the rule is interpreted as 72 // canonicalizes it using GURL. Returns kSuccess if the rule is interpreted as
73 // valid; logs a warning and returns kWarning if it is probably invalid; and 73 // valid; logs a warning and returns kWarning if it is probably invalid; and
74 // logs an error and returns kError if the rule is (almost) certainly invalid. 74 // logs an error and returns kError if the rule is (almost) certainly invalid.
75 NormalizeResult NormalizeRule(std::string* domain, Rule* rule) { 75 NormalizeResult NormalizeRule(std::string* domain, Rule* rule) {
76 NormalizeResult result = kSuccess; 76 NormalizeResult result = kSuccess;
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
122 } 122 }
123 if (!gurl.is_valid()) { 123 if (!gurl.is_valid()) {
124 LOG(WARNING) << "Keeping rule that GURL says is invalid: " << *domain; 124 LOG(WARNING) << "Keeping rule that GURL says is invalid: " << *domain;
125 result = kWarning; 125 result = kWarning;
126 } 126 }
127 domain->assign(spec.substr(host.begin, host.len)); 127 domain->assign(spec.substr(host.begin, host.len));
128 128
129 return result; 129 return result;
130 } 130 }
131 131
132 NormalizeResult NormalizeDataToRuleMap(const std::string data, 132 NormalizeResult NormalizeDataToRuleMap(const std::string data, RuleMap* rules) {
133 RuleMap* rules) {
134 CHECK(rules); 133 CHECK(rules);
135 // We do a lot of string assignment during parsing, but simplicity is more 134 // We do a lot of string assignment during parsing, but simplicity is more
136 // important than performance here. 135 // important than performance here.
137 std::string domain; 136 std::string domain;
138 NormalizeResult result = kSuccess; 137 NormalizeResult result = kSuccess;
139 size_t line_start = 0; 138 size_t line_start = 0;
140 size_t line_end = 0; 139 size_t line_end = 0;
141 bool is_private = false; 140 bool is_private = false;
142 RuleMap extra_rules; 141 RuleMap extra_rules;
143 int begin_private_length = arraysize(kBeginPrivateDomainsComment) - 1; 142 int begin_private_length = arraysize(kBeginPrivateDomainsComment) - 1;
144 int end_private_length = arraysize(kEndPrivateDomainsComment) - 1; 143 int end_private_length = arraysize(kEndPrivateDomainsComment) - 1;
145 while (line_start < data.size()) { 144 while (line_start < data.size()) {
146 if (line_start + begin_private_length < data.size() && 145 if (line_start + begin_private_length < data.size() &&
147 !data.compare(line_start, begin_private_length, 146 !data.compare(
148 kBeginPrivateDomainsComment)) { 147 line_start, begin_private_length, kBeginPrivateDomainsComment)) {
149 is_private = true; 148 is_private = true;
150 line_end = line_start + begin_private_length; 149 line_end = line_start + begin_private_length;
151 } else if (line_start + end_private_length < data.size() && 150 } else if (line_start + end_private_length < data.size() &&
152 !data.compare(line_start, end_private_length, 151 !data.compare(
153 kEndPrivateDomainsComment)) { 152 line_start, end_private_length, kEndPrivateDomainsComment)) {
154 is_private = false; 153 is_private = false;
155 line_end = line_start + end_private_length; 154 line_end = line_start + end_private_length;
156 } else if (line_start + 1 < data.size() && 155 } else if (line_start + 1 < data.size() && data[line_start] == '/' &&
157 data[line_start] == '/' && 156 data[line_start + 1] == '/') {
158 data[line_start + 1] == '/') {
159 // Skip comments. 157 // Skip comments.
160 line_end = data.find_first_of("\r\n", line_start); 158 line_end = data.find_first_of("\r\n", line_start);
161 if (line_end == std::string::npos) 159 if (line_end == std::string::npos)
162 line_end = data.size(); 160 line_end = data.size();
163 } else { 161 } else {
164 // Truncate at first whitespace. 162 // Truncate at first whitespace.
165 line_end = data.find_first_of("\r\n \t", line_start); 163 line_end = data.find_first_of("\r\n \t", line_start);
166 if (line_end == std::string::npos) 164 if (line_end == std::string::npos)
167 line_end = data.size(); 165 line_end = data.size();
168 domain.assign(data.data(), line_start, line_end - line_start); 166 domain.assign(data.data(), line_start, line_end - line_start);
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
243 NormalizeResult result = NormalizeDataToRuleMap(data, &rules); 241 NormalizeResult result = NormalizeDataToRuleMap(data, &rules);
244 242
245 if (!WriteRules(rules, out_filename)) { 243 if (!WriteRules(rules, out_filename)) {
246 LOG(ERROR) << "Error(s) writing output file"; 244 LOG(ERROR) << "Error(s) writing output file";
247 result = kError; 245 result = kError;
248 } 246 }
249 247
250 return result; 248 return result;
251 } 249 }
252 250
253
254 } // namespace tld_cleanup 251 } // namespace tld_cleanup
255 } // namespace net 252 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698