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

Side by Side Diff: chrome/browser/autofill/autocheckout/whitelist_manager.cc

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

Powered by Google App Engine
This is Rietveld 408576698