OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 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/autofill/autocheckout/whitelist_manager.h" |
| 6 |
| 7 #include "base/command_line.h" |
| 8 #include "base/logging.h" |
| 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/string_split.h" |
| 11 #include "base/string_util.h" |
| 12 #include "chrome/common/chrome_switches.h" |
| 13 #include "content/public/browser/browser_context.h" |
| 14 #include "googleurl/src/gurl.h" |
| 15 #include "net/base/load_flags.h" |
| 16 #include "net/http/http_status_code.h" |
| 17 #include "net/url_request/url_fetcher.h" |
| 18 #include "net/url_request/url_request_context_getter.h" |
| 19 |
| 20 namespace { |
| 21 |
| 22 // Back off in seconds after each whitelist download is attempted. |
| 23 const int kDownloadIntervalSeconds = 86400; // 1 day |
| 24 |
| 25 // The delay in seconds after startup before download whitelist. This helps |
| 26 // to reduce contention at startup time. |
| 27 const int kInitialDownloadDelaySeconds = 3; |
| 28 |
| 29 const char kWhitelistUrl[] = |
| 30 "http://www.gstatic.com/commerce/autocheckout/whitelist.csv"; |
| 31 |
| 32 const char kWhiteListKeyName[] = "autocheckout_whitelist_manager"; |
| 33 |
| 34 } // namespace |
| 35 |
| 36 |
| 37 namespace autofill { |
| 38 namespace autocheckout { |
| 39 |
| 40 // static |
| 41 WhitelistManager* WhitelistManager::GetForBrowserContext( |
| 42 content::BrowserContext* context) { |
| 43 WhitelistManager* whitelist_manager = static_cast<WhitelistManager*>( |
| 44 context->GetUserData(kWhiteListKeyName)); |
| 45 if (!whitelist_manager) { |
| 46 whitelist_manager = |
| 47 new WhitelistManager(context->GetRequestContext()); |
| 48 whitelist_manager->ScheduleDownload(kInitialDownloadDelaySeconds); |
| 49 context->SetUserData(kWhiteListKeyName, whitelist_manager); |
| 50 } |
| 51 return whitelist_manager; |
| 52 } |
| 53 |
| 54 WhitelistManager::WhitelistManager( |
| 55 net::URLRequestContextGetter* context_getter) |
| 56 : callback_is_pending_(false), |
| 57 context_getter_(context_getter), |
| 58 experimental_form_filling_enabled_( |
| 59 CommandLine::ForCurrentProcess()->HasSwitch( |
| 60 switches::kEnableExperimentalFormFilling)){ |
| 61 DCHECK(context_getter); |
| 62 } |
| 63 |
| 64 WhitelistManager::~WhitelistManager() {} |
| 65 |
| 66 void WhitelistManager::ScheduleDownload(size_t interval_seconds) { |
| 67 if (!experimental_form_filling_enabled_) { |
| 68 // The feature is not enabled: do not do the request. |
| 69 return; |
| 70 } |
| 71 if (download_timer_.IsRunning() || callback_is_pending_) { |
| 72 // A download activity is already scheduled or happening. |
| 73 return; |
| 74 } |
| 75 StartDownloadTimer(interval_seconds); |
| 76 } |
| 77 |
| 78 void WhitelistManager::StartDownloadTimer(size_t interval_seconds) { |
| 79 download_timer_.Start(FROM_HERE, |
| 80 base::TimeDelta::FromSeconds(interval_seconds), |
| 81 this, |
| 82 &WhitelistManager::TriggerDownload); |
| 83 } |
| 84 |
| 85 void WhitelistManager::TriggerDownload() { |
| 86 callback_is_pending_ = true; |
| 87 |
| 88 request_.reset(net::URLFetcher::Create( |
| 89 0, GURL(kWhitelistUrl), net::URLFetcher::GET, this)); |
| 90 request_->SetRequestContext(context_getter_); |
| 91 request_->SetAutomaticallyRetryOn5xx(false); |
| 92 request_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES | |
| 93 net::LOAD_DO_NOT_SEND_COOKIES); |
| 94 request_->Start(); |
| 95 } |
| 96 |
| 97 void WhitelistManager::StopDownloadTimer() { |
| 98 download_timer_.Stop(); |
| 99 callback_is_pending_ = false; |
| 100 } |
| 101 |
| 102 void WhitelistManager::OnURLFetchComplete( |
| 103 const net::URLFetcher* source) { |
| 104 DCHECK(callback_is_pending_); |
| 105 callback_is_pending_ = false; |
| 106 scoped_ptr<net::URLFetcher> old_request = request_.Pass(); |
| 107 DCHECK_EQ(source, old_request.get()); |
| 108 |
| 109 if (source->GetResponseCode() == net::HTTP_OK) { |
| 110 std::string data; |
| 111 source->GetResponseAsString(&data); |
| 112 BuildWhitelist(data); |
| 113 } |
| 114 |
| 115 ScheduleDownload(kDownloadIntervalSeconds); |
| 116 } |
| 117 |
| 118 std::string WhitelistManager::GetMatchedURLPrefix(const GURL& url) const { |
| 119 if (!experimental_form_filling_enabled_ || url.is_empty()) |
| 120 return std::string(); |
| 121 |
| 122 for (std::vector<std::string>::const_iterator it = url_prefixes_.begin(); |
| 123 it != url_prefixes_.end(); ++it) { |
| 124 // This is only for ~20 sites initially, liner search is sufficient. |
| 125 // TODO(benquan): Look for optimization options when we support |
| 126 // more sites. |
| 127 if (StartsWithASCII(url.spec(), *it, true)) |
| 128 return *it; |
| 129 } |
| 130 return std::string(); |
| 131 } |
| 132 |
| 133 void WhitelistManager::BuildWhitelist(const std::string& data) { |
| 134 std::vector<std::string> new_url_prefixes; |
| 135 |
| 136 std::vector<std::string> lines; |
| 137 base::SplitString(data, '\n', &lines); |
| 138 |
| 139 for (std::vector<std::string>::const_iterator line = lines.begin(); |
| 140 line != lines.end(); ++line) { |
| 141 if (!line->empty()) { |
| 142 std::vector<std::string> fields; |
| 143 base::SplitString(*line, ',', &fields); |
| 144 // Currently we have only one column in the whitelist file, if we decide |
| 145 // to add more metadata as additional columns, previous versions of |
| 146 // Chrome can ignore them and continue to work. |
| 147 if (!fields[0].empty()) |
| 148 new_url_prefixes.push_back(fields[0]); |
| 149 } |
| 150 } |
| 151 url_prefixes_ = new_url_prefixes; |
| 152 } |
| 153 |
| 154 } // namespace autocheckout |
| 155 } // namespace autofill |
| 156 |
OLD | NEW |