Upstream version 10.38.222.0
[platform/framework/web/crosswalk.git] / src / extensions / shell / browser / api / shell / shell_api.cc
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
3 // found in the LICENSE file.
4
5 #include "extensions/shell/browser/api/shell/shell_api.h"
6
7 #include "base/memory/scoped_ptr.h"
8 #include "base/values.h"
9 #include "extensions/common/extension.h"
10 #include "extensions/shell/browser/shell_app_window.h"
11 #include "extensions/shell/browser/shell_desktop_controller.h"
12 #include "extensions/shell/common/api/shell.h"
13
14 using base::DictionaryValue;
15
16 namespace CreateWindow = extensions::shell_api::shell::CreateWindow;
17
18 namespace extensions {
19
20 namespace {
21
22 const char kInvalidArguments[] = "Invalid arguments";
23
24 // Creates a function call result to send to the renderer.
25 DictionaryValue* CreateResult(ShellAppWindow* app_window) {
26   int view_id = app_window->GetRenderViewRoutingID();
27
28   DictionaryValue* result = new DictionaryValue;
29   result->Set("viewId", new base::FundamentalValue(view_id));
30   return result;
31 }
32
33 }  // namespace
34
35 ShellCreateWindowFunction::ShellCreateWindowFunction() {
36 }
37
38 ShellCreateWindowFunction::~ShellCreateWindowFunction() {
39 }
40
41 ExtensionFunction::ResponseAction ShellCreateWindowFunction::Run() {
42   scoped_ptr<CreateWindow::Params> params(CreateWindow::Params::Create(*args_));
43   EXTENSION_FUNCTION_VALIDATE(params.get());
44
45   // Convert "main.html" to "chrome-extension:/<id>/main.html".
46   GURL url = extension()->GetResourceURL(params->url);
47   if (!url.is_valid())
48     return RespondNow(Error(kInvalidArguments));
49
50   // The desktop keeps ownership of the window.
51   ShellAppWindow* app_window =
52       ShellDesktopController::instance()->CreateAppWindow(browser_context());
53   app_window->LoadURL(url);
54
55   // Create the reply to send to the renderer.
56   return RespondNow(OneArgument(CreateResult(app_window)));
57 }
58
59 }  // namespace extensions