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

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: revert code change for manual testing/:wq. Created 7 years, 11 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/supports_user_data.h"
Ilya Sherman 2013/01/24 22:01:55 nit: Remove this; it's already present in the head
benquan 2013/01/25 00:55:31 removed it from the header and kept this line.
12 #include "chrome/browser/autofill/autocheckout/whitelist_url.h"
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;
Ilya Sherman 2013/01/24 22:01:55 nit: I think you want a slightly larger delay for
benquan 2013/01/25 00:55:31 Download will be triggered only when we create Wh
Ilya Sherman 2013/01/25 01:22:40 Ok.
28
29 const char kWhiteListKeyName[] = "autocheckout_whitelist_manager";
30
31 } // namespace
32
33
34 namespace autocheckout {
35
36 // static
37 WhitelistManager* WhitelistManager::GetForBrowserContext(
38 content::BrowserContext* context) {
39 DCHECK(context);
Ilya Sherman 2013/01/24 22:01:55 nit: Omit this.
benquan 2013/01/25 00:55:31 Done.
40 WhitelistManager* wm = static_cast<WhitelistManager*>(
Ilya Sherman 2013/01/24 22:01:55 nit: Please avoid abbreviations in variable names.
benquan 2013/01/25 00:55:31 Done.
41 context->GetUserData(kWhiteListKeyName));
42 if (!wm) {
43 wm = new WhitelistManager(context->GetRequestContext());
44 wm->ScheduleDownload(kInitialDownloadDelaySeconds);
45 context->SetUserData(kWhiteListKeyName, wm);
46 }
47 return wm;
48 }
49
50 WhitelistManager::WhitelistManager(
51 net::URLRequestContextGetter* context_getter)
52 : context_getter_(context_getter),
53 callback_pending_(false),
54 experimental_form_filling_enabled_(
55 CommandLine::ForCurrentProcess()->HasSwitch(
56 switches::kEnableExperimentalFormFilling)) {
57 DCHECK(context_getter);
58 }
59
60 bool WhitelistManager::ScheduleDownload(int interval_seconds) {
Ilya Sherman 2013/01/24 22:01:55 nit: The return value from this method doesn't see
benquan 2013/01/25 00:55:31 The unittest uses it to check if a new download wa
Ilya Sherman 2013/01/25 01:22:40 That means that the unittest is not testing the pr
benquan 2013/01/26 02:05:53 Done.
61 if (!experimental_form_filling_enabled_) {
62 // The feature is not enabled: do not do the request.
63 return false;
64 }
65 if (download_timer_.IsRunning() || callback_pending_) {
66 // A download activity is already scheduled or happening.
67 DVLOG(1) << "Autocheckout DownloadWhitelist scheduler is already running.";
Ilya Sherman 2013/01/24 22:01:55 Will this DVLOG and others in this file be useful
benquan 2013/01/25 00:55:31 removed
68 return false;
69 }
70
71 download_timer_.Start(FROM_HERE,
72 base::TimeDelta::FromSeconds(interval_seconds),
73 this,
74 &WhitelistManager::TriggerDownload);
75 DVLOG(1) << "Autocheckout DownloadWhitelist was scheduled for "
76 << interval_seconds << " seconds.";
77 return true;
78 }
79
80 void WhitelistManager::TriggerDownload() {
81 callback_pending_ = true;
82 DVLOG(1) << "Autocheckout DownloadWhitelist...";
83
84 request_.reset(net::URLFetcher::Create(
85 0, GetAutocheckoutWhitelistUrl(), net::URLFetcher::GET, this));
86 request_->SetRequestContext(context_getter_);
87 request_->Start();
88 return;
Ilya Sherman 2013/01/24 22:01:55 nit: Omit this.
benquan 2013/01/25 00:55:31 Done.
89 }
90
91 void WhitelistManager::OnURLFetchComplete(
92 const net::URLFetcher* source) {
93 DCHECK(callback_pending_);
94 callback_pending_ = false;
95 scoped_ptr<net::URLFetcher> old_request = request_.Pass();
96 DCHECK_EQ(source, old_request.get());
97
98 DVLOG(1) << "Autocheckout got response from " << source->GetOriginalURL()
99 << ". Response code: " << source->GetResponseCode();
100
101 if (source->GetResponseCode() != net::HTTP_OK)
102 return;
Ilya Sherman 2013/01/24 22:01:55 So, if the download ever fails, you'll never sched
benquan 2013/01/25 00:55:31 fixed On 2013/01/24 22:01:55, Ilya Sherman wrote:
103
104 std::string data;
105 source->GetResponseAsString(&data);
106 DVLOG(1) << "Autocheckout whitelist response data: " << data;
107 BuildWhitelist(data);
108
109 ScheduleDownload(kDownloadIntervalSeconds);
110 }
111
112 bool WhitelistManager::IsAutocheckoutEnabled(const GURL& url) {
113 if (!experimental_form_filling_enabled_) {
114 // The feature is not enabled, return false.
Ilya Sherman 2013/01/24 22:01:55 nit: This comment is redundant with the code; plea
benquan 2013/01/25 00:55:31 Done.
115 return false;
116 }
117
118 if (url.is_empty())
119 return false;
120
121 for (std::vector<std::string>::iterator it = url_prefixes_.begin();
122 it != url_prefixes_.end(); ++it) {
123 // This is only for ~20 sites initially, liner search is sufficient.
124 // TODO(benquan): Look for optimization options when we support
125 // more sites.
126 if (url.spec().compare(0, it->size(), *it) == 0)
Ilya Sherman 2013/01/24 22:01:55 nit: Use StartsWith() from base/string_util.h for
benquan 2013/01/25 00:55:31 Done.
127 return true;
128 }
129 return false;
130 }
131
132 void WhitelistManager::BuildWhitelist(const std::string& data) {
133 // TODO(benquan): find a better way to parse csv data.
134 std::vector<std::string> new_url_prefixes;
135
136 std::stringstream dataStream(data);
Ilya Sherman 2013/01/24 22:01:55 nit: Use base::SplitString rather than using a str
benquan 2013/01/25 00:55:31 Done.
137 std::string line;
138 while (std::getline(dataStream, line)) {
139 if (!line.empty()) {
140 std::vector<std::string> fields;
141 base::SplitString(line, ',', &fields);
142 // The whilist file is a simple CSV file, and the first column is the url
143 // prefix.
Ilya Sherman 2013/01/24 22:01:55 What are the remaining columns?
benquan 2013/01/25 00:55:31 We only have one column right now. It's for backwa
Ilya Sherman 2013/01/25 01:22:40 Please mention that in the comment.
benquan 2013/01/26 02:05:53 Done.
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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698