Skip to content

Commit

Permalink
Checkpointing before removing grpc-web.
Browse files Browse the repository at this point in the history
Javascript implementation of protobuf uses eval (https://github.com/protocolbuffers/protobuf/issues/7778), which isn't allowed in Chrome extensions v3 manifest.  But we need v3 manifest to use `chrome.tabGroups` API, so I don't see an easy way to use grpc from a Chrome plugin.  And, at the end of the day, we don't really gain that much from grpc in that small kind of project.
  • Loading branch information
x746e committed Mar 7, 2021
1 parent f0531c4 commit 31aa348
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 6 deletions.
12 changes: 12 additions & 0 deletions chrome.proto
Expand Up @@ -6,8 +6,20 @@ package trackd;

service Chrome {
rpc session_changed(SessionChangedRequest) returns (google.protobuf.Empty);

rpc set_session_for_window_id(SetSessionForWindowIdRequest) returns (google.protobuf.Empty);
rpc set_active_window(SetActiveWindowRequest) returns (google.protobuf.Empty);
}

message SessionChangedRequest {
string session_name = 1;
}

message SetSessionForWindowIdRequest {
string session_name = 1;
int64 window_id = 2;
}

message SetActiveWindowRequest {
int64 window_id = 3;
}
38 changes: 38 additions & 0 deletions chrome_extension/client_chrome.js
Expand Up @@ -4,6 +4,44 @@ chrome.runtime.onInstalled.addListener(function() {
const {SessionChangedRequest} = require('./chrome_pb.js');
const {ChromeClient} = require('./chrome_grpc_web_pb.js');

function onFocusChanged(windowId) {
console.log(`${windowId} is focused`);

let groupId = windowToSessionMap.get(windowId);
chrome.tabGroups.get(groupId, group => {
console.log(`groupId: ${groupId}, group: ${group}`);
});
}

chrome.windows.onFocusChanged.addListener(onFocusChanged);

let windowToSessionMap = new Map();
windowToSessionMap.set(-1, -1);

chrome.windows.getAll({populate: true}, windows => {
console.log(windows);

windowToSessionMap.clear();

for (let window_ of windows) {
for (let tab of window_.tabs) {
if (tab.groupId != -1) {
windowToSessionMap.set(window_.id, tab.groupId);
break;
}
}
windowToSessionMap.set(window_.id, -2);
}

// The assumption is that as we just started, the Chrome should still be focused.
// It may be good to check in trackd though that Chrome's XWindow is indeed focused
// when this plugin reports so.
windows.getLastFocused({}, window_ => {
onFocusChanged(window_.id);
});
});


let client = new ChromeClient('http://localhost:3142', null, null);

let request = new SessionChangedRequest();
Expand Down
2 changes: 1 addition & 1 deletion chrome_extension/main.js

Large diffs are not rendered by default.

9 changes: 4 additions & 5 deletions chrome_extension/manifest.json
Expand Up @@ -6,12 +6,11 @@
"activeTab",
"declarativeContent",
"sessions",
"storage"
"storage",
"tabGroups"
],
"background": {
"scripts": ["main.js"],
"persistent": false
"service_worker": "main.js"
},
"manifest_version": 2,
"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'"
"manifest_version": 3
}

0 comments on commit 31aa348

Please sign in to comment.