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

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

Issue 2300453002: [Extensions] Begin making Extension port initialization asynchronous (Closed)
Patch Set: Ready Created 4 years, 3 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 "extensions/renderer/runtime_custom_bindings.h" 5 #include "extensions/renderer/runtime_custom_bindings.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 8
9 #include <memory> 9 #include <memory>
10 10
11 #include "base/bind.h" 11 #include "base/bind.h"
12 #include "base/values.h" 12 #include "base/values.h"
13 #include "content/public/child/v8_value_converter.h" 13 #include "content/public/child/v8_value_converter.h"
14 #include "content/public/renderer/render_frame.h" 14 #include "content/public/renderer/render_frame.h"
15 #include "extensions/common/extension.h" 15 #include "extensions/common/extension.h"
16 #include "extensions/common/extension_messages.h" 16 #include "extensions/common/extension_messages.h"
17 #include "extensions/common/manifest.h" 17 #include "extensions/common/manifest.h"
18 #include "extensions/renderer/extension_frame_helper.h" 18 #include "extensions/renderer/extension_frame_helper.h"
19 #include "extensions/renderer/script_context.h" 19 #include "extensions/renderer/script_context.h"
20 #include "third_party/WebKit/public/web/WebLocalFrame.h" 20 #include "third_party/WebKit/public/web/WebLocalFrame.h"
21 21
22 namespace extensions { 22 namespace extensions {
23 23
24 RuntimeCustomBindings::RuntimeCustomBindings(ScriptContext* context) 24 RuntimeCustomBindings::RuntimeCustomBindings(ScriptContext* context)
25 : ObjectBackedNativeHandler(context) { 25 : ObjectBackedNativeHandler(context) {
26 RouteFunction( 26 RouteFunction(
27 "GetManifest", 27 "GetManifest",
28 base::Bind(&RuntimeCustomBindings::GetManifest, base::Unretained(this))); 28 base::Bind(&RuntimeCustomBindings::GetManifest, base::Unretained(this)));
29 RouteFunction("OpenChannelToExtension", "runtime.connect",
30 base::Bind(&RuntimeCustomBindings::OpenChannelToExtension,
31 base::Unretained(this)));
32 RouteFunction("OpenChannelToNativeApp", "runtime.connectNative",
33 base::Bind(&RuntimeCustomBindings::OpenChannelToNativeApp,
34 base::Unretained(this)));
35 RouteFunction("GetExtensionViews", 29 RouteFunction("GetExtensionViews",
36 base::Bind(&RuntimeCustomBindings::GetExtensionViews, 30 base::Bind(&RuntimeCustomBindings::GetExtensionViews,
37 base::Unretained(this))); 31 base::Unretained(this)));
38 } 32 }
39 33
40 RuntimeCustomBindings::~RuntimeCustomBindings() { 34 RuntimeCustomBindings::~RuntimeCustomBindings() {
41 } 35 }
42 36
43 void RuntimeCustomBindings::OpenChannelToExtension(
44 const v8::FunctionCallbackInfo<v8::Value>& args) {
45 // Get the current RenderFrame so that we can send a routed IPC message from
46 // the correct source.
47 content::RenderFrame* renderframe = context()->GetRenderFrame();
48 if (!renderframe)
49 return;
50
51 // The Javascript code should validate/fill the arguments.
52 CHECK_EQ(args.Length(), 3);
53 CHECK(args[0]->IsString() && args[1]->IsString() && args[2]->IsBoolean());
54
55 ExtensionMsg_ExternalConnectionInfo info;
56
57 // For messaging APIs, hosted apps should be considered a web page so hide
58 // its extension ID.
59 const Extension* extension = context()->extension();
60 if (extension && !extension->is_hosted_app())
61 info.source_id = extension->id();
62
63 info.target_id = *v8::String::Utf8Value(args[0]);
64 info.source_url = context()->url();
65 std::string channel_name = *v8::String::Utf8Value(args[1]);
66 bool include_tls_channel_id =
67 args.Length() > 2 ? args[2]->BooleanValue() : false;
68 int port_id = -1;
69 // TODO(devlin): This file is littered with sync IPCs. Yuck.
70 renderframe->Send(new ExtensionHostMsg_OpenChannelToExtension(
71 renderframe->GetRoutingID(), info, channel_name, include_tls_channel_id,
72 &port_id));
73 args.GetReturnValue().Set(static_cast<int32_t>(port_id));
74 }
75
76 void RuntimeCustomBindings::OpenChannelToNativeApp(
77 const v8::FunctionCallbackInfo<v8::Value>& args) {
78 // The Javascript code should validate/fill the arguments.
79 CHECK_EQ(args.Length(), 1);
80 CHECK(args[0]->IsString());
81
82 // Verify that the extension has permission to use native messaging.
83 if (!context()->GetAvailability("runtime.connectNative").is_available())
84 return;
85
86 content::RenderFrame* render_frame = context()->GetRenderFrame();
87 if (!render_frame)
88 return;
89
90 std::string native_app_name = *v8::String::Utf8Value(args[0]);
91
92 int port_id = -1;
93 render_frame->Send(new ExtensionHostMsg_OpenChannelToNativeApp(
94 render_frame->GetRoutingID(), native_app_name, &port_id));
95 args.GetReturnValue().Set(static_cast<int32_t>(port_id));
96 }
97
98 void RuntimeCustomBindings::GetManifest( 37 void RuntimeCustomBindings::GetManifest(
99 const v8::FunctionCallbackInfo<v8::Value>& args) { 38 const v8::FunctionCallbackInfo<v8::Value>& args) {
100 CHECK(context()->extension()); 39 CHECK(context()->extension());
101 40
102 std::unique_ptr<content::V8ValueConverter> converter( 41 std::unique_ptr<content::V8ValueConverter> converter(
103 content::V8ValueConverter::create()); 42 content::V8ValueConverter::create());
104 args.GetReturnValue().Set(converter->ToV8Value( 43 args.GetReturnValue().Set(converter->ToV8Value(
105 context()->extension()->manifest()->value(), context()->v8_context())); 44 context()->extension()->manifest()->value(), context()->v8_context()));
106 } 45 }
107 46
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
169 v8::Maybe<bool> maybe = 108 v8::Maybe<bool> maybe =
170 v8_views->CreateDataProperty(v8_context, v8_index++, window); 109 v8_views->CreateDataProperty(v8_context, v8_index++, window);
171 CHECK(maybe.IsJust() && maybe.FromJust()); 110 CHECK(maybe.IsJust() && maybe.FromJust());
172 } 111 }
173 } 112 }
174 113
175 args.GetReturnValue().Set(v8_views); 114 args.GetReturnValue().Set(v8_views);
176 } 115 }
177 116
178 } // namespace extensions 117 } // namespace extensions
OLDNEW
« extensions/renderer/messaging_bindings.cc ('K') | « extensions/renderer/runtime_custom_bindings.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698