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/supports_user_data.h" |
| 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; |
| 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); |
| 40 WhitelistManager* wm = static_cast<WhitelistManager*>( |
| 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) { |
| 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."; |
| 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; |
| 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; |
| 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. |
| 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) |
| 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); |
| 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. |
| 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 |
OLD | NEW |