Skip to content
This repository was archived by the owner on Jul 18, 2018. It is now read-only.

Commit 7b072ad

Browse files
rijuCommit Bot
authored and
Commit Bot
committed
[sensors][permission] Add new permission types in permission module.
This is the first part of adding permission guard for sensors based on Generic Sensor Framework. There was consensus to add granular permissions for sensors here w3c/sensors#22 This CL adds permission name for the different sensors in the permission module. Spec discussion : w3c/permissions#142 BUG=606766 Review-Url: https://codereview.chromium.org/2791623004 Cr-Commit-Position: refs/heads/master@{#491303}
1 parent 1216cea commit 7b072ad

File tree

15 files changed

+143
-2
lines changed

15 files changed

+143
-2
lines changed

android_webview/browser/aw_permission_manager.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,7 @@ int AwPermissionManager::RequestPermissions(
324324
case PermissionType::PUSH_MESSAGING:
325325
case PermissionType::DURABLE_STORAGE:
326326
case PermissionType::BACKGROUND_SYNC:
327+
case PermissionType::SENSORS:
327328
case PermissionType::FLASH:
328329
NOTIMPLEMENTED() << "RequestPermissions is not implemented for "
329330
<< static_cast<int>(permissions[i]);
@@ -462,6 +463,7 @@ void AwPermissionManager::CancelPermissionRequest(int request_id) {
462463
case PermissionType::AUDIO_CAPTURE:
463464
case PermissionType::VIDEO_CAPTURE:
464465
case PermissionType::BACKGROUND_SYNC:
466+
case PermissionType::SENSORS:
465467
case PermissionType::FLASH:
466468
NOTIMPLEMENTED() << "CancelPermission not implemented for "
467469
<< static_cast<int>(permission);

chrome/browser/BUILD.gn

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,8 @@ split_static_library("browser") {
443443
"gcm/instance_id/instance_id_profile_service.h",
444444
"gcm/instance_id/instance_id_profile_service_factory.cc",
445445
"gcm/instance_id/instance_id_profile_service_factory.h",
446+
"generic_sensor/sensor_permission_context.cc",
447+
"generic_sensor/sensor_permission_context.h",
446448
"geolocation/chrome_access_token_store.cc",
447449
"geolocation/chrome_access_token_store.h",
448450
"geolocation/geolocation_permission_context.cc",

chrome/browser/generic_sensor/OWNERS

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
file://services/device/generic_sensor/OWNERS
2+
3+
per-file *permission_context*=set noparent
4+
per-file *permission_context*=file://chrome/browser/permissions/PERMISSIONS_OWNERS
5+
6+
# COMPONENT: Blink>Sensor
7+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2017 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#include "chrome/browser/generic_sensor/sensor_permission_context.h"
6+
7+
SensorPermissionContext::SensorPermissionContext(Profile* profile)
8+
: PermissionContextBase(profile,
9+
CONTENT_SETTINGS_TYPE_SENSORS,
10+
blink::WebFeaturePolicyFeature::kNotFound) {}
11+
12+
SensorPermissionContext::~SensorPermissionContext() {}
13+
14+
ContentSetting SensorPermissionContext::GetPermissionStatusInternal(
15+
content::RenderFrameHost* render_frame_host,
16+
const GURL& requesting_origin,
17+
const GURL& embedding_origin) const {
18+
if (requesting_origin != embedding_origin)
19+
return CONTENT_SETTING_BLOCK;
20+
21+
return CONTENT_SETTING_ALLOW;
22+
}
23+
24+
bool SensorPermissionContext::IsRestrictedToSecureOrigins() const {
25+
return true;
26+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2017 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#ifndef CHROME_BROWSER_GENERIC_SENSOR_SENSOR_PERMISSION_CONTEXT_H_
6+
#define CHROME_BROWSER_GENERIC_SENSOR_SENSOR_PERMISSION_CONTEXT_H_
7+
8+
#include "base/macros.h"
9+
#include "chrome/browser/permissions/permission_context_base.h"
10+
11+
class SensorPermissionContext : public PermissionContextBase {
12+
public:
13+
explicit SensorPermissionContext(Profile* profile);
14+
15+
~SensorPermissionContext() override;
16+
17+
private:
18+
// PermissionContextBase:
19+
ContentSetting GetPermissionStatusInternal(
20+
content::RenderFrameHost* render_frame_host,
21+
const GURL& requesting_origin,
22+
const GURL& embedding_origin) const override;
23+
bool IsRestrictedToSecureOrigins() const override;
24+
25+
DISALLOW_COPY_AND_ASSIGN(SensorPermissionContext);
26+
};
27+
28+
#endif // CHROME_BROWSER_GENERIC_SENSOR_SENSOR_PERMISSION_CONTEXT_H_

chrome/browser/permissions/permission_manager.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "build/build_config.h"
1313
#include "chrome/browser/background_sync/background_sync_permission_context.h"
1414
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
15+
#include "chrome/browser/generic_sensor/sensor_permission_context.h"
1516
#include "chrome/browser/media/midi_permission_context.h"
1617
#include "chrome/browser/media/midi_sysex_permission_context.h"
1718
#include "chrome/browser/media/webrtc/media_stream_device_permission_context.h"
@@ -104,6 +105,8 @@ ContentSettingsType PermissionTypeToContentSetting(PermissionType permission) {
104105
return CONTENT_SETTINGS_TYPE_BACKGROUND_SYNC;
105106
case PermissionType::FLASH:
106107
return CONTENT_SETTINGS_TYPE_PLUGINS;
108+
case PermissionType::SENSORS:
109+
return CONTENT_SETTINGS_TYPE_SENSORS;
107110
case PermissionType::NUM:
108111
// This will hit the NOTREACHED below.
109112
break;
@@ -284,6 +287,8 @@ PermissionManager::PermissionManager(Profile* profile)
284287
permission_contexts_[CONTENT_SETTINGS_TYPE_PLUGINS] =
285288
base::MakeUnique<FlashPermissionContext>(profile);
286289
#endif
290+
permission_contexts_[CONTENT_SETTINGS_TYPE_SENSORS] =
291+
base::MakeUnique<SensorPermissionContext>(profile);
287292
}
288293

289294
PermissionManager::~PermissionManager() {

chrome/browser/permissions/permission_util.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ std::string PermissionUtil::GetPermissionString(
4343
return "BackgroundSync";
4444
case CONTENT_SETTINGS_TYPE_PLUGINS:
4545
return "Flash";
46+
case CONTENT_SETTINGS_TYPE_SENSORS:
47+
return "Sensors";
4648
default:
4749
break;
4850
}
@@ -73,6 +75,8 @@ std::string PermissionUtil::ConvertContentSettingsTypeToSafeBrowsingName(
7375
return "BACKGROUND_SYNC";
7476
case CONTENT_SETTINGS_TYPE_PLUGINS:
7577
return "FLASH";
78+
case CONTENT_SETTINGS_TYPE_SENSORS:
79+
return "SENSORS";
7680
default:
7781
break;
7882
}
@@ -135,6 +139,8 @@ bool PermissionUtil::GetPermissionType(ContentSettingsType type,
135139
} else if (type == CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER) {
136140
*out = PermissionType::PROTECTED_MEDIA_IDENTIFIER;
137141
#endif
142+
} else if (type == CONTENT_SETTINGS_TYPE_SENSORS) {
143+
*out = PermissionType::SENSORS;
138144
} else {
139145
return false;
140146
}
@@ -162,6 +168,7 @@ bool PermissionUtil::IsPermission(ContentSettingsType type) {
162168
#if defined(OS_ANDROID) || defined(OS_CHROMEOS)
163169
case CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER:
164170
#endif
171+
case CONTENT_SETTINGS_TYPE_SENSORS:
165172
return true;
166173
default:
167174
return false;

components/content_settings/core/common/content_settings.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ struct HistogramValue {
2828
// content settings type name instead.
2929
//
3030
// The array size must be explicit for the static_asserts below.
31-
constexpr size_t kNumHistogramValues = 31;
31+
constexpr size_t kNumHistogramValues = 32;
3232
constexpr HistogramValue kHistogramValue[kNumHistogramValues] = {
3333
{CONTENT_SETTINGS_TYPE_COOKIES, 0},
3434
{CONTENT_SETTINGS_TYPE_IMAGES, 1},
@@ -61,6 +61,7 @@ constexpr HistogramValue kHistogramValue[kNumHistogramValues] = {
6161
{CONTENT_SETTINGS_TYPE_MEDIA_ENGAGEMENT, 35},
6262
{CONTENT_SETTINGS_TYPE_SOUND, 36},
6363
{CONTENT_SETTINGS_TYPE_CLIENT_HINTS, 37},
64+
{CONTENT_SETTINGS_TYPE_SENSORS, 38},
6465
};
6566

6667
} // namespace

components/content_settings/core/common/content_settings_types.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,13 @@ enum ContentSettingsType {
8585
// the HTTP request headers for every resource requested from that origin.
8686
CONTENT_SETTINGS_TYPE_CLIENT_HINTS,
8787

88+
// Generic Sensor API covering ambient-light-sensor, accelerometer, gyroscope
89+
// and magnetometer are all mapped to a single content_settings_type.
90+
// Setting for the Generic Sensor API covering ambient-light-sensor,
91+
// accelerometer, gyroscope and magnetometer. These are all mapped to a single
92+
// ContentSettingsType.
93+
CONTENT_SETTINGS_TYPE_SENSORS,
94+
8895
CONTENT_SETTINGS_NUM_TYPES,
8996
};
9097

content/browser/permissions/permission_service_impl.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ PermissionType PermissionDescriptorToPermissionType(
5454
return PermissionType::VIDEO_CAPTURE;
5555
case PermissionName::BACKGROUND_SYNC:
5656
return PermissionType::BACKGROUND_SYNC;
57+
case PermissionName::SENSORS:
58+
return PermissionType::SENSORS;
5759
}
5860

5961
NOTREACHED();
@@ -79,6 +81,7 @@ blink::WebFeaturePolicyFeature PermissionTypeToFeaturePolicyFeature(
7981
case PermissionType::DURABLE_STORAGE:
8082
case PermissionType::BACKGROUND_SYNC:
8183
case PermissionType::FLASH:
84+
case PermissionType::SENSORS:
8285
case PermissionType::NUM:
8386
// These aren't exposed by feature policy.
8487
return blink::WebFeaturePolicyFeature::kNotFound;

content/public/browser/permission_type.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ enum class PermissionType {
2424
VIDEO_CAPTURE = 9,
2525
BACKGROUND_SYNC = 10,
2626
FLASH = 11,
27+
SENSORS = 12,
2728

2829
// Always keep this at the end.
2930
NUM,

third_party/WebKit/LayoutTests/http/tests/permissions/resources/test-query.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,42 @@ async_test(function(test) {
2424
});
2525
}, 'Test PermissionDescription WebIDL rules in ' + get_current_scope() + ' scope.');
2626

27+
async_test(function(test) {
28+
navigator.permissions.query({name:'ambient-light-sensor'}).then(function(result) {
29+
assert_true(result instanceof PermissionStatus);
30+
test.done();
31+
}).catch(function() {
32+
assert_unreached('querying ambient-light-sensor permission should not fail.')
33+
});
34+
}, 'Test ambient-light-sensor permission in ' + get_current_scope() + ' scope.');
35+
36+
async_test(function(test) {
37+
navigator.permissions.query({name:'accelerometer'}).then(function(result) {
38+
assert_true(result instanceof PermissionStatus);
39+
test.done();
40+
}).catch(function() {
41+
assert_unreached('querying accelerometer permission should not fail.')
42+
});
43+
}, 'Test accelerometer permission in ' + get_current_scope() + ' scope.');
44+
45+
async_test(function(test) {
46+
navigator.permissions.query({name:'gyroscope'}).then(function(result) {
47+
assert_true(result instanceof PermissionStatus);
48+
test.done();
49+
}).catch(function() {
50+
assert_unreached('querying gyroscope permission should not fail.')
51+
});
52+
}, 'Test gyroscope permission in ' + get_current_scope() + ' scope.');
53+
54+
async_test(function(test) {
55+
navigator.permissions.query({name:'magnetometer'}).then(function(result) {
56+
assert_true(result instanceof PermissionStatus);
57+
test.done();
58+
}).catch(function() {
59+
assert_unreached('querying magnetometer permission should not fail.')
60+
});
61+
}, 'Test magnetometer permission in ' + get_current_scope() + ' scope.');
62+
2763
async_test(function(test) {
2864
navigator.permissions.query({name:'geolocation'}).then(function(result) {
2965
assert_true(result instanceof PermissionStatus);

third_party/WebKit/Source/modules/permissions/PermissionDescriptor.idl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ enum PermissionName {
77
"midi",
88
"notifications",
99
"push",
10-
"background-sync"
10+
"background-sync",
11+
"ambient-light-sensor",
12+
"accelerometer",
13+
"gyroscope",
14+
"magnetometer",
1115
};
1216

1317
// The PermissionDescriptor dictionary is a base to describe permissions. Some

third_party/WebKit/Source/modules/permissions/Permissions.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "modules/permissions/PermissionDescriptor.h"
2222
#include "modules/permissions/PermissionStatus.h"
2323
#include "modules/permissions/PermissionUtils.h"
24+
#include "platform/RuntimeEnabledFeatures.h"
2425
#include "platform/wtf/Functional.h"
2526
#include "platform/wtf/NotFound.h"
2627
#include "platform/wtf/PtrUtil.h"
@@ -91,6 +92,16 @@ PermissionDescriptorPtr ParsePermission(ScriptState* script_state,
9192
}
9293
if (name == "background-sync")
9394
return CreatePermissionDescriptor(PermissionName::BACKGROUND_SYNC);
95+
// TODO(riju): Remove runtime flag check when Generic Sensor feature is
96+
// stable.
97+
if (name == "ambient-light-sensor" || name == "accelerometer" ||
98+
name == "gyroscope" || name == "magnetometer") {
99+
if (!RuntimeEnabledFeatures::SensorEnabled()) {
100+
exception_state.ThrowTypeError("GenericSensor flag is not enabled.");
101+
return nullptr;
102+
}
103+
return CreatePermissionDescriptor(PermissionName::SENSORS);
104+
}
94105

95106
return nullptr;
96107
}

third_party/WebKit/public/platform/modules/permissions/permission.mojom

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ enum PermissionName {
1717
AUDIO_CAPTURE,
1818
VIDEO_CAPTURE,
1919
BACKGROUND_SYNC,
20+
SENSORS,
2021
};
2122

2223
struct MidiPermissionDescriptor {

0 commit comments

Comments
 (0)