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

Side by Side Diff: chrome/renderer/extensions/app_bindings.cc

Issue 2859233003: [Extensions] Improve app custom bindings (Closed)
Patch Set: Created 3 years, 7 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 (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "chrome/renderer/extensions/app_bindings.h" 5 #include "chrome/renderer/extensions/app_bindings.h"
6 6
7 #include <memory> 7 #include <memory>
8 8
9 #include "base/command_line.h"
10 #include "base/macros.h"
11 #include "base/strings/string16.h"
12 #include "base/strings/string_util.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "base/values.h" 9 #include "base/values.h"
15 #include "chrome/common/chrome_switches.h"
16 #include "chrome/common/extensions/extension_constants.h" 10 #include "chrome/common/extensions/extension_constants.h"
17 #include "content/public/child/v8_value_converter.h" 11 #include "content/public/child/v8_value_converter.h"
18 #include "content/public/renderer/render_frame.h" 12 #include "content/public/renderer/render_frame.h"
19 #include "extensions/common/constants.h" 13 #include "extensions/common/constants.h"
20 #include "extensions/common/extension_messages.h" 14 #include "extensions/common/extension_messages.h"
21 #include "extensions/common/extension_set.h" 15 #include "extensions/common/extension_set.h"
22 #include "extensions/common/manifest.h" 16 #include "extensions/common/manifest.h"
23 #include "extensions/renderer/console.h"
24 #include "extensions/renderer/dispatcher.h" 17 #include "extensions/renderer/dispatcher.h"
25 #include "extensions/renderer/extension_helper.h"
26 #include "extensions/renderer/renderer_extension_registry.h" 18 #include "extensions/renderer/renderer_extension_registry.h"
27 #include "extensions/renderer/script_context.h" 19 #include "extensions/renderer/script_context.h"
28 #include "third_party/WebKit/public/web/WebDocument.h" 20 #include "third_party/WebKit/public/web/WebDocument.h"
29 #include "third_party/WebKit/public/web/WebLocalFrame.h" 21 #include "third_party/WebKit/public/web/WebLocalFrame.h"
30 #include "v8/include/v8.h" 22 #include "v8/include/v8.h"
31 23
32 using blink::WebFrame; 24 using blink::WebFrame;
33 using content::V8ValueConverter; 25 using content::V8ValueConverter;
34 26
35 namespace extensions { 27 namespace extensions {
36 28
37 namespace {
38
39 const char kInvalidCallbackIdError[] = "Invalid callbackId";
40
41 } // namespace
42
43 AppBindings::AppBindings(Dispatcher* dispatcher, ScriptContext* context) 29 AppBindings::AppBindings(Dispatcher* dispatcher, ScriptContext* context)
44 : ObjectBackedNativeHandler(context), 30 : ObjectBackedNativeHandler(context),
45 dispatcher_(dispatcher) { 31 dispatcher_(dispatcher) {
46 RouteFunction( 32 RouteFunction(
47 "GetIsInstalled", "app.getIsInstalled", 33 "GetIsInstalled", "app.getIsInstalled",
48 base::Bind(&AppBindings::GetIsInstalled, base::Unretained(this))); 34 base::Bind(&AppBindings::GetIsInstalled, base::Unretained(this)));
49 RouteFunction("GetDetails", "app.getDetails", 35 RouteFunction("GetDetails", "app.getDetails",
50 base::Bind(&AppBindings::GetDetails, base::Unretained(this))); 36 base::Bind(&AppBindings::GetDetails, base::Unretained(this)));
51 RouteFunction( 37 RouteFunction(
52 "GetInstallState", "app.installState", 38 "GetInstallState", "app.installState",
(...skipping 17 matching lines...) Expand all
70 } 56 }
71 57
72 void AppBindings::GetDetails( 58 void AppBindings::GetDetails(
73 const v8::FunctionCallbackInfo<v8::Value>& args) { 59 const v8::FunctionCallbackInfo<v8::Value>& args) {
74 blink::WebLocalFrame* web_frame = context()->web_frame(); 60 blink::WebLocalFrame* web_frame = context()->web_frame();
75 CHECK(web_frame); 61 CHECK(web_frame);
76 args.GetReturnValue().Set(GetDetailsImpl(web_frame)); 62 args.GetReturnValue().Set(GetDetailsImpl(web_frame));
77 } 63 }
78 64
79 v8::Local<v8::Value> AppBindings::GetDetailsImpl(blink::WebLocalFrame* frame) { 65 v8::Local<v8::Value> AppBindings::GetDetailsImpl(blink::WebLocalFrame* frame) {
80 v8::Isolate* isolate = frame->MainWorldScriptContext()->GetIsolate(); 66 v8::Isolate* isolate = context()->isolate();
Devlin 2017/05/04 18:58:50 Even though this is the same isolate in practice,
81 if (frame->GetDocument().GetSecurityOrigin().IsUnique()) 67 if (frame->GetDocument().GetSecurityOrigin().IsUnique())
82 return v8::Null(isolate); 68 return v8::Null(isolate);
83 69
84 const Extension* extension = 70 const Extension* extension =
85 RendererExtensionRegistry::Get()->GetExtensionOrAppByURL( 71 RendererExtensionRegistry::Get()->GetExtensionOrAppByURL(
86 frame->GetDocument().Url()); 72 frame->GetDocument().Url());
87 73
88 if (!extension) 74 if (!extension)
89 return v8::Null(isolate); 75 return v8::Null(isolate);
90 76
91 std::unique_ptr<base::DictionaryValue> manifest_copy( 77 std::unique_ptr<base::DictionaryValue> manifest_copy(
92 extension->manifest()->value()->DeepCopy()); 78 extension->manifest()->value()->DeepCopy());
93 manifest_copy->SetString("id", extension->id()); 79 manifest_copy->SetString("id", extension->id());
94 std::unique_ptr<V8ValueConverter> converter(V8ValueConverter::create()); 80 std::unique_ptr<V8ValueConverter> converter(V8ValueConverter::create());
95 return converter->ToV8Value(manifest_copy.get(), 81 return converter->ToV8Value(manifest_copy.get(), context()->v8_context());
96 frame->MainWorldScriptContext());
Devlin 2017/05/04 18:58:50 This seemed *very* wrong, since it's not the conte
jbroman 2017/05/08 14:25:24 If it can be called in a content script (it seems
Devlin 2017/05/11 14:30:37 Yep, it could be called from any context, which is
97 } 82 }
98 83
99 void AppBindings::GetInstallState( 84 void AppBindings::GetInstallState(
100 const v8::FunctionCallbackInfo<v8::Value>& args) { 85 const v8::FunctionCallbackInfo<v8::Value>& args) {
101 // Get the callbackId. 86 // Get the callbackId.
102 int callback_id = 0; 87 CHECK_EQ(1, args.Length());
Devlin 2017/05/04 18:58:50 We should only get valid calls to this function, s
103 if (args.Length() == 1) { 88 CHECK(args[0]->IsInt32());
104 if (!args[0]->IsInt32()) { 89 int callback_id = args[0]->Int32Value();
105 context()->isolate()->ThrowException(v8::String::NewFromUtf8(
106 context()->isolate(), kInvalidCallbackIdError));
107 return;
108 }
109 callback_id = args[0]->Int32Value();
110 }
111 90
112 content::RenderFrame* render_frame = context()->GetRenderFrame(); 91 content::RenderFrame* render_frame = context()->GetRenderFrame();
113 CHECK(render_frame); 92 CHECK(render_frame);
114 93
115 Send(new ExtensionHostMsg_GetAppInstallState( 94 Send(new ExtensionHostMsg_GetAppInstallState(
116 render_frame->GetRoutingID(), context()->web_frame()->GetDocument().Url(), 95 render_frame->GetRoutingID(), context()->web_frame()->GetDocument().Url(),
117 GetRoutingID(), callback_id)); 96 GetRoutingID(), callback_id));
118 } 97 }
119 98
120 void AppBindings::GetRunningState( 99 void AppBindings::GetRunningState(
121 const v8::FunctionCallbackInfo<v8::Value>& args) { 100 const v8::FunctionCallbackInfo<v8::Value>& args) {
122 // To distinguish between ready_to_run and cannot_run states, we need the app 101 // To distinguish between ready_to_run and cannot_run states, we need the app
123 // from the top frame. 102 // from the top frame.
124 blink::WebSecurityOrigin top_frame_security_origin =
125 context()->web_frame()->Top()->GetSecurityOrigin();
126 const RendererExtensionRegistry* extensions = 103 const RendererExtensionRegistry* extensions =
127 RendererExtensionRegistry::Get(); 104 RendererExtensionRegistry::Get();
128 105
129 // The app associated with the top level frame. 106 // The app associated with the top level frame.
130 const Extension* top_app = extensions->GetHostedAppByURL( 107 const Extension* top_app = extensions->GetHostedAppByURL(
131 GURL(top_frame_security_origin.ToString().Utf8())); 108 context()->web_frame()->Top()->GetDocument().Url());
132 109
133 // The app associated with this frame. 110 // The app associated with this frame.
134 const Extension* this_app = extensions->GetHostedAppByURL( 111 const Extension* this_app = extensions->GetHostedAppByURL(
135 context()->web_frame()->GetDocument().Url()); 112 context()->web_frame()->GetDocument().Url());
136 113
137 if (!this_app || !top_app) { 114 if (!this_app || !top_app) {
138 args.GetReturnValue().Set(v8::String::NewFromUtf8( 115 args.GetReturnValue().Set(v8::String::NewFromUtf8(
139 context()->isolate(), extension_misc::kAppStateCannotRun)); 116 context()->isolate(), extension_misc::kAppStateCannotRun));
140 return; 117 return;
141 } 118 }
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
175 v8::Context::Scope context_scope(context()->v8_context()); 152 v8::Context::Scope context_scope(context()->v8_context());
176 v8::Local<v8::Value> argv[] = { 153 v8::Local<v8::Value> argv[] = {
177 v8::String::NewFromUtf8(isolate, state.c_str()), 154 v8::String::NewFromUtf8(isolate, state.c_str()),
178 v8::Integer::New(isolate, callback_id) 155 v8::Integer::New(isolate, callback_id)
179 }; 156 };
180 context()->module_system()->CallModuleMethodSafe( 157 context()->module_system()->CallModuleMethodSafe(
181 "app", "onInstallStateResponse", arraysize(argv), argv); 158 "app", "onInstallStateResponse", arraysize(argv), argv);
182 } 159 }
183 160
184 } // namespace extensions 161 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698