Chromium Code Reviews| Index: tools/perf/profile_creators/cookie_profile_extender.py |
| diff --git a/tools/perf/profile_creators/cookie_profile_extender.py b/tools/perf/profile_creators/cookie_profile_extender.py |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..c6136af0d8d504b71f1e63fce01f0382a2c5ac76 |
| --- /dev/null |
| +++ b/tools/perf/profile_creators/cookie_profile_extender.py |
| @@ -0,0 +1,57 @@ |
| +# Copyright 2015 The Chromium Authors. All rights reserved. |
| +# Use of this source code is governed by a BSD-style license that can be |
| +# found in the LICENSE file. |
| +import os |
| +import sqlite3 |
| + |
| +from profile_creators import fast_navigation_profile_extender |
| +from profile_creators import profile_safe_url_list |
| + |
| +class CookieProfileExtender( |
| + fast_navigation_profile_extender.FastNavigationProfileExtender): |
| + """This extender performs a large number of navigations (up to 500), with the |
| + goal of filling out the cookie database. |
| + |
| + By default, Chrome purges the cookie DB down to 3300 cookies. However, it |
| + won't purge cookies accessed in the last month. This means the extender needs |
| + to be careful not to create an artificially high number of cookies. |
| + """ |
| + _COOKIE_DB_EXPECTED_SIZE = 3300 |
| + |
| + def __init__(self): |
| + super(CookieProfileExtender, self).__init__() |
| + |
| + # A list of urls that have not yet been navigated to. This list will shrink |
| + # over time. Each navigation will add a diminishing number of new cookies, |
| + # since there's a high probability that the cookie is already present. If |
| + # the cookie DB isn't full by 500 navigations, just give up. |
| + self._navigation_urls = profile_safe_url_list.GetShuffledSafeUrls()[0:500] |
| + |
| + def GetUrlsToNavigate(self, maximum_batch_size): |
| + """Superclass override.""" |
| + max_index = min(maximum_batch_size, len(self._navigation_urls)) |
| + urls_to_return = self._navigation_urls[0:max_index] |
| + self._navigation_urls[0:max_index] = [] |
| + return urls_to_return |
| + |
| + def ShouldExit(self): |
| + """Superclass override.""" |
| + return self._IsCookieDBFull() |
| + |
| + 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: _
|
| + """Chrome does not immediately flush cookies to its database. It's possible |
| + that this method will return a false negative.""" |
| + cookie_db_path = os.path.join(self.profile_path, "Default", "Cookies") |
| + connection = sqlite3.connect(cookie_db_path) |
| + |
| + try: |
| + cursor = connection.cursor() |
| + cursor.execute("select count(*) from cookies") |
| + cookie_count = cursor.fetchone()[0] |
| + connection.close() |
| + except sqlite3.OperationalError: |
| + # There will occasionally be contention for the SQLite database. This |
| + # shouldn't happen often, so ignore the errors. |
| + return False |
| + |
| + return cookie_count > CookieProfileExtender._COOKIE_DB_EXPECTED_SIZE |