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

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: relax assert 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 this to a common place.
21 WebPermissionClient* permissionClient(ExecutionContext* executionContext)
22 {
23 if (executionContext->isDocument()) {
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 stopListening();
35 } 68 }
36 69
37 const AtomicString& PermissionStatus::interfaceName() const 70 const AtomicString& PermissionStatus::interfaceName() const
38 { 71 {
39 return EventTargetNames::PermissionStatus; 72 return EventTargetNames::PermissionStatus;
40 } 73 }
41 74
42 ExecutionContext* PermissionStatus::executionContext() const 75 ExecutionContext* PermissionStatus::executionContext() const
43 { 76 {
44 return ContextLifecycleObserver::executionContext(); 77 return ActiveDOMObject::executionContext();
45 } 78 }
46 79
47 DEFINE_TRACE(PermissionStatus) 80 void PermissionStatus::permissionChanged(WebPermissionType type, WebPermissionSt atus status)
48 { 81 {
49 RefCountedGarbageCollectedEventTargetWithInlineData<PermissionStatus>::trace (visitor); 82 ASSERT(m_type == type);
50 ContextLifecycleObserver::trace(visitor); 83 if (m_status == status)
84 return;
85
86 m_status = status;
87 dispatchEvent(Event::create(EventTypeNames::change));
88 }
89
90 bool PermissionStatus::hasPendingActivity() const
91 {
92 return m_listening;
93 }
94
95 void PermissionStatus::resume()
96 {
97 startListening();
98 }
99
100 void PermissionStatus::suspend()
101 {
102 stopListening();
103 }
104
105 void PermissionStatus::stop()
106 {
107 stopListening();
108 }
109
110 void PermissionStatus::startListening()
111 {
112 ASSERT(!m_listening);
113
114 WebPermissionClient* client = permissionClient(executionContext());
115 if (!client)
116 return;
117 m_listening = true;
118 client->startListening(m_type, KURL(KURL(), executionContext()->securityOrig in()->toString()), this);
119 }
120
121 void PermissionStatus::stopListening()
122 {
123 if (!m_listening)
124 return;
125
126 ASSERT(executionContext());
127
128 m_listening = false;
129 WebPermissionClient* client = permissionClient(executionContext());
130 if (!client)
131 return;
132 client->stopListening(this);
51 } 133 }
52 134
53 String PermissionStatus::status() const 135 String PermissionStatus::status() const
54 { 136 {
55 switch (m_status) { 137 switch (m_status) {
56 case WebPermissionStatusGranted: 138 case WebPermissionStatusGranted:
57 return "granted"; 139 return "granted";
58 case WebPermissionStatusDenied: 140 case WebPermissionStatusDenied:
59 return "denied"; 141 return "denied";
60 case WebPermissionStatusPrompt: 142 case WebPermissionStatusPrompt:
61 return "prompt"; 143 return "prompt";
62 } 144 }
63 145
64 ASSERT_NOT_REACHED(); 146 ASSERT_NOT_REACHED();
65 return "denied"; 147 return "denied";
66 } 148 }
67 149
150 DEFINE_TRACE(PermissionStatus)
151 {
152 RefCountedGarbageCollectedEventTargetWithInlineData<PermissionStatus>::trace (visitor);
153 ActiveDOMObject::trace(visitor);
154 }
155
68 } // namespace blink 156 } // namespace blink
OLDNEW
« no previous file with comments | « Source/modules/permissions/PermissionStatus.h ('k') | Source/modules/permissions/PermissionStatus.idl » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698