Skip to content

Instantly share code, notes, and snippets.

@tobiassoppa
Last active December 6, 2021 16:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tobiassoppa/71a14274bc66037d9d1b6c5fa50adb36 to your computer and use it in GitHub Desktop.
Save tobiassoppa/71a14274bc66037d9d1b6c5fa50adb36 to your computer and use it in GitHub Desktop.

Google Summer of Code 2021 - Implement ClosedTabCache

This is a short summary of my work on implementing ClosedTabCache during Google Summer of Code 2021. Details for this project are outlined in the Closed Tab Cache design document.

Introduce remove_reason enum in TabStripModelChange

Before ClosedTabCache, no tab could exist outside a tab strip (the bar on top of the browser which contains all open tabs). With the introduction of ClosedTabCache, there is a new state the browser could be in: Having an existing tab outside any tab strip. This new state should be properly communicated to any observer that is interested in it. Therefore an enum has been introduced that distinguished between different states that could occur

  • Tab has been deleted
  • Tab has been inserted into another tab strip (for instance when dragging it to a different window)
  • Tab has been cached in ClosedTabCache, a new state that previously did not exist.

Here is a link to the change.

Hook ClosedTabCache into the tab restore flow

When a recently closed tab gets restored, a new network request will be made and the tab will be rendered from scratch again. To use ClosedTabCache, we first need to store the tab in the cache instead of destroying it. In addition, we need to check the cache every time a tab is about to be restored. If it is in the cache, we return the cached tab to the caller, otherwise we use the old flow.

Here is a link to the change.

Note: This is a reland of a previous change that contained a bug. The original change can be found here.

Add a chrome://flag for ClosedTabCache

The ClosedTabCache feature is part of the code, but disabled by default. To enable it, a flag needs to be added that can be dynamically enabled or disabled from inside the browser. To enable or disable the feature, the following steps need to be performed

  • Have at least version 94 installed
  • Type chrome://flag in the URL bar and hit enter
  • Type Closed Tab Cache into the search bar
  • Enable the feature in the dropdown menu

Here is a link to the change.

Fix failing unit_tests when ClosedTabCache is enabled

Because the feature is disabled by default, it has not been tested against the numerous tests that exist in the chromium code base. When trying to run all approximately 13.500 unit tests with the flag --enable-feature=ClosedTabCache, many of them started to fail because of a faulty assert that checked the ID that acts as key for the cache. Instead of simply not querying the cache when the ID was invalid, which means there can't be an entry in the cache, the assert triggered. This bug has been fixed by simply returning, instead of asserting.

Here is a link to the change.

Fix ClosedTabCache issues with beforeunload events

It is not possible to cache any tabs that run beforeunload events. If we try to close a single tab with beforeunload events, an empty vector will be passed to the cache, which leads to crashes when accessing the last element of an empty vector. To fix this, we simply return if the passed vector is empty.

Here is a link to the change.

Note: As of writing this document, this change is not yet on the master branch. That's why the specific patchset is linked that has been the most recent one at the time of writing this document in order to precisely capture the work that has been done during Google Summer of Code.

Add caching knowledge to content::WebContents

When a user closes a tab, the tab should behave as if it has actually been closed, even though it continues to live in the cache. Otherwise the user might face situations like closing an audible tab, but continuing to hear the audio without being able to turn it off. That's why a tab needs to be completely frozen, which prevents things such as

  • Playing audible content
  • Executing JavaScript
  • Reading sensor data
  • And many more Once a tab enters the cache, the cache will freeze it. Upon restoring it from the cache, it will be unfrozen. More details can be found in this design document.

Here is a link to the change.

Note: As of writing this document, this change is not yet on the master branch. That's why the specific patchset is linked that has been the most recent one at the time of writing this document in order to precisely capture the work that has been done during Google Summer of Code.

Final presentation

Shortly before Google Summer of Code ended, I was given the chance to present my work to everyone who was involved with Chromium in GSoC 2021:

  • The administrators
  • The mentors
  • The 8 other students who worked on Chromium projects over the summer

Here is a link to my presentation

Future work

I would like to continue working on Chromium after Google Summer of Code ends. In order to get ClosedTabCache to a stable level, there are still a few things that need to be done, such as finalizing the freezing and eviction API. Right now freezing a tab that enters the cache and unfreezing it once it leaves the cache is already working, but the review process is not done yet and there are still comments to be resolved before being able to land it on the master branch (as described in the Add caching knowledge to content::WebContents section). In addition to freezing, the cache should also be able to evict entries, for instance if a tab tries to perform work it is not supposed to do. Currently this is not working as expected and is therefore one of my priorities to fix in the near future.

There are also a few other tasks that need to be solved, for instance Issue 1236077: Cache more than one tab in ClosedTabCache. Currently it is assumed that ClosedTabCache has a size of one, so only a single tab will be cached at any time. This simplifies the implementation of the cache quite a bit, but in the future the cache should be able to handle an arbitrary amount of tabs.

@MdSahil-oss
Copy link

Hey!, How can i contribute to this project

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment