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.
5 #include "extensions/shell/browser/api/shell/shell_api.h"
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"
14 using base::DictionaryValue;
16 namespace CreateWindow = extensions::shell_api::shell::CreateWindow;
18 namespace extensions {
22 const char kInvalidArguments[] = "Invalid arguments";
24 // Creates a function call result to send to the renderer.
25 DictionaryValue* CreateResult(ShellAppWindow* app_window) {
26 int view_id = app_window->GetRenderViewRoutingID();
28 DictionaryValue* result = new DictionaryValue;
29 result->Set("viewId", new base::FundamentalValue(view_id));
35 ShellCreateWindowFunction::ShellCreateWindowFunction() {
38 ShellCreateWindowFunction::~ShellCreateWindowFunction() {
41 ExtensionFunction::ResponseAction ShellCreateWindowFunction::Run() {
42 scoped_ptr<CreateWindow::Params> params(CreateWindow::Params::Create(*args_));
43 EXTENSION_FUNCTION_VALIDATE(params.get());
45 // Convert "main.html" to "chrome-extension:/<id>/main.html".
46 GURL url = extension()->GetResourceURL(params->url);
48 return RespondNow(Error(kInvalidArguments));
50 // The desktop keeps ownership of the window.
51 ShellAppWindow* app_window =
52 ShellDesktopController::instance()->CreateAppWindow(browser_context());
53 app_window->LoadURL(url);
55 // Create the reply to send to the renderer.
56 return RespondNow(OneArgument(CreateResult(app_window)));
59 } // namespace extensions