Enable 'unused-import' and 'import-error' lint check for perf/
[chromium-blink-merge.git] / tools / perf / profile_creators / cookie_profile_extender.py
blobcb53363be0db52894060bdcc502039c7ce8eb93d
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 import page_sets
10 from profile_creators import fast_navigation_profile_extender
12 class CookieProfileExtender(
13 fast_navigation_profile_extender.FastNavigationProfileExtender):
14 """This extender fills in 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, finder_options):
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__(
28 finder_options, maximum_batch_size)
30 # A list of urls that have not yet been navigated to. This list will shrink
31 # over time. Each navigation will add a diminishing number of new cookies,
32 # since there's a high probability that the cookie is already present.
33 self._page_set = page_sets.ProfileSafeUrlsPageSet()
34 urls = []
35 for user_story in self._page_set.user_stories:
36 urls.append(user_story.url)
37 self._navigation_urls = urls
39 def GetUrlIterator(self):
40 """Superclass override."""
41 return iter(self._navigation_urls)
43 def ShouldExitAfterBatchNavigation(self):
44 """Superclass override."""
45 return self._IsCookieDBFull()
47 def WebPageReplayArchivePath(self):
48 return self._page_set.WprFilePathForUserStory(
49 self._page_set.user_stories[0])
51 def FetchWebPageReplayArchives(self):
52 """Superclass override."""
53 self._page_set.wpr_archive_info.DownloadArchivesIfNeeded()
55 @staticmethod
56 def _CookieCountInDB(db_path):
57 """The number of cookies in the db at |db_path|."""
58 connection = sqlite3.connect(db_path)
59 try:
60 cursor = connection.cursor()
61 cursor.execute("select count(*) from cookies")
62 cookie_count = cursor.fetchone()[0]
63 except:
64 raise
65 finally:
66 connection.close()
67 return cookie_count
69 def _IsCookieDBFull(self):
70 """Chrome does not immediately flush cookies to its database. It's possible
71 that this method will return a false negative."""
72 cookie_db_path = os.path.join(self.profile_path, "Default", "Cookies")
73 try:
74 cookie_count = CookieProfileExtender._CookieCountInDB(cookie_db_path)
75 except sqlite3.OperationalError:
76 # There will occasionally be contention for the SQLite database. This
77 # shouldn't happen often, so ignore the errors.
78 return False
80 return cookie_count > CookieProfileExtender._COOKIE_DB_EXPECTED_SIZE