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.
8 import sqlite3
# Not present on ChromeOS DUT.
14 from profile_creators
import fast_navigation_profile_extender
16 class CookieProfileExtender(
17 fast_navigation_profile_extender
.FastNavigationProfileExtender
):
18 """This extender fills in the cookie database.
20 By default, Chrome purges the cookie DB down to 3300 cookies. However, it
21 won't purge cookies accessed in the last month. This means the extender needs
22 to be careful not to create an artificially high number of cookies.
24 _COOKIE_DB_EXPECTED_SIZE
= 3300
26 def __init__(self
, finder_options
):
27 # The rate limiting factors are fetching network resources and executing
28 # javascript. There's not much to be done about the former, and having one
29 # tab per logical core appears close to optimum for the latter.
30 maximum_batch_size
= multiprocessing
.cpu_count()
32 # Web page replay cannot handle too many requests over a duration of 4
33 # minutes (maximum segment lifetime), as it may exhaust the socket pool.
34 # Artificially limit the rate to no more than 5 simultaneous tab loads.
35 if not finder_options
.use_live_sites
:
36 maximum_batch_size
= min(5, maximum_batch_size
)
38 super(CookieProfileExtender
, self
).__init
__(
39 finder_options
, maximum_batch_size
)
41 # A list of urls that have not yet been navigated to. This list will shrink
42 # over time. Each navigation will add a diminishing number of new cookies,
43 # since there's a high probability that the cookie is already present.
44 self
._page
_set
= page_sets
.ProfileSafeUrlsPageSet()
46 for story
in self
._page
_set
.stories
:
47 urls
.append(story
.url
)
48 self
._navigation
_urls
= urls
50 def GetUrlIterator(self
):
51 """Superclass override."""
52 return iter(self
._navigation
_urls
)
54 def ShouldExitAfterBatchNavigation(self
):
55 """Superclass override."""
56 return self
._IsCookieDBFull
()
58 def WebPageReplayArchivePath(self
):
59 return self
._page
_set
.WprFilePathForStory(
60 self
._page
_set
.stories
[0])
62 def FetchWebPageReplayArchives(self
):
63 """Superclass override."""
64 self
._page
_set
.wpr_archive_info
.DownloadArchivesIfNeeded()
67 def _CookieCountInDB(db_path
):
68 """The number of cookies in the db at |db_path|."""
69 connection
= sqlite3
.connect(db_path
)
71 cursor
= connection
.cursor()
72 cursor
.execute("select count(*) from cookies")
73 cookie_count
= cursor
.fetchone()[0]
80 def _IsCookieDBFull(self
):
81 """Chrome does not immediately flush cookies to its database. It's possible
82 that this method will return a false negative."""
83 cookie_db_path
= os
.path
.join(self
.profile_path
, "Default", "Cookies")
85 cookie_count
= CookieProfileExtender
._CookieCountInDB
(cookie_db_path
)
86 except sqlite3
.OperationalError
:
87 # There will occasionally be contention for the SQLite database. This
88 # shouldn't happen often, so ignore the errors.
91 return cookie_count
> CookieProfileExtender
._COOKIE
_DB
_EXPECTED
_SIZE