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

Unified Diff: extensions/renderer/api_bindings_system.cc

Issue 2552343006: [Extensions Binding] Allow for registering custom hooks (Closed)
Patch Set: polish Created 4 years 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
Index: extensions/renderer/api_bindings_system.cc
diff --git a/extensions/renderer/api_bindings_system.cc b/extensions/renderer/api_bindings_system.cc
index 14ec34115f61085598c5fd4f22f26c85fbd3bb00..9abc8dc37b3f5ee05ce3528e11be172ee320eb0e 100644
--- a/extensions/renderer/api_bindings_system.cc
+++ b/extensions/renderer/api_bindings_system.cc
@@ -7,6 +7,7 @@
#include "base/bind.h"
#include "base/memory/ptr_util.h"
#include "base/values.h"
+#include "extensions/renderer/api_binding_hooks.h"
namespace extensions {
@@ -47,10 +48,20 @@ std::unique_ptr<APIBinding> APIBindingsSystem::CreateNewAPIBinding(
const base::ListValue* event_definitions = nullptr;
api_schema.GetList("events", &event_definitions);
+ // Find the hooks for the API. If none are registered, we create one.
jbroman 2016/12/08 16:58:56 nit: Is there a need to heap-allocate one when we
Devlin 2016/12/08 19:12:03 Mostly just because I didn't want to have to nullc
+ std::unique_ptr<APIBindingHooks> hooks;
+ auto iter = binding_hooks_.find(api_name);
+ if (iter != binding_hooks_.end()) {
+ hooks = std::move(iter->second);
+ binding_hooks_.erase(iter);
+ } else {
+ hooks = base::MakeUnique<APIBindingHooks>(api_name);
+ }
+
return base::MakeUnique<APIBinding>(
api_name, *function_definitions, type_definitions, event_definitions,
base::Bind(&APIBindingsSystem::OnAPICall, base::Unretained(this)),
- &type_reference_map_);
+ std::move(hooks), &type_reference_map_);
}
void APIBindingsSystem::CompleteRequest(int request_id,
@@ -64,6 +75,19 @@ void APIBindingsSystem::FireEventInContext(const std::string& event_name,
event_handler_.FireEventInContext(event_name, context, response);
}
+APIBindingHooks* APIBindingsSystem::GetHooksForAPI(
+ const std::string& api_name) {
+ DCHECK(api_bindings_.empty())
+ << "Hook registration must happen before creating any binding instances.";
+ auto iter = binding_hooks_.find(api_name);
jbroman 2016/12/08 16:58:56 super-duper-nit: This can be written without doing
Devlin 2016/12/08 19:12:03 I like it, done.
+ if (iter != binding_hooks_.end())
+ return iter->second.get();
+ auto hooks = base::MakeUnique<APIBindingHooks>(api_name);
+ APIBindingHooks* hooks_weak = hooks.get();
+ binding_hooks_[api_name] = std::move(hooks);
+ return hooks_weak;
+}
+
void APIBindingsSystem::OnAPICall(const std::string& name,
std::unique_ptr<base::ListValue> arguments,
v8::Isolate* isolate,
« extensions/renderer/api_bindings_system.h ('K') | « extensions/renderer/api_bindings_system.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698