Chromium Code Reviews| 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 "base/supports_user_data.h" | |
|
Ilya Sherman
2013/01/31 05:01:29
nit: This is already included in the header.
benquan
2013/01/31 23:17:08
Done.
| |
| 13 #include "chrome/common/chrome_switches.h" | |
| 14 #include "content/public/browser/browser_context.h" | |
| 15 #include "googleurl/src/gurl.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 void WhitelistManager::ScheduleDownload(size_t interval_seconds) { | |
| 65 if (!experimental_form_filling_enabled_) { | |
| 66 // The feature is not enabled: do not do the request. | |
| 67 return; | |
| 68 } | |
| 69 if (download_timer_.IsRunning() || callback_is_pending_) { | |
| 70 // A download activity is already scheduled or happening. | |
| 71 return; | |
| 72 } | |
| 73 StartDownloadTimer(interval_seconds); | |
| 74 } | |
| 75 | |
| 76 void WhitelistManager::StartDownloadTimer(size_t interval_seconds) { | |
| 77 download_timer_.Start(FROM_HERE, | |
| 78 base::TimeDelta::FromSeconds(interval_seconds), | |
| 79 this, | |
| 80 &WhitelistManager::TriggerDownload); | |
| 81 } | |
| 82 | |
| 83 void WhitelistManager::TriggerDownload() { | |
| 84 callback_is_pending_ = true; | |
| 85 | |
| 86 request_.reset(net::URLFetcher::Create( | |
| 87 0, GURL(kWhitelistUrl), net::URLFetcher::GET, this)); | |
| 88 request_->SetRequestContext(context_getter_); | |
| 89 request_->Start(); | |
| 90 } | |
| 91 | |
| 92 void WhitelistManager::StopDownloadTimer() { | |
| 93 download_timer_.Stop(); | |
| 94 } | |
| 95 | |
| 96 void WhitelistManager::OnURLFetchComplete( | |
| 97 const net::URLFetcher* source) { | |
| 98 DCHECK(callback_is_pending_); | |
| 99 callback_is_pending_ = false; | |
| 100 scoped_ptr<net::URLFetcher> old_request = request_.Pass(); | |
| 101 DCHECK_EQ(source, old_request.get()); | |
| 102 | |
| 103 if (source->GetResponseCode() == net::HTTP_OK) { | |
| 104 std::string data; | |
| 105 source->GetResponseAsString(&data); | |
| 106 BuildWhitelist(data); | |
| 107 } | |
| 108 | |
| 109 ScheduleDownload(kDownloadIntervalSeconds); | |
| 110 } | |
| 111 | |
| 112 std::string WhitelistManager::GetMatchedURLPrefix(const GURL& url) const { | |
| 113 if (!experimental_form_filling_enabled_) | |
| 114 return ""; | |
|
Ilya Sherman
2013/01/31 05:01:29
nit: Prefer std::string() to "".
benquan
2013/01/31 23:17:08
Done.
| |
| 115 | |
| 116 if (url.is_empty()) | |
| 117 return ""; | |
| 118 | |
| 119 for (std::vector<std::string>::const_iterator it = url_prefixes_.begin(); | |
| 120 it != url_prefixes_.end(); ++it) { | |
| 121 // This is only for ~20 sites initially, liner search is sufficient. | |
| 122 // TODO(benquan): Look for optimization options when we support | |
| 123 // more sites. | |
| 124 if (StartsWithASCII(url.spec(), *it, true)) | |
| 125 return *it; | |
| 126 } | |
| 127 return ""; | |
| 128 } | |
| 129 | |
| 130 void WhitelistManager::BuildWhitelist(const std::string& data) { | |
| 131 // TODO(benquan): find a better way to parse csv data. | |
|
Ilya Sherman
2013/01/31 05:01:29
nit: Is this TODO still relevant?
benquan
2013/01/31 23:17:08
Done.
| |
| 132 std::vector<std::string> new_url_prefixes; | |
| 133 | |
| 134 std::vector<std::string> lines; | |
| 135 base::SplitString(data, '\n', &lines); | |
| 136 | |
| 137 for (std::vector<std::string>::const_iterator line = lines.begin(); | |
| 138 line != lines.end(); ++line) { | |
| 139 if (!line->empty()) { | |
| 140 std::vector<std::string> fields; | |
| 141 base::SplitString(*line, ',', &fields); | |
| 142 // Currently we have only one column in the whitelist file, if we decide | |
| 143 // to add more metadata as additional columns, previous versions of | |
| 144 // Chrome can ignore them and continue to work. | |
| 145 if (!fields[0].empty()) | |
| 146 new_url_prefixes.push_back(fields[0]); | |
| 147 } | |
| 148 } | |
| 149 url_prefixes_ = new_url_prefixes; | |
| 150 } | |
| 151 | |
| 152 } // namespace autocheckout | |
| 153 } // namespace autofill | |
| 154 | |
| OLD | NEW |