Example usage for com.google.gwt.chrome.crx.client.events MessageEvent.Message cast

List of usage examples for com.google.gwt.chrome.crx.client.events MessageEvent.Message cast

Introduction

In this page you can find the example usage for com.google.gwt.chrome.crx.client.events MessageEvent.Message cast.

Prototype

@Override
@SuppressWarnings("unchecked")
public <T extends JavascriptObjectEquivalent> T cast() 

Source Link

Document

A helper method to enable cross-casting from any JavaScriptObject type to any other JavaScriptObject type.

Usage

From source file:com.google.speedtracer.client.BackgroundPage.java

License:Apache License

/**
 * Helper function that loads data from a file. This should only get called
 * when the port name is either {@link DataLoader.DATA_LOAD} or
 * {@link DataLoader.RAW_DATA_LOAD}./*from ww w  . j  a  va 2  s.c  om*/
 */
private void doDataLoad(final Port port) {
    BrowserConnectionState browserConn = browserConnectionMap.get(FILE_BROWSER_ID);
    if (browserConn == null) {
        browserConn = new BrowserConnectionState();
        browserConnectionMap.put(FILE_BROWSER_ID, browserConn);
    }

    // In situation where we open a file in a tab that was previously
    // used to open a file... we dont care. Overwrite it.
    final TabModel tabModel = new TabModel(browserAction.mtIcon());
    int tabId = port.getSender().getTab().getId();

    if (port.getName().equals(DataLoader.DATA_LOAD)) {
        final LoadFileDataInstance dataInstance = LoadFileDataInstance.create(port);
        tabModel.dataInstance = dataInstance;
        browserConn.tabMap.put(tabId, tabModel);

        // Connect the datainstance to receive data from the data_loader.
        port.getOnMessageEvent().addListener(new MessageEvent.Listener() {
            VersionedRecordConverter converter;
            boolean receivedFirstMessage;

            public void onMessage(MessageEvent.Message message) {
                if (!receivedFirstMessage) {
                    receivedFirstMessage = true;
                    dataInstance.onTimelineProfilerStarted();
                }
                EventRecordMessage eventRecordMessage = message.cast();
                if (!getVersion().equals(eventRecordMessage.getVersion())) {
                    if (converter == null) {
                        converter = VersionedRecordConverter.create(eventRecordMessage.getVersion());
                    }
                    converter.convert(dataInstance, eventRecordMessage.getEventRecord());
                    return;
                }
                dataInstance.onEventRecord(eventRecordMessage.getEventRecord());
            }
        });
    } else {
        // We are dealing with RAW data (untransformed inspector data) that still
        // needs conversion.
        final Proxy proxy = new Proxy(tabId) {
            @Override
            protected void connectToDataSource() {
                // Tell the data_loader content script to start sending.
                port.postMessage(LoadFileDataInstance.createAck());
            }
        };

        // Connect the DataInstance to receive data from the data_loader
        port.getOnMessageEvent().addListener(new MessageEvent.Listener() {
            boolean receivedFirstMessage;

            public void onMessage(MessageEvent.Message message) {
                if (!receivedFirstMessage) {
                    receivedFirstMessage = true;
                    tabModel.dataInstance.onTimelineProfilerStarted();
                }
                PageEventMessage pageEventMessage = message.cast();
                // We don't support versioning for RAW data since it would mean
                // maintaining support for multiple Chrome versions. We assume
                // that RAW data should always be the same format as the current
                // Chrome build.
                proxy.dispatchDebuggerEventRecord(pageEventMessage.getDebuggerRecord());
            }
        });

        tabModel.dataInstance = ChromeDebuggerDataInstance.create(proxy);
        browserConn.tabMap.put(tabId, tabModel);
    }

    tabModel.tabDescription = TabDescription.create(tabId, port.getSender().getTab().getTitle(),
            port.getSender().getTab().getUrl());
    openMonitor(FILE_BROWSER_ID, tabId, tabModel);
}