Update CrOS OOBE throbber to MD throbber; delete old asset
[chromium-blink-merge.git] / chrome / renderer / extensions / app_bindings.cc
blob5a495a7bd14bd5208622b6c7c667b2816adcbc58
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/renderer_extension_registry.h"
24 #include "extensions/renderer/script_context.h"
25 #include "third_party/WebKit/public/web/WebDocument.h"
26 #include "third_party/WebKit/public/web/WebLocalFrame.h"
27 #include "v8/include/v8.h"
29 using blink::WebFrame;
30 using content::V8ValueConverter;
32 namespace extensions {
34 namespace {
36 const char kInvalidCallbackIdError[] = "Invalid callbackId";
38 } // namespace
40 AppBindings::AppBindings(Dispatcher* dispatcher, ScriptContext* context)
41 : ObjectBackedNativeHandler(context),
42 dispatcher_(dispatcher) {
43 RouteFunction("GetIsInstalled",
44 base::Bind(&AppBindings::GetIsInstalled, base::Unretained(this)));
45 RouteFunction("GetDetails",
46 base::Bind(&AppBindings::GetDetails, base::Unretained(this)));
47 RouteFunction("GetInstallState",
48 base::Bind(&AppBindings::GetInstallState, base::Unretained(this)));
49 RouteFunction("GetRunningState",
50 base::Bind(&AppBindings::GetRunningState, base::Unretained(this)));
53 AppBindings::~AppBindings() {
56 void AppBindings::GetIsInstalled(
57 const v8::FunctionCallbackInfo<v8::Value>& args) {
58 const Extension* extension = context()->extension();
60 // TODO(aa): Why only hosted app?
61 bool result = extension && extension->is_hosted_app() &&
62 dispatcher_->IsExtensionActive(extension->id());
63 args.GetReturnValue().Set(result);
66 void AppBindings::GetDetails(
67 const v8::FunctionCallbackInfo<v8::Value>& args) {
68 blink::WebLocalFrame* web_frame = context()->web_frame();
69 CHECK(web_frame);
70 args.GetReturnValue().Set(GetDetailsImpl(web_frame));
73 v8::Local<v8::Value> AppBindings::GetDetailsImpl(blink::WebLocalFrame* frame) {
74 v8::Isolate* isolate = frame->mainWorldScriptContext()->GetIsolate();
75 if (frame->document().securityOrigin().isUnique())
76 return v8::Null(isolate);
78 const Extension* extension =
79 RendererExtensionRegistry::Get()->GetExtensionOrAppByURL(
80 frame->document().url());
82 if (!extension)
83 return v8::Null(isolate);
85 scoped_ptr<base::DictionaryValue> manifest_copy(
86 extension->manifest()->value()->DeepCopy());
87 manifest_copy->SetString("id", extension->id());
88 scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create());
89 return converter->ToV8Value(manifest_copy.get(),
90 frame->mainWorldScriptContext());
93 void AppBindings::GetInstallState(
94 const v8::FunctionCallbackInfo<v8::Value>& args) {
95 // Get the callbackId.
96 int callback_id = 0;
97 if (args.Length() == 1) {
98 if (!args[0]->IsInt32()) {
99 context()->isolate()->ThrowException(v8::String::NewFromUtf8(
100 context()->isolate(), kInvalidCallbackIdError));
101 return;
103 callback_id = args[0]->Int32Value();
106 content::RenderFrame* render_frame = context()->GetRenderFrame();
107 CHECK(render_frame);
109 Send(new ExtensionHostMsg_GetAppInstallState(
110 render_frame->GetRoutingID(), context()->web_frame()->document().url(),
111 GetRoutingID(), callback_id));
114 void AppBindings::GetRunningState(
115 const v8::FunctionCallbackInfo<v8::Value>& args) {
116 // To distinguish between ready_to_run and cannot_run states, we need the app
117 // from the top frame.
118 blink::WebSecurityOrigin top_frame_security_origin =
119 context()->web_frame()->top()->securityOrigin();
120 const RendererExtensionRegistry* extensions =
121 RendererExtensionRegistry::Get();
123 // The app associated with the top level frame.
124 const Extension* top_app = extensions->GetHostedAppByURL(
125 GURL(top_frame_security_origin.toString().utf8()));
127 // The app associated with this frame.
128 const Extension* this_app = extensions->GetHostedAppByURL(
129 context()->web_frame()->document().url());
131 if (!this_app || !top_app) {
132 args.GetReturnValue().Set(v8::String::NewFromUtf8(
133 context()->isolate(), extension_misc::kAppStateCannotRun));
134 return;
137 const char* state = nullptr;
138 if (dispatcher_->IsExtensionActive(top_app->id())) {
139 if (top_app == this_app)
140 state = extension_misc::kAppStateRunning;
141 else
142 state = extension_misc::kAppStateCannotRun;
143 } else if (top_app == this_app) {
144 state = extension_misc::kAppStateReadyToRun;
145 } else {
146 state = extension_misc::kAppStateCannotRun;
149 args.GetReturnValue()
150 .Set(v8::String::NewFromUtf8(context()->isolate(), state));
153 bool AppBindings::OnMessageReceived(const IPC::Message& message) {
154 IPC_BEGIN_MESSAGE_MAP(AppBindings, message)
155 IPC_MESSAGE_HANDLER(ExtensionMsg_GetAppInstallStateResponse,
156 OnAppInstallStateResponse)
157 IPC_MESSAGE_UNHANDLED(CHECK(false) << "Unhandled IPC message")
158 IPC_END_MESSAGE_MAP()
159 return true;
162 void AppBindings::OnAppInstallStateResponse(
163 const std::string& state, int callback_id) {
164 v8::Isolate* isolate = context()->isolate();
165 v8::HandleScope handle_scope(isolate);
166 v8::Context::Scope context_scope(context()->v8_context());
167 v8::Local<v8::Value> argv[] = {
168 v8::String::NewFromUtf8(isolate, state.c_str()),
169 v8::Integer::New(isolate, callback_id)
171 context()->module_system()->CallModuleMethod(
172 "app", "onInstallStateResponse", arraysize(argv), argv);
175 } // namespace extensions