Opens traces from Google Drive, either through the “Open from GDrive” sidebar button (file picker) or through Drive‘s “Open with” deep link (?state=<json>), and uploads the current trace to Google Drive through the “Share to GDrive” sidebar button. The “Install Google Drive support” command connects Perfetto to a Google account, which adds it to Drive’s “Open with” menu.
Flows
Sidebar flow (openFileFromSidebar)
Deep link flow (openFileFromDeeplink)
Upload flow (uploadTraceToGDrive)
The buttons of the “Share to Google Drive” dialog each start a flow like this one. Failures are not shown in a dialog of their own, which would replace the share dialog: they are recorded in the trace's share state and shown in a banner in the share dialog.
“Change” (pick a folder) and “Share” (Drive's sharing dialog) are the same shape: authenticate, then show the Google picker or sharing dialog, recording any failure in the banner.
Every “Authenticate” box above runs this. It never throws: every outcome is returned as a Result.
Scopes
Each action passes the OAuth scope it needs to authenticate(), one at a time. There is no default:
Scope
What it allows
Asked for by
drive.install
Perfetto is offered in Drive's “Open with” menu. No access to files
The “Install Google Drive support” command
drive.file
Reading and creating the files the user opens or picks with Perfetto
Nothing yet: the flows that open files will ask for it
Asking for one scope at a time matters. When an app asks for several together, Google's consent screen shows a tick box for each and the user can refuse any of them, and there is no way to mark one as required. A single scope is simply allowed or cancelled. The client still checks which scopes were granted, and returns missing_scopes if any that were asked for are missing.
Content Security Policy
The UI's Content Security Policy (CSP) is set in setupContentSecurityPolicy() in ui/src/frontend/index.ts. This plugin is the reason for the entries below. They apply to every user of the UI, whether or not they use Google Drive, so keep them as narrow as the plugin allows.
Directive
Entry
Why it is needed
script-src
https://accounts.google.com/gsi/client
Google Identity Services, which issues the access token
script-src
https://apis.google.com/js/api.js
The Google API loader
script-src
https://apis.google.com/_/scs/abc-static/_/js/
The modules the loader fetches (picker, sharing dialog)
frame-src
https://docs.google.com/picker and /picker/
The picker dialog
frame-src
https://drive.google.com/sharing/ and /drivesharing/
The sharing dialog
img-src
https://ssl.gstatic.com
Icons used by the picker and sharing dialog
Why the entries look the way they do:
Google's scripts cannot be bundled. They are only served by Google and change without notice, so they are loaded at runtime and need script-src entries. The module URLs contain version hashes, so only their directory can be named.
Paths, not hosts.*.google.com was removed from script-src in #8176 because those hosts serve many other scripts. Each entry here names one script, or one directory, instead.
No frame entry for connecting. The plugin only uses the OAuth token client, which opens a popup. Popups are not governed by frame-src, so accounts.google.com is not listed there.
Drive requests need nothing extra. They are plain fetch() calls to www.googleapis.com, which connect-src already allows.
Known limits
The path-based entries narrow the policy but are not a hard boundary. All of the following need an HTML injection bug in the UI before they matter, and none is known to allow arbitrary script execution.
Query strings are ignored by CSP.api.js accepts an onload parameter naming a global function to call once it has loaded. Injected markup could use that to call a top-level global function, without arguments. In production the service worker blocks external requests that have a query string, which stops this once the worker controls the page. That is not the case on localhost or on a first visit.
Paths are not checked after a redirect. Once a request is redirected, CSP only matches the host. A redirect that starts inside the module directory and stays on apis.google.com is therefore allowed, so in practice treat the whole of apis.google.com as allowed for scripts.
Every loader module is reachable, not only the two the plugin uses, and Google Identity Services can be driven from page markup (an element with the id g_id_onload). Both could be used to call a global function by name, or to show a Google sign-in prompt on this origin.
Moving script-src to hashes or nonces with 'strict-dynamic' would remove this whole class, as injected <script> tags would no longer run whichever host they point at. That is a larger change to how the UI is served and has not been done.
When changing these entries
Run the flows under No CSP violations, and get the change reviewed by someone on the security side.
Manual testing
Keep this list up to date when adding a flow or a failure mode.
What to test
Each of these links to a section below. When testing a change, copy this list into the PR description as a checklist rather than ticking it off here.
Run ui/run-dev-server and open http://localhost:10000.
The plugin is not enabled by default. Open http://localhost:10000/#!/plugins, enable dev.perfetto.GoogleDrive and reload.
http://localhost:10000 must be an authorised JavaScript origin on the OAuth client named in the config. If the picker works locally, it is.
The access token is only kept in memory, so every page load starts disconnected.
Everything Google Drive support needs is loaded once, on every page load: the config and Google's scripts. See Initialisation fails.
Every “I give up” dialog (connection, picker and download failures) has an “OK” button only, and no retry. Where there is an underlying error, it is shown in a collapsed “Error details” section of the dialog rather than logged to the console.
Two DevTools techniques are used below:
Block a request: in the Network tab, right-click a request and choose “Block request URL” (or add a pattern in the “Network request blocking” drawer tab), then reload.
The “Install Google Drive support” command asks for permission to add Perfetto to the “Open with” menu in the user's Google Drive. It does not ask for access to any files.
Open the command palette (Ctrl/Cmd+Shift+P) and run “Install Google Drive support”.
Complete the Google popup. It should ask only for permission to connect Perfetto to Google Drive, with no tick boxes and no mention of files. A “Google Drive support installed” dialog should appear.
Run the command again without reloading: the dialog should appear straight away, with no Google popup.
Close the Google popup instead of completing it: the “Couldn't connect to Google Drive” dialog should appear, see Google popup closed.
With initialisation failing (see Initialisation fails), the command should show the “Google Drive unavailable” dialog.
Sidebar flow
Click “Open from GDrive” in the sidebar.
Complete the Google popup if it asks.
Pick a trace in the picker. It should open, titled with the file's name.
Cancel the picker instead of picking: nothing should happen.
Clicking the button again while a flow is still running (e.g. while the picker is open) should do nothing.
Token reuse within a session
Complete the sidebar flow once.
Without reloading, click “Open from GDrive” again.
The picker should open straight away, with no Google popup.
The token is reused until about a minute before it expires (normally an hour). After that, or after a page reload, the Google popup is needed again.
“Open with” deep link
Drive opens the UI with a state query parameter naming the file. To reproduce that without going through Drive, paste a URL like this into the address bar of a new tab:
That is {"ids":["<FILE_ID>"],"action":"open"} URL-encoded. Take <FILE_ID> from a Drive link (https://drive.google.com/file/d/<FILE_ID>/view).
Expected:
Unless popups are allowed for the site, the “Connect to Google Drive” dialog appears first, see Popup blocked.
Once connected, the trace loads, titled with the file's real name. The deep link only carries the file ID, so the name is looked up from Drive with a separate metadata request, in parallel with the download.
The state parameter is removed from the address bar once connected. It stays if the user cancels the “Connect to Google Drive” dialog or connecting fails, so reloading retries the deep link.
If the name lookup fails the trace should still open, titled “Google Drive file”, and a warning is logged to the console. To check, block *fields=name* in DevTools and load the deep link again.
From a real “Open with” click
The app is registered with Google Drive as an “Open with” app, so the real flow can be tested too:
Connect through the UI at least once (e.g. with the sidebar flow). Granting access is what adds Perfetto to your Drive's “Open with” menu.
In Drive, right-click a trace, choose “Open with” and pick Perfetto. Drive opens the registered URL with a state parameter for that file.
To land on a local build instead of the registered URL, use a redirect extension (e.g. Requestly) to rewrite the registered URL to http://localhost:10000/, keeping the query string.
A file opened this way should already be authorised, so it should load without the “Authorize access to file” dialog.
Resource key
Some link-shared files need a resource key as well as the file ID. Their links look like .../file/d/<FILE_ID>/view?resourcekey=<KEY>. To test one, use a file you do not own and have not opened before, and add the key to the state payload:
URL-encode it and load it as the state parameter. In the DevTools Network tab, the Drive download and metadata requests should carry the header X-Goog-Drive-Resource-Keys: <FILE_ID>/<KEY>.
Drive traces are not shareable
Drive traces are access-controlled, so they must not be uploaded to the public permalink bucket.
Open a trace from Drive by either route.
The UI should not offer to create a permalink for it. (The “Share to GDrive” button is separate: for a trace opened from Drive it shows “Saved to GDrive”, see After uploading.)
Button state
The plugin is not enabled by default for anyone. Once it is enabled, the “Open from GDrive” button is shown to every user, just below “Open trace file”. It is disabled (struck through) until initialisation has succeeded: the config and Google's scripts all loaded. That happens shortly after startup. Clicking the button while it is disabled says why.
To test, enable the plugin and reload:
For a moment after the page loads the button may be disabled. Clicking it then should say that Google Drive support is still loading.
Once initialisation finishes the button should be enabled.
Initialisation fails
On every page load the plugin loads, in order:
the config (API key, client ID and app ID), from https://storage.googleapis.com/perfetto-misc/gdrive_config.json,
Google's sign-in library, from https://accounts.google.com/gsi/client,
Google's API loader script, from https://apis.google.com/js/api.js,
the picker and sharing dialog modules, also from apis.google.com.
None of this opens a popup. If any step fails, “Failed to initialize Google Drive” is logged to the console (with the failing step as its cause) and the sidebar buttons stay disabled. Nothing else is shown: no error dialog, as this runs for every user on every load.
For each of the URLs above in turn:
Block it and reload.
The “Open from GDrive” button should stay disabled, and clicking it should say that Google Drive support could not be loaded. The console should show “Failed to initialize Google Drive”.
Load a deep link URL with the request still blocked. A “Google Drive unavailable” dialog should appear, and the state parameter should stay in the address bar.
Unblock the URL and reload the same deep link. It should now proceed.
Also check that with everything blocked the rest of the UI works normally, e.g. opening a local trace.
The config fetch times out after 5 seconds, and each of the other steps after 30 seconds. Until initialisation finishes, a deep link shows nothing. A failed initialisation is not retried until the page is reloaded.
Plugin disabled
The plugin is not enabled by default, so this is the state on a fresh profile.
Open http://localhost:10000/#!/plugins, make sure dev.perfetto.GoogleDrive is disabled, then reload.
The “Open from GDrive” button should not appear.
In the DevTools Network tab there should be no requests for the config or for Google's scripts (see Initialisation fails for the URLs).
Loading a deep link URL should do nothing.
Re-enable the plugin afterwards.
Popup blocked (“Connect to Google Drive” dialog)
This only happens on a deep link: the sidebar flow starts from a click, so the browser allows the popup.
Make sure popups are not set to “Allow” for localhost:10000 (chrome://settings/content/popups).
Load the deep link URL and do not click anything while it loads.
The “Connect to Google Drive” dialog should appear, including a hint that allowing pop-ups for the site skips this step.
Click “Connect”. The Google popup should open, and once it completes the trace should load without a page reload.
Repeat and click “Cancel” instead. The dialog should close and nothing else should happen.
Set popups to “Allow” for localhost:10000 and load the deep link again. The dialog should not appear; the Google popup should open by itself.
After “Connect” the popup is opened shortly after the click rather than inside it, which Chrome allows. Other browsers may block it again, in which case the “Couldn't connect to Google Drive” dialog appears saying the popup was blocked.
Connection failure (“Couldn't connect to Google Drive” dialog)
This is shown when Google reports an error while connecting, for example because the user refuses to grant access.
Use a Google account that has not yet granted Perfetto access to Drive (or remove Perfetto‘s access from the account’s third-party access page first).
Click “Open from GDrive”, or load the deep link URL.
On Google's consent screen, press “Cancel” rather than granting access.
The “Couldn't connect to Google Drive” dialog should appear with Google's error, and the same error under “Error details”, rather than the generic crash dialog.
“OK” dismisses it and ends the flow. Start again from the sidebar button or by reloading the deep link.
Google popup closed
Set popups to “Allow” for localhost:10000, or start from the sidebar button.
When the Google popup opens, close it without completing it.
The “Couldn't connect to Google Drive” dialog should appear, saying the window was closed. There is no “Error details” section for this case.
“OK” dismisses it and ends the flow. Clicking the sidebar button again should reopen the popup; reloading a deep link should retry it.
File not yet authorised (“Authorize access to file” dialog)
The app can only read files the user has opened or picked with it.
Set popups to “Allow” for localhost:10000.
Load the deep link URL with the ID of a file that has never been opened or picked through this app.
The download should fail with HTTP 403 or 404 and the “Authorize access to file” dialog should appear.
Click “Authorize File Access”. The dialog closes and the picker opens, showing only that file. Pick it, and the trace should open, titled with the file's real name.
Repeat and click “Cancel” in the dialog, or cancel the picker: the flow should end silently.
Drive reports a file the app has not been granted as “not found”, the same as a file that does not exist. So a deep link with a made-up file ID also shows this dialog; the picker it opens will then have nothing to pick.
Invalid deep link payload
Each of these should be ignored silently: no dialog, no Google popup, and the state parameter left in the address bar.
?state=not-json
?state=%7B%22ids%22%3A%22abc%22%7D (ids is not an array)
?state=%7B%22ids%22%3A%5B%5D%7D (no file ID)
A file ID or resource key containing anything other than letters, digits, _ and -, e.g. {"ids":["../x"]}.
This assumes initialisation succeeded. The payload is only parsed after the clients have loaded, so if initialisation fails any state parameter, valid or not, shows the “Google Drive unavailable” dialog.
Download failures
The easiest way to make only the download fail is to block *googleapis.com/drive/v3/files/*alt=media* in DevTools: connecting, the picker and the name lookup still work.
A failed download shows a “Couldn't open from Google Drive” dialog (or “Couldn't open file from Google Drive” if the name is not known), with the underlying error under “Error details”.
The one exception is when Drive refuses access to the file (HTTP 403 or 404) on a deep link: that shows the “Authorize access to file” dialog instead, see File not yet authorised. On the sidebar flow a 403 or 404 is just a failed download.
Sidebar flow (block the pattern, then pick a file): the failure dialog should appear.
Deep link, for an already authorised file (block the pattern, then load the deep link): the failure dialog should appear, titled with the file's name, not the “Authorize access to file” dialog.
Deep link, the download after authorising fails (load a deep link for a file not yet authorised, click “Authorize File Access”, and block the pattern while the picker is showing, before picking the file): the failure dialog should appear.
Picker failures
Cancelling or closing the picker is not a failure: from the sidebar nothing happens, and on a deep link the flow ends silently.
If the picker cannot be built or reports an error, a “Couldn't open the Google Drive file picker” dialog should appear, with the error under “Error details”.
The picker's code is loaded during initialisation, so blocking requests afterwards does not trigger this. To simulate it, run this in the DevTools console after the page has loaded, then click “Open from GDrive”:
The “Share to GDrive” button is in the sidebar's current trace section. It is disabled under the same conditions as “Open from GDrive”, see Button state. It is also disabled for traces whose contents cannot be read back to upload, such as one loaded from trace_processor_shell --httpd; clicking it says why.
Open a trace from a local file.
Click “Share to GDrive”. The “Share to Google Drive” dialog should open with the file name filled in and focused, and “My Drive” as the location.
Clear the file name: “Upload” should be disabled and the field outlined in red. Type a name again.
Click “Upload” (or press Enter). Complete the Google popup if it asks. While it uploads, the button should show a spinner and the text beside the buttons should count up, e.g. “Uploading… 42% (12.3 of 29.4 MB)”. Use a large trace or DevTools Network throttling to make this last long enough to see.
The dialog should switch to “Saved to Google Drive”, and the file should appear in Drive under that name.
Repeat with a large trace (hundreds of MB). The upload is sent as a single request rather than with Drive's resumable upload; this has been seen to work with a 600 MB trace.
Cancel an upload
Start an upload that takes a while (large trace or DevTools Network throttling).
Click “Cancel”, which only appears while uploading. The upload should stop, the form should return to normal with no error banner, and no file should appear in Drive.
Click “Upload” again: it should start a fresh upload.
Click “Upload” and then “Cancel” while the Google popup is still open, then complete the popup: nothing should be uploaded.
Sidebar button state
The “Share to GDrive” button reflects what is happening, so an upload can be followed with the dialog closed. Clicking it always opens the dialog.
State
Text
Icon
Badge
Not uploaded
Share to GDrive
add_to_drive
preview
Uploading
Share to GDrive
add_to_drive
…, then a percentage
In Drive
Saved to GDrive
cloud_done
preview
Last action in the dialog failed
Share to GDrive
error
preview
Start an upload that takes a while and close the dialog. The badge should count up.
When it finishes, the button should change to “Saved to GDrive” with the cloud_done icon.
Make an upload fail (see Sharing failures). The icon should change to error, and back once an upload succeeds.
Open a trace from Drive: the button should show “Saved to GDrive” from the start.
Close the dialog while uploading
The dialog's state is kept with the trace, not with the dialog, so closing it does not stop or hide an upload.
Start an upload that takes a while, then close the dialog (Escape, or its close button).
Reopen it with “Share to GDrive” while the upload is still running. It should show the upload in progress, with the count still moving, the name and location fields disabled, and “Cancel” working. It should not offer a second “Upload”.
Close it again and wait for the upload to finish, then reopen it: it should show “Saved to Google Drive”.
Repeat, but make the upload fail while the dialog is closed (go offline in DevTools). Reopening it should show the red banner.
Type a different file name, close the dialog without uploading, and reopen it: the name you typed should still be there.
Close the trace while uploading
Start an upload that takes a while, then open a different trace (or close the current one).
The upload should be cancelled: in the DevTools Network tab the upload request should show as cancelled, and no file should appear in Drive.
The newly opened trace should show as not uploaded, with no progress on its “Share to GDrive” button.
Choose a folder
In the dialog, click “Change”. Complete the Google popup if it asks. The Google folder picker should open, titled “Select folder”.
Pick a folder: the location field should show its name, and the upload should go there.
Click “Change” again and cancel the picker: the location should not change, and no banner should appear.
Pick a folder inside a shared drive (under “Shared drives” in Drive, as opposed to “My Drive”) and upload: the file should appear there. Opening a trace that lives in a shared drive should work too.
After uploading
With the dialog showing “Saved to Google Drive”:
“Copy link” should copy a Perfetto link and briefly show “Copied”.
“Share” should open Google Drive's sharing dialog for the file.
“Open in Drive” should open the file in Drive in a new tab.
Close and reopen the dialog: it should still show the saved state.
Open a trace from Drive (sidebar or deep link), then click “Share to GDrive”: it should show the saved state straight away, without uploading.
Share link
“Copy link” produces a ?state=... deep link of the form <origin>/?state=%7B%22ids%22%3A%5B%22<FILE_ID>%22%5D%7D.
Paste it into a new tab. It should behave like the “Open with” deep link: connect (with the “Connect to Google Drive” dialog first unless popups are allowed), then the trace loads with its real name.
Open it as a different Google account that the file has been shared with. The “Authorize access to file” dialog should appear, see File not yet authorised.
With initialisation failing (see Initialisation fails), it should show the “Google Drive unavailable” dialog.
Sharing failures
Failures in the share dialog are shown in a red banner at the top of the dialog, not in a separate dialog, so the file name and location are kept. Where there is an underlying error it is under “Error details” in the banner. The banner clears when the next action starts, and the user retries by pressing the same button again.
Upload fails: block *upload/drive/v3/files* in DevTools and click “Upload”. The banner should say the trace could not be uploaded, and “Upload” should be usable again.
Drive rejects the upload: e.g. a full Drive. The banner‘s error details should include Drive’s explanation and the HTTP status. Not easy to provoke.
Connection fails: click “Upload”, “Change” or “Share” and press “Cancel” on Google's consent screen (with an account that has not granted access yet). The banner should say connecting to Google Drive failed.
Google popup closed: click “Upload” and close the Google popup. The banner should say the window was closed. Clicking “Upload” again should reopen the popup.
Folder picker or sharing dialog fails: not easy to provoke. The banner should say the folder picker or sharing dialog could not be opened.
No CSP violations
The Google dialogs are only allowed by specific Content Security Policy entries, see Content Security Policy. If one is missing the dialog does not appear, or appears blank, and the browser logs the reason.
Open DevTools and keep the Console visible. Reload the page.
Run each of these, and check that the dialog renders fully, icons included:
the Sidebar flow, through the Google popup and the picker;
The console should show no message starting “Refused to” (for example “Refused to frame ...” or “Refused to load the script ...”) that mentions a Google host.