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 void VariationsService::LoadVariationsSeed(PrefService* local_prefs) { |
| 33 std::string base64_seed_data = local_prefs->GetString(prefs::kVariationsSeed); |
| 34 std::string seed_data; |
| 35 |
| 36 // If the decode process fails, assume the pref value is corrupt, and clear |
| 37 // it. |
| 38 if (!base::Base64Decode(base64_seed_data, &seed_data) || |
| 39 !variations_seed_.ParseFromString(seed_data)) { |
| 40 VLOG(1) << "Variations Seed data in local pref is corrupt, clearing the " |
| 41 << "pref."; |
| 42 local_prefs->ClearPref(prefs::kVariationsSeed); |
| 43 } |
| 44 } |
| 45 |
| 46 void VariationsService::StartFetchingVariationsSeed() { |
| 47 pending_seed_request_.reset(content::URLFetcher::Create( |
| 48 GURL(kDefaultVariationsServer), content::URLFetcher::GET, this)); |
| 49 pending_seed_request_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
| 50 net::LOAD_DO_NOT_SAVE_COOKIES); |
| 51 pending_seed_request_->SetRequestContext( |
| 52 g_browser_process->system_request_context()); |
| 53 pending_seed_request_->SetMaxRetries(5); |
| 54 pending_seed_request_->Start(); |
| 55 } |
| 56 |
| 57 void VariationsService::OnURLFetchComplete(const content::URLFetcher* source) { |
| 58 DCHECK_EQ(pending_seed_request_.get(), source); |
| 59 // When we're done handling the request, the fetcher will be deleted. |
| 60 scoped_ptr<const content::URLFetcher> request( |
| 61 pending_seed_request_.release()); |
| 62 if (request->GetStatus().status() != net::URLRequestStatus::SUCCESS || |
| 63 request->GetResponseCode() != 200) |
| 64 return; |
| 65 |
| 66 std::string seed_data; |
| 67 request->GetResponseAsString(&seed_data); |
| 68 |
| 69 StoreSeedData(seed_data, g_browser_process->local_state()); |
| 70 } |
| 71 |
| 72 // static |
| 73 void VariationsService::RegisterPrefs(PrefService* prefs) { |
| 74 prefs->RegisterStringPref(prefs::kVariationsSeed, std::string()); |
| 75 } |
| 76 |
| 77 void VariationsService::StoreSeedData(const std::string& seed_data, |
| 78 PrefService* local_prefs) { |
| 79 // Only store the seed data if it parses correctly. |
| 80 chrome_variations::TrialsSeed seed; |
| 81 if (!seed.ParseFromString(seed_data)) { |
| 82 VLOG(1) << "Variations Seed data from server is not in valid proto format, " |
| 83 << "rejecting the seed."; |
| 84 return; |
| 85 } |
| 86 std::string base64_seed_data; |
| 87 if (!base::Base64Encode(seed_data, &base64_seed_data)) { |
| 88 VLOG(1) << "Variations Seed data from server fails Base64Encode, rejecting " |
| 89 << "the seed."; |
| 90 return; |
| 91 } |
| 92 local_prefs->SetString(prefs::kVariationsSeed, base64_seed_data); |
| 93 } |
| 94 |
| 95 VariationsService::VariationsService() {} |
| 96 VariationsService::~VariationsService() {} |
OLD | NEW |