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

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

Issue 1054663002: Implement Permission change observing in Blink. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 5 years, 8 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
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/PermissionStatus.h" 6 #include "modules/permissions/PermissionStatus.h"
7 7
8 #include "bindings/core/v8/ScriptPromiseResolver.h" 8 #include "bindings/core/v8/ScriptPromiseResolver.h"
9 #include "core/dom/Document.h"
10 #include "core/events/Event.h"
9 #include "modules/EventTargetModulesNames.h" 11 #include "modules/EventTargetModulesNames.h"
12 #include "modules/permissions/PermissionController.h"
13 #include "public/platform/Platform.h"
14 #include "public/platform/modules/permissions/WebPermissionClient.h"
10 15
11 namespace blink { 16 namespace blink {
12 17
18 namespace {
19
20 // TODO(mlamouri): move that to a common place.
Miguel Garcia 2015/04/01 17:41:50 nit: move *this*
mlamouri (slow - plz ping) 2015/04/01 17:53:10 Done.
21 WebPermissionClient* permissionClient(ExecutionContext* executionContext)
22 {
23 if (executionContext->isDocument()) {
Miguel Garcia 2015/04/01 17:41:51 this might sound stupid but why do we have two pos
mlamouri (slow - plz ping) 2015/04/01 17:53:10 The client associated with the frame will talk to
24 Document* document = toDocument(executionContext);
25 if (!document->frame())
26 return nullptr;
27 PermissionController* controller = PermissionController::from(*document- >frame());
28 return controller ? controller->client() : nullptr;
29 }
30 return Platform::current()->permissionClient();
31 }
32
33 } // anonymous namespace
34
13 // static 35 // static
14 PermissionStatus* PermissionStatus::take(ScriptPromiseResolver* resolver, WebPer missionStatus* status, WebPermissionType type) 36 PermissionStatus* PermissionStatus::take(ScriptPromiseResolver* resolver, WebPer missionStatus* status, WebPermissionType type)
15 { 37 {
16 PermissionStatus* permissionStatus = new PermissionStatus(resolver->executio nContext(), *status); 38 PermissionStatus* permissionStatus = PermissionStatus::create(resolver->exec utionContext(), *status, type);
39 permissionStatus->startListening();
17 delete status; 40 delete status;
18 return permissionStatus; 41 return permissionStatus;
19 } 42 }
20 43
21 // static 44 // static
22 void PermissionStatus::dispose(WebPermissionStatus* status) 45 void PermissionStatus::dispose(WebPermissionStatus* status)
23 { 46 {
24 delete status; 47 delete status;
25 } 48 }
26 49
27 PermissionStatus::PermissionStatus(ExecutionContext* executionContext, WebPermis sionStatus status) 50 PermissionStatus* PermissionStatus::create(ExecutionContext* executionContext, W ebPermissionStatus status, WebPermissionType type)
28 : ContextLifecycleObserver(executionContext) 51 {
52 PermissionStatus* permissionStatus = new PermissionStatus(executionContext, status, type);
53 permissionStatus->suspendIfNeeded();
54 return permissionStatus;
55 }
56
57 PermissionStatus::PermissionStatus(ExecutionContext* executionContext, WebPermis sionStatus status, WebPermissionType type)
58 : ActiveDOMObject(executionContext)
29 , m_status(status) 59 , m_status(status)
60 , m_type(type)
61 , m_listening(false)
30 { 62 {
31 } 63 }
32 64
33 PermissionStatus::~PermissionStatus() 65 PermissionStatus::~PermissionStatus()
34 { 66 {
67 if (m_listening)
68 stopListening();
35 } 69 }
36 70
37 const AtomicString& PermissionStatus::interfaceName() const 71 const AtomicString& PermissionStatus::interfaceName() const
38 { 72 {
39 return EventTargetNames::PermissionStatus; 73 return EventTargetNames::PermissionStatus;
40 } 74 }
41 75
42 ExecutionContext* PermissionStatus::executionContext() const 76 ExecutionContext* PermissionStatus::executionContext() const
43 { 77 {
44 return ContextLifecycleObserver::executionContext(); 78 return ActiveDOMObject::executionContext();
45 } 79 }
46 80
47 DEFINE_TRACE(PermissionStatus) 81 void PermissionStatus::permissionChanged(WebPermissionType type, WebPermissionSt atus status)
48 { 82 {
49 RefCountedGarbageCollectedEventTargetWithInlineData<PermissionStatus>::trace (visitor); 83 ASSERT(m_type == type);
50 ContextLifecycleObserver::trace(visitor); 84 if (m_status == status)
85 return;
86
87 m_status = status;
88 dispatchEvent(Event::create(EventTypeNames::change));
89 }
90
91 bool PermissionStatus::hasPendingActivity() const
92 {
93 return m_listening;
94 }
95
96 void PermissionStatus::resume()
97 {
98 startListening();
99 }
100
101 void PermissionStatus::suspend()
102 {
103 stopListening();
104 }
105
106 void PermissionStatus::stop()
107 {
108 stopListening();
109 }
110
111 void PermissionStatus::startListening()
112 {
113 ASSERT(!m_listening);
114
115 WebPermissionClient* client = permissionClient(executionContext());
116 if (!client)
117 return;
118 m_listening = true;
119 client->startListening(m_type, KURL(KURL(), executionContext()->securityOrig in()->toString()), this);
120 }
121
122 void PermissionStatus::stopListening()
123 {
124 ASSERT(m_listening);
125 ASSERT(executionContext());
126
127 m_listening = false;
128 WebPermissionClient* client = permissionClient(executionContext());
129 if (!client)
130 return;
131 client->stopListening(this);
51 } 132 }
52 133
53 String PermissionStatus::status() const 134 String PermissionStatus::status() const
54 { 135 {
55 switch (m_status) { 136 switch (m_status) {
56 case WebPermissionStatusGranted: 137 case WebPermissionStatusGranted:
57 return "granted"; 138 return "granted";
58 case WebPermissionStatusDenied: 139 case WebPermissionStatusDenied:
59 return "denied"; 140 return "denied";
60 case WebPermissionStatusPrompt: 141 case WebPermissionStatusPrompt:
61 return "prompt"; 142 return "prompt";
62 } 143 }
63 144
64 ASSERT_NOT_REACHED(); 145 ASSERT_NOT_REACHED();
65 return "denied"; 146 return "denied";
66 } 147 }
67 148
149 DEFINE_TRACE(PermissionStatus)
150 {
151 RefCountedGarbageCollectedEventTargetWithInlineData<PermissionStatus>::trace (visitor);
152 ActiveDOMObject::trace(visitor);
153 }
154
68 } // namespace blink 155 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698