[Session restore] Rename group name Enabled to Restore.
[chromium-blink-merge.git] / tools / perf / profile_creators / cookie_profile_extender.py
blob854e96978b70cc7b73be457a1d98d9b29c79a61c
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 multiprocessing
5 import os
6 import sqlite3
8 from profile_creators import fast_navigation_profile_extender
9 from profile_creators import profile_safe_url_list
11 class CookieProfileExtender(
12 fast_navigation_profile_extender.FastNavigationProfileExtender):
13 """This extender performs a large number of navigations (up to 500), with the
14 goal of filling out the cookie database.
16 By default, Chrome purges the cookie DB down to 3300 cookies. However, it
17 won't purge cookies accessed in the last month. This means the extender needs
18 to be careful not to create an artificially high number of cookies.
19 """
20 _COOKIE_DB_EXPECTED_SIZE = 3300
22 def __init__(self):
23 # The rate limiting factors are fetching network resources and executing
24 # javascript. There's not much to be done about the former, and having one
25 # tab per logical core appears close to optimum for the latter.
26 maximum_batch_size = multiprocessing.cpu_count()
27 super(CookieProfileExtender, self).__init__(maximum_batch_size)
29 # A list of urls that have not yet been navigated to. This list will shrink
30 # over time. Each navigation will add a diminishing number of new cookies,
31 # since there's a high probability that the cookie is already present. If
32 # the cookie DB isn't full by 500 navigations, just give up.
33 self._navigation_urls = profile_safe_url_list.GetShuffledSafeUrls()[0:500]
35 def GetUrlIterator(self):
36 """Superclass override."""
37 return iter(self._navigation_urls)
39 def ShouldExitAfterBatchNavigation(self):
40 """Superclass override."""
41 return self._IsCookieDBFull()
43 @staticmethod
44 def _CookieCountInDB(db_path):
45 """The number of cookies in the db at |db_path|."""
46 connection = sqlite3.connect(db_path)
47 try:
48 cursor = connection.cursor()
49 cursor.execute("select count(*) from cookies")
50 cookie_count = cursor.fetchone()[0]
51 except:
52 raise
53 finally:
54 connection.close()
55 return cookie_count
57 def _IsCookieDBFull(self):
58 """Chrome does not immediately flush cookies to its database. It's possible
59 that this method will return a false negative."""
60 cookie_db_path = os.path.join(self.profile_path, "Default", "Cookies")
61 try:
62 cookie_count = CookieProfileExtender._CookieCountInDB(cookie_db_path)
63 except sqlite3.OperationalError:
64 # There will occasionally be contention for the SQLite database. This
65 # shouldn't happen often, so ignore the errors.
66 return False
68 return cookie_count > CookieProfileExtender._COOKIE_DB_EXPECTED_SIZE