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

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 many nits 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/escape.h"
SteveT 2012/05/03 23:46:24 Is it still here?
jwd 2012/05/04 19:33:06 I told you, I'm not good with computers. It should
16 #include "net/base/load_flags.h"
17 #include "net/url_request/url_request_status.h"
18
19 namespace {
20 // Default server of Variations seed info.
21 const char kDefaultVariationsServer[] =
22 "https://clients4.google.com/chrome-variations/seed";
23 };
SteveT 2012/05/03 23:46:24 No semicolons after namespace closing braces, meth
jwd 2012/05/04 19:33:06 Done.
24
25 VariationsService::VariationsService() {}
26 VariationsService::~VariationsService() {}
27
28 void VariationsService::LoadVariationsSeed(PrefService* local_prefs) {
29 std::string base64_seed_data = local_prefs->GetString(prefs::kVariationsSeed);
30 std::string seed_data;
31
32 // If the decode process fails, assume the pref value is corrupt, and clear
33 // it.
SteveT 2012/05/03 23:46:24 With that in mind, it's actually kind of weird tha
34 if (!(base::Base64Decode(base64_seed_data, &seed_data) &&
Alexei Svitkine (slow) 2012/05/04 03:09:53 Shouldn't you check for empty string first, since
jwd 2012/05/04 19:33:06 The empty string does parse. I figured it was beca
35 variations_seed_.ParseFromString(seed_data))) {
Alexei Svitkine (slow) 2012/05/04 03:09:53 Nit: Change if (!(a && b)) to if (!a || !b).
jwd 2012/05/04 19:33:06 Done.
36 local_prefs->ClearPref(prefs::kVariationsSeed);
MAD 2012/05/04 12:29:57 I would add a DCHECK here or at least a VLOG(1)...
jwd 2012/05/04 19:33:06 Done.
37 }
38 }
39
40 void VariationsService::StartFetchingVariationsSeed() {
41 pending_seed_request_.reset(content::URLFetcher::Create(
42 GURL(kDefaultVariationsServer), content::URLFetcher::GET, this));
43 pending_seed_request_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES |
44 net::LOAD_DO_NOT_SAVE_COOKIES);
45 pending_seed_request_->SetRequestContext(
46 g_browser_process->system_request_context());
47 pending_seed_request_->SetMaxRetries(5);
48 pending_seed_request_->Start();
49 }
50
51 void VariationsService::OnURLFetchComplete(const content::URLFetcher* source) {
52 if (pending_seed_request_.get() == source) {
53 // When we're done handling the request, the fetcher will be deleted.
54 scoped_ptr<const content::URLFetcher> current_request(
Alexei Svitkine (slow) 2012/05/04 03:09:53 Nit: rename to |request|. Then you can have less
jwd 2012/05/04 19:33:06 Done.
55 pending_seed_request_.release());
56 if (current_request->GetStatus().status() !=
57 net::URLRequestStatus::SUCCESS ||
58 current_request->GetResponseCode() != 200)
59 return;
60
61 std::string seed_data;
62 current_request->GetResponseAsString(&seed_data);
63
64 StoreSeedData(seed_data, g_browser_process->local_state());
65 }
jwd 2012/05/03 22:27:49 I'm not sure if this function should be responsibl
MAD 2012/05/04 20:53:12 You could add a DCHECK that you received the one y
jwd 2012/05/04 22:11:23 Done.
66 }
67
68 // static
69 void VariationsService::RegisterPrefs(PrefService* prefs) {
70 prefs->RegisterStringPref(prefs::kVariationsSeed, std::string());
71 }
72
73 void VariationsService::StoreSeedData(const std::string& seed_data,
74 PrefService* local_prefs) {
75 // Only store the seed data if it parses correctly.
76 chrome_variations::TrialsSeed seed;
77 if (seed.ParseFromString(seed_data)){
Alexei Svitkine (slow) 2012/05/04 03:09:53 Nit: space before {
Alexei Svitkine (slow) 2012/05/04 03:09:53 Nit: do an early return here too instead of a nest
jwd 2012/05/04 19:33:06 Done.
78 std::string base64_seed_data;
79 if (!base::Base64Encode(seed_data, &base64_seed_data))
80 return;
MAD 2012/05/04 12:29:57 Please add VLOG(1) where we fail to do save...
jwd 2012/05/04 19:33:06 Done.
81
82 local_prefs->SetString(prefs::kVariationsSeed, base64_seed_data);
83 }
84 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698