<commit_before>// Copyright 2015 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 "device/devices_app/devices_app.h"

#include "base/bind.h"
#include "base/callback.h"
#include "base/macros.h"
#include "base/sequenced_task_runner.h"
#include "base/thread_task_runner_handle.h"
#include "base/threading/thread.h"
#include "base/time/time.h"
#include "device/core/device_client.h"
#include "device/devices_app/usb/device_manager_impl.h"
#include "device/devices_app/usb/public/cpp/device_manager_delegate.h"
#include "device/usb/usb_service.h"
#include "mojo/application/public/cpp/application_connection.h"
#include "mojo/application/public/cpp/application_impl.h"
#include "third_party/mojo/src/mojo/public/cpp/bindings/interface_request.h"
#include "url/gurl.h"

namespace device {

namespace {

// The number of seconds to wait without any bound DeviceManagers before
// exiting the app.
const int64 kIdleTimeoutInSeconds = 10;

// A usb::DeviceManagerDelegate implementation which provides origin-based
// device access control.
class USBDeviceManagerDelegate : public usb::DeviceManagerDelegate {
 public:
  explicit USBDeviceManagerDelegate(const GURL& remote_url)
      : remote_url_(remote_url) {}
  ~USBDeviceManagerDelegate() override {}

 private:
  // usb::DeviceManagerDelegate:
  bool IsDeviceAllowed(const usb::DeviceInfo& device) override {
    // Limited set of conditions to allow localhost connection for testing. This
    // does not presume to catch all common local host strings.
    if (remote_url_.host() == "127.0.0.1" || remote_url_.host() == "localhost")
      return true;

    // Also let browser apps and mojo apptests talk to all devices.
    if (remote_url_.SchemeIs("system") ||
        remote_url_ == GURL("mojo://devices_apptests/"))
      return true;

    // TODO(rockot/reillyg): Implement origin-based device access control.
    return false;
  }

  GURL remote_url_;

  DISALLOW_COPY_AND_ASSIGN(USBDeviceManagerDelegate);
};

// A DeviceClient implementation to be constructed iff the app is not running
// in an embedder that provides a DeviceClient (i.e. running as a standalone
// Mojo app, not in Chrome).
class AppDeviceClient : public DeviceClient {
 public:
  explicit AppDeviceClient(
      scoped_refptr<base::SequencedTaskRunner> blocking_task_runner)
      : blocking_task_runner_(blocking_task_runner) {}
  ~AppDeviceClient() override {}

 private:
  // DeviceClient:
  UsbService* GetUsbService() override {<commit_msg>Remove localhost from devices app whitelist<commit_after>// Copyright 2015 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 "device/devices_app/devices_app.h"

#include "base/bind.h"
#include "base/callback.h"
#include "base/macros.h"
#include "base/sequenced_task_runner.h"
#include "base/thread_task_runner_handle.h"
#include "base/threading/thread.h"
#include "base/time/time.h"
#include "device/core/device_client.h"
#include "device/devices_app/usb/device_manager_impl.h"
#include "device/devices_app/usb/public/cpp/device_manager_delegate.h"
#include "device/usb/usb_service.h"
#include "mojo/application/public/cpp/application_connection.h"
#include "mojo/application/public/cpp/application_impl.h"
#include "third_party/mojo/src/mojo/public/cpp/bindings/interface_request.h"
#include "url/gurl.h"

namespace device {

namespace {

// The number of seconds to wait without any bound DeviceManagers before
// exiting the app.
const int64 kIdleTimeoutInSeconds = 10;

// A usb::DeviceManagerDelegate implementation which provides origin-based
// device access control.
//
// TODO(rockot/reillyg): We should just get rid of DeviceManagerDelegate, or
// at least repurpose it as test support to provide device mocks.
class USBDeviceManagerDelegate : public usb::DeviceManagerDelegate {
 public:
  explicit USBDeviceManagerDelegate(const GURL& remote_url)
      : remote_url_(remote_url) {}
  ~USBDeviceManagerDelegate() override {}

 private:
  // usb::DeviceManagerDelegate:
  bool IsDeviceAllowed(const usb::DeviceInfo& device) override {
    // Also let browser apps and mojo apptests talk to all devices.
    if (remote_url_.SchemeIs("system") ||
        remote_url_ == GURL("mojo://devices_apptests/"))
      return true;
    return false;
  }

  GURL remote_url_;

  DISALLOW_COPY_AND_ASSIGN(USBDeviceManagerDelegate);
};

// A DeviceClient implementation to be constructed iff the app is not running
// in an embedder that provides a DeviceClient (i.e. running as a standalone
// Mojo app, not in Chrome).
class AppDeviceClient : public DeviceClient {
 public:
  explicit AppDeviceClient(
      scoped_refptr<base::SequencedTaskRunner> blocking_task_runner)
      : blocking_task_runner_(blocking_task_runner) {}
  ~AppDeviceClient() override {}

 private:
  // DeviceClient:
  UsbService* GetUsbService() override {
{'fraction_non_alphanumeric': 0.10253341312587273, 'fraction_numerical': 0.004388589666866148, 'mean_word_length': 4.665536723163842, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 1, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4917638', 'n_tokens_mistral': 1529, 'n_tokens_neox': 1505, 'n_words': 413}