# cast.[framework](https://developers.google.com/cast/docs/reference/web_sender/cast.framework).CastContext

class static

Manages cast interaction. A singleton object that is fetched using
getInstance.
Manages states - provide the state of the cast icon, and session object.
Cast interaction is not supported till the cast options are provided by
setOptions.

## Constructor

### CastContext

new
CastContext()

## Methods

### getInstance

static

getInstance() returns [cast.framework.CastContext](https://developers.google.com/cast/docs/reference/web_sender/cast.framework.CastContext)

Returns the CastContext singleton instance.

Returns

:   `https://developers.google.com/cast/docs/reference/web_sender/cast.framework.CastContext` A singleton instance
    of the CastContext.

### addEventListener

addEventListener(type, handler)

Adds an event listener.

| #### Parameter ||
|---|---|
| type | [cast.framework.CastContextEventType](https://developers.google.com/cast/docs/reference/web_sender/cast.framework#.CastContextEventType) Event type. Value must not be null. |
| handler | (non-null function(non-null [cast.framework.CastStateEventData](https://developers.google.com/cast/docs/reference/web_sender/cast.framework.CastStateEventData)) or non-null function(non-null [cast.framework.SessionStateEventData](https://developers.google.com/cast/docs/reference/web_sender/cast.framework.SessionStateEventData))) |

### endCurrentSession

endCurrentSession(stopCasting)

Ends current session.

| #### Parameter ||
|---|---|
| stopCasting | boolean Should the receiver application be stopped or just disconnected. |

### getCastState

getCastState() returns [cast.framework.CastState](https://developers.google.com/cast/docs/reference/web_sender/cast.framework#.CastState)

Returns

:   `non-null https://developers.google.com/cast/docs/reference/web_sender/cast.framework#.CastState` Current cast icon state.

### getCurrentSession

getCurrentSession() returns [cast.framework.CastSession](https://developers.google.com/cast/docs/reference/web_sender/cast.framework.CastSession)

Returns

:   `nullable https://developers.google.com/cast/docs/reference/web_sender/cast.framework.CastSession` Current session.

### getSessionState

getSessionState() returns [cast.framework.SessionState](https://developers.google.com/cast/docs/reference/web_sender/cast.framework#.SessionState)

Returns

:   `non-null https://developers.google.com/cast/docs/reference/web_sender/cast.framework#.SessionState` Current session state.

### removeEventListener

removeEventListener(type, handler)

Removes an event listener.

| #### Parameter ||
|---|---|
| type | [cast.framework.CastContextEventType](https://developers.google.com/cast/docs/reference/web_sender/cast.framework#.CastContextEventType) Event type. Value must not be null. |
| handler | (non-null function(non-null [cast.framework.CastStateEventData](https://developers.google.com/cast/docs/reference/web_sender/cast.framework.CastStateEventData)) or non-null function(non-null [cast.framework.SessionStateEventData](https://developers.google.com/cast/docs/reference/web_sender/cast.framework.SessionStateEventData))) |

### requestSession

requestSession() returns Promise containing nullable [chrome.cast.ErrorCode](https://developers.google.com/cast/docs/reference/web_sender/chrome.cast#.ErrorCode)

Opens the cast selection UI, to allow user to start or stop session.

Throws

:   `Error` If the context options were not provided yet.

Returns

:   `non-null Promise containing nullable https://developers.google.com/cast/docs/reference/web_sender/chrome.cast#.ErrorCode` A promise to indicate session
    is starting, or failed (rejected with error code).

### setLaunchCredentialsData

setLaunchCredentialsData(credentialsData)

Sets the user credentials data. Will be ignored if called before
setOptions. Calling setOptions after setLaunchCredentialsData will
clear the value set in setLaunchCredentialsData.

| #### Parameter ||
|---|---|
| credentialsData | [chrome.cast.CredentialsData](https://developers.google.com/cast/docs/reference/web_sender/chrome.cast.CredentialsData) Credentials data used to identify the credentials of the sender. Value must not be null. |

### setOptions

setOptions(options)

Sets the cast options.
Events will only be sent once config was provided.

| #### Parameter ||
|---|---|
| options | [cast.framework.CastOptions](https://developers.google.com/cast/docs/reference/web_sender/cast.framework.CastOptions) Cast options for the page. Value must not be null. |

Throws

:   `non-null Error` If the options are missing application id.