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.
10 from profile_creators
import fast_navigation_profile_extender
11 from profile_creators
import profile_safe_url_list
13 class CookieProfileExtender(
14 fast_navigation_profile_extender
.FastNavigationProfileExtender
):
15 """This extender fills in the cookie database.
17 By default, Chrome purges the cookie DB down to 3300 cookies. However, it
18 won't purge cookies accessed in the last month. This means the extender needs
19 to be careful not to create an artificially high number of cookies.
21 _COOKIE_DB_EXPECTED_SIZE
= 3300
24 # The rate limiting factors are fetching network resources and executing
25 # javascript. There's not much to be done about the former, and having one
26 # tab per logical core appears close to optimum for the latter.
27 maximum_batch_size
= multiprocessing
.cpu_count()
28 super(CookieProfileExtender
, self
).__init
__(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()
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()
56 def _CookieCountInDB(db_path
):
57 """The number of cookies in the db at |db_path|."""
58 connection
= sqlite3
.connect(db_path
)
60 cursor
= connection
.cursor()
61 cursor
.execute("select count(*) from cookies")
62 cookie_count
= cursor
.fetchone()[0]
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")
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.
80 return cookie_count
> CookieProfileExtender
._COOKIE
_DB
_EXPECTED
_SIZE