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

Side by Side Diff: rlz/lib/rlz_lib.cc

Issue 2857343004: Add rlz_utils as a new build target to rlz/ (Closed)
Patch Set: build.gn fix Created 3 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
« no previous file with comments | « rlz/lib/rlz_lib.h ('k') | rlz/lib/rlz_lib_test.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) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 // A library to manage RLZ information for access-points shared 5 // A library to manage RLZ information for access-points shared
6 // across different client applications. 6 // across different client applications.
7 7
8 #include "rlz/lib/rlz_lib.h" 8 #include "rlz/lib/rlz_lib.h"
9 9
10 #include <algorithm> 10 #include <algorithm>
11 11
12 #include "base/macros.h" 12 #include "base/macros.h"
13 #include "base/strings/string_util.h" 13 #include "base/strings/string_util.h"
14 #include "base/strings/stringprintf.h" 14 #include "base/strings/stringprintf.h"
15 #include "build/build_config.h" 15 #include "build/build_config.h"
16 #include "rlz/lib/assert.h" 16 #include "rlz/lib/assert.h"
17 #include "rlz/lib/crc32.h"
18 #include "rlz/lib/financial_ping.h" 17 #include "rlz/lib/financial_ping.h"
19 #include "rlz/lib/lib_values.h" 18 #include "rlz/lib/lib_values.h"
19 #include "rlz/lib/net_response_check.h"
20 #include "rlz/lib/rlz_value_store.h" 20 #include "rlz/lib/rlz_value_store.h"
21 #include "rlz/lib/string_utils.h" 21 #include "rlz/lib/string_utils.h"
22 22
23 namespace { 23 namespace {
24 24
25 // Event information returned from ping response. 25 // Event information returned from ping response.
26 struct ReturnedEvent { 26 struct ReturnedEvent {
27 rlz_lib::AccessPoint access_point; 27 rlz_lib::AccessPoint access_point;
28 rlz_lib::Event event_type; 28 rlz_lib::Event event_type;
29 }; 29 };
(...skipping 340 matching lines...) Expand 10 before | Expand all | Expand 10 after
370 return false; 370 return false;
371 371
372 if (request_string.size() >= request_buffer_size) 372 if (request_string.size() >= request_buffer_size)
373 return false; 373 return false;
374 374
375 strncpy(request, request_string.c_str(), request_buffer_size); 375 strncpy(request, request_string.c_str(), request_buffer_size);
376 request[request_buffer_size - 1] = 0; 376 request[request_buffer_size - 1] = 0;
377 return true; 377 return true;
378 } 378 }
379 379
380 bool IsPingResponseValid(const char* response, int* checksum_idx) {
381 if (!response || !response[0])
382 return false;
383
384 if (checksum_idx)
385 *checksum_idx = -1;
386
387 if (strlen(response) > kMaxPingResponseLength) {
388 ASSERT_STRING("IsPingResponseValid: response is too long to parse.");
389 return false;
390 }
391
392 // Find the checksum line.
393 std::string response_string(response);
394
395 std::string checksum_param("\ncrc32: ");
396 int calculated_crc;
397 int checksum_index = response_string.find(checksum_param);
398 if (checksum_index >= 0) {
399 // Calculate checksum of message preceeding checksum line.
400 // (+ 1 to include the \n)
401 std::string message(response_string.substr(0, checksum_index + 1));
402 if (!Crc32(message.c_str(), &calculated_crc))
403 return false;
404 } else {
405 checksum_param = "crc32: "; // Empty response case.
406 if (!base::StartsWith(response_string, checksum_param,
407 base::CompareCase::SENSITIVE))
408 return false;
409
410 checksum_index = 0;
411 if (!Crc32("", &calculated_crc))
412 return false;
413 }
414
415 // Find the checksum value on the response.
416 int checksum_end = response_string.find("\n", checksum_index + 1);
417 if (checksum_end < 0)
418 checksum_end = response_string.size();
419
420 int checksum_begin = checksum_index + checksum_param.size();
421 std::string checksum = response_string.substr(checksum_begin,
422 checksum_end - checksum_begin + 1);
423 base::TrimWhitespaceASCII(checksum, base::TRIM_ALL, &checksum);
424
425 if (checksum_idx)
426 *checksum_idx = checksum_index;
427
428 return calculated_crc == HexStringToInteger(checksum.c_str());
429 }
430
431 // Complex helpers built on top of other functions. 380 // Complex helpers built on top of other functions.
432 381
433 bool ParseFinancialPingResponse(Product product, const char* response) { 382 bool ParseFinancialPingResponse(Product product, const char* response) {
434 // Update the last ping time irrespective of success. 383 // Update the last ping time irrespective of success.
435 FinancialPing::UpdateLastPingTime(product); 384 FinancialPing::UpdateLastPingTime(product);
436 // Parse the ping response - update RLZs, clear events. 385 // Parse the ping response - update RLZs, clear events.
437 return ParsePingResponse(product, response); 386 return ParsePingResponse(product, response);
438 } 387 }
439 388
440 bool SendFinancialPing(Product product, const AccessPoint* access_points, 389 bool SendFinancialPing(Product product, const AccessPoint* access_points,
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after
627 if (cgi_string.size() >= cgi_size) 576 if (cgi_string.size() >= cgi_size)
628 return false; 577 return false;
629 578
630 strncpy(cgi, cgi_string.c_str(), cgi_size); 579 strncpy(cgi, cgi_string.c_str(), cgi_size);
631 cgi[cgi_size - 1] = 0; 580 cgi[cgi_size - 1] = 0;
632 581
633 return true; 582 return true;
634 } 583 }
635 584
636 } // namespace rlz_lib 585 } // namespace rlz_lib
OLDNEW
« no previous file with comments | « rlz/lib/rlz_lib.h ('k') | rlz/lib/rlz_lib_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698