Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(134)

Side by Side Diff: Source/modules/permissions/Permissions.cpp

Issue 1234023002: blink: permissions: add plumbing to revoke permissions (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Rebase Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Source/modules/permissions/Permissions.h ('k') | Source/modules/permissions/Permissions.idl » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "config.h" 5 #include "config.h"
6 #include "modules/permissions/Permissions.h" 6 #include "modules/permissions/Permissions.h"
7 7
8 #include "bindings/core/v8/ScriptPromise.h" 8 #include "bindings/core/v8/ScriptPromise.h"
9 #include "bindings/core/v8/ScriptPromiseResolver.h" 9 #include "bindings/core/v8/ScriptPromiseResolver.h"
10 #include "bindings/modules/v8/V8MidiPermissionDescriptor.h" 10 #include "bindings/modules/v8/V8MidiPermissionDescriptor.h"
11 #include "bindings/modules/v8/V8PermissionDescriptor.h" 11 #include "bindings/modules/v8/V8PermissionDescriptor.h"
12 #include "bindings/modules/v8/V8PushPermissionDescriptor.h" 12 #include "bindings/modules/v8/V8PushPermissionDescriptor.h"
13 #include "core/dom/DOMException.h" 13 #include "core/dom/DOMException.h"
14 #include "core/dom/Document.h" 14 #include "core/dom/Document.h"
15 #include "core/dom/ExceptionCode.h" 15 #include "core/dom/ExceptionCode.h"
16 #include "modules/permissions/PermissionCallback.h"
16 #include "modules/permissions/PermissionController.h" 17 #include "modules/permissions/PermissionController.h"
17 #include "modules/permissions/PermissionDescriptor.h" 18 #include "modules/permissions/PermissionDescriptor.h"
18 #include "modules/permissions/PermissionQueryCallback.h"
19 #include "modules/permissions/PermissionStatus.h" 19 #include "modules/permissions/PermissionStatus.h"
20 #include "public/platform/Platform.h" 20 #include "public/platform/Platform.h"
21 #include "public/platform/modules/permissions/WebPermissionClient.h" 21 #include "public/platform/modules/permissions/WebPermissionClient.h"
22 22
23 namespace blink { 23 namespace blink {
24 24
25 namespace {
26
27 // Here we handle some common permission patterns to prevent propogation up to t he content layer
28 // Examples of behaviours which would go here are rejecting unsupported permissi ons, accepting
29 // permissions which are always granted or always denied according to the spec e tc.
30 bool handleDefaultBehaviour(
31 ScriptState* scriptState, const ScriptValue& rawPermission, PassRefPtr<Scrip tPromiseResolver> resolver, WebPermissionType type, TrackExceptionState& excepti onState)
32 {
33 if (type == WebPermissionTypePushNotifications) {
34 PushPermissionDescriptor pushPermission = NativeValueTraits<PushPermissi onDescriptor>::nativeValue(scriptState->isolate(), rawPermission.v8Value(), exce ptionState);
35 // Only "userVisibleOnly" push is supported for now.
36 if (!pushPermission.userVisibleOnly()) {
37 resolver->reject(DOMException::create(NotSupportedError, "Push Permi ssion without userVisibleOnly:true isn't supported yet."));
38 return true;
39 }
40 } else if (type == WebPermissionTypeMidiSysEx) {
41 MidiPermissionDescriptor midiPermission = NativeValueTraits<MidiPermissi onDescriptor>::nativeValue(scriptState->isolate(), rawPermission.v8Value(), exce ptionState);
42 // Only sysex usage requires a permission, otherwise it is granted.
43 if (!midiPermission.sysex()) {
44 resolver->resolve(PermissionStatus::create(scriptState->executionCon text(), WebPermissionStatusGranted, WebPermissionTypeMidi));
45 return true;
46 }
47 }
48 return false;
49 }
50
51 WebPermissionType convertPermissionStringToType(const String& name)
52 {
53 if (name == "geolocation")
54 return WebPermissionTypeGeolocation;
55 if (name == "notifications")
56 return WebPermissionTypeNotifications;
57 if (name == "push")
58 return WebPermissionTypePushNotifications;
59 if (name == "midi")
60 return WebPermissionTypeMidiSysEx;
61
62 ASSERT_NOT_REACHED();
63 return WebPermissionTypeGeolocation;
64 }
65
66 } // anonymous namespace
67
25 // static 68 // static
26 WebPermissionClient* Permissions::getClient(ExecutionContext* executionContext) 69 WebPermissionClient* Permissions::getClient(ExecutionContext* executionContext)
27 { 70 {
28 if (executionContext->isDocument()) { 71 if (executionContext->isDocument()) {
29 Document* document = toDocument(executionContext); 72 Document* document = toDocument(executionContext);
30 if (!document->frame()) 73 if (!document->frame())
31 return nullptr; 74 return nullptr;
32 PermissionController* controller = PermissionController::from(*document- >frame()); 75 PermissionController* controller = PermissionController::from(*document- >frame());
33 return controller ? controller->client() : nullptr; 76 return controller ? controller->client() : nullptr;
34 } 77 }
(...skipping 10 matching lines...) Expand all
45 PermissionDescriptor permission = NativeValueTraits<PermissionDescriptor>::n ativeValue(scriptState->isolate(), rawPermission.v8Value(), exceptionState); 88 PermissionDescriptor permission = NativeValueTraits<PermissionDescriptor>::n ativeValue(scriptState->isolate(), rawPermission.v8Value(), exceptionState);
46 89
47 90
48 if (exceptionState.hadException()) 91 if (exceptionState.hadException())
49 return ScriptPromise::reject(scriptState, v8::Exception::TypeError(v8Str ing(scriptState->isolate(), exceptionState.message()))); 92 return ScriptPromise::reject(scriptState, v8::Exception::TypeError(v8Str ing(scriptState->isolate(), exceptionState.message())));
50 93
51 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scrip tState); 94 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scrip tState);
52 ScriptPromise promise = resolver->promise(); 95 ScriptPromise promise = resolver->promise();
53 96
54 String name = permission.name(); 97 String name = permission.name();
55 WebPermissionType type; 98 WebPermissionType type = convertPermissionStringToType(name);
56 if (name == "geolocation") { 99
57 type = WebPermissionTypeGeolocation; 100 if (handleDefaultBehaviour(scriptState, rawPermission, resolver, type, excep tionState))
58 } else if (name == "notifications") { 101 return promise;
59 type = WebPermissionTypeNotifications;
60 } else if (name == "push") {
61 PushPermissionDescriptor pushPermission = NativeValueTraits<PushPermissi onDescriptor>::nativeValue(scriptState->isolate(), rawPermission.v8Value(), exce ptionState);
62 // Only "userVisibleOnly" push is supported for now.
63 if (!pushPermission.userVisibleOnly()) {
64 resolver->reject(DOMException::create(NotSupportedError, "Push Permi ssion without userVisibleOnly:true isn't supported yet."));
65 return promise;
66 }
67 type = WebPermissionTypePushNotifications;
68 } else if (name == "midi") {
69 MidiPermissionDescriptor midiPermission = NativeValueTraits<MidiPermissi onDescriptor>::nativeValue(scriptState->isolate(), rawPermission.v8Value(), exce ptionState);
70 // Only sysex usage requires a permission, otherwise it is granted.
71 if (!midiPermission.sysex()) {
72 resolver->resolve(PermissionStatus::create(scriptState->executionCon text(), WebPermissionStatusGranted, WebPermissionTypeMidi));
73 return promise;
74 }
75 type = WebPermissionTypeMidiSysEx;
76 } else {
77 ASSERT_NOT_REACHED();
78 type = WebPermissionTypeGeolocation;
79 }
80 102
81 // If the current origin is a file scheme, it will unlikely return a 103 // If the current origin is a file scheme, it will unlikely return a
82 // meaningful value because most APIs are broken on file scheme and no 104 // meaningful value because most APIs are broken on file scheme and no
83 // permission prompt will be shown even if the returned permission will most 105 // permission prompt will be shown even if the returned permission will most
84 // likely be "prompt". 106 // likely be "prompt".
85 client->queryPermission(type, KURL(KURL(), scriptState->executionContext()-> securityOrigin()->toString()), new PermissionQueryCallback(resolver, type)); 107 client->queryPermission(type, KURL(KURL(), scriptState->executionContext()-> securityOrigin()->toString()), new PermissionCallback(resolver, type));
86 return promise; 108 return promise;
87 } 109 }
88 110
111 ScriptPromise Permissions::revoke(ScriptState* scriptState, const ScriptValue& r awPermission)
112 {
113 WebPermissionClient* client = getClient(scriptState->executionContext());
114 if (!client)
115 return ScriptPromise::rejectWithDOMException(scriptState, DOMException:: create(InvalidStateError, "In its current state, the global scope can't revoke p ermissions."));
116
117 TrackExceptionState exceptionState;
118 PermissionDescriptor permission = NativeValueTraits<PermissionDescriptor>::n ativeValue(scriptState->isolate(), rawPermission.v8Value(), exceptionState);
119
120 if (exceptionState.hadException())
121 return ScriptPromise::reject(scriptState, v8::Exception::TypeError(v8Str ing(scriptState->isolate(), exceptionState.message())));
122
123 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scrip tState);
124 ScriptPromise promise = resolver->promise();
125
126 String name = permission.name();
127 WebPermissionType type = convertPermissionStringToType(name);
128
129 if (handleDefaultBehaviour(scriptState, rawPermission, resolver, type, excep tionState))
130 return promise;
131
132 client->revokePermission(type, KURL(KURL(), scriptState->executionContext()- >securityOrigin()->toString()), new PermissionCallback(resolver, type));
133 return promise;
134 }
135
89 } // namespace blink 136 } // namespace blink
OLDNEW
« no previous file with comments | « Source/modules/permissions/Permissions.h ('k') | Source/modules/permissions/Permissions.idl » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698