text:
/**
 * @implements {Timeline.TimelineLifecycleDelegate}
 * @implements {Timeline.TimelineModeViewDelegate}
 * @implements {UI.Searchable}
 * @unrestricted
 */
Timeline.TimelinePanel = class extends UI.Panel {
  constructor() {
    super('timeline');
    this.registerRequiredCSS('timeline/timelinePanel.css');
    this.element.addEventListener('contextmenu', this._contextMenu.bind(this), false);
    this._dropTarget = new UI.DropTarget(
        this.element, [UI.DropTarget.Types.Files, UI.DropTarget.Types.URIList],
        Common.UIString('Drop timeline file or URL here'), this._handleDrop.bind(this));

    /** @type {!Array<!UI.ToolbarItem>} */
    this._recordingOptionUIControls = [];
    this._state = Timeline.TimelinePanel.State.Idle;
    this._windowStartTime = 0;
    this._windowEndTime = Infinity;
    this._millisecondsToRecordAfterLoadEvent = 3000;
    this._toggleRecordAction =
        /** @type {!UI.Action }*/ (UI.actionRegistry.action('timeline.toggle-recording'));

    /** @type {!Array<!TimelineModel.TimelineModel.Filter>} */
    this._filters = [];
    if (!Runtime.experiments.isEnabled('timelineShowAllEvents')) {
      this._filters.push(Timeline.TimelineUIUtils.visibleEventsFilter());
      this._filters.push(new TimelineModel.ExcludeTopLevelFilter());
    }

    // Create models.
    this._tracingModelBackingStorage = new Bindings.TempFileBackingStorage('tracing');
    this._tracingModel = new SDK.TracingModel(this._tracingModelBackingStorage);
    this._model = new TimelineModel.TimelineModel(Timeline.TimelineUIUtils.visibleEventsFilter());
    this._frameModel =
        new TimelineModel.TimelineFrameModel(event => Timeline.TimelineUIUtils.eventStyle(event).category.name);
    this._filmStripModel = new SDK.FilmStripModel(this._tracingModel);
    this._irModel = new TimelineModel.TimelineIRModel();
    /** @type {!Array<!{title: string, model: !SDK.TracingModel}>} */
    this._extensionTracingModels = [];
    this._cpuThrottlingManager = new Components.CPUThrottlingManager();

    /** @type {!Array<!Timeline.TimelineModeView>} */
    this._currentViews = [];

    this._disableCaptureJSProfileSetting = Common.settings.createSetting('timelineDisableJSSampling', false);
    this._captureLayersAndPicturesSetting = Common.settings.createSetting('timelineCaptureLayersAndPictures', false);

    this._showScreenshotsSetting = Common.settings.createLocalSetting('timelineShowScreenshots', true);
    this._showScreenshotsSetting.addChangeListener(this._onModeChanged, this);
    this._showMemorySetting = Common.settings.createLocalSetting('timelineShowMemory', false);
    this._showMemorySetting.addChangeListener(this._onModeChanged, this);

    this._panelToolbar = new UI.Toolbar('', this.element);
    this._createSettingsPane();
    this._updateShowSettingsToolbarButton();

    this._timelinePane = new UI.VBox();
    this._timelinePane.show(this.element);
    var topPaneElement = this._timelinePane.element.createChild('div', 'hbox');
    topPaneElement.id = 'timeline-overview-panel';

    // Create top overview component.
    this._overviewPane = new PerfUI.TimelineOverviewPane('timeline');
    this._overviewPane.addEventListener(
        PerfUI.TimelineOverviewPane.Events.WindowChanged, this._onWindowChanged.bind(this));
    this._overviewPane.show(topPaneElement);
    this._statusPaneContainer = this._timelinePane.element.createChild('div', 'status-pane-container fill');

    this._createFileSelector();

    SDK.targetManager.addEventListener(SDK.TargetManager.Events.PageReloadRequested, this._pageReloadRequested, this);
    SDK.targetManager.addEventListener(SDK.TargetManager.Events.Load, this._loadEventFired, this);

    // Create top level properties splitter.
    this._detailsSplitWidget = new UI.SplitWidget(false, true, 'timelinePanelDetailsSplitViewState');
    this._detailsSplitWidget.element.classList.add('timeline-details-split');
    this._detailsView =
        new Timeline.TimelineDetailsView(this._model, this._frameModel, this._filmStripModel, this._filters, this);
    this._detailsSplitWidget.installResizer(this._detailsView.headerElement());
    this._detailsSplitWidget.setSidebarWidget(this._detailsView);

    this._searchableView = new UI.SearchableView(this);
    this._searchableView.setMinimumSize(0, 100);
    this._searchableView.element.classList.add('searchable-view');
    this._detailsSplitWidget.setMainWidget(this._searchableView);

    this._stackView = new UI.StackView(false);
    this._stackView.element.classList.add('timeline-view-stack');

    this._stackView.show(this._searchableView.element);
    this._onModeChanged();

    this._populateToolbar();
    this._showLandingPage();

    Extensions.extensionServer.addEventListener(
        Extensions.ExtensionServer.Events.TraceProviderAdded, this._appendExtensionsToToolbar, this);

    this._detailsSplitWidget.show(this._timelinePane.element);
    this._detailsSplitWidget.hideSidebar();
    SDK.targetManager.addEventListener(SDK.TargetManager.Events.SuspendStateChanged, this._onSuspendStateChanged, this);

    /** @type {!SDK.TracingModel.Event}|undefined */
    this._selectedSearchResult;
    /** @type {!Array<!SDK.TracingModel.Event>}|undefined */
    this._searchResults;
    /** @type {?symbol} */
    this._sessionGeneration = null;
    /** @type {number} */
    this._recordingStartTime = 0;
  }

  /**
   * @return {!Timeline.TimelinePanel}
   */
  static instance() {
    return /** @type {!Timeline.TimelinePanel} */ (self.runtime.sharedInstance(Timeline.TimelinePanel));
  }

  /**
   * @override
   * @return {?UI.SearchableView}
   */
  searchableView() {
    return this._searchableView;
  }

  /**
   * @override
   */
  wasShown() {
    UI.context.setFlavor(Timeline.TimelinePanel, this);
  }

  /**
   * @override
   */
  willHide() {
    UI.context.setFlavor(Timeline.TimelinePanel, null);
  }

  /**
   * @return {number}
   */
  windowStartTime() {
    if (this._windowStartTime)
      return this._windowStartTime;
    return this._model.minimumRecordTime();
  }

  /**
   * @return {number}
   */
  windowEndTime() {
    if (this._windowEndTime < Infinity)
      return this._windowEndTime;
    return this._model.maximumRecordTime() || Infinity;
  }

  /**
   * @param {!Common.Event} event
   */
  _onWindowChanged(event) {
    this._windowStartTime = event.data.startTime;
    this._windowEndTime = event.data.endTime;

    for (var i = 0; i < this._currentViews.length; ++i)
      this._currentViews[i].setWindowTimes(this._windowStartTime, this._windowEndTime);

    if (!this._selection || this._selection.type() === Timeline.TimelineSelection.Type.Range)
      this.select(null);
  }

  /**
   * @param {!Common.Event} event
   */
  _onOverviewSelectionChanged(event) {
    var selection = /** @type {!Timeline.TimelineSelection} */ (event.data);
    this.select(selection);
  }

  /**
   * @override
   * @param {number} windowStartTime
   * @param {number} windowEndTime
   */
  requestWindowTimes(windowStartTime, windowEndTime) {
    this._overviewPane.requestWindowTimes(windowStartTime, windowEndTime);
  }

  /**
   * @param {!Timeline.TimelineModeView} modeView
   */
  _addModeView(modeView) {
    modeView.setWindowTimes(this.windowStartTime(), this.windowEndTime());
    modeView.refreshRecords();
    var splitWidget =
        this._stackView.appendView(modeView.view(), 'timelinePanelTimelineStackSplitViewState', undefined, 112);
    var resizer = modeView.resizerElement();
    if (splitWidget && resizer) {
      splitWidget.hideDefaultResizer();
      splitWidget.installResizer(resizer);
    }
    this._currentViews.push(modeView);
  }

  _removeAllModeViews() {
    this._currentViews.forEach(view => view.dispose());
    this._currentViews = [];
    this._stackView.detachChildWidgets();
  }

  /**
   * @param {!Timeline.TimelinePanel.State} state
   */
  _setState(state) {
    this._state = state;
    this._updateTimelineControls();
  }

  /**
   * @param {string} name
   * @param {!Common.Setting} setting
   * @param {string} tooltip
   * @return {!UI.ToolbarItem}
   */
  _createSettingCheckbox(name, setting, tooltip) {
    const checkboxItem = new UI.ToolbarCheckbox(name, tooltip, setting);
    this._recordingOptionUIControls.push(checkboxItem);
    return checkboxItem;
  }

  _populateToolbar() {
    // Record
    this._panelToolbar.appendToolbarItem(UI.Toolbar.createActionButton(this._toggleRecordAction));
    this._panelToolbar.appendToolbarItem(UI.Toolbar.createActionButtonForId('main.reload'));
    var clearButton = new UI.ToolbarButton(Common.UIString('Clear'), 'largeicon-clear');
    clearButton.addEventListener(UI.ToolbarButton.Events.Click, () => this._clear());
    this._panelToolbar.appendToolbarItem(clearButton);
    this._panelToolbar.appendSeparator();

    // View
    this._panelToolbar.appendSeparator();
    this._showScreenshotsToolbarCheckbox = this._createSettingCheckbox(
        Common.UIString('Screenshots'), this._showScreenshotsSetting, Common.UIString('Capture screenshots'));
    this._panelToolbar.appendToolbarItem(this._showScreenshotsToolbarCheckbox);

    this._showMemoryToolbarCheckbox = this._createSettingCheckbox(
        Common.UIString('Memory'), this._showMemorySetting, Common.UIString('Show memory timeline.'));
    this._panelToolbar.appendToolbarItem(this._showMemoryToolbarCheckbox);

    // Settings
    this._panelToolbar.appendToolbarItem(this._showSettingsPaneButton);

    // GC
    this._panelToolbar.appendToolbarItem(UI.Toolbar.createActionButtonForId('components.collect-garbage'));
  }

  _createSettingsPane() {
    this._showSettingsPaneSetting = Common.settings.createSetting('timelineShowSettingsToolbar', false);
    this._showSettingsPaneButton = new UI.ToolbarSettingToggle(
        this._showSettingsPaneSetting, 'largeicon-settings-gear', Common.UIString('Capture settings'));
    SDK.multitargetNetworkManager.addEventListener(
        SDK.MultitargetNetworkManager.Events.ConditionsChanged, this._updateShowSettingsToolbarButton, this);
    this._cpuThrottlingManager.addEventListener(
        Components.CPUThrottlingManager.Events.RateChanged, this._updateShowSettingsToolbarButton, this);
    this._disableCaptureJSProfileSetting.addChangeListener(this._updateShowSettingsToolbarButton, this);
    this._captureLayersAndPicturesSetting.addChangeListener(this._updateShowSettingsToolbarButton, this);

    this._settingsPane = new UI.HBox();
    this._settingsPane.element.classList.add('timeline-settings-pane');
    this._settingsPane.show(this.element);

    var captureToolbar = new UI.Toolbar('', this._settingsPane.element);
    captureToolbar.element.classList.add('flex-auto');
    captureToolbar.makeVertical();
    captureToolbar.appendToolbarItem(this._createSettingCheckbox(
        Common.UIString('Disable JavaScript Samples'), this._disableCaptureJSProfileSetting,
        Common.UIString('Disables JavaScript sampling, reduces overhead when running against mobile devices')));
    captureToolbar.appendToolbarItem(this._createSettingCheckbox(
        Common.UIString('Enable advanced paint instrumentation (slow)'), this._captureLayersAndPicturesSetting,
        Common.UIString('Captures advanced paint instrumentation, introduces significant performance overhead')));

    var throttlingPane = new UI.VBox();
    throttlingPane.element.classList.add('flex-auto');
    throttlingPane.show(this._settingsPane.element);

    var throttlingToolbar1 = new UI.Toolbar('', throttlingPane.element);
    throttlingToolbar1.appendText(Common.UIString('Network:'));
    throttlingToolbar1.appendToolbarItem(this._createNetworkConditionsSelect());
    var throttlingToolbar2 = new UI.Toolbar('', throttlingPane.element);
    throttlingToolbar2.appendText(Common.UIString('CPU:'));
    throttlingToolbar2.appendToolbarItem(this._cpuThrottlingManager.createControl());

    this._showSettingsPaneSetting.addChangeListener(this._updateSettingsPaneVisibility.bind(this));
    this._updateSettingsPaneVisibility();
  }

  /**
    * @param {!Common.Event} event
    */
  _appendExtensionsToToolbar(event) {
    var provider = /** @type {!Extensions.ExtensionTraceProvider} */ (event.data);
    const setting = Timeline.TimelinePanel._settingForTraceProvider(provider);
    const checkbox = this._createSettingCheckbox(provider.shortDisplayName(), setting, provider.longDisplayName());
    this._panelToolbar.appendToolbarItem(checkbox);
  }

  /**
   * @param {!Extensions.ExtensionTraceProvider} traceProvider
   * @return {!Common.Setting<boolean>}
   */
  static _settingForTraceProvider(traceProvider) {
    var setting = traceProvider[Timeline.TimelinePanel._traceProviderSettingSymbol];
    if (!setting) {
      var providerId = traceProvider.persistentIdentifier();
      setting = Common.settings.createSetting(providerId, false);
      traceProvider[Timeline.TimelinePanel._traceProviderSettingSymbol] = setting;
    }
    return setting;
  }

  /**
   * @return {!UI.ToolbarComboBox}
   */
  _createNetworkConditionsSelect() {
    var toolbarItem = new UI.ToolbarComboBox(null);
    toolbarItem.setMaxWidth(140);
    Components.NetworkConditionsSelector.decorateSelect(toolbarItem.selectElement());
    return toolbarItem;
  }

  _prepareToLoadTimeline() {
    console.assert(this._state === Timeline.TimelinePanel.State.Idle);
    this._setState(Timeline.TimelinePanel.State.Loading);
  }

  _createFileSelector() {
    if (this._fileSelectorElement)
      this._fileSelectorElement.remove();
    this._fileSelectorElement = UI.createFileSelectorElement(this._loadFromFile.bind(this));
    this._timelinePane.element.appendChild(this._fileSelectorElement);
  }

  /**
   * @param {!Event} event
   */
  _contextMenu(event) {
    var contextMenu = new UI.ContextMenu(event);
    contextMenu.appendItemsAtLocation('timelineMenu');
    contextMenu.show();
  }

  /**
   * @return {boolean}
   */
  _saveToFile() {
    if (this._state !== Timeline.TimelinePanel.State.Idle)
      return true;
    if (this._model.isEmpty())
      return true;

    var now = new Date();
    var fileName = 'Profile-' + now.toISO8601Compact() + '.json';
    var stream = new Bindings.FileOutputStream();

    /**
     * @param {boolean} accepted
     * @this {Timeline.TimelinePanel}
     */
    function callback(accepted) {
      if (!accepted)
        return;
      var saver = new Timeline.TracingTimelineSaver();
      this._tracingModelBackingStorage.writeToStream(stream, saver);
    }
    stream.open(fileName, callback.bind(this));
    return true;
  }

  /**
   * @return {boolean}
   */
  _selectFileToLoad() {
    this._fileSelectorElement.click();
    return true;
  }

  /**
   * @param {!File} file
   */
  _loadFromFile(file) {
    if (this._state !== Timeline.TimelinePanel.State.Idle)
      return;
    this._prepareToLoadTimeline();
    this._loader = Timeline.TimelineLoader.loadFromFile(this._tracingModel, file, this);
    this._createFileSelector();
  }

  /**
   * @param {string} url
   */
  _loadFromURL(url) {
    if (this._state !== Timeline.TimelinePanel.State.Idle)
      return;
    this._prepareToLoadTimeline();
    this._loader = Timeline.TimelineLoader.loadFromURL(this._tracingModel, url, this);
  }

  _refreshViews() {
    this._currentViews.forEach(view => view.refreshRecords());
  }

  _onModeChanged() {
    const showMemory = this._showMemorySetting.get();
    const showScreenshots = this._showScreenshotsSetting.get();
    // Set up overview controls.
    this._overviewControls = [];
    this._overviewControls.push(new Timeline.TimelineEventOverviewResponsiveness(this._model, this._frameModel));
    if (Runtime.experiments.isEnabled('inputEventsOnTimelineOverview'))
      this._overviewControls.push(new Timeline.TimelineEventOverviewInput(this._model));
    this._overviewControls.push(new Timeline.TimelineEventOverviewFrames(this._model, this._frameModel));
    this._overviewControls.push(new Timeline.TimelineEventOverviewCPUActivity(this._model));
    this._overviewControls.push(new Timeline.TimelineEventOverviewNetwork(this._model));
    if (showScreenshots)
      this._overviewControls.push(new Timeline.TimelineFilmStripOverview(this._model, this._filmStripModel));
    if (showMemory)
      this._overviewControls.push(new Timeline.TimelineEventOverviewMemory(this._model));
    this._overviewPane.setOverviewControls(this._overviewControls);

    // Set up the main view.
    this._removeAllModeViews();
    this._flameChart = new Timeline.TimelineFlameChartView(
        this, this._model, this._frameModel, this._irModel, this._extensionTracingModels, this._filters);
    this._addModeView(this._flameChart);

    if (showMemory) {
      this._addModeView(
          new Timeline.MemoryCountersGraph(this, this._model, [Timeline.TimelineUIUtils.visibleEventsFilter()]));
    }

    this.doResize();
    this.select(null);
  }

  _updateSettingsPaneVisibility() {
    if (this._showSettingsPaneSetting.get())
      this._settingsPane.showWidget();
    else
      this._settingsPane.hideWidget();
  }

  _updateShowSettingsToolbarButton() {
    var messages = [];
    if (this._cpuThrottlingManager.rate() !== 1)
      messages.push(Common.UIString('- CPU throttling is enabled'));
    if (SDK.multitargetNetworkManager.isThrottling())
      messages.push(Common.UIString('- Network throttling is enabled'));
    if (this._captureLayersAndPicturesSetting.get())
      messages.push(Common.UIString('- Significant overhead due to paint instrumentation'));
    if (this._disableCaptureJSProfileSetting.get())
      messages.push(Common.UIString('- JavaScript sampling is disabled'));

    this._showSettingsPaneButton.setDefaultWithRedColor(messages.length);
    this._showSettingsPaneButton.setToggleWithRedColor(messages.length);

    if (messages.length) {
      var tooltipElement = createElement('div');
      messages.forEach(message => {
        tooltipElement.createChild('div').textContent = message;
      });
      this._showSettingsPaneButton.setTitle(tooltipElement);
    } else {
      this._showSettingsPaneButton.setTitle(Common.UIString('Capture settings'));
    }
  }

  /**
   * @param {boolean} enabled
   */
  _setUIControlsEnabled(enabled) {
    this._recordingOptionUIControls.forEach(control => control.setEnabled(enabled));
  }

  /**
   * @param {boolean} userInitiated
   */
  _startRecording(userInitiated) {
    console.assert(!this._statusPane, 'Status pane is already opened.');
    var mainTarget = SDK.targetManager.mainTarget();
    if (!mainTarget)
      return;
    this._setState(Timeline.TimelinePanel.State.StartPending);
    this._showRecordingStarted();

    this._sessionGeneration = Symbol('timelineSessionGeneration');
    this._autoRecordGeneration = userInitiated ? null : Symbol('Generation');
    var enabledTraceProviders = Extensions.extensionServer.traceProviders().filter(
        provider => Timeline.TimelinePanel._settingForTraceProvider(provider).get());

    const recordingOptions = {
      enableJSSampling: !this._disableCaptureJSProfileSetting.get(),
      capturePictures: this._captureLayersAndPicturesSetting.get(),
      captureFilmStrip: this._showScreenshotsSetting.get()
    };

    this._controller = new Timeline.TimelineController(mainTarget, this, this._tracingModel);
    this._controller.startRecording(recordingOptions, enabledTraceProviders);
    this._recordingStartTime = Date.now();

    for (var i = 0; i < this._overviewControls.length; ++i)
      this._overviewControls[i].timelineStarted();

    Host.userMetrics.actionTaken(
        userInitiated ? Host.UserMetrics.Action.TimelineStarted : Host.UserMetrics.Action.TimelinePageReloadStarted);
    this._setUIControlsEnabled(false);
    this._hideLandingPage();
  }

  _stopRecording() {
    if (this._statusPane) {
      this._statusPane.finish();
      this._statusPane.updateStatus(Common.UIString('Stopping timeline\u2026'));
      this._statusPane.updateProgressBar(Common.UIString('Received'), 0);
    }
    this._setState(Timeline.TimelinePanel.State.StopPending);
    this._autoRecordGeneration = null;
    this._controller.stopRecording();
    this._controller = null;
    this._setUIControlsEnabled(true);
  }

  _onSuspendStateChanged() {
    this._updateTimelineControls();
  }

  _updateTimelineControls() {
    var state = Timeline.TimelinePanel.State;
    this._toggleRecordAction.setToggled(this._state === state.Recording);
    this._toggleRecordAction.setEnabled(this._state === state.Recording || this._state === state.Idle);
    this._panelToolbar.setEnabled(this._state !== state.Loading);
    this._dropTarget.setEnabled(this._state === state.Idle);
  }

  _toggleRecording() {
    if (this._state === Timeline.TimelinePanel.State.Idle)
      this._startRecording(true);
    else if (this._state === Timeline.TimelinePanel.State.Recording)
      this._stopRecording();
  }

  _clear() {
    this._showLandingPage();
    this._detailsSplitWidget.hideSidebar();
    this._sessionGeneration = null;
    this._recordingStartTime = 0;
    this._reset();
  }

  _reset() {
    PerfUI.LineLevelProfile.instance().reset();
    this._tracingModel.reset();
    this._model.reset();
    for (let extensionEntry of this._extensionTracingModels)
      extensionEntry.model.reset();
    this._extensionTracingModels.splice(0);

    this.requestWindowTimes(0, Infinity);
    delete this._selection;
    this._frameModel.reset();
    this._filmStripModel.reset(this._tracingModel);
    this._overviewPane.reset();
    this._currentViews.forEach(view => view.reset());
    this._overviewControls.forEach(overview => overview.reset());
    this.select(null);
  }

  /**
   * @override
   */
  recordingStarted() {
    this._reset();
    this._setState(Timeline.TimelinePanel.State.Recording);
    this._showRecordingStarted();
    this._statusPane.updateStatus(Common.UIString('Profiling\u2026'));
    this._statusPane.updateProgressBar(Common.UIString('Buffer usage'), 0);
    this._statusPane.startTimer();
    this._hideLandingPage();
  }

  /**
   * @override
   * @param {number} usage
   */
  recordingProgress(usage) {
    this._statusPane.updateProgressBar(Common.UIString('Buffer usage'), usage * 100);
  }

  /**
   * @override
   * @param {string} title
   * @param {!SDK.TracingModel} tracingModel
   * @param {number} timeOffset
   */
  addExtensionEvents(title, tracingModel, timeOffset) {
    this._extensionTracingModels.push({title: title, model: tracingModel, timeOffset: timeOffset});
    if (this._state !== Timeline.TimelinePanel.State.Idle)
      return;
    tracingModel.adjustTime(this._model.minimumRecordTime() + (timeOffset / 1000) - this._recordingStartTime);
    for (let view of this._currentViews)
      view.extensionDataAdded();
  }

  /**
   * @override
   * @return {?symbol}
   */
  sessionGeneration() {
    return this._sessionGeneration;
  }

  _showLandingPage() {
    if (this._landingPage) {
      this._landingPage.show(this._statusPaneContainer);
      return;
    }

    /**
     * @param {string} tagName
     * @param {string} contents
     */
    function encloseWithTag(tagName, contents) {
      var e = createElement(tagName);
      e.textContent = contents;
      return e;
    }

    var learnMoreNode = UI.createExternalLink(
        'https://developers.google.com/web/tools/chrome-devtools/evaluate-performance/', Common.UIString('Learn more'));
    var recordNode =
        encloseWithTag('b', UI.shortcutRegistry.shortcutDescriptorsForAction('timeline.toggle-recording')[0].name);
    var reloadNode = encloseWithTag('b', UI.shortcutRegistry.shortcutDescriptorsForAction('main.reload')[0].name);
    var navigateNode = encloseWithTag('b', Common.UIString('WASD'));

    this._landingPage = new UI.VBox();
    this._landingPage.contentElement.classList.add('timeline-landing-page', 'fill');
    var centered = this._landingPage.contentElement.createChild('div');

    var p = centered.createChild('p');
    p.createTextChild(Common.UIString(
        'The Performance panel lets you record what the browser does during page load and user interaction. ' +
        'The timeline it generates can help you determine why certain parts of your page are slow.'));

    p = centered.createChild('p');
    p.appendChild(UI.formatLocalized(
        'To capture a new recording, click the record toolbar button or hit %s. ' +
        'To evaluate page load performance, hit %s to record the reload.',
        [recordNode, reloadNode]));

    p = centered.createChild('p');
    p.appendChild(UI.formatLocalized(
        'After recording, select an area of interest in the overview by dragging. ' +
        'Then, zoom and pan the timeline with the mousewheel or %s keys.',
        [navigateNode]));

    p = centered.createChild('p');
    p.appendChild(learnMoreNode);

    var timelineSpan = encloseWithTag('b', Common.UIString('Timeline'));
    var performanceSpan = encloseWithTag('b', Common.UIString('Performance'));

    p = centered.createChild('p', 'timeline-landing-warning');
    p.appendChild(UI.formatLocalized(
        'The %s panel has been enriched with the JavaScript profiler capabilities and is now called %s.%s' +
        'You can find the legacy JavaScript CPU profiler under %s%s \u2192 More Tools \u2192 JavaScript Profiler.',
        [timelineSpan, performanceSpan, createElement('p'), createElement('br'), UI.Icon.create('largeicon-menu')]));

    this._landingPage.show(this._statusPaneContainer);
  }

  _hideLandingPage() {
    this._landingPage.detach();
  }

  /**
   * @override
   */
  loadingStarted() {
    this._hideLandingPage();

    if (this._statusPane)
      this._statusPane.hide();
    this._statusPane = new Timeline.TimelinePanel.StatusPane(false, this._cancelLoading.bind(this));
    this._statusPane.showPane(this._statusPaneContainer);
    this._statusPane.updateStatus(Common.UIString('Loading profile\u2026'));
    // FIXME: make loading from backend cancelable as well.
    if (!this._loader)
      this._statusPane.finish();
    this.loadingProgress(0);
  }

  /**
   * @override
   * @param {number=} progress
   */
  loadingProgress(progress) {
    if (typeof progress === 'number')
      this._statusPane.updateProgressBar(Common.UIString('Received'), progress * 100);
  }

  /**
   * @override
   * @param {boolean} success
   */
  loadingComplete(success) {
    var loadedFromFile = !!this._loader;
    delete this._loader;
    this._setState(Timeline.TimelinePanel.State.Idle);

    if (!success) {
      this._statusPane.hide();
      delete this._statusPane;
      this._clear();
      return;
    }

    if (this._statusPane)
      this._statusPane.updateStatus(Common.UIString('Processing profile\u2026'));
    this._model.setEvents(this._tracingModel, loadedFromFile);
    this._frameModel.reset();
    this._frameModel.addTraceEvents(
        SDK.targetManager.mainTarget(), this._model.inspectedTargetEvents(), this._model.sessionId() || '');
    this._filmStripModel.reset(this._tracingModel);
    var groups = TimelineModel.TimelineModel.AsyncEventGroup;
    var asyncEventsByGroup = this._model.mainThreadAsyncEvents();
    this._irModel.populate(asyncEventsByGroup.get(groups.input), asyncEventsByGroup.get(groups.animation));
    this._model.cpuProfiles().forEach(profile => PerfUI.LineLevelProfile.instance().appendCPUProfile(profile));
    if (this._statusPane)
      this._statusPane.hide();
    delete this._statusPane;

    for (let entry of this._extensionTracingModels)
      entry.model.adjustTime(this._model.minimumRecordTime() + (entry.timeOffset / 1000) - this._recordingStartTime);

    this._flameChart.resizeToPreferredHeights();
    this._overviewPane.reset();
    this._overviewPane.setBounds(this._model.minimumRecordTime(), this._model.maximumRecordTime());
    this._setAutoWindowTimes();
    this._refreshViews();
    for (var i = 0; i < this._overviewControls.length; ++i)
      this._overviewControls[i].timelineStopped();
    this._setMarkers();
    this._overviewPane.scheduleUpdate();
    this._updateSearchHighlight(false, true);
    this._detailsSplitWidget.showBoth();
  }

  _showRecordingStarted() {
    if (this._statusPane)
      return;
    this._statusPane = new Timeline.TimelinePanel.StatusPane(true, this._stopRecording.bind(this));
    this._statusPane.showPane(this._statusPaneContainer);
    this._statusPane.updateStatus(Common.UIString('Initializing profiler\u2026'));
  }

  _cancelLoading() {
    if (this._loader)
      this._loader.cancel();
  }

  _setMarkers() {
    var markers = new Map();
    var recordTypes = TimelineModel.TimelineModel.RecordType;
    var zeroTime = this._model.minimumRecordTime();
    for (var record of this._model.eventDividerRecords()) {
      if (record.type() === recordTypes.TimeStamp || record.type() === recordTypes.ConsoleTime)
        continue;
      markers.set(record.startTime(), Timeline.TimelineUIUtils.createDividerForRecord(record, zeroTime, 0));
    }
    this._overviewPane.setMarkers(markers);
  }

  /**
   * @param {!Common.Event} event
   */
  _pageReloadRequested(event) {
    if (this._state !== Timeline.TimelinePanel.State.Idle || !this.isShowing())
      return;
    this._startRecording(false);
  }

  /**
   * @param {!Common.Event} event
   */
  _loadEventFired(event) {
    if (this._state !== Timeline.TimelinePanel.State.Recording || !this._autoRecordGeneration)
      return;
    setTimeout(stopRecordingOnReload.bind(this, this._autoRecordGeneration), this._millisecondsToRecordAfterLoadEvent);

    /**
     * @this {Timeline.TimelinePanel}
     * @param {!Object} recordGeneration
     */
    function stopRecordingOnReload(recordGeneration) {
      // Check if we're still in the same recording session.
      if (this._state !== Timeline.TimelinePanel.State.Recording || this._autoRecordGeneration !== recordGeneration)
        return;
      this._stopRecording();
    }
  }

  // UI.Searchable implementation

  /**
   * @override
   */
  jumpToNextSearchResult() {
    if (!this._searchResults || !this._searchResults.length)
      return;
    var index = this._selectedSearchResult ? this._searchResults.indexOf(this._selectedSearchResult) : -1;
    this._jumpToSearchResult(index + 1);
  }

  /**
   * @override
   */
  jumpToPreviousSearchResult() {
    if (!this._searchResults || !this._searchResults.length)
      return;
    var index = this._selectedSearchResult ? this._searchResults.indexOf(this._selectedSearchResult) : 0;
    this._jumpToSearchResult(index - 1);
  }

  /**
   * @override
   * @return {boolean}
   */
  supportsCaseSensitiveSearch() {
    return false;
  }

  /**
   * @override
   * @return {boolean}
   */
  supportsRegexSearch() {
    return false;
  }

  /**
   * @param {number} index
   */
  _jumpToSearchResult(index) {
    this._selectSearchResult((index + this._searchResults.length) % this._searchResults.length);
    this._currentViews[0].highlightSearchResult(this._selectedSearchResult, this._searchRegex, true);
  }

  /**
   * @param {number} index
   */
  _selectSearchResult(index) {
    this._selectedSearchResult = this._searchResults[index];
    this._searchableView.updateCurrentMatchIndex(index);
  }

  _clearHighlight() {
    this._currentViews[0].highlightSearchResult(null);
  }

  /**
   * @param {boolean} revealRecord
   * @param {boolean} shouldJump
   * @param {boolean=} jumpBackwards
   */
  _updateSearchHighlight(revealRecord, shouldJump, jumpBackwards) {
    if (!this._searchRegex) {
      this._clearHighlight();
      return;
    }

    if (!this._searchResults)
      this._updateSearchResults(shouldJump, jumpBackwards);
    this._currentViews[0].highlightSearchResult(this._selectedSearchResult, this._searchRegex, revealRecord);
  }

  /**
   * @param {boolean} shouldJump
   * @param {boolean=} jumpBackwards
   */
  _updateSearchResults(shouldJump, jumpBackwards) {
    if (!this._searchRegex)
      return;

    // FIXME: search on all threads.
    var events = this._model.mainThreadEvents();
    var filters = this._filters.concat([new Timeline.TimelineTextFilter(this._searchRegex)]);
    var matches = [];
    for (var index = events.lowerBound(this._windowStartTime, (time, event) => time - event.startTime);
         index < events.length; ++index) {
      var event = events[index];
      if (event.startTime > this._windowEndTime)
        break;
      if (TimelineModel.TimelineModel.isVisible(filters, event))
        matches.push(event);
    }

    var matchesCount = matches.length;
    if (matchesCount) {
      this._searchResults = matches;
      this._searchableView.updateSearchMatchesCount(matchesCount);

      var selectedIndex = matches.indexOf(this._selectedSearchResult);
      if (shouldJump && selectedIndex === -1)
        selectedIndex = jumpBackwards ? this._searchResults.length - 1 : 0;
      this._selectSearchResult(selectedIndex);
    } else {
      this._searchableView.updateSearchMatchesCount(0);
      delete this._selectedSearchResult;
    }
  }

  /**
   * @override
   */
  searchCanceled() {
    this._clearHighlight();
    delete this._searchResults;
    delete this._selectedSearchResult;
    delete this._searchRegex;
  }

  /**
   * @override
   * @param {!UI.SearchableView.SearchConfig} searchConfig
   * @param {boolean} shouldJump
   * @param {boolean=} jumpBackwards
   */
  performSearch(searchConfig, shouldJump, jumpBackwards) {
    var query = searchConfig.query;
    this._searchRegex = createPlainTextSearchRegex(query, 'i');
    delete this._searchResults;
    this._updateSearchHighlight(true, shouldJump, jumpBackwards);
  }

  /**
   * @param {!Timeline.TimelineSelection} selection
   * @return {?TimelineModel.TimelineFrame}
   */
  _frameForSelection(selection) {
    switch (selection.type()) {
      case Timeline.TimelineSelection.Type.Frame:
        return /** @type {!TimelineModel.TimelineFrame} */ (selection.object());
      case Timeline.TimelineSelection.Type.Range:
        return null;
      case Timeline.TimelineSelection.Type.TraceEvent:
        return this._frameModel.filteredFrames(selection._endTime, selection._endTime)[0];
      default:
        console.assert(false, 'Should never be reached');
        return null;
    }
  }

  /**
   * @param {number} offset
   */
  _jumpToFrame(offset) {
    var currentFrame = this._frameForSelection(this._selection);
    if (!currentFrame)
      return;
    var frames = this._frameModel.frames();
    var index = frames.indexOf(currentFrame);
    console.assert(index >= 0, 'Can\'t find current frame in the frame list');
    index = Number.constrain(index + offset, 0, frames.length - 1);
    var frame = frames[index];
    this._revealTimeRange(frame.startTime, frame.endTime);
    this.select(Timeline.TimelineSelection.fromFrame(frame));
    return true;
  }

  /**
   * @override
   * @param {?Timeline.TimelineSelection} selection
   * @param {!Timeline.TimelineDetailsView.Tab=} preferredTab
   */
  select(selection, preferredTab) {
    if (!selection)
      selection = Timeline.TimelineSelection.fromRange(this._windowStartTime, this._windowEndTime);
    this._selection = selection;
    if (preferredTab)
      this._detailsView.setPreferredTab(preferredTab);
    for (var view of this._currentViews)
      view.setSelection(selection);
    this._detailsView.setSelection(selection);
  }

  /**
   * @override
   * @param {number} time
   */
  selectEntryAtTime(time) {
    var events = this._model.mainThreadEvents();
    // Find best match, then backtrack to the first visible entry.
    for (var index = events.upperBound(time, (time, event) => time - event.startTime) - 1; index >= 0; --index) {
      var event = events[index];
      var endTime = event.endTime || event.startTime;
      if (SDK.TracingModel.isTopLevelEvent(event) && endTime < time)
        break;
      if (TimelineModel.TimelineModel.isVisible(this._filters, event) && endTime >= time) {
        this.select(Timeline.TimelineSelection.fromTraceEvent(event));
        return;
      }
    }
    this.select(null);
  }

  /**
   * @override
   * @param {?SDK.TracingModel.Event} event
   */
  highlightEvent(event) {
    for (var view of this._currentViews)
      view.highlightEvent(event);
  }

  /**
   * @param {number} startTime
   * @param {number} endTime
   */
  _revealTimeRange(startTime, endTime) {
    var timeShift = 0;
    if (this._windowEndTime < endTime)
      timeShift = endTime - this._windowEndTime;
    else if (this._windowStartTime > startTime)
      timeShift = startTime - this._windowStartTime;
    if (timeShift)
      this.requestWindowTimes(this._windowStartTime + timeShift, this._windowEndTime + timeShift);
  }

  /**
   * @param {!DataTransfer} dataTransfer
   */
  _handleDrop(dataTransfer) {
    var items = dataTransfer.items;
    if (!items.length)
      return;
    var item = items[0];
    if (item.kind === 'string') {
      var url = dataTransfer.getData('text/uri-list');
      if (new Common.ParsedURL(url).isValid)
        this._loadFromURL(url);
    } else if (item.kind === 'file') {
      var entry = items[0].webkitGetAsEntry();
      if (!entry.isFile)
        return;
      entry.file(this._loadFromFile.bind(this));
    }
  }

  _setAutoWindowTimes() {
    var tasks = this._model.mainThreadTasks();
    if (!tasks.length) {
      this.requestWindowTimes(this._tracingModel.minimumRecordTime(), this._tracingModel.maximumRecordTime());
      return;
    }
    /**
     * @param {number} startIndex
     * @param {number} stopIndex
     * @return {number}
     */
    function findLowUtilizationRegion(startIndex, stopIndex) {
      var /** @const */ threshold = 0.1;
      var cutIndex = startIndex;
      var cutTime = (tasks[cutIndex].startTime() + tasks[cutIndex].endTime()) / 2;
      var usedTime = 0;
      var step = Math.sign(stopIndex - startIndex);
      for (var i = startIndex; i !== stopIndex; i += step) {
        var task = tasks[i];
        var taskTime = (task.startTime() + task.endTime()) / 2;
        var interval = Math.abs(cutTime - taskTime);
        if (usedTime < threshold * interval) {
          cutIndex = i;
          cutTime = taskTime;
          usedTime = 0;
        }
        usedTime += task.endTime() - task.startTime();
      }
      return cutIndex;
    }
    var rightIndex = findLowUtilizationRegion(tasks.length - 1, 0);
    var leftIndex = findLowUtilizationRegion(0, rightIndex);
    var leftTime = tasks[leftIndex].startTime();
    var rightTime = tasks[rightIndex].endTime();
    var span = rightTime - leftTime;
    var totalSpan = this._tracingModel.maximumRecordTime() - this._tracingModel.minimumRecordTime();
    if (span < totalSpan * 0.1) {
      leftTime = this._tracingModel.minimumRecordTime();
      rightTime = this._tracingModel.maximumRecordTime();
    } else {
      leftTime = Math.max(leftTime - 0.05 * span, this._tracingModel.minimumRecordTime());
      rightTime = Math.min(rightTime + 0.05 * span, this._tracingModel.maximumRecordTime());
    }
    this.requestWindowTimes(leftTime, rightTime);
  }
};

/**
 * @enum {symbol}
 */
Timeline.TimelinePanel.State = {
  Idle: Symbol('Idle'),
  StartPending: Symbol('StartPending'),
  Recording: Symbol('Recording'),
  StopPending: Symbol('StopPending'),
  Loading: Symbol('Loading')
};

// Define row and header height, should be in sync with styles for timeline graphs.
Timeline.TimelinePanel.rowHeight = 18;
Timeline.TimelinePanel.headerHeight = 20;

/**
 * @interface
 */
Timeline.LoaderClient = function() {};

Timeline.LoaderClient.prototype = {
  loadingStarted() {},

  /**
   * @param {number=} progress
   */
  loadingProgress(progress) {},

  /**
   * @param {boolean} success
   */
  loadingComplete(success) {},
};

/**
 * @interface
 * @extends {Timeline.LoaderClient}
 */
Timeline.TimelineLifecycleDelegate = function() {};

Timeline.TimelineLifecycleDelegate.prototype = {
  recordingStarted() {},

  /**
   * @param {number} usage
   */
  recordingProgress(usage) {},

  /**
   * @param {string} title
   * @param {!SDK.TracingModel} tracingModel
   * @param {number} timeOffset
   */
  addExtensionEvents(title, tracingModel, timeOffset) {},

  /** @return {?symbol} */
  sessionGeneration() {}
};

Timeline.TimelineSelection = class {
  /**
   * @param {!Timeline.TimelineSelection.Type} type
   * @param {number} startTime
   * @param {number} endTime
   * @param {!Object=} object
   */
  constructor(type, startTime, endTime, object) {
    this._type = type;
    this._startTime = startTime;
    this._endTime = endTime;
    this._object = object || null;
  }

  /**
   * @param {!TimelineModel.TimelineFrame} frame
   * @return {!Timeline.TimelineSelection}
   */
  static fromFrame(frame) {
    return new Timeline.TimelineSelection(Timeline.TimelineSelection.Type.Frame, frame.startTime, frame.endTime, frame);
  }

  /**
   * @param {!TimelineModel.TimelineModel.NetworkRequest} request
   * @return {!Timeline.TimelineSelection}
   */
  static fromNetworkRequest(request) {
    return new Timeline.TimelineSelection(
        Timeline.TimelineSelection.Type.NetworkRequest, request.startTime, request.endTime || request.startTime,
        request);
  }

  /**
   * @param {!SDK.TracingModel.Event} event
   * @return {!Timeline.TimelineSelection}
   */
  static fromTraceEvent(event) {
    return new Timeline.TimelineSelection(
        Timeline.TimelineSelection.Type.TraceEvent, event.startTime, event.endTime || (event.startTime + 1), event);
  }

  /**
   * @param {number} startTime
   * @param {number} endTime
   * @return {!Timeline.TimelineSelection}
   */
  static fromRange(startTime, endTime) {
    return new Timeline.TimelineSelection(Timeline.TimelineSelection.Type.Range, startTime, endTime);
  }

  /**
   * @return {!Timeline.TimelineSelection.Type}
   */
  type() {
    return this._type;
  }

  /**
   * @return {?Object}
   */
  object() {
    return this._object;
  }

  /**
   * @return {number}
   */
  startTime() {
    return this._startTime;
  }

  /**
   * @return {number}
   */
  endTime() {
    return this._endTime;
  }
};

/**
 * @enum {string}
 */
Timeline.TimelineSelection.Type = {
  Frame: 'Frame',
  NetworkRequest: 'NetworkRequest',
  TraceEvent: 'TraceEvent',
  Range: 'Range'
};

/**
 * @interface
 * @extends {Common.EventTarget}
 */
Timeline.TimelineModeView = function() {};

Timeline.TimelineModeView.prototype = {
  /**
   * @return {!UI.Widget}
   */
  view() {},

  dispose() {},

  /**
   * @return {?Element}
   */
  resizerElement() {},

  reset() {},

  refreshRecords() {},

  extensionDataAdded() {},

  /**
   * @param {?SDK.TracingModel.Event} event
   * @param {string=} regex
   * @param {boolean=} select
   */
  highlightSearchResult(event, regex, select) {},

  /**
   * @param {number} startTime
   * @param {number} endTime
   */
  setWindowTimes(startTime, endTime) {},

  /**
   * @param {?Timeline.TimelineSelection} selection
   */
  setSelection(selection) {},

  /**
   * @param {?SDK.TracingModel.Event} event
   */
  highlightEvent(event) {}
};

/**
 * @interface
 */
Timeline.TimelineModeViewDelegate = function() {};

Timeline.TimelineModeViewDelegate.prototype = {
  /**
   * @param {number} startTime
   * @param {number} endTime
   */
  requestWindowTimes(startTime, endTime) {},

  /**
   * @param {?Timeline.TimelineSelection} selection
   * @param {!Timeline.TimelineDetailsView.Tab=} preferredTab
   */
  select(selection, preferredTab) {},

  /**
   * @param {number} time
   */
  selectEntryAtTime(time) {},

  /**
   * @param {?SDK.TracingModel.Event} event
   */
  highlightEvent(event) {}
};

/**
 * @unrestricted
 */
Timeline.TimelineCategoryFilter = class extends TimelineModel.TimelineModel.Filter {
  constructor() {
    super();
  }

  /**
   * @override
   * @param {!SDK.TracingModel.Event} event
   * @return {boolean}
   */
  accept(event) {
    return !Timeline.TimelineUIUtils.eventStyle(event).category.hidden;
  }
};

/**
 * @unrestricted
 */
Timeline.TimelineIsLongFilter = class extends TimelineModel.TimelineModel.Filter {
  constructor() {
    super();
    this._minimumRecordDuration = 0;
  }

  /**
   * @param {number} value
   */
  setMinimumRecordDuration(value) {
    this._minimumRecordDuration = value;
  }

  /**
   * @override
   * @param {!SDK.TracingModel.Event} event
   * @return {boolean}
   */
  accept(event) {
    var duration = event.endTime ? event.endTime - event.startTime : 0;
    return duration >= this._minimumRecordDuration;
  }
};

Timeline.TimelineTextFilter = class extends TimelineModel.TimelineModel.Filter {
  /**
   * @param {!RegExp=} regExp
   */
  constructor(regExp) {
    super();
    /** @type {?RegExp} */
    this._regExp;
    this._setRegExp(regExp || null);
  }

  /**
   * @param {?RegExp} regExp
   */
  _setRegExp(regExp) {
    this._regExp = regExp;
  }

  /**
   * @override
   * @param {!SDK.TracingModel.Event} event
   * @return {boolean}
   */
  accept(event) {
    return !this._regExp || Timeline.TimelineUIUtils.testContentMatching(event, this._regExp);
  }
};

/**
 * @unrestricted
 */
Timeline.TimelinePanel.StatusPane = class extends UI.VBox {
  /**
   * @param {boolean} showTimer
   * @param {function()} stopCallback
   */
  constructor(showTimer, stopCallback) {
    super(true);
    this.registerRequiredCSS('timeline/timelineStatusDialog.css');
    this.contentElement.classList.add('timeline-status-dialog');

    var statusLine = this.contentElement.createChild('div', 'status-dialog-line status');
    statusLine.createChild('div', 'label').textContent = Common.UIString('Status');
    this._status = statusLine.createChild('div', 'content');

    if (showTimer) {
      var timeLine = this.contentElement.createChild('div', 'status-dialog-line time');
      timeLine.createChild('div', 'label').textContent = Common.UIString('Time');
      this._time = timeLine.createChild('div', 'content');
    }
    var progressLine = this.contentElement.createChild('div', 'status-dialog-line progress');
    this._progressLabel = progressLine.createChild('div', 'label');
    this._progressBar = progressLine.createChild('div', 'indicator-container').createChild('div', 'indicator');

    this._stopButton = UI.createTextButton(Common.UIString('Stop'), stopCallback);
    this.contentElement.createChild('div', 'stop-button').appendChild(this._stopButton);
  }

  finish() {
    this._stopTimer();
    this._stopButton.disabled = true;
  }

  hide() {
    this.element.parentNode.classList.remove('tinted');
    this.element.remove();
  }

  /**
   * @param {!Element} parent
   */
  showPane(parent) {
    this.show(parent);
    parent.classList.add('tinted');
  }

  /**
   * @param {string} text
   */
  updateStatus(text) {
    this._status.textContent = text;
  }

  /**
   * @param {string} activity
   * @param {number} percent
   */
  updateProgressBar(activity, percent) {
    this._progressLabel.textContent = activity;
    this._progressBar.style.width = percent.toFixed(1) + '%';
    this._updateTimer();
  }

  startTimer() {
    this._startTime = Date.now();
    this._timeUpdateTimer = setInterval(this._updateTimer.bind(this, false), 1000);
    this._updateTimer();
  }

  _stopTimer() {
    if (!this._timeUpdateTimer)
      return;
    clearInterval(this._timeUpdateTimer);
    this._updateTimer(true);
    delete this._timeUpdateTimer;
  }

  /**
   * @param {boolean=} precise
   */
  _updateTimer(precise) {
    if (!this._timeUpdateTimer)
      return;
    var elapsed = (Date.now() - this._startTime) / 1000;
    this._time.textContent = Common.UIString('%s\u2009sec', elapsed.toFixed(precise ? 1 : 0));
  }
};

/**
 * @implements {Common.QueryParamHandler}
 * @unrestricted
 */
Timeline.LoadTimelineHandler = class {
  /**
   * @override
   * @param {string} value
   */
  handleQueryParam(value) {
    UI.viewManager.showView('timeline').then(() => {
      Timeline.TimelinePanel.instance()._loadFromURL(window.decodeURIComponent(value));
    });
  }
};

/**
 * @implements {UI.ActionDelegate}
 * @unrestricted
 */
Timeline.TimelinePanel.ActionDelegate = class {
  /**
   * @override
   * @param {!UI.Context} context
   * @param {string} actionId
   * @return {boolean}
   */
  handleAction(context, actionId) {
    var panel = UI.context.flavor(Timeline.TimelinePanel);
    console.assert(panel && panel instanceof Timeline.TimelinePanel);
    switch (actionId) {
      case 'timeline.toggle-recording':
        panel._toggleRecording();
        return true;
      case 'timeline.save-to-file':
        panel._saveToFile();
        return true;
      case 'timeline.load-from-file':
        panel._selectFileToLoad();
        return true;
      case 'timeline.jump-to-previous-frame':
        panel._jumpToFrame(-1);
        return true;
      case 'timeline.jump-to-next-frame':
        panel._jumpToFrame(1);
        return true;
    }
    return false;
  }
};

/**
 * @unrestricted
 */
Timeline.TimelineFilters = class extends Common.Object {
  constructor() {
    super();

    this._categoryFilter = new Timeline.TimelineCategoryFilter();
    this._durationFilter = new Timeline.TimelineIsLongFilter();
    this._textFilter = new Timeline.TimelineTextFilter();
    this._filters = [this._categoryFilter, this._durationFilter, this._textFilter];

    this._createFilterBar();
  }

  /**
   * @return {!Array<!TimelineModel.TimelineModel.Filter>}
   */
  filters() {
    return this._filters;
  }

  /**
   * @return {?RegExp}
   */
  searchRegExp() {
    return this._textFilter._regExp;
  }

  /**
   * @return {!UI.ToolbarItem}
   */
  filterButton() {
    return this._filterBar.filterButton();
  }

  /**
   * @return {!UI.Widget}
   */
  filtersWidget() {
    return this._filterBar;
  }

  _createFilterBar() {
    this._filterBar = new UI.FilterBar('timelinePanel');

    this._textFilterUI = new UI.TextFilterUI();
    this._textFilterUI.addEventListener(UI.FilterUI.Events.FilterChanged, textFilterChanged, this);
    this._filterBar.addFilter(this._textFilterUI);

    var durationOptions = [];
    for (var durationMs of Timeline.TimelineFilters._durationFilterPresetsMs) {
      var durationOption = {};
      if (!durationMs) {
        durationOption.label = Common.UIString('All');
        durationOption.title = Common.UIString('Show all records');
      } else {
        durationOption.label = Common.UIString('\u2265 %dms', durationMs);
        durationOption.title = Common.UIString('Hide records shorter than %dms', durationMs);
      }
      durationOption.value = durationMs;
      durationOptions.push(durationOption);
    }
    var durationFilterUI = new UI.ComboBoxFilterUI(durationOptions);
    durationFilterUI.addEventListener(UI.FilterUI.Events.FilterChanged, durationFilterChanged, this);
    this._filterBar.addFilter(durationFilterUI);

    var categoryFiltersUI = {};
    var categories = Timeline.TimelineUIUtils.categories();
    for (var categoryName in categories) {
      var category = categories[categoryName];
      if (!category.visible)
        continue;
      var filter = new UI.CheckboxFilterUI(category.name, category.title);
      filter.setColor(category.color, 'rgba(0, 0, 0, 0.2)');
      categoryFiltersUI[category.name] = filter;
      filter.addEventListener(UI.FilterUI.Events.FilterChanged, categoriesFilterChanged.bind(this, categoryName));
      this._filterBar.addFilter(filter);
    }
    return this._filterBar;

    /**
     * @this {Timeline.TimelineFilters}
     */
    function textFilterChanged() {
      var searchQuery = this._textFilterUI.value();
      this._textFilter._setRegExp(searchQuery ? createPlainTextSearchRegex(searchQuery, 'i') : null);
      this._notifyFiltersChanged();
    }

    /**
     * @this {Timeline.TimelineFilters}
     */
    function durationFilterChanged() {
      var duration = durationFilterUI.value();
      var minimumRecordDuration = parseInt(duration, 10);
      this._durationFilter.setMinimumRecordDuration(minimumRecordDuration);
      this._notifyFiltersChanged();
    }

    /**
     * @param {string} name
     * @this {Timeline.TimelineFilters}
     */
    function categoriesFilterChanged(name) {
      var categories = Timeline.TimelineUIUtils.categories();
      categories[name].hidden = !categoryFiltersUI[name].checked();
      this._notifyFiltersChanged();
    }
  }

  _notifyFiltersChanged() {
    this.dispatchEventToListeners(Timeline.TimelineFilters.Events.FilterChanged);
  }
};

Timeline.TimelinePanel._traceProviderSettingSymbol = Symbol('traceProviderSetting');

/** @enum {symbol} */
Timeline.TimelineFilters.Events = {
  FilterChanged: Symbol('FilterChanged')
};

Timeline.TimelineFilters._durationFilterPresetsMs = [0, 1, 15];


text:
@interface MyPluginLayer : UIView

@property (nonatomic) UIView *webView;
@property (nonatomic) GoogleMapsViewController* mapCtrl;
@property (nonatomic) NSDictionary *embedRect;
@property (nonatomic) BOOL debuggable;
@property (nonatomic) BOOL clickable;
@property (nonatomic) NSMutableDictionary *HTMLNodes;

- (id)initWithFrame:(CGRect)aRect;
- (void)putHTMLElement:(NSString *)domId size:(NSDictionary *)size;
- (void)removeHTMLElement:(NSString *)domId;
- (void)clearHTMLElement;
@end

redpajama_set_name: RedPajamaGithub


text:
//  (C) Copyright Gennadiy Rozental 2005.
//  Use, modification, and distribution are subject to the
//  Boost Software License, Version 1.0. (See accompanying file
//  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

//  See http://www.boost.org/libs/test for the library home page.
//
//  File        : $RCSfile: basic_parameter.hpp,v $
//
//  Version     : $Revision: 1.1 $
//
//  Description : generic custom parameter generator
// ***************************************************************************

#ifndef BOOST_RT_CLA_BASIC_PARAMETER_HPP_062604GER
#define BOOST_RT_CLA_BASIC_PARAMETER_HPP_062604GER

// Boost.Runtime.Parameter
#include <boost/test/utils/runtime/config.hpp>

#include <boost/test/utils/runtime/cla/typed_parameter.hpp>

// Boost.Test
#include <boost/test/utils/rtti.hpp>

namespace boost {

namespace BOOST_RT_PARAM_NAMESPACE {

namespace cla {

// ************************************************************************** //
// **************         runtime::cla::basic_parameter        ************** //
// ************************************************************************** //

template<typename T, typename IdPolicy>
class basic_parameter : public typed_parameter<T> {
public:
    // Constructors
    explicit    basic_parameter( cstring n )
    : typed_parameter<T>( m_id_policy )
    {
        this->accept_modifier( name = n );
    }

    // parameter properties modification
    template<typename Modifier>
    void        accept_modifier( Modifier const& m )
    {
        typed_parameter<T>::accept_modifier( m );

        m_id_policy.accept_modifier( m );
    }

private:
    IdPolicy    m_id_policy;
};

//____________________________________________________________________________//

#define BOOST_RT_CLA_NAMED_PARAM_GENERATORS( param_type )                                       \
template<typename T>                                                                            \
inline shared_ptr<param_type ## _t<T> >                                                         \
param_type( cstring name = cstring() )                                                          \
{                                                                                \
    return shared_ptr<param_type ## _t<T> >( new param_type ## _t<T>( name ) );                 \
}                                                                                \
                                                                                \
inline shared_ptr<param_type ## _t<cstring> >                                                   \
param_type( cstring name = cstring() )                                                          \
{                                                                                \
    return shared_ptr<param_type ## _t<cstring> >( new param_type ## _t<cstring>( name ) );     \
}                                                                                \
/**/

//____________________________________________________________________________//

} // namespace cla

} // namespace BOOST_RT_PARAM_NAMESPACE

} // namespace boost

// ************************************************************************** //
//   Revision History:
//
//   $Log: basic_parameter.hpp,v $
//   Revision 1.1  2005/04/12 06:42:43  rogeeff
//   Runtime.Param library initial commit
//
// ************************************************************************** //

#endif // BOOST_RT_CLA_BASIC_PARAMETER_HPP_062604GER


text:
Clazz.declarePackage ("J.jvxl.readers");
Clazz.load (["J.jvxl.readers.VolumeFileReader"], "J.jvxl.readers.DelPhiBinaryReader", ["JU.SB", "$.V3", "J.util.Logger"], function () {
c$ = Clazz.decorateAsClass (function () {
this.data = null;
this.pt = 0;
Clazz.instantialize (this, arguments);
}, J.jvxl.readers, "DelPhiBinaryReader", J.jvxl.readers.VolumeFileReader);
Clazz.makeConstructor (c$,
function () {
Clazz.superConstructor (this, J.jvxl.readers.DelPhiBinaryReader, []);
});
$_V(c$, "init2",
function (sg, brNull) {
var fileName = (sg.getReaderData ())[0];
this.init2VFR (sg, this.br);
this.binarydoc = this.newBinaryDocument ();
this.binarydoc.setStream (sg.getAtomDataServer ().getBufferedInputStream (fileName), false);
this.nSurfaces = 1;
if (this.params.thePlane == null) this.params.insideOut = !this.params.insideOut;
this.allowSigma = false;
this.isAngstroms = true;
}, "J.jvxl.readers.SurfaceGenerator,java.io.BufferedReader");
$_V(c$, "readParameters",
function () {
var uplbl = this.readString ();
J.util.Logger.info (uplbl);
var nxttoplbl = this.readString ();
J.util.Logger.info (nxttoplbl);
this.data = this.readFloatArray ();
J.util.Logger.info ("DelPhi data length: " + this.data.length);
var botlbl = this.readString ();
J.util.Logger.info (botlbl);
var scalemid = this.readFloatArray ();
var scale = scalemid[0];
J.util.Logger.info ("DelPhi scale: " + scale);
var dx = (scale == 1 ? 0.84375 : 1 / scale);
this.volumetricVectors[0] = JU.V3.new3 (0, 0, dx);
this.volumetricVectors[1] = JU.V3.new3 (0, dx, 0);
this.volumetricVectors[2] = JU.V3.new3 (dx, 0, 0);
J.util.Logger.info ("DelPhi resolution (pts/angstrom) set to: " + dx);
var nx = 65;
this.voxelCounts[0] = this.voxelCounts[1] = this.voxelCounts[2] = nx;
J.util.Logger.info ("DelPhi voxel counts: " + nx);
dx *= Clazz.doubleToInt ((nx - 1) / 2);
this.volumetricOrigin.set (scalemid[1], scalemid[2], scalemid[3]);
J.util.Logger.info ("DelPhi center " + this.volumetricOrigin);
this.volumetricOrigin.x -= dx;
this.volumetricOrigin.y -= dx;
this.volumetricOrigin.z -= dx;
this.jvxlFileHeaderBuffer =  new JU.SB ();
this.jvxlFileHeaderBuffer.append ("DelPhi DATA ").append (nxttoplbl.$replace ('\n', ' ').trim ()).append ("\n\n");
});
$_M(c$, "readString",
($fz = function () {
var n = this.binarydoc.readInt ();
var buf =  Clazz.newByteArray (n, 0);
this.binarydoc.readByteArray (buf, 0, n);
this.binarydoc.readInt ();
return  String.instantialize (buf);
}, $fz.isPrivate = true, $fz));
$_M(c$, "readFloatArray",
($fz = function () {
var n = this.binarydoc.readInt () >> 2;
var a =  Clazz.newFloatArray (n, 0);
for (var i = 0; i < n; i++) a[i] = this.binarydoc.readFloat ();

this.binarydoc.readInt ();
return a;
}, $fz.isPrivate = true, $fz));
$_V(c$, "nextVoxel",
function () {
this.nBytes += 4;
return this.data[this.pt++];
});
$_V(c$, "skipData",
function (nPoints) {
this.pt += nPoints;
}, "~N");
});


text:
* {
    font-family: 'Open Sans', sans-serif;
}

.color_gallery div{
    display:inline-block;
}

#vatvalue{
    float:rigt;
}

.variant-table th{
    width:20% !important;
}

.variant-table th:first-child{
    width:inherit !important;
}


text:
@class IBAutolayoutIssueResolvingViewController;

@protocol IBAutolayoutIssueResolvingViewControllerDelegate <NSObject>
- (void)issueResolvingViewControllerDidFinishResolvingIssues:(IBAutolayoutIssueResolvingViewController *)arg1;
@end


text:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Eleven41.Logging
{
	public abstract class LogglyBase
	{
		protected Loggly.ILogglyClient _client;

		public LogglyBase()
		{
			_client = new Loggly.LogglyClient();

			this.Data = new Dictionary<string, object>();
			this.DateTimeProvider = new Eleven41.Logging.DateTimeProviders.UtcDateTimeProvider();
		}

		public LogglyBase(Dictionary<string, object> data)
		{
			_client = new Loggly.LogglyClient();

			if (data != null)
				this.Data = new Dictionary<string, object>(data);
			else
				this.Data = new Dictionary<string, object>();
			this.DateTimeProvider = new Eleven41.Logging.DateTimeProviders.UtcDateTimeProvider();
		}

		public static void SetLogglyCustomerToken(string customerToken)
		{
			Loggly.Config.LogglyConfig.Instance.CustomerToken = customerToken;
		}

		private Dictionary<string, object> _data;

		public Dictionary<string, object> Data
		{
			get { return _data; }
			set
			{
				// Ensure we this.Data is never null
				if (value == null)
					throw new ArgumentNullException("Data", "Data must not be null");

				_data = value;
			}
		}

		public bool IsSync { get; set; }

		IDateTimeProvider _dateTimeProvider;

		public IDateTimeProvider DateTimeProvider
		{
			get
			{
				return _dateTimeProvider;
			}
			set
			{
				if (value == null)
					throw new ArgumentNullException();
				_dateTimeProvider = value;
			}
		}
	}
}


text:
DartVarDeclTransform
================

Simple Command line tool to transform variable declarations list into variable declaration list which only constists of one.
This is not possible when variable declarations is used inside a for statement.

## Introduction

This tool is written by Jesper Lindstrøm Nielsen and Troels Leth Jensen as part of a study in Dart at Aarhus University @ 2014.

## Install

When cloned you should get the packages needed to use via. `pub get`.

## Run

To use the tool just do:

  ./vardecl_transform.dart [-w --override] file(s)

The tool implement these flags:

Override `-w` or `--override` flag to specify of the given file should be overwritten whit the transformed result.


text:
package com.yonferol.java;

/**
 * Created by wangy on 2016/10/20.
 */
public class TestCollection {
    public static void main(String[] args) {
        System.out.println();
    }
}


text:
package eu.ensure.ppe;

import org.gautelis.vopn.lang.Number;
import org.gautelis.vopn.statistics.MovingAverage;
import eu.ensure.ppe.model.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.sql.DataSource;
import java.util.*;

/**
 * Calculates score for Global Preservation Plans.
 * <p>
 * Written at an altitude of 11.277 meters, just above Le Mans, France.
 * The outside temperature is -55°C :)
 * </p>
 * Created by Frode Randers at 2012-10-16 17:57
 */
public class Calculator {
    private static final Logger log = LogManager.getLogger(Calculator.class);

    private final LocalizedPluginCalculator localizedPluginCalculator;
    private final FixityPluginCalculator fixityPluginCalculator;
    private final EncryptionPluginCalculator encryptionPluginCalculator;
    private final GeneralPluginCalculator generalPluginCalculator;

    public Calculator(DataSource dataSource) {
        this.localizedPluginCalculator = new LocalizedPluginCalculator(dataSource);
        this.fixityPluginCalculator = new FixityPluginCalculator(dataSource);
        this.encryptionPluginCalculator = new EncryptionPluginCalculator(dataSource);
        this.generalPluginCalculator = new GeneralPluginCalculator(dataSource);
    }

    private Double calculateScore(
            Aggregation aggregation,
            String event,
            Plugin plugin,
            Collection<String> storyLine,
            Collection<Consequence> consequences
    ) throws EvaluationException {

        if (log.isDebugEnabled()) {
            log.debug("--- Plugin: " + plugin.getDescription());
        }

        if (plugin instanceof StoragePlugin || plugin instanceof ComputePlugin) {
            Double score = localizedPluginCalculator.calculateScore(
                    aggregation, event, (LocalizedPlugin) plugin, storyLine, consequences
            );

            // Get a second opinion in debug
            if (log.isDebugEnabled()) {
                Double v = localizedPluginCalculator.calculateScoreVerification(
                        aggregation, event, (LocalizedPlugin) plugin
                );
                if ((null != score && !score.equals(v)) || (null != v && !v.equals(score))) {
                    String _score = null != score ? Double.toString(score) : "none";
                    String _verification = null != v ? Double.toString(v) : "none";
                    String info = "Calculations differ: score=" + _score + " verification=" + _verification;
                    log.debug(info);
                }
            }
            return score;
        }
        else if (plugin instanceof FixityPlugin) {
            return fixityPluginCalculator.calculateScore(
                    aggregation, event, (FixityPlugin) plugin, storyLine, consequences
            );
        }
        else if (plugin instanceof EncryptionPlugin) {
            return encryptionPluginCalculator.calculateScore(
                    aggregation, event, (EncryptionPlugin) plugin, storyLine, consequences
            );
        }
        else {
            Double score = generalPluginCalculator.calculateScore(
                    aggregation, event, plugin, storyLine, consequences
            );

            // Get a second opinion in debug
            if (log.isDebugEnabled()) {
                Double v = generalPluginCalculator.calculateScoreVerification(
                        aggregation, event, plugin
                );
                if ((null != score && !score.equals(v)) || (null != v && !v.equals(score))) {
                    String _score = null != score ? Double.toString(score) : "none";
                    String _verification = null != v ? Double.toString(v) : "none";
                    String info = "Calculations differ: score=" + _score + " verification=" + _verification;
                    log.debug(info);
                }
            }
            return score;
        }
    }

    private void ON_INGEST_analysis(
            Aggregation aggregation,
            Map<String, Collection<Plugin>> processMap,
            Collection<String> storyLine,
            Collection<Consequence> consequences,
            MovingAverage interAggregation
    ) {
        boolean noStrongFixityCheck = true;
        boolean noEncryption = true;

        Collection<Plugin> ingest = processMap.get(Event.ON_INGEST);
        if (null == ingest)
            return;

        for (Plugin plugin : ingest) {
            if (plugin instanceof FixityPlugin) {
                FixityPlugin fixityPlugin = (FixityPlugin) plugin;
                double rank = FixityAssessment.rank(fixityPlugin.getFixityAlgorithm());

                if (rank >= FixityAssessment.COMPETENT_ALGORITHM_SCORE) {
                    noStrongFixityCheck = false;
                }
            }
            else if (plugin instanceof EncryptionPlugin) {
                EncryptionPlugin encryptionPlugin = (EncryptionPlugin) plugin;

                String algorithm = encryptionPlugin.getEncryptionAlgorithm();

                // TODO Currently assuming that any type of encryption (except NONE) is OK
                noEncryption = "NONE".equalsIgnoreCase(algorithm);
            }
        }
        for (Purpose purpose : aggregation.getPrimaryPurposes()) {
            String purposeName = purpose.toString();
            if (Purpose.EVIDENCE.equalsIgnoreCase(purposeName)) {
                if (noStrongFixityCheck) {
                    // TODO Update after Year 2 review
                    //interAggregation.update(AggregationLevelScore.FAILURE_SCORE);

                    // ---- Consequences ----
                    double probability = 55.0; // TODO
                    double suitability = 25.0; // TODO

                    //
                    StringBuilder _description = new StringBuilder();
                    _description.append("During ingest no fixity check is prepared or used. ");

                    _description.append("Hence the suitability as ")
                       .append(purposeName.toLowerCase()).append(" is assessed to be ")
                       .append(Double.toString(suitability)).append(" % with probability ")
                       .append(Double.toString(probability)).append(" % of this happening. ");

                    Consequence consequence = new Consequence(probability, suitability, _description.toString());
                    consequences.add(consequence);

                    if (log.isInfoEnabled()) {
                        log.info(_description.toString());
                    }

                    // ---- Story line ----
                    StringBuilder story = new StringBuilder();
                    story.append("purpose=\"").append(purpose).append("\"");
                    story.append(" event=\"").append(Event.ON_INGEST).append("\"");
                    story.append(": \"No strong or competent fixity algorithm used during ingest. It will not guarantee integrity!").append("\"");
                    storyLine.add(story.toString());
                }
            }
        }

        if (noEncryption && aggregation.getNeedsEncryption()) {
            // TODO Update after Year 2 review
            //interAggregation.update(AggregationLevelScore.FAILURE_SCORE);

            // ---- Consequences ----
            double probability = 55.0; // TODO
            double suitability = 25.0; // TODO

            //
            StringBuilder _description = new StringBuilder();
            _description.append("During ingest no encryption is done whereas the requirements state this demand. ");

            _description.append("Hence the suitability as is assessed to be ")
               .append(Double.toString(suitability)).append(" % with probability ")
               .append(Double.toString(probability)).append(" % of this happening. ");

            Consequence consequence = new Consequence(probability, suitability, _description.toString());
            consequences.add(consequence);

            if (log.isInfoEnabled()) {
                log.info(_description.toString());
            }

            // ---- Story line ----
            StringBuilder story = new StringBuilder();
            story.append("event=\"").append(Event.ON_INGEST).append("\"");
            story.append(": \"No encryption scheduled for ingest while the aggregation assumes encryption. It may not guarantee confidentiality!").append("\"");
            storyLine.add(story.toString());
        }
    }

    private void ON_ACCESS_analysis(
            Aggregation aggregation,
            Map<String, Collection<Plugin>> processMap,
            Collection<String> storyLine,
            Collection<Consequence> consequences,
            MovingAverage interAggregation
    ) {
        boolean noStrongFixityCheck = true;
        boolean noEncryption = true;

        Collection<Plugin> access = processMap.get(Event.ON_ACCESS);
        if (null == access)
            return;

        for(Plugin plugin : access) {
            if (plugin instanceof FixityPlugin) {
                FixityPlugin fixityPlugin = (FixityPlugin) plugin;
                double rank = FixityAssessment.rank(fixityPlugin.getFixityAlgorithm());

                if (rank >= FixityAssessment.COMPETENT_ALGORITHM_SCORE) {
                    noStrongFixityCheck = false;
                }
            }
            else if (plugin instanceof EncryptionPlugin) {
                EncryptionPlugin encryptionPlugin = (EncryptionPlugin) plugin;

                String algorithm = encryptionPlugin.getEncryptionAlgorithm();

                // TODO Currently assuming that any type of encryption (except NONE) is OK
                noEncryption = "NONE".equalsIgnoreCase(algorithm);
            }
        }

        for (Purpose purpose : aggregation.getPrimaryPurposes()) {
            String purposeName = purpose.toString();
            if (Purpose.EVIDENCE.equalsIgnoreCase(purposeName)) {
                if (noStrongFixityCheck) {
                    // TODO Update after Year 2 review
                    // interAggregation.update(AggregationLevelScore.FAILURE_SCORE);

                    // ---- Consequences ----
                    double probability = 55.0; // TODO
                    double suitability = 25.0; // TODO

                    //
                    StringBuilder _description = new StringBuilder();
                    _description.append("During access no fixity check is done. ");

                    _description.append("Hence the suitability as ")
                       .append(purposeName.toLowerCase()).append(" is assessed to be ")
                       .append(Double.toString(suitability)).append(" % with probability ")
                       .append(Double.toString(probability)).append(" % of this happening. ");

                    Consequence consequence = new Consequence(probability, suitability, _description.toString());
                    consequences.add(consequence);

                    if (log.isInfoEnabled()) {
                        log.info(_description.toString());
                    }

                    // ---- Story line ----
                    StringBuilder story = new StringBuilder();
                    story.append("purpose=\"").append(purpose).append("\"");
                    story.append(" event=\"").append(Event.ON_ACCESS).append("\"");
                    story.append(": \"No strong or competent fixity algorithm used during access. It will not guarantee integrity!").append("\"");
                    storyLine.add(story.toString());
                }
            }
        }

        if (noEncryption && aggregation.getNeedsEncryption()) {
            // TODO Update after Year 2 review
            //interAggregation.update(AggregationLevelScore.FAILURE_SCORE);

            // ---- Consequences ----
            double probability = 100.0; // TODO
            double suitability = 25.0; // TODO

            //
            StringBuilder _description = new StringBuilder();
            _description.append("During access no decryption is done whereas the requirements state this demand. ");

            _description.append("Hence the suitability as is assessed to be ")
               .append(Double.toString(suitability)).append(" % with probability ")
               .append(Double.toString(probability)).append(" % of this happening. ");

            Consequence consequence = new Consequence(probability, suitability, _description.toString());
            consequences.add(consequence);

            if (log.isInfoEnabled()) {
                log.info(_description.toString());
            }

            // ---- Story line ----
            StringBuilder story = new StringBuilder();
            story.append("event=\"").append(Event.ON_ACCESS).append("\"");
            story.append(": \"No decryption scheduled for access while the aggregation assumes encryption. It may not guarantee confidentiality!").append("\"");
            storyLine.add(story.toString());
        }
    }

    private Double mtbfAnalysis(Aggregation aggregation, Plugin plugin) {
        Double score = null;

        Double mtbf = plugin.getMtbf();
        String unit = plugin.getMtbfUnit();
        if (null != mtbf && null != unit && unit.length() > 0) {
            if ("HOUR".equalsIgnoreCase(unit)) {
                Double mtbfYear = (((mtbf / 24.0) / 7.0) / 52.0);
                int retentionYears = aggregation.getRetentionPeriod();
                if (mtbfYear >= retentionYears) {
                    return 100.0;
                }
                else {
                    score = mtbfYear / retentionYears;
                    score *= 100;
                }
            }
            else {
                String info = "Unknown MTBF unit: " + unit;
                log.warn(info);
            }
        }
        return score;
    }

    /**
     *
     * @param customer
     * @param scores
     * @param aggregationMap
     * @return
     * @throws EvaluationException
     */
    public CustomerLevelScore calculateScore(
            PreservationPlan customer, Collection<AggregationLevelScore> scores, Map<String, String> aggregationMap
    ) throws EvaluationException {

        Collection<String> gppStoryLine = new LinkedList<String>();

        Map<String, Collection<Plugin>> processMap = new HashMap<String, Collection<Plugin>>();

        MovingAverage intraAggregation = new MovingAverage();

        if (log.isDebugEnabled()) {
            log.debug("\n<<Calculating quality metrics...>>");
        }
        List<Aggregation> aggregations = customer.getAggregations();
        for (Aggregation aggregation : aggregations) {
            aggregationMap.put(aggregation.getId(), aggregation.getXml());

            if (log.isDebugEnabled()) {
                StringBuilder buf = new StringBuilder();
                buf.append("\n- Aggregation: ");
                buf.append(" name=\"").append(aggregation.getName());
                buf.append("\" purposes={");
                String separator = "";
                for (Purpose purpose : aggregation.getPrimaryPurposes()) {
                    buf.append(separator).append(purpose.getPurposeClass().name());
                    separator = ", ";
                }
                buf.append("}");
                log.debug(buf);
            }

            Collection<String> aggrStoryLine = new LinkedList<String>();
            Collection<Consequence> consequences = new LinkedList<Consequence>();

            MovingAverage interAggregation = new MovingAverage();

            //--------------------------------------------------------------------
            // Iterate through aggregation specific actions (on a per event basis)
            //--------------------------------------------------------------------
            Collection<Event> events = aggregation.getEvents().values();
            for (Event event : events) {
                Event.EventType type = event.getType();
                String trigger = type.name(); // ON_INGEST, ...

                Collection<Plugin> eventPlugins = processMap.get(trigger);
                if (null == eventPlugins) {
                    eventPlugins = new LinkedList<Plugin>();
                    processMap.put(trigger, eventPlugins);
                }

                if (log.isDebugEnabled()) {
                    StringBuilder buf = new StringBuilder();
                    buf.append("-- Event: ");
                    buf.append(" event=").append(trigger);
                    log.debug(buf);
                }

                // Aggregation specific plugins
                List<Plugin> plugins = event.getPlugins();
                for (Plugin plugin : plugins) {
                    eventPlugins.add(plugin);

                    Double score = calculateScore(aggregation, trigger, plugin, aggrStoryLine, consequences);
                    if (null != score) {
                        if (log.isDebugEnabled()) {
                            log.debug("    score=" + Number.roundTwoDecimals(score) + "%");
                        }
                        interAggregation.update(score);
                    }
                }
            }

            //--------------------------------------------------------------------
            // Iterate through aggregation specific actions (on a per copy basis)
            //--------------------------------------------------------------------
            Collection<Copy> copies = aggregation.getCopies().values();
            for (Copy copy : copies) {
                // Storage plugin
                Plugin plugin = copy.getStoragePlugin();
                Double score = calculateScore(aggregation, /* irrelevant */ null, plugin, aggrStoryLine, consequences);
                if (null != score) {
                    if (log.isDebugEnabled()) {
                        log.debug("    score=" + Number.roundTwoDecimals(score) + "%");
                    }
                    interAggregation.update(score);
                }

                // Compute plugin - if one was assigned
                plugin = copy.getComputePlugin();
                if (null != plugin) {
                    score = calculateScore(aggregation, /* irrelevant */ null, plugin, aggrStoryLine, consequences);
                    if (null != score) {
                        if (log.isDebugEnabled()) {
                            log.debug("    score=" + Number.roundTwoDecimals(score) + "%");
                        }
                        interAggregation.update(score);
                    }
                }
            }

            //--------------------------------------------------------------------
            // Iterate through system actions (on a per event basis). These are
            // actions that apply to all aggregations in addition to the actions
            // that are specific to individual aggregations.
            //--------------------------------------------------------------------
            events = customer.getSystemWideEvents().values();
            for (Event event : events) {
                Event.EventType type = event.getType();
                String trigger = type.name(); // ON_INGEST, ...

                Collection<Plugin> eventPlugins = processMap.get(trigger);
                if (null == eventPlugins) {
                    eventPlugins = new LinkedList<Plugin>();
                    processMap.put(trigger, eventPlugins);
                }

                if (log.isDebugEnabled()) {
                    StringBuilder buf = new StringBuilder();
                    buf.append("-- (System) Event: ");
                    buf.append(" event=").append(trigger);
                    log.debug(buf);
                }

                // These are plugins bound to system actions
                List<Plugin> plugins = event.getPlugins();
                for (Plugin plugin : plugins) {
                    eventPlugins.add(plugin);

                    Double score = calculateScore(aggregation, trigger, plugin, aggrStoryLine, consequences);
                    if (null != score) {
                        if (log.isDebugEnabled()) {
                            log.debug("    score=" + Number.roundTwoDecimals(score) + "%");
                        }
                        interAggregation.update(score);
                    }
                }
            }

            //-----------------------------------------------------------------------------------------------
            // Do analysis on process level ON_INGEST -> ... -> ON_ACCESS
            //   SHA-512 better than MD5 during ingest and access, but using MD5 during storage (ON_TIME) has
            //   no such implications. [OK - covered]
            //   Check whether fixity checks are done during ON_INGEST and ON_ACCESS!  [DONE!]
            //-----------------------------------------------------------------------------------------------
            ON_INGEST_analysis(aggregation, processMap, aggrStoryLine, consequences, interAggregation);
            ON_ACCESS_analysis(aggregation, processMap, aggrStoryLine, consequences, interAggregation);

            //-----------------------------------------------------------------------------------------------
            // If purpose is provided to choose weights, should we track
            // usage-profile i.e. how we want to use the information (frequency, time, ...);
            // "Glacier would be no-good since your usage-profile indicates that the latency is too high"
            // Popularity of material? Time of staying? Number of accesses?
            //-----------------------------------------------------------------------------------------------

            //-----------------------------------------------------------------------------------------------
            // "Quality of service"-aspects, such as uptime of service... MTBF (related to monitoring below)
            //-----------------------------------------------------------------------------------------------
            {
                if (log.isDebugEnabled()) {
                    log.debug("-- Calculations for MTBF");
                }

                // Iterate over all events and all plugins in each event
                /*  Don't assess quality based on MTBF for "normal" plugins
                Collection<Collection<Plugin>> eventCollections = processMap.values();
                for (Collection<Plugin> collection : eventCollections) {
                    for (Plugin plugin : collection) {
                        Double score = mtbfAnalysis(aggregation, plugin);
                        if (null != score) {
                            if (log.isDebugEnabled()) {
                                log.debug("    score (mtbf)=" + Number.roundTwoDecimals(score) + "%");
                            }
                            interAggregation.update(score);
                        }
                    }
                }
                */

                // Iterate over copy storage plugins
                for (Copy copy : aggregation.getCopies().values()) {
                    Plugin plugin = copy.getStoragePlugin();
                    if (null != plugin) {
                        Double score = mtbfAnalysis(aggregation, plugin);
                        if (null != score) {
                            if (log.isDebugEnabled()) {
                                log.debug("--- Plugin: " + plugin.getServiceProvider() + "/" + plugin.getSystemId());
                                log.debug("    score (mtbf)=" + Number.roundTwoDecimals(score) + "%");
                            }
                            interAggregation.update(score);
                        }
                    }
                    /* Skip compute plugin - only storage plugin considered for MTBF analysis.
                    plugin = copy.getComputePlugin();
                    if (null != plugin) {
                        Double score = mtbfAnalysis(aggregation, plugin);
                        if (null != score) {
                            if (log.isDebugEnabled()) {
                                log.debug("--- Plugin: " + plugin.getServiceProvider() + "/" + plugin.getSystemId());
                                log.debug("    score (mtbf)=" + Number.roundTwoDecimals(score) + "%");
                            }
                            interAggregation.update(score);
                        }
                    }
                    */
                }
            }

            //-----------------------------------------------------------------------------------------------
            // Could we do anything with information generated by PDSCloud/PDALM during use,
            // adjusting auxiliary data for given plugins. I.e. monitoring information fed back
            // to auxiliary data and used when determining quality of plugins. [Independent on
            // events etc]
            //-----------------------------------------------------------------------------------------------

            //-----------------------------------------------------------------------------------------------
            // Running a transformation plugin from a provider on a specific compute cloud component,
            // should the choice of platform be judged as part of judging transformation plugin. How does
            // using a specific version of the JRE influence the score? (since we are in part depending on
            // built-in functionality for, say, hash-value calculations)
            //-----------------------------------------------------------------------------------------------

            // Update GPP data
            intraAggregation.update(interAggregation.getAverage());

            // Aggregation data
            AggregationLevelScore score = new AggregationLevelScore(
                    aggregation.getId(), aggregation.getName(), interAggregation, aggrStoryLine, consequences
            );
            scores.add(score);

            if (log.isDebugEnabled()) {
                double aggrScore = Number.roundTwoDecimals(interAggregation.getAverage());
                double aggrCV = Number.roundTwoDecimals(interAggregation.getCV());
                long aggSetSize = interAggregation.getCount();

                String info = "Aggregation score=" + Number.roundTwoDecimals(aggrScore) + "% ";
                info += "cv=" + Number.roundTwoDecimals(aggrCV) + "% ";
                info += "data-set=" + aggSetSize + " pts";
                log.debug(info);
            }

            if (log.isDebugEnabled()) {
                log.debug("Aggregation story line:");
                for (String story : aggrStoryLine) {
                    log.debug(" * " + story);
                }
            }
        }

        //-----------------------------------------------------------------------------------------------
        // Analysis of risks.
        //
        //
        //-----------------------------------------------------------------------------------------------
        double customerImpact = 0.0;
        double providerImpact = 0.0;
        double reconfigurationImpact = 0.0;

        // GPP data
        if (log.isDebugEnabled()) {
            log.debug("GPP story line:");
            for (String story : gppStoryLine) {
                log.debug(" * " + story);
            }
        }
        return new CustomerLevelScore(customer.getId(), intraAggregation, customerImpact, providerImpact, reconfigurationImpact, gppStoryLine);
    }
}


text:
namespace podbeuter {

enum dlstatus_t { DL_QUEUED = 0, DL_DOWNLOADING, DL_CANCELLED, DL_DELETED, DL_FINISHED, DL_FAILED, DL_ALREADY_DOWNLOADED, DL_READY, DL_PLAYED };

class pb_controller;

class download {
	public:
		download(pb_controller * c = 0);
		~download();
		double percents_finished();
		const char * status_text();
		inline dlstatus_t status() const {
			return dlstatus;
		}
		const char * filename();
		const char * url();
		void set_filename(const std::string& str);
		void set_url(const std::string& url);
		void set_progress(double cur, double max);
		void set_status(dlstatus_t dls);
		void set_kbps(double kbps);
		double kbps();
		void set_offset(unsigned long offset);

		inline double current_size() const {
			return cursize + offs;
		}
		inline double total_size() const {
			return totalsize + offs;
		}

	private:
		std::string fn;
		std::string url_;
		dlstatus_t dlstatus;
		float cursize;
		float totalsize;
		double curkbps;
		unsigned long offs;
		pb_controller * ctrl;
};

}

#endif


text:
require 'spec_helper'

module IMS::LTI::Models
  describe RestService do
    subject{RestService.new(endpoint: 'http://example.com/endpoint', format: "my_format", action: "POST", id: 'example.com:unique_id')}

    it 'converts to a RestServiceProfile' do
      profile = subject.profile

      expect(profile).to be_a RestServiceProfile
      expect(profile.type).to eq 'RestServiceProfile'
      expect(profile.service).to eq 'http://example.com/endpoint'
      expect(profile.action).to eq "POST"
    end

    it 'pluralizes format' do
      expect(subject.formats).to eq ["my_format"]
    end

    it 'pluralizes action' do
      expect(subject.actions).to eq ["POST"]
    end
  end
end


text:
package Alcohols;

import Consts.MagicStrings;
import java.util.LinkedHashMap;

public class Water implements Drink, Beer, MagicStrings {

    private final LinkedHashMap<String, Integer> ingredients;

    public Water() {
        ingredients = new LinkedHashMap();
        ingredients.put(WATER, 200);
    }

    @Override
    public LinkedHashMap<String, Integer> getIngredients() {
        return ingredients;
    }
}


text:
package fedops_encryption

import (
  "crypto/ecdsa"
  "crypto/elliptic"
  "crypto/rand"
  // _ "crypto/rsa"
  "crypto/x509"
  "crypto/x509/pkix"
  "encoding/pem"
  "fmt"
  "log"
  "math/big"
  "net"
  "os"
  "time"
)

type Cert_Config struct {
  IP string
}

func GenerateCert(certConfig Cert_Config) Cert {
  cert := Cert{}
  cert.Generate(certConfig)
  return cert
}

type Cert struct {
  // host string
  // ValidFrom string
  // ValidTo string
  // CA bool
  // KeySize int
  CertificatePem []byte
  PublicPem  []byte
  PrivatePem []byte
}

func (c *Cert) Generate(certConfig Cert_Config) {
  // TODO :: use the fedops keypair type
  // fmt.Println("Creating EC Key")
  priv, err := ecdsa.GenerateKey(elliptic.P521(), rand.Reader)
  if err != nil {
    fmt.Println(err)
    return
  }

  priv_der, err := x509.MarshalECPrivateKey(priv)
  if err != nil {
    fmt.Fprintf(os.Stderr, "Unable to marshal ECDSA private key: %v", err)
    return
  }
  priv_blk := pem.Block{
    // Type:    "RSA PRIVATE KEY",
    Type:    "EC PRIVATE KEY",
    Headers: nil,
    Bytes:   priv_der,
  }

  // Public Key generation
  pub := priv.PublicKey
  pub_der, err := x509.MarshalPKIXPublicKey(&pub)
  if err != nil {
    fmt.Println("Failed to get der format for PublicKey.", err)
    return
  }

  pub_blk := pem.Block{
    Type:    "PUBLIC KEY",
    Headers: nil,
    Bytes:   pub_der,
  }

  notBefore := time.Now()
  var validFor time.Duration
  validFor = 365 * 24 * time.Hour
  notAfter := notBefore.Add(validFor)

  serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
  serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
  if err != nil {
    log.Fatalf("failed to generate serial number: %s", err)
  }

  template := x509.Certificate{
    DNSNames: []string{"*"},
    IPAddresses: []net.IP{net.ParseIP(certConfig.IP)},
    SerialNumber: serialNumber,
    Subject: pkix.Name{
      Organization: []string{"Fedops Daemon Certificate"},
    },
    NotBefore: notBefore,
    NotAfter:  notAfter,
    KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
    ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
    BasicConstraintsValid: true,
    IsCA: true,
  }

  derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, &priv.PublicKey, priv)
  if err != nil {
    log.Fatalf("Failed to create certificate: %s", err)
  }

  cert_blk := pem.Block{
    Type: "CERTIFICATE",
    Bytes: derBytes,
  }

  //////////////////////////
  // Write to memory
  //////////////////////////

  c.CertificatePem = pem.EncodeToMemory(&cert_blk)
  c.PrivatePem = pem.EncodeToMemory(&priv_blk)
  c.PublicPem = pem.EncodeToMemory(&pub_blk)

  // //////////////////////////
  // // Write to disk
  // //////////////////////////
  // os.Mkdir("./.security", 0777)
  // fmt.Println("Writing Cert")
  // certOut, err := os.Create("./.security/cert.pem")
  // if err != nil {
  //   log.Fatalf("failed to open cert.pem for writing: %s", err)
  // }
  // pem.Encode(certOut, &cert_blk)
  // certOut.Close()
  // log.Print("written cert.pem\n")

  // fmt.Println("Writing Key")
  // keyOut, err := os.OpenFile("./.security/key.pem", os.O_WRONLY | os.O_CREATE | os.O_TRUNC, 0600)
  // if err != nil {
  //   log.Print("failed to open key.pem for writing:", err)
  //   return
  // }

  // pem.Encode(keyOut, &priv_blk)
  // keyOut.Close()
  // log.Print("written key.pem\n")
}


text:
package cleanimports

import (
	"go/ast"
	"go/format"
	"go/parser"
	"go/token"
	"io"
	"strings"

	"golang.org/x/tools/go/ast/astutil"
)

// Clean writes the clean source to io.Writer. The source can be a io.Reader,
// string or []bytes
func Clean(w io.Writer, src interface{}) error {
	fset := token.NewFileSet()
	file, err := parser.ParseFile(fset, "clean.go", src, parser.ParseComments)
	if err != nil {
		return err
	}
	// Clean unused imports
	imports := astutil.Imports(fset, file)
	for _, group := range imports {
		for _, imp := range group {
			path := strings.Trim(imp.Path.Value, `"`)
			if !astutil.UsesImport(file, path) {
				astutil.DeleteImport(fset, file, path)
			}
		}
	}
	ast.SortImports(fset, file)
	// Write formatted code without unused imports
	return format.Node(w, fset, file)
}