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

Side by Side Diff: extensions/renderer/api_bindings_system.cc

Issue 2552343006: [Extensions Binding] Allow for registering custom hooks (Closed)
Patch Set: jbroman 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 unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 "extensions/renderer/api_bindings_system.h" 5 #include "extensions/renderer/api_bindings_system.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/memory/ptr_util.h" 8 #include "base/memory/ptr_util.h"
9 #include "base/values.h" 9 #include "base/values.h"
10 #include "extensions/renderer/api_binding_hooks.h"
10 11
11 namespace extensions { 12 namespace extensions {
12 13
13 APIBindingsSystem::Request::Request() {} 14 APIBindingsSystem::Request::Request() {}
14 APIBindingsSystem::Request::~Request() {} 15 APIBindingsSystem::Request::~Request() {}
15 16
16 APIBindingsSystem::APIBindingsSystem(const binding::RunJSFunction& call_js, 17 APIBindingsSystem::APIBindingsSystem(const binding::RunJSFunction& call_js,
17 const GetAPISchemaMethod& get_api_schema, 18 const GetAPISchemaMethod& get_api_schema,
18 const SendRequestMethod& send_request) 19 const SendRequestMethod& send_request)
19 : request_handler_(call_js), 20 : request_handler_(call_js),
(...skipping 20 matching lines...) Expand all
40 const base::DictionaryValue& api_schema = get_api_schema_.Run(api_name); 41 const base::DictionaryValue& api_schema = get_api_schema_.Run(api_name);
41 42
42 const base::ListValue* function_definitions = nullptr; 43 const base::ListValue* function_definitions = nullptr;
43 CHECK(api_schema.GetList("functions", &function_definitions)); 44 CHECK(api_schema.GetList("functions", &function_definitions));
44 const base::ListValue* type_definitions = nullptr; 45 const base::ListValue* type_definitions = nullptr;
45 // Type definitions might not exist for the given API. 46 // Type definitions might not exist for the given API.
46 api_schema.GetList("types", &type_definitions); 47 api_schema.GetList("types", &type_definitions);
47 const base::ListValue* event_definitions = nullptr; 48 const base::ListValue* event_definitions = nullptr;
48 api_schema.GetList("events", &event_definitions); 49 api_schema.GetList("events", &event_definitions);
49 50
51 // Find the hooks for the API. If none are registered, we create one.
52 std::unique_ptr<APIBindingHooks> hooks;
53 auto iter = binding_hooks_.find(api_name);
54 if (iter != binding_hooks_.end()) {
55 hooks = std::move(iter->second);
56 binding_hooks_.erase(iter);
57 }
58
50 return base::MakeUnique<APIBinding>( 59 return base::MakeUnique<APIBinding>(
51 api_name, *function_definitions, type_definitions, event_definitions, 60 api_name, *function_definitions, type_definitions, event_definitions,
52 base::Bind(&APIBindingsSystem::OnAPICall, base::Unretained(this)), 61 base::Bind(&APIBindingsSystem::OnAPICall, base::Unretained(this)),
53 &type_reference_map_); 62 std::move(hooks), &type_reference_map_);
54 } 63 }
55 64
56 void APIBindingsSystem::CompleteRequest(int request_id, 65 void APIBindingsSystem::CompleteRequest(int request_id,
57 const base::ListValue& response) { 66 const base::ListValue& response) {
58 request_handler_.CompleteRequest(request_id, response); 67 request_handler_.CompleteRequest(request_id, response);
59 } 68 }
60 69
61 void APIBindingsSystem::FireEventInContext(const std::string& event_name, 70 void APIBindingsSystem::FireEventInContext(const std::string& event_name,
62 v8::Local<v8::Context> context, 71 v8::Local<v8::Context> context,
63 const base::ListValue& response) { 72 const base::ListValue& response) {
64 event_handler_.FireEventInContext(event_name, context, response); 73 event_handler_.FireEventInContext(event_name, context, response);
65 } 74 }
66 75
76 APIBindingHooks* APIBindingsSystem::GetHooksForAPI(
77 const std::string& api_name) {
78 DCHECK(api_bindings_.empty())
79 << "Hook registration must happen before creating any binding instances.";
80 std::unique_ptr<APIBindingHooks>& hooks = binding_hooks_[api_name];
81 if (!hooks)
82 hooks = base::MakeUnique<APIBindingHooks>();
83 return hooks.get();
84 }
85
67 void APIBindingsSystem::OnAPICall(const std::string& name, 86 void APIBindingsSystem::OnAPICall(const std::string& name,
68 std::unique_ptr<base::ListValue> arguments, 87 std::unique_ptr<base::ListValue> arguments,
69 v8::Isolate* isolate, 88 v8::Isolate* isolate,
70 v8::Local<v8::Context> context, 89 v8::Local<v8::Context> context,
71 v8::Local<v8::Function> callback) { 90 v8::Local<v8::Function> callback) {
72 auto request = base::MakeUnique<Request>(); 91 auto request = base::MakeUnique<Request>();
73 if (!callback.IsEmpty()) { 92 if (!callback.IsEmpty()) {
74 request->request_id = 93 request->request_id =
75 request_handler_.AddPendingRequest(isolate, callback, context); 94 request_handler_.AddPendingRequest(isolate, callback, context);
76 request->has_callback = true; 95 request->has_callback = true;
77 } 96 }
78 // TODO(devlin): Query and curry user gestures around. 97 // TODO(devlin): Query and curry user gestures around.
79 request->has_user_gesture = false; 98 request->has_user_gesture = false;
80 request->arguments = std::move(arguments); 99 request->arguments = std::move(arguments);
81 request->method_name = name; 100 request->method_name = name;
82 101
83 send_request_.Run(std::move(request), context); 102 send_request_.Run(std::move(request), context);
84 } 103 }
85 104
86 } // namespace extensions 105 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698