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

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

Powered by Google App Engine
This is Rietveld 408576698