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

Unified 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, 9 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « Source/modules/permissions/PermissionStatus.h ('k') | Source/modules/permissions/PermissionStatus.idl » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: Source/modules/permissions/PermissionStatus.cpp
diff --git a/Source/modules/permissions/PermissionStatus.cpp b/Source/modules/permissions/PermissionStatus.cpp
index 0441f3688fa91ea8c640a306ca3e7e4afab024d8..60d892e2eff1e6e81573472db716bbd209615528 100644
--- a/Source/modules/permissions/PermissionStatus.cpp
+++ b/Source/modules/permissions/PermissionStatus.cpp
@@ -6,14 +6,37 @@
#include "modules/permissions/PermissionStatus.h"
#include "bindings/core/v8/ScriptPromiseResolver.h"
+#include "core/dom/Document.h"
+#include "core/events/Event.h"
#include "modules/EventTargetModulesNames.h"
+#include "modules/permissions/PermissionController.h"
+#include "public/platform/Platform.h"
+#include "public/platform/modules/permissions/WebPermissionClient.h"
namespace blink {
+namespace {
+
+// TODO(mlamouri): move this to a common place.
+WebPermissionClient* permissionClient(ExecutionContext* executionContext)
+{
+ if (executionContext->isDocument()) {
+ Document* document = toDocument(executionContext);
+ if (!document->frame())
+ return nullptr;
+ PermissionController* controller = PermissionController::from(*document->frame());
+ return controller ? controller->client() : nullptr;
+ }
+ return Platform::current()->permissionClient();
+}
+
+} // anonymous namespace
+
// static
PermissionStatus* PermissionStatus::take(ScriptPromiseResolver* resolver, WebPermissionStatus* status, WebPermissionType type)
{
- PermissionStatus* permissionStatus = new PermissionStatus(resolver->executionContext(), *status);
+ PermissionStatus* permissionStatus = PermissionStatus::create(resolver->executionContext(), *status, type);
+ permissionStatus->startListening();
delete status;
return permissionStatus;
}
@@ -24,14 +47,24 @@ void PermissionStatus::dispose(WebPermissionStatus* status)
delete status;
}
-PermissionStatus::PermissionStatus(ExecutionContext* executionContext, WebPermissionStatus status)
- : ContextLifecycleObserver(executionContext)
+PermissionStatus* PermissionStatus::create(ExecutionContext* executionContext, WebPermissionStatus status, WebPermissionType type)
+{
+ PermissionStatus* permissionStatus = new PermissionStatus(executionContext, status, type);
+ permissionStatus->suspendIfNeeded();
+ return permissionStatus;
+}
+
+PermissionStatus::PermissionStatus(ExecutionContext* executionContext, WebPermissionStatus status, WebPermissionType type)
+ : ActiveDOMObject(executionContext)
, m_status(status)
+ , m_type(type)
+ , m_listening(false)
{
}
PermissionStatus::~PermissionStatus()
{
+ stopListening();
}
const AtomicString& PermissionStatus::interfaceName() const
@@ -41,13 +74,62 @@ const AtomicString& PermissionStatus::interfaceName() const
ExecutionContext* PermissionStatus::executionContext() const
{
- return ContextLifecycleObserver::executionContext();
+ return ActiveDOMObject::executionContext();
}
-DEFINE_TRACE(PermissionStatus)
+void PermissionStatus::permissionChanged(WebPermissionType type, WebPermissionStatus status)
{
- RefCountedGarbageCollectedEventTargetWithInlineData<PermissionStatus>::trace(visitor);
- ContextLifecycleObserver::trace(visitor);
+ ASSERT(m_type == type);
+ if (m_status == status)
+ return;
+
+ m_status = status;
+ dispatchEvent(Event::create(EventTypeNames::change));
+}
+
+bool PermissionStatus::hasPendingActivity() const
+{
+ return m_listening;
+}
+
+void PermissionStatus::resume()
+{
+ startListening();
+}
+
+void PermissionStatus::suspend()
+{
+ stopListening();
+}
+
+void PermissionStatus::stop()
+{
+ stopListening();
+}
+
+void PermissionStatus::startListening()
+{
+ ASSERT(!m_listening);
+
+ WebPermissionClient* client = permissionClient(executionContext());
+ if (!client)
+ return;
+ m_listening = true;
+ client->startListening(m_type, KURL(KURL(), executionContext()->securityOrigin()->toString()), this);
+}
+
+void PermissionStatus::stopListening()
+{
+ if (!m_listening)
+ return;
+
+ ASSERT(executionContext());
+
+ m_listening = false;
+ WebPermissionClient* client = permissionClient(executionContext());
+ if (!client)
+ return;
+ client->stopListening(this);
}
String PermissionStatus::status() const
@@ -65,4 +147,10 @@ String PermissionStatus::status() const
return "denied";
}
+DEFINE_TRACE(PermissionStatus)
+{
+ RefCountedGarbageCollectedEventTargetWithInlineData<PermissionStatus>::trace(visitor);
+ ActiveDOMObject::trace(visitor);
+}
+
} // namespace blink
« 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