text:
package com.yunikov.algorithms.graphs.search;

import com.yunikov.algorithms.graphs.Graph;
import com.yunikov.algorithms.graphs.GraphAlgorithmTest;
import com.yunikov.algorithms.graphs.Vertex;
import com.yunikov.algorithms.util.FileUtils;
import org.junit.Assert;
import org.junit.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class GraphSearchTest extends GraphAlgorithmTest {

    protected static final Graph TEST_SEARCH_GRAPH = new Graph();
    protected static final List<Vertex> TEST_SEARCH_VERTICES = new ArrayList<>(4);
    static {
        final Vertex vertexOne = new Vertex(1);
        final Vertex vertexTwo = new Vertex(2);
        final Vertex vertexThree = new Vertex(3);
        final Vertex vertexFour = new Vertex(4);
        final Vertex vertexFive = new Vertex(5);
        final Vertex vertexSix = new Vertex(6);

        TEST_SEARCH_GRAPH.addVertex(vertexOne);
        TEST_SEARCH_GRAPH.addVertex(vertexTwo);
        TEST_SEARCH_GRAPH.addVertex(vertexThree);
        TEST_SEARCH_GRAPH.addVertex(vertexFour);
        TEST_SEARCH_GRAPH.addVertex(vertexFive);
        TEST_SEARCH_GRAPH.addVertex(vertexSix);

        TEST_SEARCH_VERTICES.add(vertexOne);
        TEST_SEARCH_VERTICES.add(vertexTwo);
        TEST_SEARCH_VERTICES.add(vertexThree);
        TEST_SEARCH_VERTICES.add(vertexFour);
        TEST_SEARCH_VERTICES.add(vertexFive);
        TEST_SEARCH_VERTICES.add(vertexSix);

        TEST_SEARCH_GRAPH.createEdge(vertexOne, vertexTwo, 1);
        TEST_SEARCH_GRAPH.createEdge(vertexOne, vertexThree, 4);
        TEST_SEARCH_GRAPH.createEdge(vertexTwo, vertexFour, 2);
        TEST_SEARCH_GRAPH.createEdge(vertexThree, vertexFour, 6);
        TEST_SEARCH_GRAPH.createEdge(vertexThree, vertexFive, 3);
        TEST_SEARCH_GRAPH.createEdge(vertexFour, vertexFive, 3);
        TEST_SEARCH_GRAPH.createEdge(vertexFive, vertexSix, 3);
    }

    @Test
    public void testBreadthFirstSearch() {
        testSearch("Breadth-first search", new BreadthFirstSearch());
    }

    @Test
    public void testDepthFirstSearch() {
        testSearch("Depth-first search", new DepthFirstSearch());
    }

    private void testSearch(final String algorithmName, final GraphSearch graphSearch) {
        runningTimeOf(algorithmName, () -> {
            Assert.assertTrue(graphSearch.search(createTestGraph(), testVertices.get(3)).isPresent());
            Assert.assertTrue(graphSearch.search(createTestGraph(), new Vertex(4)).isPresent());
            Assert.assertTrue(graphSearch.search(createTestGraph(), 4).isPresent());
            Assert.assertFalse(graphSearch.search(createTestGraph(), new Vertex(99)).isPresent());
            Assert.assertFalse(graphSearch.search(createTestGraph(), 99).isPresent());
            Assert.assertTrue(graphSearch.search(TEST_SEARCH_GRAPH, new Vertex(4)).isPresent());
            Assert.assertTrue(graphSearch.search(TEST_SEARCH_GRAPH, TEST_SEARCH_VERTICES.get(5)).isPresent());
            Assert.assertFalse(graphSearch.search(TEST_SEARCH_GRAPH, new Vertex(9)).isPresent());
            Assert.assertTrue(graphSearch.search(TEST_SEARCH_GRAPH, new Vertex(6)).isPresent());

            try {
                final Graph graph = Graph.createGraph(FileUtils.twoDimensionalArray("src/test/java/ua/yyunikov/algorithms/graphs/testGraph.txt"));
                Assert.assertTrue(graphSearch.search(graph, 151).isPresent());
            } catch (final IOException e) {
                System.err.println("Unexpected error: " + e);
            }
        });
    }
}


text:
var WebTorrent = require("webtorrent");
var client = new WebTorrent();

client.seed('/Users/Baoyee/Codesmith/wf-react/test/fixtures/assets/images/falcon.jpg', function(torrent) {
  --totalSeeds;
  console.log('🐣 ', torrent.files[0].name, ' now seeding at hash ', torrent.infoHash);

  if (!totalSeeds) console.log("🕊 all seeds active")
});

client.seed('/Users/Baoyee/Codesmith/wf-react/test/fixtures/assets/images/eagle.jpg', function(torrent) {
  --totalSeeds;
  console.log('🐣 ', torrent.files[0].name, ' now seeding at hash ', torrent.infoHash);

  if (!totalSeeds) console.log("🕊 all seeds active")
});

client.seed('/Users/Baoyee/Codesmith/wf-react/test/fixtures/assets/videos/birds.gif', function(torrent) {
  --totalSeeds;
  console.log('🐣 ', torrent.files[0].name, ' now seeding at hash ', torrent.infoHash);

  if (!totalSeeds) console.log("🕊 all seeds active")
});

client.seed('/Users/Baoyee/Codesmith/wf-react/test/fixtures/assets/images/owl.jpg', function(torrent) {
  --totalSeeds;
  console.log('🐣 ', torrent.files[0].name, ' now seeding at hash ', torrent.infoHash);

  if (!totalSeeds) console.log("🕊 all seeds active")
});

redpajama_set_name: RedPajamaGithub


text:
package com.liveperson.migdalor.schema.api;

import com.liveperson.migdalor.schema.exception.MigdalorEncodingException;

/**
 * Created with IntelliJ IDEA.
 * User: ehudl
 * Date: 6/10/13
 * Time: 3:08 PM
 *
 */
public interface SchemaEncoder<T> {

    abstract public byte [] encode(T event) throws MigdalorEncodingException;
}


text:
package batch.partition;

import java.util.List;

import batch.util.BatchFactory;

public interface PartitionFactory<E> extends BatchFactory<E> {

	public E Other(Object external, E... subs);

	public E Other(Object external, List<E> subs);

	public E DynamicCall(E target, String method, List<E> args);

	public E Mobile(String type, E exp);

	public E setExtra(E exp, Object extraKey, Object extraInfo);
}


text:
Initializers are the main way you expand your actionhero server.  This is where you connect to databases, modify the global `api` object with new classes and helper methods, and set up your [middleware](docs#middleware).

Initializers run in 3 phases coinciding with the lifecycles of the application: `init`, `start`, and `stop`.  All `init` steps happen before all `start` steps.  Initializers can define both methods and priorities which will happen at each phase of the server's lifecycle.

System initializers (like setting up redis and the cache) have priority levels in the 100 to 1000 level range.  Application initializers should run with a priority level of over 1000 to use methods created by the system.  You can of course set priority levels lower than 1000 in your application (perhaps you connect to a database).  The priroity level split is purely convention.

In general, `initialize()` methods should create prototypes and new objects, and `start()` should boot things or connect to external resources.

## Format

```javascript
// initializers/stuffInit.js

module.exports = {
  loadPriority:  1000,
  startPriority: 1000,
  stopPriority:  1000,
  initialize: function(api, next){
    api.myObject = {};

    next();
  },
  start: function(api, next){
    // connect to server
    next();
  },
  stop: function(api, next){
    // disconnect from server
    next();
  }
}
```

To use a custom initializer, create a `initializers` directory in your project. Export an object with at least one of `start`, `stop` or `initialize` and specify your priorities.

You can generate a file of this type with `actionhero generateInitializer --name=stuffInit`

## Errors

You can pass an error to the callback of any step in the initializer.  Doing so will cause actionhero to log the error and stop the server.  For example, you might throw an error if you cannot connect to an external service at boot, [like a database](https://github.com/evantahler/ah-sequelize-plugin/blob/master/initializers/sequelize.js).


text:
namespace JSON{
	namespace util{
		std::string pack(const std::string& json){
			std::string result;

			for(auto it : json){
				if(isspace(it)) continue;
				result.push_back(it);
			}

			return result;
		}
		std::string pack(std::string&& json){
			return pack(json);
		}

		bool good(const std::string& json){
			auto it = json.cbegin();
			auto end = json.cend();
			if(it == end) return false;

			using namespace JSON::reader;

			if(isspace(*it) && !go_until(it, end, isspace, false))
				return false;
			if(!is_elem(it, end))
				return false;
			if(it == end) return true;
			if(!isspace(*it)) return false;
			if(!go_until(it, end, isspace, false))
				return true;
			return false;
		}
	}
}


text:
package org.mifos.framework.components.logger;

import java.util.ResourceBundle;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;

/**
 * This class contains a logger object log the messages. Also contains functions
 * to log the messages at different levels
 */
public class Log4jLogger extends MifosLogger {

    /** logger to log the statements */
    private Logger logger = null;

    /**
     * Constructor: Obtains an instance of the logger with a specified name.
     * Root logger is "org.mifos"
     *
     * @param name
     *            The name of the Logger
     */
    public Log4jLogger(String name) {
        logger = Logger.getLogger(name);
    }

    /**
     * Constructor: Obtains an instance of the logger with a specified name.
     * Root logger is "org.mifos"
     *
     * @param name
     *            The name of the Logger
     * @param resourceBundle
     *            The resource bundle associated with the logger
     */
    public Log4jLogger(String name, ResourceBundle resourceBundle) {
        logger = Logger.getLogger(name);
        // sets the resource bundle for the logger
        logger.setResourceBundle(resourceBundle);
    }

    @Override
    public String getUserID() {
        return ApplicationConfig.getUserId();
    }

    @Override
    public String getOfficeID() {
        return ApplicationConfig.getOfficeId();
    }

    @Override
    protected void logKey(Level level, String key, Object[] args1, Throwable exception) {
        logger.l7dlog(level, key, args1, exception);
    }

    @Override
    protected void logNonKey(Level level, String message, Throwable exception) {
        logger.log(level, message, exception);
    }

}


text:
<!--
FrozenIsBool False
-->

##Example

Do a simple one disconnected rate model


text:
// Generated by the protocol buffer compiler.  DO NOT EDIT!
// source: google/logging/v2/logging_config.proto

package com.google.logging.v2;

public interface ListExclusionsRequestOrBuilder
    extends
    // @@protoc_insertion_point(interface_extends:google.logging.v2.ListExclusionsRequest)
    com.google.protobuf.MessageOrBuilder {

  /**
   *
   *
   *

   * Required. The parent resource whose exclusions are to be listed.
   *     "projects/[PROJECT_ID]"
   *     "organizations/[ORGANIZATION_ID]"
   *     "billingAccounts/[BILLING_ACCOUNT_ID]"
   *     "folders/[FOLDER_ID]"
   *

   *
   * string parent = 1;
   */
  java.lang.String getParent();
  /**
   *
   *
   *

   * Required. The parent resource whose exclusions are to be listed.
   *     "projects/[PROJECT_ID]"
   *     "organizations/[ORGANIZATION_ID]"
   *     "billingAccounts/[BILLING_ACCOUNT_ID]"
   *     "folders/[FOLDER_ID]"
   *

   *
   * string parent = 1;
   */
  com.google.protobuf.ByteString getParentBytes();

  /**
   *
   *
   *

   * Optional. If present, then retrieve the next batch of results from the
   * preceding call to this method.  `pageToken` must be the value of
   * `nextPageToken` from the previous response.  The values of other method
   * parameters should be identical to those in the previous call.
   *

   *
   * string page_token = 2;
   */
  java.lang.String getPageToken();
  /**
   *
   *
   *

   * Optional. If present, then retrieve the next batch of results from the
   * preceding call to this method.  `pageToken` must be the value of
   * `nextPageToken` from the previous response.  The values of other method
   * parameters should be identical to those in the previous call.
   *

   *
   * string page_token = 2;
   */
  com.google.protobuf.ByteString getPageTokenBytes();

  /**
   *
   *
   *

   * Optional. The maximum number of results to return from this request.
   * Non-positive values are ignored.  The presence of `nextPageToken` in the
   * response indicates that more results might be available.
   *

   *
   * int32 page_size = 3;
   */
  int getPageSize();
}


text:
/* 	2012-9  Created by jorya_txj
  *	xxxxxx   please added here
  */

#ifndef ACTIVE_MEMORY_H
#define ACTIVE_MEMORY_H

void *active_event_memory_allocate(MEM_POOL *pool, RAW_U16 sig);
void active_memory_collect(STATE_EVENT *event);

#endif


text:
#pragma once

#include <af/defines.h>
#include <common/MemoryManager.hpp>

#include <functional>
#include <memory>

namespace cpu
{

template<typename T>
using uptr = std::unique_ptr<T[], std::function<void(T[])>>;

template<typename T> std::unique_ptr<T[], std::function<void(T *)>> memAlloc(const size_t &elements);
void *memAllocUser(const size_t &bytes);

// Need these as 2 separate function and not a default argument
// This is because it is used as the deleter in shared pointer
// which cannot support default arguments
template<typename T> void memFree(T* ptr);
void memFreeUser(void* ptr);

void memLock(const void *ptr);
void memUnlock(const void *ptr);
bool isLocked(const void *ptr);

template<typename T> T* pinnedAlloc(const size_t &elements);
template<typename T> void pinnedFree(T* ptr);

size_t getMaxBytes();
unsigned getMaxBuffers();

void deviceMemoryInfo(size_t *alloc_bytes, size_t *alloc_buffers,
                      size_t *lock_bytes,  size_t *lock_buffers);
void garbageCollect();
void pinnedGarbageCollect();

void printMemInfo(const char *msg, const int device);

void setMemStepSize(size_t step_bytes);
size_t getMemStepSize(void);
bool checkMemoryLimit();

class MemoryManager : public common::MemoryManager<cpu::MemoryManager>
{
    public:
        MemoryManager();
        ~MemoryManager();
        int getActiveDeviceId();
        size_t getMaxMemorySize(int id);
        void *nativeAlloc(const size_t bytes);
        void nativeFree(void *ptr);
};
}


text:
"""Tests for tillicum.timer."""

import unittest

import tillicum.timer as timer

class TimerTest(unittest.TestCase):

    def test_decorator_plain(self):
        tfunc = lambda: "OBOY"
        rval = timer.timer(tfunc)()
        self.assertTrue(isinstance(rval, tuple),
                        "Expected tuple, got %s %s" %(
                type(rval), rval))
        self.assertEqual(len(rval), 2)
        (rval, timings) = rval
        self.assertEqual(rval, 'OBOY')
        self.assertTrue(isinstance(timings, list))
        self.assertEqual(len(timings), 3)

    def test_decorator_called(self):
        tfunc = lambda: "OBOY"
        rval = timer.timer()(tfunc)()
        self.assertTrue(isinstance(rval, tuple),
                        "Expected tuple, got %s %s" %(
                type(rval), rval))
        self.assertEqual(len(rval), 2)
        (rval, timings) = rval
        self.assertEqual(rval, 'OBOY')
        self.assertTrue(isinstance(timings, list))
        self.assertEqual(len(timings), 3)

    def test_manager(self):
        with timer.timer() as timings:
            self.assertTrue(isinstance(timings, list))

        self.assertTrue(isinstance(timings, list))
        self.assertEqual(len(timings), 3)

if __name__ == '__main__':
    unittest.main()


text:
//
//  ArticleListCell.h
//  YueSheng
//
//  Created by yellow on 16/7/27.
//  Copyright © 2016年 yellow. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "ArticleListStatus.h"

@class ArticleListCell;

@protocol ArticleListCellDelegate <NSObject>

@optional
-(void)articleListCellDidClickPraiseBtn:(ArticleListCell*)articleListCell articleListStatus:(ArticleListStatus *)status;

-(void)articleListCellDidClickCommentBtn:(ArticleListCell*)articleListCell articleListStatus:(ArticleListStatus *)status;

@end

@interface ArticleListCell : UITableViewCell

@property(nonatomic,strong)ArticleListStatus *status;

/**
 *  通过一个tableView来创建一个cell
 */
+ (instancetype)cellWithTableView:(UITableView *)tableView;

@property(nonatomic,weak)id<ArticleListCellDelegate> delegate;

@end


text:
package com.ghgande.j2mod.modbus.msg;

import com.ghgande.j2mod.modbus.Modbus;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

/**
 * Class implementing a <tt>WriteCoilResponse</tt>. The implementation directly
 * correlates with the class 0 function write coil (FC 5). It
 * encapsulates the corresponding response message.
 *
 * @author Dieter Wimberger
 * @author Steve O'Hara (4NG)
 * @version 2.0 (March 2016)
 */
public class WriteCoilResponse extends ModbusResponse {
    private boolean coil = false;
    private int reference;

    /**
     * Constructs a new <tt>WriteCoilResponse</tt> instance.
     */
    public WriteCoilResponse() {
        super();

        setFunctionCode(Modbus.WRITE_COIL);
        setDataLength(4);
    }

    /**
     * Constructs a new <tt>WriteCoilResponse</tt> instance.
     *
     * @param reference the offset were writing was started from.
     * @param b         the state of the coil; true set, false reset.
     */
    public WriteCoilResponse(int reference, boolean b) {
        super();

        setFunctionCode(Modbus.WRITE_COIL);
        setDataLength(4);

        setReference(reference);
        setCoil(b);
    }

    /**
     * Gets the state that has been returned in this <tt>WriteCoilRequest</tt>.
     *
     * @return true if the coil is set, false if unset.
     */
    public boolean getCoil() {
        return coil;
    }

    /**
     * Sets the state that has been returned in the raw response.
     *
     * @param b true if the coil should be set of false if it should be unset.
     */
    public void setCoil(boolean b) {
        coil = b;
    }

    /**
     * Returns the reference of the register of the coil that has been written
     * to with the request.
     *

     *
     * @return the reference of the coil's register.
     */
    public int getReference() {
        return reference;
    }

    /**
     * Sets the reference of the register of the coil that has been written to
     * with the request.
     *

     *
     * @param ref the reference of the coil's register.
     */
    public void setReference(int ref) {
        reference = ref;
    }

    @Override
    public void writeData(DataOutput dout) throws IOException {
        dout.write(getMessage());
    }

    @Override
    public void readData(DataInput din) throws IOException {
        byte[] data = new byte[4];
        din.readFully(data);

        setReference(((data[0] << 8) | (data[1] & 0xff)));
        setCoil(data[2] == Modbus.COIL_ON);

        setDataLength(4);
    }

    @Override
    public byte[] getMessage() {
        byte[] result = new byte[4];

        result[0] = (byte)((reference >> 8) & 0xff);
        result[1] = (byte)(reference & 0xff);
        if (coil) {
            result[2] = Modbus.COIL_ON_BYTES[0];
            result[3] = Modbus.COIL_ON_BYTES[1];
        }
        else {
            result[2] = Modbus.COIL_OFF_BYTES[0];
            result[3] = Modbus.COIL_OFF_BYTES[1];
        }
        return result;
    }
}


text:
Sequel.migration do
  change do
    create_table :reg_c405 do
      column :id, Integer, primary_key: true
      column :id_pai, Integer, index: true, null: false
      column :dt_doc, Date
      column :cro, String, size: 3
      column :crz, String, size: 6
      column :num_coo_fin, String, size: 9
      column :gt_fin, BigDecimal, size: [18, 2]
      column :vl_brt, BigDecimal, size: [18, 2]
      column :cnpj_pai, String, size: 14, index: true
    end
  end
end


text:
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
        Toggle navigation

      </button>
      {{ link_to('/', Config::get('packages/l4mod/sentryuser/sentryuser.site-title'), array('class' => 'navbar-brand')) }}

          {{UserHelper::getUserDisplayName()}}

{{ link_to('edit-profile', 'Edit Profile') }}

{{ link_to('user/logout', 'Logout') }}


text:
id }} corners-post-lists bolshaya-content-row-grid">
    {{ data_get($data, 'categories.0.title') }}
             {{ $data->views }}

url) }}">{{ $data->title or 'Title' }}

{{ $data->small_txt or 'Small Text' }}

    @if(isset($data->images) && count($data->images)>0)
    url) }}">
        images[0]->img) }}" class="img-responsive">

    @else

    @endif


text:
'use strict';

require('dotenv').config();

const githubhook = require('githubhook');
const winston    = require('winston');
const shell      = require('shelljs');

// Setup winston to log to logger.log
const logger = new (winston.Logger)({
    transports: [ new (winston.transports.File)({ filename: __dirname + '/logger.log' }) ]
});

// Initialize githubhook
const github = githubhook({
    path: '/payload',
    port: 8010,
    logger: logger,
    secret: process.env.SECRET
});

// Start listening for POST events
github.listen();
logger.info('Listening...');

// Watch for 'push' event
github.on('push', function(repo, ref, data) {
    const deliveryId = data.request.headers['x-github-delivery'];
    logger.info('Hit by push event (delivery id: %s)', deliveryId);
    deploy(deliveryId);
});

// Watch for 'release' event
github.on('release', function(repo, ref, data) {
    const deliveryId = data.request.headers['x-github-delivery'];
    logger.info('Hit by release event (delivery id: %s)', deliveryId);
    deploy(deliveryId);
});

// Deploy
function deploy(deliveryId) {
    logger.info('Deployment started... (delivery id: %s)', deliveryId);

    shell.cd(process.env.SITE_PATH);
    shell.exec('git fetch --all');
    shell.exec('git reset --hard origin/master');

    logger.info('Deployed! (delivery id: %s)', deliveryId);
};


text:
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/browser/renderer_host/render_view_host_impl.h"

#include <algorithm>
#include <set>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

#include "base/bind.h"
#include "base/callback.h"
#include "base/command_line.h"
#include "base/debug/dump_without_crashing.h"
#include "base/feature_list.h"
#include "base/hash/hash.h"
#include "base/i18n/rtl.h"
#include "base/json/json_reader.h"
#include "base/metrics/field_trial.h"
#include "base/metrics/histogram_macros.h"
#include "base/metrics/user_metrics.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/supports_user_data.h"
#include "base/system/sys_info.h"
#include "base/task/post_task.h"
#include "base/time/time.h"
#include "base/trace_event/trace_event.h"
#include "base/trace_event/typed_macros.h"
#include "base/values.h"
#include "build/build_config.h"
#include "cc/base/switches.h"
#include "content/browser/bad_message.h"
#include "content/browser/child_process_security_policy_impl.h"
#include "content/browser/dom_storage/session_storage_namespace_impl.h"
#include "content/browser/gpu/compositor_util.h"
#include "content/browser/gpu/gpu_data_manager_impl.h"
#include "content/browser/gpu/gpu_process_host.h"
#include "content/browser/renderer_host/agent_scheduling_group_host.h"
#include "content/browser/renderer_host/frame_tree.h"
#include "content/browser/renderer_host/frame_tree_node.h"
#include "content/browser/renderer_host/input/timeout_monitor.h"
#include "content/browser/renderer_host/navigation_controller_impl.h"
#include "content/browser/renderer_host/render_frame_proxy_host.h"
#include "content/browser/renderer_host/render_process_host_impl.h"
#include "content/browser/renderer_host/render_view_host_delegate.h"
#include "content/browser/renderer_host/render_view_host_delegate_view.h"
#include "content/browser/renderer_host/render_widget_host_delegate.h"
#include "content/browser/renderer_host/render_widget_host_view_base.h"
#include "content/browser/scoped_active_url.h"
#include "content/common/agent_scheduling_group.mojom.h"
#include "content/common/content_switches_internal.h"
#include "content/common/render_message_filter.mojom.h"
#include "content/common/renderer.mojom.h"
#include "content/public/browser/ax_event_notification_details.h"
#include "content/public/browser/browser_accessibility_state.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_message_filter.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/content_browser_client.h"
#include "content/public/browser/context_menu_params.h"
#include "content/public/browser/native_web_keyboard_event.h"
#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/notification_types.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_widget_host_iterator.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/common/bindings_policy.h"
#include "content/public/common/content_client.h"
#include "content/public/common/content_constants.h"
#include "content/public/common/content_features.h"
#include "content/public/common/content_switches.h"
#include "content/public/common/result_codes.h"
#include "content/public/common/url_constants.h"
#include "content/public/common/url_utils.h"
#include "media/base/media_switches.h"
#include "net/base/url_util.h"
#include "services/network/public/cpp/features.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/common/web_preferences/web_preferences.h"
#include "third_party/perfetto/include/perfetto/tracing/traced_value.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/base/clipboard/clipboard.h"
#include "ui/base/device_form_factor.h"
#include "ui/base/pointer/pointer_device.h"
#include "ui/base/ui_base_features.h"
#include "ui/base/ui_base_switches.h"
#include "ui/display/display.h"
#include "ui/display/display_switches.h"
#include "ui/display/screen.h"
#include "ui/events/blink/blink_features.h"
#include "ui/gfx/animation/animation.h"
#include "ui/gfx/color_space.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/native_widget_types.h"
#include "ui/gl/gpu_switching_manager.h"
#include "ui/native_theme/native_theme_features.h"
#include "url/url_constants.h"

#if BUILDFLAG(IS_WIN)
#include "ui/display/win/screen_win.h"
#include "ui/gfx/geometry/dip_util.h"
#include "ui/gfx/system_fonts_win.h"
#endif

#if !BUILDFLAG(IS_ANDROID)
#include "content/browser/host_zoom_map_impl.h"
#endif

#if defined(USE_OZONE)
#include "ui/base/ui_base_features.h"
#endif

using blink::WebInputEvent;

namespace content {
namespace {

using perfetto::protos::pbzero::ChromeTrackEvent;

// <process id, routing id>
using RenderViewHostID = std::pair<int32_t, int32_t>;
using RoutingIDViewMap =
    std::unordered_map<RenderViewHostID,
                       RenderViewHostImpl*,
                       base::IntPairHash<RenderViewHostID>>;
base::LazyInstance<RoutingIDViewMap>::Leaky g_routing_id_view_map =
    LAZY_INSTANCE_INITIALIZER;

#if BUILDFLAG(IS_WIN)
// Fetches the name and font size of a particular Windows system font.
void GetFontInfo(gfx::win::SystemFont system_font,
                 std::u16string* name,
                 int32_t* size) {
  const gfx::Font& font = gfx::win::GetSystemFont(system_font);
  *name = base::UTF8ToUTF16(font.GetFontName());
  *size = font.GetFontSize();
}
#endif  // BUILDFLAG(IS_WIN)

// Set of RenderViewHostImpl* that can be attached as UserData to a
// RenderProcessHost. Used to keep track of whether any RenderViewHostImpl
// instances are in the bfcache.
class PerProcessRenderViewHostSet : public base::SupportsUserData::Data {
 public:
  static PerProcessRenderViewHostSet* GetOrCreateForProcess(
      RenderProcessHost* process) {
    DCHECK(process);
    auto* set = static_cast<PerProcessRenderViewHostSet*>(
        process->GetUserData(UserDataKey()));
    if (!set) {
      auto new_set = std::make_unique<PerProcessRenderViewHostSet>();
      set = new_set.get();
      process->SetUserData(UserDataKey(), std::move(new_set));
    }
    return set;
  }

  void Insert(const RenderViewHostImpl* rvh) {
    render_view_host_instances_.insert(rvh);
  }

  void Erase(const RenderViewHostImpl* rvh) {
    auto it = render_view_host_instances_.find(rvh);
    DCHECK(it != render_view_host_instances_.end());
    render_view_host_instances_.erase(it);
  }

  bool HasNonBackForwardCachedInstances() const {
    return std::find_if(render_view_host_instances_.begin(),
                        render_view_host_instances_.end(),
                        [](const RenderViewHostImpl* rvh) {
                          return !rvh->is_in_back_forward_cache();
                        }) != render_view_host_instances_.end();
  }

 private:
  static const void* UserDataKey() { return &kUserDataKey; }

  static const int kUserDataKey = 0;

  std::unordered_set<const RenderViewHostImpl*> render_view_host_instances_;
};

const int PerProcessRenderViewHostSet::kUserDataKey;

}  // namespace

// static
const base::TimeDelta RenderViewHostImpl::kUnloadTimeout;

///////////////////////////////////////////////////////////////////////////////
// RenderViewHost, public:

// static
RenderViewHost* RenderViewHost::FromID(int render_process_id,
                                       int render_view_id) {
  return RenderViewHostImpl::FromID(render_process_id, render_view_id);
}

// static
RenderViewHost* RenderViewHost::From(RenderWidgetHost* rwh) {
  return RenderViewHostImpl::From(rwh);
}

///////////////////////////////////////////////////////////////////////////////
// RenderViewHostImpl, public:

// static
RenderViewHostImpl* RenderViewHostImpl::FromID(int process_id, int routing_id) {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  RoutingIDViewMap* views = g_routing_id_view_map.Pointer();
  auto it = views->find(RenderViewHostID(process_id, routing_id));
  return it == views->end() ? nullptr : it->second;
}

// static
RenderViewHostImpl* RenderViewHostImpl::From(RenderWidgetHost* rwh) {
  DCHECK(rwh);
  RenderWidgetHostOwnerDelegate* owner_delegate =
      RenderWidgetHostImpl::From(rwh)->owner_delegate();
  if (!owner_delegate)
    return nullptr;
  RenderViewHostImpl* rvh = static_cast<RenderViewHostImpl*>(owner_delegate);
  DCHECK_EQ(rwh, rvh->GetWidget());
  return rvh;
}

// static
void RenderViewHostImpl::GetPlatformSpecificPrefs(
    blink::RendererPreferences* prefs) {
#if BUILDFLAG(IS_WIN)
  // Note that what is called "height" in this struct is actually the font size;
  // font "height" typically includes ascender, descender, and padding and is
  // often a third or so larger than the given font size.
  GetFontInfo(gfx::win::SystemFont::kCaption, &prefs->caption_font_family_name,
              &prefs->caption_font_height);
  GetFontInfo(gfx::win::SystemFont::kSmallCaption,
              &prefs->small_caption_font_family_name,
              &prefs->small_caption_font_height);
  GetFontInfo(gfx::win::SystemFont::kMenu, &prefs->menu_font_family_name,
              &prefs->menu_font_height);
  GetFontInfo(gfx::win::SystemFont::kMessage, &prefs->message_font_family_name,
              &prefs->message_font_height);
  GetFontInfo(gfx::win::SystemFont::kStatus, &prefs->status_font_family_name,
              &prefs->status_font_height);

  prefs->vertical_scroll_bar_width_in_dips =
      display::win::ScreenWin::GetSystemMetricsInDIP(SM_CXVSCROLL);
  prefs->horizontal_scroll_bar_height_in_dips =
      display::win::ScreenWin::GetSystemMetricsInDIP(SM_CYHSCROLL);
  prefs->arrow_bitmap_height_vertical_scroll_bar_in_dips =
      display::win::ScreenWin::GetSystemMetricsInDIP(SM_CYVSCROLL);
  prefs->arrow_bitmap_width_horizontal_scroll_bar_in_dips =
      display::win::ScreenWin::GetSystemMetricsInDIP(SM_CXHSCROLL);
#elif BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
  prefs->system_font_family_name = gfx::Font().GetFontName();
#elif BUILDFLAG(IS_FUCHSIA)
  // Make Blink's "focus ring" invisible. The focus ring is a hairline border
  // that's rendered around clickable targets.
  // TODO(crbug.com/1066605): Consider exposing this as a FIDL parameter.
  prefs->focus_ring_color = SK_AlphaTRANSPARENT;
#endif
#if defined(USE_OZONE)
  prefs->selection_clipboard_buffer_available =
      ui::Clipboard::IsSupportedClipboardBuffer(
          ui::ClipboardBuffer::kSelection);
#endif
}

// static
bool RenderViewHostImpl::HasNonBackForwardCachedInstancesForProcess(
    RenderProcessHost* process) {
  return PerProcessRenderViewHostSet::GetOrCreateForProcess(process)
      ->HasNonBackForwardCachedInstances();
}

RenderViewHostImpl::RenderViewHostImpl(
    FrameTree* frame_tree,
    SiteInstance* instance,
    std::unique_ptr<RenderWidgetHostImpl> widget,
    RenderViewHostDelegate* delegate,
    int32_t routing_id,
    int32_t main_frame_routing_id,
    bool swapped_out,
    bool has_initialized_audio_host,
    scoped_refptr<BrowsingContextState> main_browsing_context_state)
    : render_widget_host_(std::move(widget)),
      delegate_(delegate),
      render_view_host_map_id_(frame_tree->GetRenderViewHostMapId(
          static_cast<SiteInstanceImpl*>(instance)->group())),
      storage_partition_config_(instance->GetStoragePartitionConfig()),
      routing_id_(routing_id),
      main_frame_routing_id_(main_frame_routing_id),
      frame_tree_(frame_tree),
      main_browsing_context_state_(std::move(main_browsing_context_state)) {
  TRACE_EVENT("navigation", "RenderViewHostImpl::RenderViewHostImpl",
              ChromeTrackEvent::kRenderViewHost, *this);
  DCHECK(delegate_);
  DCHECK_NE(GetRoutingID(), render_widget_host_->GetRoutingID());

  PerProcessRenderViewHostSet::GetOrCreateForProcess(GetProcess())
      ->Insert(this);

  std::pair<RoutingIDViewMap::iterator, bool> result =
      g_routing_id_view_map.Get().emplace(
          RenderViewHostID(GetProcess()->GetID(), routing_id_), this);
  CHECK(result.second) << "Inserting a duplicate item!";
  GetAgentSchedulingGroup().AddRoute(routing_id_, this);

  GetProcess()->AddObserver(this);
  ui::GpuSwitchingManager::GetInstance()->AddObserver(this);

  // New views may be created during RenderProcessHost::ProcessDied(), within a
  // brief window where the internal ChannelProxy is null. This ensures that the
  // ChannelProxy is re-initialized in such cases so that subsequent messages
  // make their way to the new renderer once its restarted.
  // TODO(crbug.com/1111231): Should this go via AgentSchedulingGroupHost? Is it
  // even needed after the migration?
  GetProcess()->EnableSendQueue();

  if (!is_active())
    GetWidget()->UpdatePriority();

  close_timeout_ = std::make_unique<TimeoutMonitor>(base::BindRepeating(
      &RenderViewHostImpl::ClosePageTimeout, weak_factory_.GetWeakPtr()));

  input_device_change_observer_ =
      std::make_unique<InputDeviceChangeObserver>(this);

  bool initially_hidden = frame_tree_->delegate()->IsHidden();
  page_lifecycle_state_manager_ = std::make_unique<PageLifecycleStateManager>(
      this, initially_hidden ? blink::mojom::PageVisibilityState::kHidden
                             : blink::mojom::PageVisibilityState::kVisible);

  GetWidget()->set_owner_delegate(this);
  frame_tree_->RegisterRenderViewHost(render_view_host_map_id_, this);
}

RenderViewHostImpl::~RenderViewHostImpl() {
  TRACE_EVENT_INSTANT("navigation", "~RenderViewHostImpl()",
                      ChromeTrackEvent::kRenderViewHost, *this);
  // TODO(https://crbug.com/1234634): Remove this.
  // If the view is destroyed while we were are still waiting for an ack,
  // then log how long we have been waiting.
  if (page_lifecycle_state_manager_->persisted_pageshow_timestamp_bug_1234634()
          .has_value()) {
    base::TimeDelta delta =
        base::Time::Now() - page_lifecycle_state_manager_
                                ->persisted_pageshow_timestamp_bug_1234634()
                                .value();
    base::UmaHistogramMediumTimes("Event.PageShow.Persisted.ViewDestroyed.Time",
                                  delta);
  }

  PerProcessRenderViewHostSet::GetOrCreateForProcess(GetProcess())->Erase(this);

  // Destroy the RenderWidgetHost.
  GetWidget()->ShutdownAndDestroyWidget(false);
  if (IsRenderViewLive()) {
    // Destroy the RenderView, which will also destroy the RenderWidget.
    GetAgentSchedulingGroup().DestroyView(GetRoutingID());
  }

  ui::GpuSwitchingManager::GetInstance()->RemoveObserver(this);

  // Detach the routing ID as the object is going away.
  GetAgentSchedulingGroup().RemoveRoute(GetRoutingID());
  g_routing_id_view_map.Get().erase(
      RenderViewHostID(GetProcess()->GetID(), GetRoutingID()));

  delegate_->RenderViewDeleted(this);
  GetProcess()->RemoveObserver(this);

  // If |this| is in the BackForwardCache, then it was already removed from
  // the FrameTree at the time it entered the BackForwardCache.
  if (!is_in_back_forward_cache_)
    frame_tree_->UnregisterRenderViewHost(render_view_host_map_id_, this);
}

RenderViewHostDelegate* RenderViewHostImpl::GetDelegate() {
  return delegate_;
}

bool RenderViewHostImpl::CreateRenderView(
    const absl::optional<blink::FrameToken>& opener_frame_token,
    int proxy_route_id,
    bool window_was_opened_by_another_window) {
  TRACE_EVENT0("renderer_host,navigation",
               "RenderViewHostImpl::CreateRenderView");
  DCHECK(!IsRenderViewLive()) << "Creating view twice";

  // The process may (if we're sharing a process with another host that already
  // initialized it) or may not (we have our own process or the old process
  // crashed) have been initialized. Calling Init() multiple times will be
  // ignored, so this is safe.
  if (!GetAgentSchedulingGroup().Init())
    return false;
  DCHECK(GetProcess()->IsInitializedAndNotDead());
  DCHECK(GetProcess()->GetBrowserContext());

  // Exactly one of main_frame_routing_id_ or proxy_route_id should be set.
  CHECK(!(main_frame_routing_id_ != MSG_ROUTING_NONE &&
          proxy_route_id != MSG_ROUTING_NONE));
  CHECK(!(main_frame_routing_id_ == MSG_ROUTING_NONE &&
          proxy_route_id == MSG_ROUTING_NONE));

  RenderFrameHostImpl* main_rfh = nullptr;
  RenderFrameProxyHost* main_rfph = nullptr;
  if (main_frame_routing_id_ != MSG_ROUTING_NONE) {
    main_rfh = RenderFrameHostImpl::FromID(GetProcess()->GetID(),
                                           main_frame_routing_id_);
    DCHECK(main_rfh);
  } else {
    main_rfph =
        RenderFrameProxyHost::FromID(GetProcess()->GetID(), proxy_route_id);
    DCHECK(main_rfph);
  }
  const FrameTreeNode* const frame_tree_node =
      main_rfh ? main_rfh->frame_tree_node() : main_rfph->frame_tree_node();

  mojom::CreateViewParamsPtr params = mojom::CreateViewParams::New();

  params->renderer_preferences = delegate_->GetRendererPrefs();
  RenderViewHostImpl::GetPlatformSpecificPrefs(&params->renderer_preferences);
  params->web_preferences = delegate_->GetOrCreateWebPreferences();
  params->view_id = GetRoutingID();
  params->opener_frame_token = opener_frame_token;
  params->replication_state =
      frame_tree_node->current_replication_state().Clone();
  params->devtools_main_frame_token = frame_tree_node->devtools_frame_token();
  DCHECK_EQ(frame_tree_node->frame_tree(), frame_tree_);
  params->is_prerendering = frame_tree_->is_prerendering();

  if (main_rfh) {
    auto local_frame_params = mojom::CreateLocalMainFrameParams::New();
    local_frame_params->token = main_rfh->GetFrameToken();
    local_frame_params->routing_id = main_frame_routing_id_;
    mojo::PendingAssociatedRemote<mojom::Frame> pending_frame_remote;
    local_frame_params->frame =
        pending_frame_remote.InitWithNewEndpointAndPassReceiver();
    main_rfh->SetMojomFrameRemote(std::move(pending_frame_remote));
    main_rfh->BindBrowserInterfaceBrokerReceiver(
        local_frame_params->interface_broker.InitWithNewPipeAndPassReceiver());

    local_frame_params->is_on_initial_empty_document =
        main_rfh->frame_tree_node()->is_on_initial_empty_document();

    // If this is a new RenderFrameHost for a frame that has already committed a
    // document, we don't have a PolicyContainerHost yet. Indeed, in that case,
    // this RenderFrameHost will not display any document until it commits a
    // navigation. The policy container for the navigated document will be sent
    // to Blink at CommitNavigation time and then stored in this RenderFrameHost
    // in DidCommitNewDocument.
    if (main_rfh->policy_container_host()) {
      local_frame_params->policy_container =
          main_rfh->policy_container_host()->CreatePolicyContainerForBlink();
    }

    local_frame_params->widget_params =
        main_rfh->GetRenderWidgetHost()
            ->BindAndGenerateCreateFrameWidgetParams();

    local_frame_params->subresource_loader_factories =
        main_rfh->CreateSubresourceLoaderFactoriesForInitialEmptyDocument();

    params->main_frame = mojom::CreateMainFrameUnion::NewLocalParams(
        std::move(local_frame_params));
  } else {
    params->main_frame = mojom::CreateMainFrameUnion::NewRemoteParams(
        mojom::CreateRemoteMainFrameParams::New(
            main_rfph->GetFrameToken(), proxy_route_id,
            main_rfph->CreateAndBindRemoteMainFrameInterfaces()));
  }

  params->session_storage_namespace_id =
      frame_tree_->controller()
          .GetSessionStorageNamespace(storage_partition_config_)
          ->id();
  params->hidden = frame_tree_->delegate()->IsHidden();
  params->never_composited = delegate_->IsNeverComposited();
  params->window_was_opened_by_another_window =
      window_was_opened_by_another_window;
  params->base_background_color = delegate_->GetBaseBackgroundColor();

  bool is_portal = frame_tree_->delegate()->IsPortal();
  bool is_guest_view = delegate_->IsGuest();
  bool is_fenced_frame = frame_tree_->type() == FrameTree::Type::kFencedFrame;

  // GuestViews in the same StoragePartition need to find each other's frames.
  params->renderer_wide_named_frame_lookup = is_guest_view;

  if (is_fenced_frame) {
    params->type = mojom::ViewWidgetType::kFencedFrame;
  } else if (is_portal) {
    DCHECK(!is_guest_view);
    params->type = mojom::ViewWidgetType::kPortal;
  } else if (is_guest_view) {
    params->type = mojom::ViewWidgetType::kGuestView;
  } else {
    params->type = mojom::ViewWidgetType::kTopLevel;
  }

  // RenderViweHostImpls is reused after a crash, so reset any endpoint that
  // might be a leftover from a crash.
  page_broadcast_.reset();
  params->blink_page_broadcast =
      page_broadcast_.BindNewEndpointAndPassReceiver();

  // The renderer process's `RenderView` is owned by this `RenderViewHost`. This
  // call must, therefore, be accompanied by a `DestroyView()` [see destructor]
  // or else there will be a leak in the renderer process.
  GetAgentSchedulingGroup().CreateView(std::move(params));

  // Set the bit saying we've made the RenderView in the renderer and notify
  // content public observers.
  RenderViewCreated(main_rfh);

  // This must be posted after the RenderViewHost is marked live, with
  // `renderer_view_created_`.
  PostRenderViewReady();
  return true;
}

void RenderViewHostImpl::SetMainFrameRoutingId(int routing_id) {
  main_frame_routing_id_ = routing_id;
  GetWidget()->UpdatePriority();
  // TODO(crbug.com/419087): If a local main frame is no longer attached to this
  // RenderView then the RenderWidgetHostImpl owned by this class should be
  // informed that its renderer widget is no longer created. The RenderViewHost
  // will need to track its own live-ness then.
}

void RenderViewHostImpl::SetFrameTree(FrameTree& frame_tree) {
  TRACE_EVENT("navigation", "RenderViewHostImpl::SetFrameTree",
              ChromeTrackEvent::kRenderViewHost, *this);
  frame_tree_->UnregisterRenderViewHost(render_view_host_map_id_, this);
  frame_tree_ = &frame_tree;
  frame_tree_->RegisterRenderViewHost(render_view_host_map_id_, this);
}

void RenderViewHostImpl::EnterBackForwardCache() {
  if (!will_enter_back_forward_cache_callback_for_testing_.is_null())
    will_enter_back_forward_cache_callback_for_testing_.Run();

  TRACE_EVENT("navigation", "RenderViewHostImpl::EnterBackForwardCache",
              ChromeTrackEvent::kRenderViewHost, *this);
  frame_tree_->UnregisterRenderViewHost(render_view_host_map_id_, this);
  is_in_back_forward_cache_ = true;
  page_lifecycle_state_manager_->SetIsInBackForwardCache(
      is_in_back_forward_cache_, /*page_restore_params=*/nullptr,
      /*restoring_main_frame_from_back_forward_cache=*/false);
}

void RenderViewHostImpl::PrepareToLeaveBackForwardCache(
    base::OnceClosure done_cb) {
  page_lifecycle_state_manager_->SetIsLeavingBackForwardCache(
      std::move(done_cb));
}

void RenderViewHostImpl::LeaveBackForwardCache(
    blink::mojom::PageRestoreParamsPtr page_restore_params,
    bool restoring_main_frame_from_back_forward_cache) {
  TRACE_EVENT("navigation", "RenderViewHostImpl::LeaveBackForwardCache",
              ChromeTrackEvent::kRenderViewHost, *this);
  // At this point, the frames |this| RenderViewHostImpl belongs to are
  // guaranteed to be committed, so it should be reused going forward.
  frame_tree_->RegisterRenderViewHost(render_view_host_map_id_, this);
  is_in_back_forward_cache_ = false;
  page_lifecycle_state_manager_->SetIsInBackForwardCache(
      is_in_back_forward_cache_, std::move(page_restore_params),
      restoring_main_frame_from_back_forward_cache);
}

void RenderViewHostImpl::ActivatePrerenderedPage(
    base::TimeTicks activation_start,
    base::OnceClosure callback) {
  // TODO(https://crbug.com/1217977): Consider using a ScopedClosureRunner here
  // in case the renderer crashes before it can send us the callback. But we
  // can't do that until the linked bug is fixed, or else we can reach
  // DidActivateForPrerendering() outside of a Mojo message dispatch which
  // breaks the DCHECK for releasing Mojo Capability Control.
  page_broadcast_->ActivatePrerenderedPage(activation_start,
                                           std::move(callback));
}

void RenderViewHostImpl::SetFrameTreeVisibility(
    blink::mojom::PageVisibilityState visibility) {
  page_lifecycle_state_manager_->SetFrameTreeVisibility(visibility);
}

void RenderViewHostImpl::SetIsFrozen(bool frozen) {
  page_lifecycle_state_manager_->SetIsFrozen(frozen);
}

void RenderViewHostImpl::OnBackForwardCacheTimeout() {
  // TODO(yuzus): Implement a method to get a list of RenderFrameHosts
  // associated with |this|, instead of iterating through all the
  // RenderFrameHosts in bfcache.
  const auto& entries =
      frame_tree_->controller().GetBackForwardCache().GetEntries();
  for (auto& entry : entries) {
    for (auto* const rvh : entry->render_view_hosts()) {
      if (rvh == this) {
        RenderFrameHostImpl* rfh = entry->render_frame_host();
        rfh->EvictFromBackForwardCacheWithReason(
            BackForwardCacheMetrics::NotRestoredReason::kTimeoutPuttingInCache);
        break;
      }
    }
  }
}

void RenderViewHostImpl::MaybeEvictFromBackForwardCache() {
  // TODO(yuzus): Implement a method to get a list of RenderFrameHosts
  // associated with |this|, instead of iterating through all the
  // RenderFrameHosts in bfcache.
  const auto& entries =
      frame_tree_->controller().GetBackForwardCache().GetEntries();
  for (auto& entry : entries) {
    for (auto* const rvh : entry->render_view_hosts()) {
      if (rvh == this) {
        RenderFrameHostImpl* rfh = entry->render_frame_host();
        rfh->MaybeEvictFromBackForwardCache();
      }
    }
  }
}

void RenderViewHostImpl::EnforceBackForwardCacheSizeLimit() {
  frame_tree_->controller().GetBackForwardCache().EnforceCacheSizeLimit();
}

bool RenderViewHostImpl::DidReceiveBackForwardCacheAck() {
  return GetPageLifecycleStateManager()->DidReceiveBackForwardCacheAck();
}

bool RenderViewHostImpl::IsRenderViewLive() {
  return GetProcess()->IsInitializedAndNotDead() && renderer_view_created_;
}

void RenderViewHostImpl::SetBackgroundOpaque(bool opaque) {
  GetWidget()->GetAssociatedFrameWidget()->SetBackgroundOpaque(opaque);
}

bool RenderViewHostImpl::IsMainFrameActive() {
  return is_active();
}

bool RenderViewHostImpl::IsNeverComposited() {
  return GetDelegate()->IsNeverComposited();
}

blink::web_pref::WebPreferences
RenderViewHostImpl::GetWebkitPreferencesForWidget() {
  if (!delegate_)
    return blink::web_pref::WebPreferences();
  return delegate_->GetOrCreateWebPreferences();
}

void RenderViewHostImpl::RenderViewCreated(
    RenderFrameHostImpl* local_main_frame) {
  renderer_view_created_ = true;
  if (local_main_frame) {
    // If there is a main frame in this RenderViewHost, then the renderer-side
    // main frame will be created along with the RenderView. The RenderFrameHost
    // initializes its RenderWidgetHost as well, if it exists.
    local_main_frame->RenderFrameCreated();
  }
}

RenderFrameHostImpl* RenderViewHostImpl::GetMainRenderFrameHost() {
  // If the RenderViewHost is active, it should always have a main frame
  // RenderFrameHostImpl. If it is inactive, it could've been created for a
  // speculative main frame navigation, in which case it will transition to
  // active once that navigation commits. In this case, return the speculative
  // main frame RenderFrameHostImpl, as that's expected by certain code paths,
  // such as RenderViewHostImpl::SetUIProperty().  If there's no speculative
  // main frame navigation, return nullptr.
  //
  // TODO(alexmos, creis): Migrate these code paths to use RenderFrameHost APIs
  // and remove this fallback.  See https://crbug.com/763548.
  if (is_active()) {
    return RenderFrameHostImpl::FromID(GetProcess()->GetID(),
                                       main_frame_routing_id_);
  }
  return frame_tree_->root()->render_manager()->speculative_frame_host();
}

void RenderViewHostImpl::ClosePage() {
  is_waiting_for_page_close_completion_ = true;

  if (IsRenderViewLive() && !SuddenTerminationAllowed()) {
    close_timeout_->Start(kUnloadTimeout);

    // TODO(creis): Should this be moved to Shutdown?  It may not be called for
    // RenderViewHosts that have been swapped out.
#if !BUILDFLAG(IS_ANDROID)
    static_cast<HostZoomMapImpl*>(
        HostZoomMap::Get(GetMainRenderFrameHost()->GetSiteInstance()))
        ->WillCloseRenderView(GetProcess()->GetID(), GetRoutingID());
#endif

    GetMainRenderFrameHost()->GetAssociatedLocalMainFrame()->ClosePage(
        base::BindOnce(&RenderViewHostImpl::OnPageClosed,
                       weak_factory_.GetWeakPtr()));
  } else {
    // This RenderViewHost doesn't have a live renderer, so just skip the close
    // event and close the page.
    ClosePageIgnoringUnloadEvents();
  }
}

void RenderViewHostImpl::ClosePageIgnoringUnloadEvents() {
  close_timeout_->Stop();
  is_waiting_for_page_close_completion_ = false;

  sudden_termination_allowed_ = true;
  delegate_->Close(this);
}

void RenderViewHostImpl::ZoomToFindInPageRect(const gfx::Rect& rect_to_zoom) {
  GetMainRenderFrameHost()->GetAssociatedLocalMainFrame()->ZoomToFindInPageRect(
      rect_to_zoom);
}

void RenderViewHostImpl::RenderProcessExited(
    RenderProcessHost* host,
    const ChildProcessTerminationInfo& info) {
  // TODO(https://crbug.com/1234634): Remove this.
  // If the renderer has exited while we were are still waiting for a ack,
  // then log information about the exit.
  if (page_lifecycle_state_manager_->persisted_pageshow_timestamp_bug_1234634()
          .has_value()) {
    base::TimeDelta delta =
        base::Time::Now() - page_lifecycle_state_manager_
                                ->persisted_pageshow_timestamp_bug_1234634()
                                .value();
    // We want to understand if we are losing pageshows because renderers are
    // exiting soon after restoring from BFCache. We keep the normal exits
    // separate from the unexpected.
    const char* histogram =
        info.status == base::TERMINATION_STATUS_NORMAL_TERMINATION
            ? "Event.PageShow.Persisted.Termination.Normal.Time"
            : "Event.PageShow.Persisted.Termination.Unexpected.Time";
    base::UmaHistogramMediumTimes(histogram, delta);
    // We don't record this as an enum because the enum is platform dependent.
    // Since this is temporary debugging, 20 seems a safe upper limit for the
    // number of elements.
    base::UmaHistogramExactLinear("Event.PageShow.Persisted.Termination.Status",
                                  static_cast<int>(info.status), 20);
  }

  renderer_view_created_ = false;
  GetWidget()->RendererExited();
  delegate_->RenderViewTerminated(this, info.status, info.exit_code);
  // |this| might have been deleted. Do not add code here.
}

RenderWidgetHostImpl* RenderViewHostImpl::GetWidget() {
  return render_widget_host_.get();
}

AgentSchedulingGroupHost& RenderViewHostImpl::GetAgentSchedulingGroup() {
  return render_widget_host_->agent_scheduling_group();
}

RenderProcessHost* RenderViewHostImpl::GetProcess() {
  return GetAgentSchedulingGroup().GetProcess();
}

int RenderViewHostImpl::GetRoutingID() {
  return routing_id_;
}

void RenderViewHostImpl::RenderWidgetGotFocus() {
  RenderViewHostDelegateView* view = delegate_->GetDelegateView();
  if (view)
    view->GotFocus(GetWidget());
}

void RenderViewHostImpl::RenderWidgetLostFocus() {
  RenderViewHostDelegateView* view = delegate_->GetDelegateView();
  if (view)
    view->LostFocus(GetWidget());
}

void RenderViewHostImpl::SetInitialFocus(bool reverse) {
  GetMainRenderFrameHost()->GetAssociatedLocalMainFrame()->SetInitialFocus(
      reverse);
}

void RenderViewHostImpl::AnimateDoubleTapZoom(const gfx::Point& point,
                                              const gfx::Rect& rect) {
  GetMainRenderFrameHost()->GetAssociatedLocalMainFrame()->AnimateDoubleTapZoom(
      point, rect);
}

bool RenderViewHostImpl::SuddenTerminationAllowed() {
  // If there is a JavaScript dialog up, don't bother sending the renderer the
  // close event because it is known unresponsive, waiting for the reply from
  // the dialog.
  return sudden_termination_allowed_ ||
         delegate_->IsJavaScriptDialogShowing() ||
         GetMainRenderFrameHost()->BeforeUnloadTimedOut();
}

///////////////////////////////////////////////////////////////////////////////
// RenderViewHostImpl, IPC message handlers:

bool RenderViewHostImpl::OnMessageReceived(const IPC::Message& msg) {
  return false;
}

std::string RenderViewHostImpl::ToDebugString() {
  return "RVHI:" + delegate_->GetCreatorLocation().ToString();
}

void RenderViewHostImpl::OnTakeFocus(bool reverse) {
  RenderViewHostDelegateView* view = delegate_->GetDelegateView();
  if (view)
    view->TakeFocus(reverse);
}

void RenderViewHostImpl::OnPageClosed() {
  ClosePageIgnoringUnloadEvents();
}

void RenderViewHostImpl::OnFocus() {
  // Note: We allow focus and blur from swapped out RenderViewHosts, even when
  // the active RenderViewHost is in a different BrowsingInstance (e.g., WebUI).
  delegate_->Activate();
}

void RenderViewHostImpl::BindPageBroadcast(
    mojo::PendingAssociatedRemote<blink::mojom::PageBroadcast> page_broadcast) {
  page_broadcast_.reset();
  page_broadcast_.Bind(std::move(page_broadcast));
}

const mojo::AssociatedRemote<blink::mojom::PageBroadcast>&
RenderViewHostImpl::GetAssociatedPageBroadcast() {
  return page_broadcast_;
}

void RenderViewHostImpl::RenderWidgetDidForwardMouseEvent(
    const blink::WebMouseEvent& mouse_event) {
  if (mouse_event.GetType() == WebInputEvent::Type::kMouseWheel &&
      GetWidget()->IsIgnoringInputEvents()) {
    delegate_->OnIgnoredUIEvent();
  }
}

bool RenderViewHostImpl::MayRenderWidgetForwardKeyboardEvent(
    const NativeWebKeyboardEvent& key_event) {
  if (GetWidget()->IsIgnoringInputEvents()) {
    if (key_event.GetType() == WebInputEvent::Type::kRawKeyDown)
      delegate_->OnIgnoredUIEvent();
    return false;
  }
  return true;
}

bool RenderViewHostImpl::ShouldContributePriorityToProcess() {
  return is_active();
}

void RenderViewHostImpl::SendWebPreferencesToRenderer() {
  if (auto& broadcast = GetAssociatedPageBroadcast())
    broadcast->UpdateWebPreferences(delegate_->GetOrCreateWebPreferences());
}

void RenderViewHostImpl::SendRendererPreferencesToRenderer(
    const blink::RendererPreferences& preferences) {
  if (auto& broadcast = GetAssociatedPageBroadcast()) {
    if (!will_send_renderer_preferences_callback_for_testing_.is_null())
      will_send_renderer_preferences_callback_for_testing_.Run(preferences);
    broadcast->UpdateRendererPreferences(preferences);
  }
}

void RenderViewHostImpl::OnHardwareConfigurationChanged() {
  delegate_->RecomputeWebPreferencesSlow();
}

void RenderViewHostImpl::EnablePreferredSizeMode() {
  if (is_active()) {
    GetMainRenderFrameHost()
        ->GetAssociatedLocalMainFrame()
        ->EnablePreferredSizeChangedMode();
  }
}

void RenderViewHostImpl::PostRenderViewReady() {
  GetProcess()->PostTaskWhenProcessIsReady(base::BindOnce(
      &RenderViewHostImpl::RenderViewReady, weak_factory_.GetWeakPtr()));
}

void RenderViewHostImpl::OnGpuSwitched(gl::GpuPreference active_gpu_heuristic) {
  OnHardwareConfigurationChanged();
}

void RenderViewHostImpl::RenderViewReady() {
  DCHECK_CURRENTLY_ON(BrowserThread::UI);
  delegate_->RenderViewReady(this);
}

void RenderViewHostImpl::ClosePageTimeout() {
  if (delegate_->ShouldIgnoreUnresponsiveRenderer())
    return;

  ClosePageIgnoringUnloadEvents();
}

std::vector<viz::SurfaceId> RenderViewHostImpl::CollectSurfaceIdsForEviction() {
  if (!is_active())
    return {};
  RenderFrameHostImpl* rfh = GetMainRenderFrameHost();
  if (!rfh || !rfh->IsActive())
    return {};
  FrameTreeNode* root = rfh->frame_tree_node();
  FrameTree* tree = root->frame_tree();
  std::vector<viz::SurfaceId> ids;
  for (FrameTreeNode* node : tree->SubtreeNodes(root)) {
    if (!node->current_frame_host()->is_local_root())
      continue;
    RenderWidgetHostViewBase* view = static_cast<RenderWidgetHostViewBase*>(
        node->current_frame_host()->GetView());
    if (!view)
      continue;
    viz::SurfaceId id = view->GetCurrentSurfaceId();
    if (id.is_valid())
      ids.push_back(id);
    view->set_is_evicted();
  }
  return ids;
}

bool RenderViewHostImpl::IsTestRenderViewHost() const {
  return false;
}

void RenderViewHostImpl::SetWillEnterBackForwardCacheCallbackForTesting(
    const WillEnterBackForwardCacheCallbackForTesting& callback) {
  will_enter_back_forward_cache_callback_for_testing_ = callback;
}

void RenderViewHostImpl::SetWillSendRendererPreferencesCallbackForTesting(
    const WillSendRendererPreferencesCallbackForTesting& callback) {
  will_send_renderer_preferences_callback_for_testing_ = callback;
}

void RenderViewHostImpl::WriteIntoTrace(perfetto::TracedValue context) {
  auto dict = std::move(context).WriteDictionary();
  dict.Add("routing_id", GetRoutingID());
  dict.Add("process", GetProcess());
}

void RenderViewHostImpl::WriteIntoTrace(
    perfetto::TracedProto<perfetto::protos::pbzero::RenderViewHost> proto) {
  proto->set_rvh_map_id(render_view_host_map_id_.value());
  proto->set_routing_id(GetRoutingID());
  proto->set_process_id(GetProcess()->GetID());
  proto->set_is_in_back_forward_cache(is_in_back_forward_cache_);
  proto->set_renderer_view_created(renderer_view_created_);
}

}  // namespace content


text:
require "States.BaseState"
ParentState = {}

function ParentState:new()
    --local parent = nil
    --local from = ""
    local state = BaseState:new("ParentState", parent, from)

    function state:onEnterState(event)
        self:reset()
    end

    function state:onExitState(event)
    end

    function state:tick(time)
    end

    function state:reset()
    end

    return state
end

return ParentState


text:
package com.bwsw.sj.crud.rest.instance.validator

import com.bwsw.sj.common.dal.model.service.{KafkaServiceDomain, TStreamServiceDomain}
import com.bwsw.sj.common.dal.model.stream.KafkaStreamDomain
import com.bwsw.sj.common.si.model.instance.RegularInstance
import com.bwsw.sj.common.si.model.module.Specification
import com.bwsw.sj.common.utils.EngineLiterals._
import com.bwsw.sj.common.utils.StreamLiterals._
import com.bwsw.sj.common.utils.StreamUtils._
import com.typesafe.scalalogging.Logger
import scaldi.Injector

import scala.collection.mutable.ArrayBuffer
import scala.util.{Failure, Success, Try}

/**
  * Validator for [[com.bwsw.sj.common.si.model.instance.RegularInstance]]
  *
  * @author Kseniya Tomskikh
  */
class RegularInstanceValidator(implicit injector: Injector) extends InstanceValidator {

  import messageResourceUtils.createMessage

  private val logger: Logger = Logger(getClass.getName)
  override type T = RegularInstance

  override def validate(instance: T, specification: Specification): ArrayBuffer[String] = {
    logger.debug(s"Instance: ${instance.name}. Start a validation of instance of regular-streaming type.")
    val errors = new ArrayBuffer[String]()
    errors ++= super.validate(instance, specification
    )

    // 'checkpoint-mode' field
    Option(instance.checkpointMode) match {
      case None =>
        errors += createMessage("rest.validator.attribute.required", "checkpointMode")
      case Some(x) =>
        if (x.isEmpty) {
          errors += createMessage("rest.validator.attribute.required", "checkpointMode")
        }
        else {
          if (!checkpointModes.contains(x)) {
            errors += createMessage("rest.validator.attribute.unknown.value", "checkpointMode", x) + ". " +
              createMessage(
                "rest.validator.attribute.must.one_of",
                "checkpointMode",
                checkpointModes.mkString("[", ", ", "]"))
          }
        }
    }

    // 'checkpoint-interval' field
    if (instance.checkpointInterval <= 0) {
      errors += createMessage("rest.validator.attribute.required", "checkpointInterval") + ". " +
        createMessage("rest.validator.attribute.must.greater.than.zero", "checkpointInterval")
    }

    // 'event-wait-idle-time' field
    if (instance.eventWaitIdleTime <= 0) {
      errors += createMessage("rest.validator.attribute.must.greater.than.zero", "eventWaitIdleTime")
    }

    // 'state-management' field
    if (!stateManagementModes.contains(instance.stateManagement)) {
      errors += createMessage("rest.validator.attribute.unknown.value", "stateManagement", instance.stateManagement) +
        ". " + createMessage(
        "rest.validator.attribute.must.one_of",
        "stateManagement",
        stateManagementModes.mkString("[", ", ", "]"))
    } else {
      if (instance.stateManagement != noneStateMode) {
        // 'state-full-checkpoint' field
        if (instance.stateFullCheckpoint <= 0) {
          errors += createMessage("rest.validator.attribute.must.greater.than.zero", "stateFullCheckpoint")
        }
      }
    }

    errors
  }

  override protected def validateStreamOptions(instance: RegularInstance, specification: Specification): Seq[String] = {
    logger.debug(s"Instance: ${instance.name}. Stream options validation.")
    val errors = new ArrayBuffer[String]()

    // 'inputs' field
    val inputModes = instance.inputs.map(i => getStreamMode(i))
    if (inputModes.exists(m => !streamModes.contains(m))) {
      errors += createMessage("rest.validator.unknown.stream.mode", streamModes.mkString("[", ", ", "]"))
    }
    val inputsCardinality = specification.inputs.cardinality
    if (instance.inputs.length < inputsCardinality(0)) {
      errors += createMessage("rest.validator.cardinality.cannot.less", "inputs", s"${inputsCardinality(0)}")
    }
    if (instance.inputs.length > inputsCardinality(1)) {
      errors += createMessage("rest.validator.cardinality.cannot.more", "inputs", s"${inputsCardinality(1)}")
    }

    val clearInputs = instance.inputs.map(clearStreamFromMode)
    if (doesContainDuplicates(clearInputs)) {
      errors += createMessage("rest.validator.sources.not.unique", "Inputs")
    }
    val inputStreams = getStreams(clearInputs)
    clearInputs.foreach { streamName =>
      if (!inputStreams.exists(s => s.name == streamName)) {
        errors += createMessage("rest.validator.source_stream.not.exist", "Input", streamName)
      }
    }

    val inputTypes = specification.inputs.types
    if (inputStreams.exists(s => !inputTypes.contains(s.streamType))) {
      errors += createMessage("rest.validator.source_stream.must.one.of", "Input", inputTypes.mkString("[", ", ", "]"))
    }

    val kafkaStreams = inputStreams.filter(_.streamType == kafkaType).map(_.asInstanceOf[KafkaStreamDomain])
    if (kafkaStreams.nonEmpty) {
      if (kafkaStreams.exists(s => !s.service.isInstanceOf[KafkaServiceDomain])) {
        errors += createMessage("rest.validator.service.must", "kafka streams", "KfkQ")
      }
    }

    // 'start-from' field
    val startFrom = instance.startFrom
    if (inputStreams.exists(_.streamType == kafkaType)) {
      if (!startFromModes.contains(startFrom)) {
        errors += createMessage(
          "rest.validator.attribute.must.if.instance.have",
          "startFrom",
          startFromModes.mkString("[", ", ", "]"))
      }
    } else {
      if (!startFromModes.contains(startFrom)) {
        Try(startFrom.toLong) match {
          case Success(_) =>
          case Failure(_: NumberFormatException) =>
            errors += createMessage(
              "rest.validator.attribute.must.one_of",
              "startFrom",
              s"${startFromModes.mkString("[", ", ", "]")} or timestamp")
          case Failure(e) => throw e
        }
      }
    }

    // 'outputs' field
    val outputsCardinality = specification.outputs.cardinality
    if (instance.outputs.length < outputsCardinality(0)) {
      errors += createMessage("rest.validator.cardinality.cannot.less", "outputs", s"${outputsCardinality(0)}")
    }
    if (instance.outputs.length > outputsCardinality(1)) {
      errors += createMessage("rest.validator.cardinality.cannot.more", "outputs", s"${outputsCardinality(1)}")
    }
    if (doesContainDuplicates(instance.outputs)) {
      errors += createMessage("rest.validator.sources.not.unique", "Outputs")
    }
    val outputStreams = getStreams(instance.outputs)
    instance.outputs.toList.foreach { streamName =>
      if (!outputStreams.exists(s => s.name == streamName)) {
        errors += createMessage("rest.validator.source_stream.not.exist", "Output", streamName)
      }
    }
    val outputTypes = specification.outputs.types
    if (outputStreams.exists(s => !outputTypes.contains(s.streamType))) {
      errors += createMessage("rest.validator.source_stream.must.one.of", "Output", outputTypes.mkString("[", ", ", "]"))
    }

    // 'parallelism' field
    val partitions = getStreamsPartitions(inputStreams)
    val minPartitionCount = if (partitions.nonEmpty) partitions.min else 0
    errors ++= validateParallelism(instance.parallelism, minPartitionCount)

    val allStreams = inputStreams.union(outputStreams)
    val tStreamsServices = getStreamServices(allStreams.filter { s =>
      s.streamType.equals(tstreamsType)
    })
    if (tStreamsServices.nonEmpty) {
      if (tStreamsServices.size > 1) {
        errors += createMessage("rest.validator.t_stream.same.service")
      } else {
        val service = serviceRepository.get(tStreamsServices.head)
        if (!service.get.isInstanceOf[TStreamServiceDomain]) {
          errors += createMessage("rest.validator.service.must", "t-streams", "TstrQ")
        }
      }
    }

    errors
  }
}


text:
import pandas as pd
from sklearn.datasets.samples_generator import make_blobs
from sklearn.ensemble import RandomForestClassifier
import random
from tl_algs import peters, tnb, trbag, tl_baseline, burak, tca, tca_plus
RAND_SEED = 2016
random.seed(RAND_SEED) # change this to see new random data!

# randomly generate some data
X, domain_index = make_blobs(n_samples=15, centers=3, n_features=2, cluster_std=5)

# randomly assigning domain and label
all_instances = pd.DataFrame({"x_coord" : [x[0] for x in X],
              "y_coord" : [x[1] for x in X],
              "domain_index" : domain_index,
              "label" : [random.choice([True,False]) for _ in X]},
             columns = ['x_coord','y_coord','domain_index', 'label']
            )

#arbitrarily set domain index 0 as target
test_set_domain = 0
# we are going to set the first three instances as test data
# note that this means that some of the training set has target instances!
test_set = all_instances[all_instances.domain_index == test_set_domain].sample(3, random_state=RAND_SEED)
test_set_X = test_set.loc[:, ["x_coord", "y_coord"]].reset_index(drop=True)
test_set_y = test_set.loc[:, ["label"]].reset_index(drop=True)

# gather all non-test indexes
train_pool = all_instances.iloc[all_instances.index.difference(test_set.index), ]
train_pool_X = train_pool.loc[:, ["x_coord", "y_coord"]].reset_index(drop=True)
train_pool_y = train_pool["label"].reset_index(drop=True)
train_pool_domain = train_pool.domain_index

# We don't have much training data, but we got some predictions with confidence levels!

transfer_learners = [
    tca_plus.TCAPlus(test_set_X=test_set_X,
                test_set_domain=test_set_domain,
                train_pool_X=train_pool_X,
                train_pool_y=train_pool_y,
                train_pool_domain=train_pool_domain,
                Base_Classifier=RandomForestClassifier,
                rand_seed=RAND_SEED
                ),
    tca.TCA(test_set_X=test_set_X,
                test_set_domain=test_set_domain,
                train_pool_X=train_pool_X,
                train_pool_y=train_pool_y,
                train_pool_domain=train_pool_domain,
                Base_Classifier=RandomForestClassifier,
                rand_seed=RAND_SEED
                ),
    tl_baseline.Source_Baseline(
                test_set_X=test_set_X,
                test_set_domain=test_set_domain,
                train_pool_X=train_pool_X,
                train_pool_y=train_pool_y,
                train_pool_domain=train_pool_domain,
                Base_Classifier=RandomForestClassifier,
                rand_seed=RAND_SEED
               ),
    burak.Burak(
                test_set_X=test_set_X,
                test_set_domain=test_set_domain,
                train_pool_X=train_pool_X,
                train_pool_y=train_pool_y,
                train_pool_domain=train_pool_domain,
                cluster_factor = 15,
                k = 2,
                Base_Classifier=RandomForestClassifier,
                rand_seed=RAND_SEED
           ),
    peters.Peters(test_set_X=test_set_X,
                  test_set_domain=test_set_domain,
                  train_pool_X=train_pool_X,
                  train_pool_y=train_pool_y,
                  train_pool_domain=train_pool_domain,
                  cluster_factor=15,
                  Base_Classifier=RandomForestClassifier,
                  rand_seed=RAND_SEED
                 ),
    tnb.TransferNaiveBayes(test_set_X=test_set_X,
                  test_set_domain=test_set_domain,
                  train_pool_X=train_pool_X,
                  train_pool_y=train_pool_y,
                  train_pool_domain=train_pool_domain,
                  rand_seed=RAND_SEED
                 ),
    trbag.TrBag(test_set_X=test_set_X,
                  test_set_domain=test_set_domain,
                  train_pool_X=train_pool_X,
                  train_pool_y=train_pool_y,
                  train_pool_domain=train_pool_domain,
                  Base_Classifier=RandomForestClassifier,
                  sample_size=test_set_y.shape[0],
                  rand_seed=RAND_SEED
                 ),
    tl_baseline.Hybrid_Baseline(test_set_X=test_set_X,
                  test_set_domain=test_set_domain,
                  train_pool_X=train_pool_X,
                  train_pool_y=train_pool_y,
                  train_pool_domain=train_pool_domain,
                  Base_Classifier=RandomForestClassifier,
                  rand_seed=RAND_SEED
                 ),
    tl_baseline.Target_Baseline(test_set_X=test_set_X,
                  test_set_domain=test_set_domain,
                  train_pool_X=train_pool_X,
                  train_pool_y=train_pool_y,
                  train_pool_domain=train_pool_domain,
                  Base_Classifier=RandomForestClassifier,
                  rand_seed=RAND_SEED
                 )
]

for transfer_learner in transfer_learners:
    print(transfer_learner.train_filter_test())


text:
package cn.eoe.usingservice;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by lyb on 14-12-8.
 */
public class EchoService  extends Service{

    @Override
    public IBinder onBind(Intent arg0) {
        System.out.println("onBind");
        return echoServiceBinder;
    }

    private final EchoServiceBinder echoServiceBinder = new EchoServiceBinder();

    public class EchoServiceBinder extends Binder{

        public EchoService getService(){
            return EchoService.this;
        }

    }

    public int getCurrentNum(){
        return i;
    }

    @Override
    public void onCreate() {
        System.out.println("onCreate");

        startTimer();

        super.onCreate();
    }

    @Override
    public void onDestroy() {
        System.out.println("onDestory");

        stopTimer();

        super.onDestroy();
    }

    private int i = 0;

    public void startTimer(){
        if (timer == null) {
            timer = new Timer();
            task = new TimerTask() {
                @Override
                public void run() {
                    i++;
                    System.out.println(i);
                }
            };

            timer.schedule(task,1000,1000);
        }
    }
    public void stopTimer(){
        if (timer != null) {
            task.cancel();
            timer.cancel();

            task=null;
            timer=null;
        }
    }

    private Timer timer=null;
    private TimerTask task=null;
}


text:
define(['ko', 'scripts/tools/utils'], function (ko, utils) {

    var Player = function Player(parent, team, name, id) {
        var _id = "player-" + utils.guid();
        if (id) {
            _id = id;
        }
        this.types = ["player"];
        this.parent = parent; //game
        this.team = ko.observable(team);
        this.name = ko.observable(name);

        this.updateTeam = function (team) {
            if (this.team() != team) {
                if (this.team())
                    this.team().tryRemovePlayer(this);
                this.team(team);
            }
        }.bind(this);

        this.id = ko.computed(function () {
            return _id;
        });

        this.lifeTotal = function () {
            return this.team().lifeTotal();
        }

        this.setLifeTotal = function (value) {
            this.team().setLifeTotal(value);
        }

        this.isDealtCombatDamage = function (combatDamage) {
            return this.team().isDealtCombatDamage(combatDamage);
        }

        this.loseLife = function (points) {
            return this.team().loseLife(points);
        }

        this.gainsLife = function (points) {
            return this.team().gainsLife(points);
        }

        this.dealsLifelinkDamageTo = function (player, damage) {
            this.team().dealsLifelinkDamageTo(player, damage);
        }

        this.drainsLife = function (points, players) {
            this.team().drainsLife(points, players);
        }

        //default is rounded down.
        this.loseHalfLifeTotal = function (roundedUp) {
            //TODO: 2HG regels na zoeken???
            var half = this.lifeTotal() / 2.0;
            if (roundedUp) {
                return this.loseLife(Math.ceil(half));
            } else {
                return this.loseLife(Math.floor(half));
            }
        }

        this.toJsonObject = function () {
            return {
                "id": _id,
                "name": this.name()
            };
        }

        //some final logic...
        if (this.team()) {
            this.team().addPlayer(this);
        }
    }

    Player.fromJsonObject = function (object, parent, team) {
        return new Player(parent || null, team || null, object.name, object.id);
    }

    return Player;
});


text:
@implementation ZBWSpecial
- (NSString *)description
{
    return [NSString stringWithFormat:@"%@ - %@",self.text, NSStringFromRange(self.range)];
}
@end


text:
<?php

$_POST = json_decode(file_get_contents('php://input'), true);

require_once '../db.php';

$con = new pdo_db();

$sql = "SELECT employee_id FROM personal_infos WHERE employee_no = $_POST[employee_no]";
$id = $con->getData($sql);

echo $id[0]['employee_id'];

?>


text:
import sys
import os

# Put pydevconsole in the path.
sys.argv[0] = os.path.dirname(sys.argv[0])
sys.path.insert(1, os.path.join(os.path.dirname(sys.argv[0])))

print('Running tests with:', sys.executable)
print('PYTHONPATH:')
print('\n'.join(sorted(sys.path)))

import threading
import unittest

import pydevconsole
from pydev_imports import xmlrpclib, SimpleXMLRPCServer

try:
    raw_input
    raw_input_name = 'raw_input'
except NameError:
    raw_input_name = 'input'

#=======================================================================================================================
# Test
#=======================================================================================================================
class Test(unittest.TestCase):
    def startClientThread(self, client_port):
        class ClientThread(threading.Thread):
            def __init__(self, client_port):
                threading.Thread.__init__(self)
                self.client_port = client_port

            def run(self):
                class HandleRequestInput:
                    def RequestInput(self):
                        return 'RequestInput: OK'

                handle_request_input = HandleRequestInput()

                import pydev_localhost

                print('Starting client with:', pydev_localhost.get_localhost(), self.client_port)
                client_server = SimpleXMLRPCServer((pydev_localhost.get_localhost(), self.client_port), logRequests=False)
                client_server.register_function(handle_request_input.RequestInput)
                client_server.serve_forever()

        client_thread = ClientThread(client_port)
        client_thread.setDaemon(True)
        client_thread.start()
        return client_thread

    def getFreeAddresses(self):
        import socket

        s = socket.socket()
        s.bind(('', 0))
        port0 = s.getsockname()[1]

        s1 = socket.socket()
        s1.bind(('', 0))
        port1 = s1.getsockname()[1]
        s.close()
        s1.close()
        return port0, port1

    def testServer(self):
        client_port, server_port = self.getFreeAddresses()

        class ServerThread(threading.Thread):
            def __init__(self, client_port, server_port):
                threading.Thread.__init__(self)
                self.client_port = client_port
                self.server_port = server_port

            def run(self):
                import pydev_localhost

                print('Starting server with:', pydev_localhost.get_localhost(), self.server_port, self.client_port)
                pydevconsole.StartServer(pydev_localhost.get_localhost(), self.server_port, self.client_port)

        server_thread = ServerThread(client_port, server_port)
        server_thread.setDaemon(True)
        server_thread.start()

        client_thread = self.startClientThread(client_port)  #@UnusedVariable

        import time

        time.sleep(.3)  #let's give it some time to start the threads

        import pydev_localhost

        server = xmlrpclib.Server('http://%s:%s' % (pydev_localhost.get_localhost(), server_port))
        server.addExec("import sys; print('Running with: %s %s' % (sys.executable or sys.platform, sys.version))")
        server.addExec('class Foo:')
        server.addExec('    pass')
        server.addExec('')
        server.addExec('foo = Foo()')
        server.addExec('a = %s()' % raw_input_name)
        server.addExec('print (a)')

#=======================================================================================================================
# main
#=======================================================================================================================
if __name__ == '__main__':
    unittest.main()


text:
#include "QsfpModule.h"

#include <boost/assign.hpp>
#include <string>
#include <limits>
#include <iomanip>
#include "fboss/agent/FbossError.h"
#include "fboss/agent/TransceiverImpl.h"
#include "fboss/agent/SffFieldInfo.h"

namespace facebook { namespace fboss {
  using std::memcpy;
  using std::mutex;
  using std::lock_guard;

// As per SFF-8436, QSFP+ 10 Gbs 4X PLUGGABLE TRANSCEIVER spec

static SffFieldInfo::SffFieldMap qsfpFields = {
  // Base page values, including alarms and sensors
  {SffField::IDENTIFIER, {QsfpPages::LOWER, 0, 1} },
  {SffField::STATUS, {QsfpPages::LOWER, 1, 2} },
  {SffField::TEMPERATURE_ALARMS, {QsfpPages::LOWER, 6, 1} },
  {SffField::VCC_ALARMS, {QsfpPages::LOWER, 7, 1} },
  {SffField::CHANNEL_RX_PWR_ALARMS, {QsfpPages::LOWER, 9, 2} },
  {SffField::CHANNEL_TX_BIAS_ALARMS, {QsfpPages::LOWER, 11, 2} },
  {SffField::TEMPERATURE, {QsfpPages::LOWER, 22, 2} },
  {SffField::VCC, {QsfpPages::LOWER, 26, 2} },
  {SffField::CHANNEL_RX_PWR, {QsfpPages::LOWER, 34, 8} },
  {SffField::CHANNEL_TX_BIAS, {QsfpPages::LOWER, 42, 8} },
  {SffField::POWER_CONTROL, {QsfpPages::LOWER, 93, 1} },
  {SffField::PAGE_SELECT_BYTE, {QsfpPages::LOWER, 127, 1} },

  // Page 0 values, including vendor info:
  {SffField::EXTENDED_IDENTIFIER, {QsfpPages::PAGE0, 129, 1} },
  {SffField::ETHERNET_COMPLIANCE, {QsfpPages::PAGE0, 131, 1} },
  {SffField::LENGTH_SM_KM, {QsfpPages::PAGE0, 142, 1} },
  {SffField::LENGTH_OM3, {QsfpPages::PAGE0, 143, 1} },
  {SffField::LENGTH_OM2, {QsfpPages::PAGE0, 144, 1} },
  {SffField::LENGTH_OM1, {QsfpPages::PAGE0, 145, 1} },
  {SffField::LENGTH_COPPER, {QsfpPages::PAGE0, 146, 1} },
  {SffField::VENDOR_NAME, {QsfpPages::PAGE0, 148, 16} },
  {SffField::VENDOR_OUI, {QsfpPages::PAGE0, 165, 3} },
  {SffField::PART_NUMBER, {QsfpPages::PAGE0, 168, 16} },
  {SffField::REVISION_NUMBER, {QsfpPages::PAGE0, 184, 2} },
  {SffField::VENDOR_SERIAL_NUMBER, {QsfpPages::PAGE0, 196, 16} },
  {SffField::MFG_DATE, {QsfpPages::PAGE0, 212, 8} },
  {SffField::DIAGNOSTIC_MONITORING_TYPE, {QsfpPages::PAGE0, 220, 1} },

  // Page 3 values, including alarm and warning threshold values:
  {SffField::TEMPERATURE_THRESH, {QsfpPages::PAGE3, 128, 8} },
  {SffField::VCC_THRESH, {QsfpPages::PAGE3, 144, 8} },
  {SffField::RX_PWR_THRESH, {QsfpPages::PAGE3, 176, 8} },
  {SffField::TX_BIAS_THRESH, {QsfpPages::PAGE3, 184, 8} },
};

static SffFieldMultiplier qsfpMultiplier = {
  {SffField::LENGTH_SM_KM, 1000},
  {SffField::LENGTH_OM3, 2},
  {SffField::LENGTH_OM2, 1},
  {SffField::LENGTH_OM1, 1},
  {SffField::LENGTH_COPPER, 1},
};

void getQsfpFieldAddress(SffField field, int &dataAddress,
                         int &offset, int &length) {
  auto info = SffFieldInfo::getSffFieldAddress(qsfpFields, field);
  dataAddress = info.dataAddress;
  offset = info.offset;
  length = info.length;
}

/*
 * Given a byte, extract bit fields for various alarm flags;
 * note the we might want to use the lower or the upper nibble,
 * so offset is the number of the bit to start at;  this is usually
 * 0 or 4.
 */

FlagLevels QsfpModule::getQsfpFlags(const uint8_t *data,
                                    int offset) {
  FlagLevels flags;

  CHECK_GE(offset, 0);
  CHECK_LE(offset, 4);
  flags.warn.low = (*data & (1 << offset));
  flags.warn.high = (*data & (1 << ++offset));
  flags.alarm.low = (*data & (1 << ++offset));
  flags.alarm.high = (*data & (1 << ++offset));

  return flags;
}

FlagLevels QsfpModule::getQsfpSensorFlags(SffField fieldName) {
  int offset;
  int length;
  int dataAddress;

  /* Determine if QSFP is present */
  getQsfpFieldAddress(fieldName, dataAddress, offset, length);
  const uint8_t *data = getQsfpValuePtr(dataAddress, offset, length);
  return getQsfpFlags(data, 4);
}

bool QsfpModule::getSensorInfo(GlobalSensors& info) {
  info.temp.value = getQsfpSensor(SffField::TEMPERATURE,
                                  SffFieldInfo::getTemp);
  info.temp.flags = getQsfpSensorFlags(SffField::TEMPERATURE_ALARMS);
  info.temp.__isset.flags = true;
  info.vcc.value = getQsfpSensor(SffField::VCC, SffFieldInfo::getVcc);
  info.vcc.flags = getQsfpSensorFlags(SffField::VCC_ALARMS);
  info.vcc.__isset.flags = true;
  return true;
}

bool QsfpModule::getVendorInfo(Vendor &vendor) {
  vendor.name = getQsfpString(SffField::VENDOR_NAME);
  vendor.oui = getQsfpString(SffField::VENDOR_OUI);
  vendor.partNumber = getQsfpString(SffField::PART_NUMBER);
  vendor.rev = getQsfpString(SffField::REVISION_NUMBER);
  vendor.serialNumber = getQsfpString(SffField::VENDOR_SERIAL_NUMBER);
  vendor.dateCode = getQsfpString(SffField::MFG_DATE);
  return true;
}

bool QsfpModule::getCableInfo(Cable &cable) {
  cable.singleMode = getQsfpCableLength(SffField::LENGTH_SM_KM);
  cable.__isset.singleMode = (cable.singleMode != 0);
  cable.om3 = getQsfpCableLength(SffField::LENGTH_OM3);
  cable.__isset.om3 = (cable.om3 != 0);
  cable.om2 = getQsfpCableLength(SffField::LENGTH_OM2);
  cable.__isset.om2 = (cable.om2 != 0);
  cable.om1 = getQsfpCableLength(SffField::LENGTH_OM1);
  cable.__isset.om1 = (cable.om1 != 0);
  cable.copper = getQsfpCableLength(SffField::LENGTH_COPPER);
  cable.__isset.copper = (cable.copper != 0);
  return (cable.__isset.copper || cable.__isset.om1 || cable.__isset.om2 ||
          cable.__isset.om3 || cable.__isset.singleMode);
}

/*
 * Threhold values are stored just once;  they aren't per-channel,
 * so in all cases we simple assemble two-byte values and convert
 * them based on the type of the field.
 */

ThresholdLevels QsfpModule::getThresholdValues(SffField field,
                               double (*conversion)(uint16_t value)) {
  int offset;
  int length;
  int dataAddress;
  ThresholdLevels thresh;

  CHECK(!flatMem_);

  getQsfpFieldAddress(field, dataAddress, offset, length);
  const uint8_t *data = getQsfpValuePtr(dataAddress, offset, length);

  CHECK_GE(length, 8);
  thresh.alarm.high = conversion(data[0] << 8 | data[1]);
  thresh.alarm.low = conversion(data[2] << 8 | data[3]);
  thresh.warn.high = conversion(data[4] << 8 | data[5]);
  thresh.warn.low = conversion(data[6] << 8 | data[7]);

  return thresh;
}

bool QsfpModule::getThresholdInfo(AlarmThreshold &threshold) {
  if (flatMem_) {
    return false;
  }
  threshold.temp = getThresholdValues(SffField::TEMPERATURE_THRESH,
                                      SffFieldInfo::getTemp);
  threshold.vcc = getThresholdValues(SffField::VCC_THRESH,
                                     SffFieldInfo::getVcc);
  threshold.rxPwr= getThresholdValues(SffField::RX_PWR_THRESH,
                                      SffFieldInfo::getPwr);
  threshold.txBias = getThresholdValues(SffField::TX_BIAS_THRESH,
                                        SffFieldInfo::getTxBias);
  return true;
}

/*
 * Iterate through the four channels collecting appropriate data;
 * always assumes CHANNEL_COUNT channels is four.
 */

bool QsfpModule::getSensorsPerChanInfo(std::vector<Channel>& channels) {
  int offset;
  int length;
  int dataAddress;

  /*
   * Interestingly enough, the QSFP stores the four alarm flags
   * (alarm high, alarm low, warning high, warning low) in two bytes by
   * channel in order 2, 1, 4, 3;  by using this set of offsets, we
   * should be able to read them in order, by reading the appriopriate
   * bit offsets combined with a byte offset into the data.
   *
   * That is, read bits 4 through 7 of the first byte, then 0 through 3,
   * then 4 through 7 of the second byte, and so on.  Ugh.
   */
  int bitOffset[] = {4, 0, 4, 0};
  int byteOffset[] = {0, 0, 1, 1};

  getQsfpFieldAddress(SffField::CHANNEL_RX_PWR_ALARMS, dataAddress,
                      offset, length);
  const uint8_t *data = getQsfpValuePtr(dataAddress, offset, length);

  for (int channel = 0; channel < CHANNEL_COUNT; channel++) {
    channels[channel].sensors.rxPwr.flags =
      getQsfpFlags(data + byteOffset[channel], bitOffset[channel]);
    channels[channel].sensors.rxPwr.__isset.flags = true;
  }

  getQsfpFieldAddress(SffField::CHANNEL_TX_BIAS_ALARMS, dataAddress,
                      offset, length);
  data = getQsfpValuePtr(dataAddress, offset, length);

  for (int channel = 0; channel < CHANNEL_COUNT; channel++) {
    channels[channel].sensors.txBias.flags =
      getQsfpFlags(data + byteOffset[channel], bitOffset[channel]);
    channels[channel].sensors.txBias.__isset.flags = true;
  }

  getQsfpFieldAddress(SffField::CHANNEL_RX_PWR, dataAddress,
                      offset, length);
  data = getQsfpValuePtr(dataAddress, offset, length);

  for (auto& channel : channels) {
    uint16_t value = data[0] << 8 | data[1];
    channel.sensors.rxPwr.value = SffFieldInfo::getPwr(value);
    data += 2;
    length--;
  }
  CHECK_GE(length, 0);

  getQsfpFieldAddress(SffField::CHANNEL_TX_BIAS, dataAddress,
                      offset, length);
  data = getQsfpValuePtr(dataAddress, offset, length);
  for (auto& channel : channels) {
    uint16_t value = data[0] << 8 | data[1];
    channel.sensors.txBias.value = SffFieldInfo::getTxBias(value);
    data += 2;
    length--;
  }
  CHECK_GE(length, 0);
  // QSFP doesn't report Tx power, so we can't try to report that.
  return true;
}

std::string QsfpModule::getQsfpString(SffField field) {
  int offset;
  int length;
  int dataAddress;

  getQsfpFieldAddress(field, dataAddress, offset, length);
  const uint8_t *data = getQsfpValuePtr(dataAddress, offset, length);

  while (length > 0 && data[length - 1] == ' ') {
    --length;
  }
  return std::string(reinterpret_cast<const char*>(data), length);
}

double QsfpModule::getQsfpSensor(SffField field,
    double (*conversion)(uint16_t value)) {

  auto info = SffFieldInfo::getSffFieldAddress(qsfpFields, field);
  const uint8_t *data = getQsfpValuePtr(info.dataAddress,
                                        info.offset, info.length);
  return conversion(data[0] << 8 | data[1]);
}

/*
 * Cable length is report as a single byte;  each field has a
 * specific multiplier to use to get the true length.  For instance,
 * single mode fiber length is specified in km, so the multiplier
 * is 1000.  In addition, the raw value of 255 indicates that the
 * cable is longer than can be represented.  We use a negative
 * value of the appropriate magnitude to communicate that to thrift
 * clients.
 */

int QsfpModule::getQsfpCableLength(SffField field) {
  int length;
  auto info = SffFieldInfo::getSffFieldAddress(qsfpFields, field);
  const uint8_t *data = getQsfpValuePtr(info.dataAddress,
                                        info.offset, info.length);
  auto multiplier = qsfpMultiplier.at(field);
  length = *data * multiplier;
  if (*data == MAX_CABLE_LEN) {
    length = -(MAX_CABLE_LEN - 1) * multiplier;
  }
  return length;
}

QsfpModule::QsfpModule(std::unique_ptr<TransceiverImpl> qsfpImpl)
  : qsfpImpl_(std::move(qsfpImpl)) {
  present_ = false;
  dirty_ = true;
}

void QsfpModule::setQsfpIdprom() {
  uint8_t status[2];
  int offset;
  int length;
  int dataAddress;

  if (!present_) {
    throw FbossError("QSFP IDProm set failed as QSFP is not present");
  }

  // Check if the data is ready
  getQsfpFieldAddress(SffField::STATUS,
                      dataAddress, offset, length);
  getQsfpValue(dataAddress, offset, length, status);
  if (status[1] & (1 << 0)) {
    dirty_ = true;
    throw FbossError("QSFP IDProm failed as QSFP is not read");
  }
  flatMem_ = status[1] & (1 << 2);
  dirty_ = false;
}

const uint8_t* QsfpModule::getQsfpValuePtr(int dataAddress, int offset,
                                           int length) const {
  /* if the cached values are not correct */
  if (!cacheIsValid()) {
    throw FbossError("Qsfp is either not present or the data is not read");
  }
  if (dataAddress == QsfpPages::LOWER) {
    CHECK_LE(offset + length, sizeof(qsfpIdprom_));
    /* Copy data from the cache */
    return(qsfpIdprom_ + offset);
  } else {
    offset -= MAX_QSFP_PAGE_SIZE;
    CHECK_GE(offset, 0);
    CHECK_LE(offset, MAX_QSFP_PAGE_SIZE);
    if (dataAddress == QsfpPages::PAGE0) {
      CHECK_LE(offset + length, sizeof(qsfpPage0_));
      /* Copy data from the cache */
      return(qsfpPage0_ + offset);
    } else if (dataAddress == QsfpPages::PAGE3 && !flatMem_) {
      CHECK_LE(offset + length, sizeof(qsfpPage3_));
      /* Copy data from the cache */
      return(qsfpPage3_ + offset);
    } else {
      throw FbossError("Invalid Data Address 0x%d", dataAddress);
    }
  }
}

void QsfpModule::getQsfpValue(int dataAddress, int offset, int length,
                              uint8_t* data) const {
  const uint8_t *ptr = getQsfpValuePtr(dataAddress, offset, length);

  memcpy(data, ptr, length);
}

bool QsfpModule::isPresent() const {
  lock_guard<std::mutex> g(qsfpModuleMutex_);
  return present_;
}

void QsfpModule::setPresent(bool present) {
  present_ = present;
  /* Set the dirty bit as the QSFP was removed and
   * the cached data is no longer valid until next
   * set IDProm is called
   */
  if (present_ == false) {
    dirty_ = true;
  }
}

// Note that this needs to be called while holding the
// qsfpModuleMutex_
bool QsfpModule::cacheIsValid() const {
  return present_ && !dirty_;
}

void QsfpModule::getSfpDom(SfpDom &) {
}

void QsfpModule::getTransceiverInfo(TransceiverInfo &info) {
  lock_guard<std::mutex> g(qsfpModuleMutex_);
  info.present = present_;
  info.transceiver = type();
  info.port = qsfpImpl_->getNum();
  if (!cacheIsValid()) {
    return;
  }

  if (getSensorInfo(info.sensor)) {
    info.__isset.sensor = true;
  }
  if (getVendorInfo(info.vendor)) {
    info.__isset.vendor = true;
  }
  if (getCableInfo(info.cable)) {
    info.__isset.cable = true;
  }
  if (getThresholdInfo(info.thresholds)) {
    info.__isset.thresholds = true;
  }
  for (int i = 0; i < CHANNEL_COUNT; i++) {
    Channel chan;
    chan.channel = i;
    info.channels.push_back(chan);
  }

  if (getSensorsPerChanInfo(info.channels)) {
    info.__isset.channels = true;
  } else {
    info.channels.clear();
  }
}

void QsfpModule::detectTransceiver() {
  lock_guard<std::mutex> g(qsfpModuleMutex_);
  auto currentQsfpStatus = qsfpImpl_->detectTransceiver();
  if (currentQsfpStatus != present_) {
    LOG(INFO) << "Port: " << folly::to<std::string>(qsfpImpl_->getName()) <<
                  " QSFP status changed to " << currentQsfpStatus;
    setPresent(currentQsfpStatus);
    if (currentQsfpStatus) {
      updateQsfpData();
      customizeTransceiver();
    }
  }
}

int QsfpModule::getFieldValue(SffField fieldName,
                             uint8_t* fieldValue) {
  lock_guard<std::mutex> g(qsfpModuleMutex_);
  int offset;
  int length;
  int dataAddress;

  /* Determine if QSFP is present */
  if (cacheIsValid()) {
    try {
      getQsfpFieldAddress(fieldName, dataAddress, offset, length);
      getQsfpValue(dataAddress, offset, length, fieldValue);
    } catch (const std::exception& ex) {
      LOG(ERROR) << "Error reading field value for transceiver:" <<
             folly::to<std::string>(qsfpImpl_->getName()) << " " << ex.what();
    }
  }
  return -1;
}

void QsfpModule::updateTransceiverInfoFields() {
  lock_guard<std::mutex> g(qsfpModuleMutex_);
  updateQsfpData();
}

void QsfpModule::updateQsfpData() {
  if (present_) {
    try {
      qsfpImpl_->readTransceiver(0x50, 0, sizeof(qsfpIdprom_), qsfpIdprom_);
      dirty_ = false;
      setQsfpIdprom();
      /*
       * XXX:  Should we bother to read this other data every time?
       *       Surely, it isn't changing?  Is there some other utility to
       *       update this stuff?  Perhaps just reading the base 128 bytes
       *       is enough after we initially determine that somethings there?
       */

      // If we have flat memory, we don't have to set the page
      if (!flatMem_) {
        uint8_t page = 0;
        qsfpImpl_->writeTransceiver(0x50, 127, sizeof(page), &page);
      }
      qsfpImpl_->readTransceiver(0x50, 128, sizeof(qsfpPage0_), qsfpPage0_);
      if (!flatMem_) {
        uint8_t page = 3;
        qsfpImpl_->writeTransceiver(0x50, 127, sizeof(page), &page);
        qsfpImpl_->readTransceiver(0x50, 128, sizeof(qsfpPage3_), qsfpPage3_);
      }
    } catch (const std::exception& ex) {
      dirty_ = true;
      LOG(WARNING) << "Error reading data for transceiver:" <<
           folly::to<std::string>(qsfpImpl_->getName()) << " " << ex.what();
    }
  }
}

void QsfpModule::customizeTransceiver() {
  /*
   * Determine whether we need to customize any of the QSFP registers.
   * Wedge forces Low Power mode via a pin;  we have to reset this
   * to force High Power mode on LR4s.
   *
   * Note that this function expects to be called with qsfpModuleMutex_
   * held.
   */

  if (dirty_ == true) {
    return;
  }

  int offset;
  int length;
  int dataAddress;

  getQsfpFieldAddress(SffField::EXTENDED_IDENTIFIER, dataAddress,
                      offset, length);
  const uint8_t *extId = getQsfpValuePtr(dataAddress, offset, length);
  getQsfpFieldAddress(SffField::ETHERNET_COMPLIANCE, dataAddress,
                      offset, length);
  const uint8_t *ethCompliance = getQsfpValuePtr(dataAddress, offset, length);

  int pwrCtrlAddress;
  int pwrCtrlOffset;
  int pwrCtrlLength;
  getQsfpFieldAddress(SffField::POWER_CONTROL, pwrCtrlAddress,
                      pwrCtrlOffset, pwrCtrlLength);
  const uint8_t *pwrCtrl = getQsfpValuePtr(pwrCtrlAddress,
                                           pwrCtrlOffset, pwrCtrlLength);

  /*
   * It is not clear whether we'll have to use some of these values
   * in future to determine whether or not to set the high power override.
   * Leave the logging in until this is fully debugged -- this should
   * only trigger on QSFP insertion.
   */

  VLOG(1) << "Port: " << folly::to<std::string>(qsfpImpl_->getName()) <<
             " QSFP Ext ID " << std::hex << (int) *extId <<
             " Ether Compliance " << std::hex << (int) *ethCompliance <<
             " Power Control " << std::hex << (int) *pwrCtrl;

  int highPowerLevel = (*extId & EXT_ID_HI_POWER_MASK);
  int powerLevel = (*extId & EXT_ID_MASK) >> EXT_ID_SHIFT;

  if (highPowerLevel > 0 || powerLevel > 0) {
      uint8_t power = POWER_OVERRIDE;
      if (highPowerLevel > 0) {
        power |= HIGH_POWER_OVERRIDE;
      }

      // Note that we don't have to set the page here, but there should
      // probably be a setQsfpValue() function to handle pages, etc.

      if (pwrCtrlAddress != QsfpPages::LOWER) {
         throw FbossError("QSFP failed to set POWER_CONTROL for LR4 "
                          "due to incorrect page number");
      }
      if (pwrCtrlLength != sizeof(power)) {
         throw FbossError("QSFP failed to set POWER_CONTROL for LR4 "
                          "due to incorrect length");
      }

      qsfpImpl_->writeTransceiver(0x50, pwrCtrlOffset, sizeof(power), &power);
      LOG(INFO) << "Port: " << folly::to<std::string>(qsfpImpl_->getName()) <<
                " QSFP set to override low power";
  }
}

}} //namespace facebook::fboss