Skip to content

Commit

Permalink
feat: Add basic support for the Common Access Token (#7651)
Browse files Browse the repository at this point in the history
Close #7649
  • Loading branch information
avelad authored Nov 25, 2024
1 parent 5c3f642 commit c10b796
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
36 changes: 36 additions & 0 deletions lib/net/networking_engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ shaka.net.NetworkingEngine = class extends shaka.util.FakeEventTarget {

/** @private {number} */
this.minBytesForProgressEvents_ = 16e3;

/** @private {!Map.<string, string>} */
this.hostCommonAccessTokenMap_ = new Map();
}

/**
Expand Down Expand Up @@ -246,6 +249,15 @@ shaka.net.NetworkingEngine = class extends shaka.util.FakeEventTarget {
this.responseFilters_.clear();
}

/**
* Clears Common Access Token map.
*
* @export
*/
clearCommonAccessTokenMap() {
this.hostCommonAccessTokenMap_.clear();
}

/**
* Gets a copy of the default retry parameters.
*
Expand Down Expand Up @@ -294,6 +306,7 @@ shaka.net.NetworkingEngine = class extends shaka.util.FakeEventTarget {
this.destroyed_ = true;
this.requestFilters_.clear();
this.responseFilters_.clear();
this.hostCommonAccessTokenMap_.clear();

// FakeEventTarget implements IReleasable
super.release();
Expand Down Expand Up @@ -538,6 +551,13 @@ shaka.net.NetworkingEngine = class extends shaka.util.FakeEventTarget {
}
const progressSupport = object.progressSupport;

// Add headers from CommonAccessToken
const commonAccessToken =
this.hostCommonAccessTokenMap_.get(uri.getDomain());
if (commonAccessToken) {
request.headers[shaka.net.NetworkingEngine.CommonAccessTokenHeaderName_] =
commonAccessToken;
}

// Every attempt must have an associated backoff.attempt() call so that the
// accounting is correct.
Expand Down Expand Up @@ -629,6 +649,14 @@ shaka.net.NetworkingEngine = class extends shaka.util.FakeEventTarget {
if (response.timeMs == undefined) {
response.timeMs = Date.now() - startTimeMs;
}
// Process headers to get the new CommonAccessToken
const commonAccessTokenHeader = response.headers[
shaka.net.NetworkingEngine.CommonAccessTokenHeaderName_];
if (commonAccessTokenHeader) {
const responseHost = new goog.Uri(response.uri);
this.hostCommonAccessTokenMap_.set(
responseHost.getDomain(), commonAccessTokenHeader);
}
const responseAndGotProgress = {
response: response,
gotProgress: gotProgress,
Expand Down Expand Up @@ -857,6 +885,14 @@ class extends shaka.util.AbortableOperation {
}
};

/**
* @const {string}
* @private
*/
shaka.net.NetworkingEngine.CommonAccessTokenHeaderName_ =
'common-access-token';


/**
* Request types. Allows a filter to decide which requests to read/alter.
*
Expand Down
4 changes: 4 additions & 0 deletions lib/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -1527,6 +1527,10 @@ shaka.Player = class extends shaka.util.FakeEventTarget {

this.completionPercent_ = NaN;

if (this.networkingEngine_) {
this.networkingEngine_.clearCommonAccessTokenMap();
}

// Make sure that the app knows of the new buffering state.
this.updateBufferState_();
} finally {
Expand Down
4 changes: 4 additions & 0 deletions test/test/util/fake_networking_engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ shaka.test.FakeNetworkingEngine = class {
this.setMinBytesForProgressEvents =
jasmine.createSpy('setMinBytesForProgressEvents').and.stub();

/** @type {!jasmine.Spy} */
this.clearCommonAccessTokenMap =
jasmine.createSpy('clearCommonAccessTokenMap').and.stub();

/** @private {number} */
this.maxUris_ = 1;

Expand Down

0 comments on commit c10b796

Please sign in to comment.