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

Side by Side Diff: chrome/browser/metrics/variations_service.cc

Issue 10343007: Retrieving Chrome Variations seed from server and storing in local prefs. Loading seed data from lo… (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixing more nits from everyone! Created 8 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/metrics/variations_service.h"
6
7 #include "base/base64.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "chrome/browser/browser_process.h"
10 #include "chrome/browser/metrics/proto/trials_seed.pb.h"
11 #include "chrome/browser/prefs/pref_service.h"
12 #include "chrome/common/pref_names.h"
13 #include "content/public/common/url_fetcher.h"
14 #include "googleurl/src/gurl.h"
15 #include "net/base/load_flags.h"
16 #include "net/url_request/url_request_status.h"
17
18 namespace {
19 // Default server of Variations seed info.
MAD 2012/05/04 20:53:12 We usually put a newline at the beginning and end
jwd 2012/05/04 22:11:23 Done.
20 const char kDefaultVariationsServer[] =
21 "https://clients4.google.com/chrome-variations/seed";
22 }
Alexei Svitkine (slow) 2012/05/04 19:39:33 Nit: Change to "} // namespace"
jwd 2012/05/04 22:11:23 Done.
23
24 VariationsService::VariationsService() {}
25 VariationsService::~VariationsService() {}
26
27 void VariationsService::LoadVariationsSeed(PrefService* local_prefs) {
28 std::string base64_seed_data = local_prefs->GetString(prefs::kVariationsSeed);
29 std::string seed_data;
30
31 // If the decode process fails, assume the pref value is corrupt, and clear
32 // it.
33 if (!base::Base64Decode(base64_seed_data, &seed_data) ||
34 !variations_seed_.ParseFromString(seed_data)) {
Alexei Svitkine (slow) 2012/05/04 19:39:33 Nit: This should be aligned.
jwd 2012/05/04 22:11:23 Done.
35 VLOG(1) << "Variations Seed data in local pref is corrupt, clearing the "
36 << "pref.";
37 local_prefs->ClearPref(prefs::kVariationsSeed);
38 }
39 }
40
41 void VariationsService::StartFetchingVariationsSeed() {
42 pending_seed_request_.reset(content::URLFetcher::Create(
43 GURL(kDefaultVariationsServer), content::URLFetcher::GET, this));
44 pending_seed_request_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES |
45 net::LOAD_DO_NOT_SAVE_COOKIES);
46 pending_seed_request_->SetRequestContext(
47 g_browser_process->system_request_context());
48 pending_seed_request_->SetMaxRetries(5);
49 pending_seed_request_->Start();
50 }
51
52 void VariationsService::OnURLFetchComplete(const content::URLFetcher* source) {
53 if (pending_seed_request_.get() == source) {
54 // When we're done handling the request, the fetcher will be deleted.
55 scoped_ptr<const content::URLFetcher> request(
56 pending_seed_request_.release());
57 if (request->GetStatus().status() !=
58 net::URLRequestStatus::SUCCESS ||
Alexei Svitkine (slow) 2012/05/04 19:39:33 This can go on the previous line.
jwd 2012/05/04 22:11:23 Done.
59 request->GetResponseCode() != 200)
60 return;
61
62 std::string seed_data;
63 request->GetResponseAsString(&seed_data);
64
65 StoreSeedData(seed_data, g_browser_process->local_state());
66 }
67 }
68
69 // static
70 void VariationsService::RegisterPrefs(PrefService* prefs) {
71 prefs->RegisterStringPref(prefs::kVariationsSeed, std::string());
72 }
73
74 void VariationsService::StoreSeedData(const std::string& seed_data,
75 PrefService* local_prefs) {
76 // Only store the seed data if it parses correctly.
77 chrome_variations::TrialsSeed seed;
78 if (!seed.ParseFromString(seed_data)) {
79 VLOG(1) << "Variations Seed data from server is not in valid proto format, "
80 << "rejecting the seed.";
81 return;
82 }
83 std::string base64_seed_data;
84 if (!base::Base64Encode(seed_data, &base64_seed_data)) {
85 VLOG(1) << "Variations Seed data from server fails Base64Encode, rejecting "
86 << "the seed.";
87 return;
88 }
89 local_prefs->SetString(prefs::kVariationsSeed, base64_seed_data);
90 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698