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
3 // found in the LICENSE file.
5 #include "chrome/renderer/extensions/app_bindings.h"
7 #include "base/command_line.h"
8 #include "base/strings/string16.h"
9 #include "base/strings/string_util.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "base/values.h"
12 #include "chrome/common/chrome_switches.h"
13 #include "chrome/common/extensions/extension_constants.h"
14 #include "content/public/child/v8_value_converter.h"
15 #include "content/public/renderer/render_frame.h"
16 #include "extensions/common/constants.h"
17 #include "extensions/common/extension_messages.h"
18 #include "extensions/common/extension_set.h"
19 #include "extensions/common/manifest.h"
20 #include "extensions/renderer/console.h"
21 #include "extensions/renderer/dispatcher.h"
22 #include "extensions/renderer/extension_helper.h"
23 #include "extensions/renderer/script_context.h"
24 #include "third_party/WebKit/public/web/WebDocument.h"
25 #include "third_party/WebKit/public/web/WebLocalFrame.h"
26 #include "v8/include/v8.h"
28 using blink::WebFrame
;
29 using content::V8ValueConverter
;
31 namespace extensions
{
35 const char kInvalidCallbackIdError
[] = "Invalid callbackId";
39 AppBindings::AppBindings(Dispatcher
* dispatcher
, ScriptContext
* context
)
40 : ObjectBackedNativeHandler(context
),
41 dispatcher_(dispatcher
) {
42 RouteFunction("GetIsInstalled",
43 base::Bind(&AppBindings::GetIsInstalled
, base::Unretained(this)));
44 RouteFunction("GetDetails",
45 base::Bind(&AppBindings::GetDetails
, base::Unretained(this)));
46 RouteFunction("GetInstallState",
47 base::Bind(&AppBindings::GetInstallState
, base::Unretained(this)));
48 RouteFunction("GetRunningState",
49 base::Bind(&AppBindings::GetRunningState
, base::Unretained(this)));
52 AppBindings::~AppBindings() {
55 void AppBindings::GetIsInstalled(
56 const v8::FunctionCallbackInfo
<v8::Value
>& args
) {
57 const Extension
* extension
= context()->extension();
59 // TODO(aa): Why only hosted app?
60 bool result
= extension
&& extension
->is_hosted_app() &&
61 dispatcher_
->IsExtensionActive(extension
->id());
62 args
.GetReturnValue().Set(result
);
65 void AppBindings::GetDetails(
66 const v8::FunctionCallbackInfo
<v8::Value
>& args
) {
67 blink::WebLocalFrame
* web_frame
= context()->web_frame();
69 args
.GetReturnValue().Set(GetDetailsImpl(web_frame
));
72 v8::Local
<v8::Value
> AppBindings::GetDetailsImpl(blink::WebLocalFrame
* frame
) {
73 v8::Isolate
* isolate
= frame
->mainWorldScriptContext()->GetIsolate();
74 if (frame
->document().securityOrigin().isUnique())
75 return v8::Null(isolate
);
77 const Extension
* extension
=
78 dispatcher_
->extensions()->GetExtensionOrAppByURL(
79 frame
->document().url());
82 return v8::Null(isolate
);
84 scoped_ptr
<base::DictionaryValue
> manifest_copy(
85 extension
->manifest()->value()->DeepCopy());
86 manifest_copy
->SetString("id", extension
->id());
87 scoped_ptr
<V8ValueConverter
> converter(V8ValueConverter::create());
88 return converter
->ToV8Value(manifest_copy
.get(),
89 frame
->mainWorldScriptContext());
92 void AppBindings::GetInstallState(
93 const v8::FunctionCallbackInfo
<v8::Value
>& args
) {
94 // Get the callbackId.
96 if (args
.Length() == 1) {
97 if (!args
[0]->IsInt32()) {
98 context()->isolate()->ThrowException(v8::String::NewFromUtf8(
99 context()->isolate(), kInvalidCallbackIdError
));
102 callback_id
= args
[0]->Int32Value();
105 content::RenderFrame
* render_frame
= context()->GetRenderFrame();
108 Send(new ExtensionHostMsg_GetAppInstallState(
109 render_frame
->GetRoutingID(), context()->web_frame()->document().url(),
110 GetRoutingID(), callback_id
));
113 void AppBindings::GetRunningState(
114 const v8::FunctionCallbackInfo
<v8::Value
>& args
) {
115 // To distinguish between ready_to_run and cannot_run states, we need the app
116 // from the top frame.
117 blink::WebSecurityOrigin top_frame_security_origin
=
118 context()->web_frame()->top()->securityOrigin();
119 const ExtensionSet
* extensions
= dispatcher_
->extensions();
121 // The app associated with the top level frame.
122 const Extension
* top_app
= extensions
->GetHostedAppByURL(
123 GURL(top_frame_security_origin
.toString().utf8()));
125 // The app associated with this frame.
126 const Extension
* this_app
= extensions
->GetHostedAppByURL(
127 context()->web_frame()->document().url());
129 if (!this_app
|| !top_app
) {
130 args
.GetReturnValue().Set(v8::String::NewFromUtf8(
131 context()->isolate(), extension_misc::kAppStateCannotRun
));
135 const char* state
= nullptr;
136 if (dispatcher_
->IsExtensionActive(top_app
->id())) {
137 if (top_app
== this_app
)
138 state
= extension_misc::kAppStateRunning
;
140 state
= extension_misc::kAppStateCannotRun
;
141 } else if (top_app
== this_app
) {
142 state
= extension_misc::kAppStateReadyToRun
;
144 state
= extension_misc::kAppStateCannotRun
;
147 args
.GetReturnValue()
148 .Set(v8::String::NewFromUtf8(context()->isolate(), state
));
151 bool AppBindings::OnMessageReceived(const IPC::Message
& message
) {
152 IPC_BEGIN_MESSAGE_MAP(AppBindings
, message
)
153 IPC_MESSAGE_HANDLER(ExtensionMsg_GetAppInstallStateResponse
,
154 OnAppInstallStateResponse
)
155 IPC_MESSAGE_UNHANDLED(CHECK(false) << "Unhandled IPC message")
156 IPC_END_MESSAGE_MAP()
160 void AppBindings::OnAppInstallStateResponse(
161 const std::string
& state
, int callback_id
) {
162 v8::Isolate
* isolate
= context()->isolate();
163 v8::HandleScope
handle_scope(isolate
);
164 v8::Context::Scope
context_scope(context()->v8_context());
165 v8::Local
<v8::Value
> argv
[] = {
166 v8::String::NewFromUtf8(isolate
, state
.c_str()),
167 v8::Integer::New(isolate
, callback_id
)
169 context()->module_system()->CallModuleMethod(
170 "app", "onInstallStateResponse", arraysize(argv
), argv
);
173 } // namespace extensions