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/browser/extensions/api/app_window/app_window_api.h"
7 #include "apps/app_window.h"
8 #include "apps/app_window_contents.h"
9 #include "apps/app_window_registry.h"
10 #include "apps/apps_client.h"
11 #include "apps/ui/native_app_window.h"
12 #include "base/command_line.h"
13 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/string_util.h"
15 #include "base/time/time.h"
16 #include "base/values.h"
17 #include "chrome/browser/devtools/devtools_window.h"
18 #include "chrome/common/extensions/api/app_window.h"
19 #include "chrome/common/extensions/features/feature_channel.h"
20 #include "content/public/browser/notification_registrar.h"
21 #include "content/public/browser/notification_types.h"
22 #include "content/public/browser/render_process_host.h"
23 #include "content/public/browser/render_view_host.h"
24 #include "content/public/browser/web_contents.h"
25 #include "content/public/common/url_constants.h"
26 #include "extensions/browser/extensions_browser_client.h"
27 #include "extensions/browser/image_util.h"
28 #include "extensions/common/switches.h"
29 #include "third_party/skia/include/core/SkColor.h"
30 #include "ui/base/ui_base_types.h"
31 #include "ui/gfx/rect.h"
34 using apps::AppWindow;
36 namespace app_window = extensions::api::app_window;
37 namespace Create = app_window::Create;
39 namespace extensions {
41 namespace app_window_constants {
42 const char kInvalidWindowId[] =
43 "The window id can not be more than 256 characters long.";
44 const char kInvalidColorSpecification[] =
45 "The color specification could not be parsed.";
46 const char kColorWithFrameNone[] = "Windows with no frame cannot have a color.";
47 const char kInactiveColorWithoutColor[] =
48 "frame.inactiveColor must be used with frame.color.";
49 const char kConflictingBoundsOptions[] =
50 "The $1 property cannot be specified for both inner and outer bounds.";
51 } // namespace app_window_constants
53 const char kNoneFrameOption[] = "none";
54 // TODO(benwells): Remove HTML titlebar injection.
55 const char kHtmlFrameOption[] = "experimental-html";
59 // Opens an inspector window and delays the response to the
60 // AppWindowCreateFunction until the DevToolsWindow has finished loading, and is
61 // ready to stop on breakpoints in the callback.
62 class DevToolsRestorer : public base::RefCounted<DevToolsRestorer> {
64 DevToolsRestorer(AppWindowCreateFunction* delayed_create_function,
65 content::RenderViewHost* created_view)
66 : delayed_create_function_(delayed_create_function) {
67 AddRef(); // Balanced in LoadCompleted.
68 DevToolsWindow* devtools_window =
69 DevToolsWindow::OpenDevToolsWindow(
71 DevToolsToggleAction::ShowConsole());
72 devtools_window->SetLoadCompletedCallback(
73 base::Bind(&DevToolsRestorer::LoadCompleted, this));
77 friend class base::RefCounted<DevToolsRestorer>;
78 ~DevToolsRestorer() {}
80 void LoadCompleted() {
81 delayed_create_function_->SendDelayedResponse();
85 scoped_refptr<AppWindowCreateFunction> delayed_create_function_;
88 // If the same property is specified for the inner and outer bounds, raise an
90 bool CheckBoundsConflict(const scoped_ptr<int>& inner_property,
91 const scoped_ptr<int>& outer_property,
92 const std::string& property_name,
94 if (inner_property.get() && outer_property.get()) {
95 std::vector<std::string> subst;
96 subst.push_back(property_name);
97 *error = ReplaceStringPlaceholders(
98 app_window_constants::kConflictingBoundsOptions, subst, NULL);
105 // Copy over the bounds specification properties from the API to the
106 // AppWindow::CreateParams.
108 const extensions::api::app_window::BoundsSpecification* input_spec,
109 apps::AppWindow::BoundsSpecification* create_spec) {
113 if (input_spec->left.get())
114 create_spec->bounds.set_x(*input_spec->left);
115 if (input_spec->top.get())
116 create_spec->bounds.set_y(*input_spec->top);
117 if (input_spec->width.get())
118 create_spec->bounds.set_width(*input_spec->width);
119 if (input_spec->height.get())
120 create_spec->bounds.set_height(*input_spec->height);
121 if (input_spec->min_width.get())
122 create_spec->minimum_size.set_width(*input_spec->min_width);
123 if (input_spec->min_height.get())
124 create_spec->minimum_size.set_height(*input_spec->min_height);
125 if (input_spec->max_width.get())
126 create_spec->maximum_size.set_width(*input_spec->max_width);
127 if (input_spec->max_height.get())
128 create_spec->maximum_size.set_height(*input_spec->max_height);
133 AppWindowCreateFunction::AppWindowCreateFunction()
134 : inject_html_titlebar_(false) {}
136 void AppWindowCreateFunction::SendDelayedResponse() {
140 bool AppWindowCreateFunction::RunAsync() {
141 // Don't create app window if the system is shutting down.
142 if (extensions::ExtensionsBrowserClient::Get()->IsShuttingDown())
145 scoped_ptr<Create::Params> params(Create::Params::Create(*args_));
146 EXTENSION_FUNCTION_VALIDATE(params.get());
148 GURL url = GetExtension()->GetResourceURL(params->url);
149 // Allow absolute URLs for component apps, otherwise prepend the extension
151 if (GetExtension()->location() == extensions::Manifest::COMPONENT) {
152 GURL absolute = GURL(params->url);
153 if (absolute.has_scheme())
157 // TODO(jeremya): figure out a way to pass the opening WebContents through to
158 // AppWindow::Create so we can set the opener at create time rather than
159 // with a hack in AppWindowCustomBindings::GetView().
160 AppWindow::CreateParams create_params;
161 app_window::CreateWindowOptions* options = params->options.get();
163 if (options->id.get()) {
164 // TODO(mek): use URL if no id specified?
165 // Limit length of id to 256 characters.
166 if (options->id->length() > 256) {
167 error_ = app_window_constants::kInvalidWindowId;
171 create_params.window_key = *options->id;
173 if (options->singleton && *options->singleton == false) {
175 content::CONSOLE_MESSAGE_LEVEL_WARNING,
176 "The 'singleton' option in chrome.apps.window.create() is deprecated!"
177 " Change your code to no longer rely on this.");
180 if (!options->singleton || *options->singleton) {
181 AppWindow* window = apps::AppWindowRegistry::Get(browser_context())
182 ->GetAppWindowForAppAndKey(
183 extension_id(), create_params.window_key);
185 content::RenderViewHost* created_view =
186 window->web_contents()->GetRenderViewHost();
187 int view_id = MSG_ROUTING_NONE;
188 if (render_view_host_->GetProcess()->GetID() ==
189 created_view->GetProcess()->GetID()) {
190 view_id = created_view->GetRoutingID();
193 if (options->focused.get() && !*options->focused.get())
194 window->Show(AppWindow::SHOW_INACTIVE);
196 window->Show(AppWindow::SHOW_ACTIVE);
198 base::DictionaryValue* result = new base::DictionaryValue;
199 result->Set("viewId", new base::FundamentalValue(view_id));
200 window->GetSerializedState(result);
201 result->SetBoolean("existingWindow", true);
202 // TODO(benwells): Remove HTML titlebar injection.
203 result->SetBoolean("injectTitlebar", false);
211 if (!GetBoundsSpec(*options, &create_params, &error_))
214 if (GetCurrentChannel() <= chrome::VersionInfo::CHANNEL_DEV ||
215 GetExtension()->location() == extensions::Manifest::COMPONENT) {
216 if (options->type == extensions::api::app_window::WINDOW_TYPE_PANEL) {
217 create_params.window_type = AppWindow::WINDOW_TYPE_PANEL;
221 if (!GetFrameOptions(*options, &create_params))
224 if (options->transparent_background.get() &&
225 (GetExtension()->HasAPIPermission(APIPermission::kExperimental) ||
226 CommandLine::ForCurrentProcess()->HasSwitch(
227 switches::kEnableExperimentalExtensionApis))) {
228 create_params.transparent_background = *options->transparent_background;
231 if (options->hidden.get())
232 create_params.hidden = *options->hidden.get();
234 if (options->resizable.get())
235 create_params.resizable = *options->resizable.get();
237 if (options->always_on_top.get() &&
238 GetExtension()->HasAPIPermission(APIPermission::kAlwaysOnTopWindows))
239 create_params.always_on_top = *options->always_on_top.get();
241 if (options->focused.get())
242 create_params.focused = *options->focused.get();
244 if (options->type != extensions::api::app_window::WINDOW_TYPE_PANEL) {
245 switch (options->state) {
246 case extensions::api::app_window::STATE_NONE:
247 case extensions::api::app_window::STATE_NORMAL:
249 case extensions::api::app_window::STATE_FULLSCREEN:
250 create_params.state = ui::SHOW_STATE_FULLSCREEN;
252 case extensions::api::app_window::STATE_MAXIMIZED:
253 create_params.state = ui::SHOW_STATE_MAXIMIZED;
255 case extensions::api::app_window::STATE_MINIMIZED:
256 create_params.state = ui::SHOW_STATE_MINIMIZED;
262 create_params.creator_process_id =
263 render_view_host_->GetProcess()->GetID();
265 AppWindow* app_window = apps::AppsClient::Get()->CreateAppWindow(
266 browser_context(), GetExtension());
268 url, new apps::AppWindowContentsImpl(app_window), create_params);
270 if (ExtensionsBrowserClient::Get()->IsRunningInForcedAppMode())
271 app_window->ForcedFullscreen();
273 content::RenderViewHost* created_view =
274 app_window->web_contents()->GetRenderViewHost();
275 int view_id = MSG_ROUTING_NONE;
276 if (create_params.creator_process_id == created_view->GetProcess()->GetID())
277 view_id = created_view->GetRoutingID();
279 base::DictionaryValue* result = new base::DictionaryValue;
280 result->Set("viewId", new base::FundamentalValue(view_id));
281 result->Set("injectTitlebar",
282 new base::FundamentalValue(inject_html_titlebar_));
283 result->Set("id", new base::StringValue(app_window->window_key()));
284 app_window->GetSerializedState(result);
287 if (apps::AppWindowRegistry::Get(browser_context())
288 ->HadDevToolsAttached(created_view)) {
289 new DevToolsRestorer(this, created_view);
297 bool AppWindowCreateFunction::GetBoundsSpec(
298 const extensions::api::app_window::CreateWindowOptions& options,
299 apps::AppWindow::CreateParams* params,
300 std::string* error) {
304 if (options.inner_bounds.get() || options.outer_bounds.get()) {
305 // Parse the inner and outer bounds specifications. If developers use the
306 // new API, the deprecated fields will be ignored - do not attempt to merge
309 const extensions::api::app_window::BoundsSpecification* inner_bounds =
310 options.inner_bounds.get();
311 const extensions::api::app_window::BoundsSpecification* outer_bounds =
312 options.outer_bounds.get();
313 if (inner_bounds && outer_bounds) {
314 if (!CheckBoundsConflict(
315 inner_bounds->left, outer_bounds->left, "left", error)) {
318 if (!CheckBoundsConflict(
319 inner_bounds->top, outer_bounds->top, "top", error)) {
322 if (!CheckBoundsConflict(
323 inner_bounds->width, outer_bounds->width, "width", error)) {
326 if (!CheckBoundsConflict(
327 inner_bounds->height, outer_bounds->height, "height", error)) {
330 if (!CheckBoundsConflict(inner_bounds->min_width,
331 outer_bounds->min_width,
336 if (!CheckBoundsConflict(inner_bounds->min_height,
337 outer_bounds->min_height,
342 if (!CheckBoundsConflict(inner_bounds->max_width,
343 outer_bounds->max_width,
348 if (!CheckBoundsConflict(inner_bounds->max_height,
349 outer_bounds->max_height,
356 CopyBoundsSpec(inner_bounds, &(params->content_spec));
357 CopyBoundsSpec(outer_bounds, &(params->window_spec));
359 // Parse deprecated fields.
360 // Due to a bug in NativeAppWindow::GetFrameInsets() on Windows and ChromeOS
361 // the bounds set the position of the window and the size of the content.
362 // This will be preserved as apps may be relying on this behavior.
364 if (options.default_width.get())
365 params->content_spec.bounds.set_width(*options.default_width.get());
366 if (options.default_height.get())
367 params->content_spec.bounds.set_height(*options.default_height.get());
368 if (options.default_left.get())
369 params->window_spec.bounds.set_x(*options.default_left.get());
370 if (options.default_top.get())
371 params->window_spec.bounds.set_y(*options.default_top.get());
373 if (options.width.get())
374 params->content_spec.bounds.set_width(*options.width.get());
375 if (options.height.get())
376 params->content_spec.bounds.set_height(*options.height.get());
377 if (options.left.get())
378 params->window_spec.bounds.set_x(*options.left.get());
379 if (options.top.get())
380 params->window_spec.bounds.set_y(*options.top.get());
382 if (options.bounds.get()) {
383 app_window::ContentBounds* bounds = options.bounds.get();
384 if (bounds->width.get())
385 params->content_spec.bounds.set_width(*bounds->width.get());
386 if (bounds->height.get())
387 params->content_spec.bounds.set_height(*bounds->height.get());
388 if (bounds->left.get())
389 params->window_spec.bounds.set_x(*bounds->left.get());
390 if (bounds->top.get())
391 params->window_spec.bounds.set_y(*bounds->top.get());
394 gfx::Size& minimum_size = params->content_spec.minimum_size;
395 if (options.min_width.get())
396 minimum_size.set_width(*options.min_width);
397 if (options.min_height.get())
398 minimum_size.set_height(*options.min_height);
399 gfx::Size& maximum_size = params->content_spec.maximum_size;
400 if (options.max_width.get())
401 maximum_size.set_width(*options.max_width);
402 if (options.max_height.get())
403 maximum_size.set_height(*options.max_height);
409 AppWindow::Frame AppWindowCreateFunction::GetFrameFromString(
410 const std::string& frame_string) {
411 if (frame_string == kHtmlFrameOption &&
412 (GetExtension()->HasAPIPermission(APIPermission::kExperimental) ||
413 CommandLine::ForCurrentProcess()->HasSwitch(
414 switches::kEnableExperimentalExtensionApis))) {
415 inject_html_titlebar_ = true;
416 return AppWindow::FRAME_NONE;
419 if (frame_string == kNoneFrameOption)
420 return AppWindow::FRAME_NONE;
422 return AppWindow::FRAME_CHROME;
425 bool AppWindowCreateFunction::GetFrameOptions(
426 const app_window::CreateWindowOptions& options,
427 AppWindow::CreateParams* create_params) {
431 DCHECK(options.frame->as_string || options.frame->as_frame_options);
432 if (options.frame->as_string) {
433 create_params->frame = GetFrameFromString(*options.frame->as_string);
437 if (options.frame->as_frame_options->type)
438 create_params->frame =
439 GetFrameFromString(*options.frame->as_frame_options->type);
441 if (options.frame->as_frame_options->color.get()) {
442 if (create_params->frame != AppWindow::FRAME_CHROME) {
443 error_ = app_window_constants::kColorWithFrameNone;
447 if (!image_util::ParseCSSColorString(
448 *options.frame->as_frame_options->color,
449 &create_params->active_frame_color)) {
450 error_ = app_window_constants::kInvalidColorSpecification;
454 create_params->has_frame_color = true;
455 create_params->inactive_frame_color = create_params->active_frame_color;
457 if (options.frame->as_frame_options->inactive_color.get()) {
458 if (!image_util::ParseCSSColorString(
459 *options.frame->as_frame_options->inactive_color,
460 &create_params->inactive_frame_color)) {
461 error_ = app_window_constants::kInvalidColorSpecification;
469 if (options.frame->as_frame_options->inactive_color.get()) {
470 error_ = app_window_constants::kInactiveColorWithoutColor;
477 } // namespace extensions