Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 # Copyright 2015 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 import os | |
| 5 import sqlite3 | |
| 6 | |
| 7 from profile_creators import fast_navigation_profile_extender | |
| 8 from profile_creators import profile_safe_url_list | |
| 9 | |
| 10 class CookieProfileExtender( | |
| 11 fast_navigation_profile_extender.FastNavigationProfileExtender): | |
| 12 """This extender performs a large number of navigations (up to 500), with the | |
| 13 goal of filling out the cookie database. | |
| 14 | |
| 15 By default, Chrome purges the cookie DB down to 3300 cookies. However, it | |
| 16 won't purge cookies accessed in the last month. This means the extender needs | |
| 17 to be careful not to create an artificially high number of cookies. | |
| 18 """ | |
| 19 _COOKIE_DB_EXPECTED_SIZE = 3300 | |
| 20 | |
| 21 def __init__(self): | |
| 22 super(CookieProfileExtender, self).__init__() | |
| 23 | |
| 24 # A list of urls that have not yet been navigated to. This list will shrink | |
| 25 # over time. Each navigation will add a diminishing number of new cookies, | |
| 26 # since there's a high probability that the cookie is already present. If | |
| 27 # the cookie DB isn't full by 500 navigations, just give up. | |
| 28 self._navigation_urls = profile_safe_url_list.GetShuffledSafeUrls()[0:500] | |
| 29 | |
| 30 def GetUrlsToNavigate(self, maximum_batch_size): | |
| 31 """Superclass override.""" | |
| 32 max_index = min(maximum_batch_size, len(self._navigation_urls)) | |
| 33 urls_to_return = self._navigation_urls[0:max_index] | |
| 34 self._navigation_urls[0:max_index] = [] | |
| 35 return urls_to_return | |
| 36 | |
| 37 def ShouldExit(self): | |
| 38 """Superclass override.""" | |
| 39 return self._IsCookieDBFull() | |
| 40 | |
| 41 def _IsCookieDBFull(self): | |
|
nednguyen
2015/02/18 22:43:14
Can you add unittest coverage for this?
erikchen
2015/02/18 23:32:30
I moved the meat of the logic into a new method: _
| |
| 42 """Chrome does not immediately flush cookies to its database. It's possible | |
| 43 that this method will return a false negative.""" | |
| 44 cookie_db_path = os.path.join(self.profile_path, "Default", "Cookies") | |
| 45 connection = sqlite3.connect(cookie_db_path) | |
| 46 | |
| 47 try: | |
| 48 cursor = connection.cursor() | |
| 49 cursor.execute("select count(*) from cookies") | |
| 50 cookie_count = cursor.fetchone()[0] | |
| 51 connection.close() | |
| 52 except sqlite3.OperationalError: | |
| 53 # There will occasionally be contention for the SQLite database. This | |
| 54 # shouldn't happen often, so ignore the errors. | |
| 55 return False | |
| 56 | |
| 57 return cookie_count > CookieProfileExtender._COOKIE_DB_EXPECTED_SIZE | |
| OLD | NEW |