OLD | NEW |
(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 "base/memory/singleton.h" |
| 10 #include "chrome/browser/browser_process.h" |
| 11 #include "chrome/browser/metrics/proto/trials_seed.pb.h" |
| 12 #include "chrome/browser/prefs/pref_service.h" |
| 13 #include "chrome/common/pref_names.h" |
| 14 #include "content/public/common/url_fetcher.h" |
| 15 #include "googleurl/src/gurl.h" |
| 16 #include "net/base/load_flags.h" |
| 17 #include "net/url_request/url_request_status.h" |
| 18 |
| 19 namespace { |
| 20 |
| 21 // Default server of Variations seed info. |
| 22 const char kDefaultVariationsServer[] = |
| 23 "https://clients4.google.com/chrome-variations/seed"; |
| 24 |
| 25 } // namespace |
| 26 |
| 27 // Static |
| 28 VariationsService* VariationsService::GetInstance() { |
| 29 return Singleton<VariationsService>::get(); |
| 30 } |
| 31 |
| 32 VariationsService::~VariationsService() {} |
| 33 |
| 34 void VariationsService::LoadVariationsSeed(PrefService* local_prefs) { |
| 35 std::string base64_seed_data = local_prefs->GetString(prefs::kVariationsSeed); |
| 36 std::string seed_data; |
| 37 |
| 38 // If the decode process fails, assume the pref value is corrupt, and clear |
| 39 // it. |
| 40 if (!base::Base64Decode(base64_seed_data, &seed_data) || |
| 41 !variations_seed_.ParseFromString(seed_data)) { |
| 42 VLOG(1) << "Variations Seed data in local pref is corrupt, clearing the " |
| 43 << "pref."; |
| 44 local_prefs->ClearPref(prefs::kVariationsSeed); |
| 45 } |
| 46 } |
| 47 |
| 48 void VariationsService::StartFetchingVariationsSeed() { |
| 49 pending_seed_request_.reset(content::URLFetcher::Create( |
| 50 GURL(kDefaultVariationsServer), content::URLFetcher::GET, this)); |
| 51 pending_seed_request_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
| 52 net::LOAD_DO_NOT_SAVE_COOKIES); |
| 53 pending_seed_request_->SetRequestContext( |
| 54 g_browser_process->system_request_context()); |
| 55 pending_seed_request_->SetMaxRetries(5); |
| 56 pending_seed_request_->Start(); |
| 57 } |
| 58 |
| 59 void VariationsService::OnURLFetchComplete(const content::URLFetcher* source) { |
| 60 DCHECK_EQ(pending_seed_request_.get(), source); |
| 61 // When we're done handling the request, the fetcher will be deleted. |
| 62 scoped_ptr<const content::URLFetcher> request( |
| 63 pending_seed_request_.release()); |
| 64 if (request->GetStatus().status() != net::URLRequestStatus::SUCCESS || |
| 65 request->GetResponseCode() != 200) |
| 66 return; |
| 67 |
| 68 std::string seed_data; |
| 69 request->GetResponseAsString(&seed_data); |
| 70 |
| 71 StoreSeedData(seed_data, g_browser_process->local_state()); |
| 72 } |
| 73 |
| 74 // static |
| 75 void VariationsService::RegisterPrefs(PrefService* prefs) { |
| 76 prefs->RegisterStringPref(prefs::kVariationsSeed, std::string()); |
| 77 } |
| 78 |
| 79 void VariationsService::StoreSeedData(const std::string& seed_data, |
| 80 PrefService* local_prefs) { |
| 81 // Only store the seed data if it parses correctly. |
| 82 chrome_variations::TrialsSeed seed; |
| 83 if (!seed.ParseFromString(seed_data)) { |
| 84 VLOG(1) << "Variations Seed data from server is not in valid proto format, " |
| 85 << "rejecting the seed."; |
| 86 return; |
| 87 } |
| 88 std::string base64_seed_data; |
| 89 if (!base::Base64Encode(seed_data, &base64_seed_data)) { |
| 90 VLOG(1) << "Variations Seed data from server fails Base64Encode, rejecting " |
| 91 << "the seed."; |
| 92 return; |
| 93 } |
| 94 local_prefs->SetString(prefs::kVariationsSeed, base64_seed_data); |
| 95 } |
| 96 |
| 97 VariationsService::VariationsService() {} |
OLD | NEW |