summaryrefslogtreecommitdiffstats
path: root/chrome/common/extensions/permissions/bluetooth_device_permission.cc
blob: bc35443393388cc54bde988656bee221ebf4c9a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// 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 "chrome/common/extensions/permissions/bluetooth_device_permission.h"

#include <string>
#include <vector>

#include "base/logging.h"
#include "base/string16.h"
#include "base/string_util.h"
#include "base/utf_string_conversions.h"
#include "chrome/common/extensions/permissions/bluetooth_device_permission_data.h"
#include "chrome/common/extensions/permissions/permissions_info.h"
#include "device/bluetooth/bluetooth_adapter.h"
#include "device/bluetooth/bluetooth_adapter_factory.h"
#include "device/bluetooth/bluetooth_device.h"
#include "grit/generated_resources.h"
#include "ui/base/l10n/l10n_util.h"

namespace {

const char* kSeparator = "|";

}  // namespace

namespace extensions {

BluetoothDevicePermission::BluetoothDevicePermission(
    const APIPermissionInfo* info)
  : SetDisjunctionPermission<BluetoothDevicePermissionData,
                             BluetoothDevicePermission>(info) {
}

BluetoothDevicePermission::~BluetoothDevicePermission() {
}

std::string BluetoothDevicePermission::ToString() const {
  std::vector<std::string> parts;
  parts.push_back(name());
  for (std::set<BluetoothDevicePermissionData>::const_iterator i =
      data_set_.begin(); i != data_set_.end(); ++i) {
    parts.push_back(i->GetAsString());
  }
  return JoinString(parts, kSeparator);
}

bool BluetoothDevicePermission::ManifestEntryForbidden() const {
  return true;
}

PermissionMessages BluetoothDevicePermission::GetMessages() const {
  DCHECK(HasMessages());
  PermissionMessages result;

  scoped_refptr<device::BluetoothAdapter> bluetooth_adapter =
      device::BluetoothAdapterFactory::DefaultAdapter();

  for (std::set<BluetoothDevicePermissionData>::const_iterator i =
      data_set_.begin(); i != data_set_.end(); ++i) {

    const std::string& device_address = i->GetAsString();
    string16 device_identifier;
    if (bluetooth_adapter) {
      device::BluetoothDevice* device =
          bluetooth_adapter->GetDevice(device_address);
      if (device)
        device_identifier = device->GetName();
    }

    if (device_identifier.length() == 0) {
      UTF8ToUTF16(device_address.c_str(), device_address.length(),
          &device_identifier);
    }

    result.push_back(PermissionMessage(
          PermissionMessage::kBluetoothDevice,
          l10n_util::GetStringFUTF16(
              IDS_EXTENSION_PROMPT_WARNING_BLUETOOTH_DEVICE,
              device_identifier)));
  }

  return result;
}

}  // namespace extensions