- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / app_current_window_internal / app_current_window_internal_api.cc
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.
4
5 #include "chrome/browser/extensions/api/app_current_window_internal/app_current_window_internal_api.h"
6
7 #include "apps/shell_window.h"
8 #include "apps/shell_window_registry.h"
9 #include "apps/ui/native_app_window.h"
10 #include "base/command_line.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/common/extensions/api/app_current_window_internal.h"
13 #include "chrome/common/extensions/api/app_window.h"
14 #include "chrome/common/extensions/features/feature_channel.h"
15 #include "chrome/common/extensions/features/simple_feature.h"
16 #include "extensions/common/switches.h"
17 #include "third_party/skia/include/core/SkRegion.h"
18
19 namespace app_current_window_internal =
20     extensions::api::app_current_window_internal;
21
22 namespace SetBounds = app_current_window_internal::SetBounds;
23 namespace SetMinWidth = app_current_window_internal::SetMinWidth;
24 namespace SetMinHeight = app_current_window_internal::SetMinHeight;
25 namespace SetMaxWidth = app_current_window_internal::SetMaxWidth;
26 namespace SetMaxHeight = app_current_window_internal::SetMaxHeight;
27 namespace SetIcon = app_current_window_internal::SetIcon;
28 namespace SetInputRegion = app_current_window_internal::SetInputRegion;
29 namespace SetAlwaysOnTop = app_current_window_internal::SetAlwaysOnTop;
30
31 using apps::ShellWindow;
32 using app_current_window_internal::Bounds;
33 using app_current_window_internal::Region;
34 using app_current_window_internal::RegionRect;
35
36 namespace extensions {
37
38 namespace {
39
40 const char kNoAssociatedShellWindow[] =
41     "The context from which the function was called did not have an "
42     "associated shell window.";
43
44 const char kDevChannelOnly[] =
45     "This function is currently only available in the Dev channel.";
46
47 const int kUnboundedSize = apps::ShellWindow::SizeConstraints::kUnboundedSize;
48
49 }  // namespace
50
51 bool AppCurrentWindowInternalExtensionFunction::RunImpl() {
52   apps::ShellWindowRegistry* registry =
53       apps::ShellWindowRegistry::Get(GetProfile());
54   DCHECK(registry);
55   content::RenderViewHost* rvh = render_view_host();
56   if (!rvh)
57     // No need to set an error, since we won't return to the caller anyway if
58     // there's no RVH.
59     return false;
60   ShellWindow* window = registry->GetShellWindowForRenderViewHost(rvh);
61   if (!window) {
62     error_ = kNoAssociatedShellWindow;
63     return false;
64   }
65   return RunWithWindow(window);
66 }
67
68 bool AppCurrentWindowInternalFocusFunction::RunWithWindow(ShellWindow* window) {
69   window->GetBaseWindow()->Activate();
70   return true;
71 }
72
73 bool AppCurrentWindowInternalFullscreenFunction::RunWithWindow(
74     ShellWindow* window) {
75   window->Fullscreen();
76   return true;
77 }
78
79 bool AppCurrentWindowInternalMaximizeFunction::RunWithWindow(
80     ShellWindow* window) {
81   window->Maximize();
82   return true;
83 }
84
85 bool AppCurrentWindowInternalMinimizeFunction::RunWithWindow(
86     ShellWindow* window) {
87   window->Minimize();
88   return true;
89 }
90
91 bool AppCurrentWindowInternalRestoreFunction::RunWithWindow(
92     ShellWindow* window) {
93   window->Restore();
94   return true;
95 }
96
97 bool AppCurrentWindowInternalDrawAttentionFunction::RunWithWindow(
98     ShellWindow* window) {
99   window->GetBaseWindow()->FlashFrame(true);
100   return true;
101 }
102
103 bool AppCurrentWindowInternalClearAttentionFunction::RunWithWindow(
104     ShellWindow* window) {
105   window->GetBaseWindow()->FlashFrame(false);
106   return true;
107 }
108
109 bool AppCurrentWindowInternalShowFunction::RunWithWindow(
110     ShellWindow* window) {
111   window->GetBaseWindow()->Show();
112   return true;
113 }
114
115 bool AppCurrentWindowInternalHideFunction::RunWithWindow(
116     ShellWindow* window) {
117   window->GetBaseWindow()->Hide();
118   return true;
119 }
120
121 bool AppCurrentWindowInternalSetBoundsFunction::RunWithWindow(
122     ShellWindow* window) {
123   // Start with the current bounds, and change any values that are specified in
124   // the incoming parameters.
125   gfx::Rect bounds = window->GetClientBounds();
126   scoped_ptr<SetBounds::Params> params(SetBounds::Params::Create(*args_));
127   CHECK(params.get());
128   if (params->bounds.left)
129     bounds.set_x(*(params->bounds.left));
130   if (params->bounds.top)
131     bounds.set_y(*(params->bounds.top));
132   if (params->bounds.width)
133     bounds.set_width(*(params->bounds.width));
134   if (params->bounds.height)
135     bounds.set_height(*(params->bounds.height));
136
137   bounds.Inset(-window->GetBaseWindow()->GetFrameInsets());
138   window->GetBaseWindow()->SetBounds(bounds);
139   return true;
140 }
141
142 bool AppCurrentWindowInternalSetMinWidthFunction::RunWithWindow(
143     ShellWindow* window) {
144   scoped_ptr<SetMinWidth::Params> params(SetMinWidth::Params::Create(*args_));
145   CHECK(params.get());
146   gfx::Size min_size = window->size_constraints().GetMinimumSize();
147   min_size.set_width(params->min_width.get() ?
148       *(params->min_width) : kUnboundedSize);
149   window->SetMinimumSize(min_size);
150   return true;
151 }
152
153 bool AppCurrentWindowInternalSetMinHeightFunction::RunWithWindow(
154     ShellWindow* window) {
155   scoped_ptr<SetMinHeight::Params> params(SetMinHeight::Params::Create(*args_));
156   CHECK(params.get());
157   gfx::Size min_size = window->size_constraints().GetMinimumSize();
158   min_size.set_height(params->min_height.get() ?
159       *(params->min_height) : kUnboundedSize);
160   window->SetMinimumSize(min_size);
161   return true;
162 }
163
164 bool AppCurrentWindowInternalSetMaxWidthFunction::RunWithWindow(
165     ShellWindow* window) {
166   scoped_ptr<SetMaxWidth::Params> params(SetMaxWidth::Params::Create(*args_));
167   CHECK(params.get());
168   gfx::Size max_size = window->size_constraints().GetMaximumSize();
169   max_size.set_width(params->max_width.get() ?
170       *(params->max_width) : kUnboundedSize);
171   window->SetMaximumSize(max_size);
172   return true;
173 }
174
175 bool AppCurrentWindowInternalSetMaxHeightFunction::RunWithWindow(
176     ShellWindow* window) {
177   scoped_ptr<SetMaxHeight::Params> params(SetMaxHeight::Params::Create(*args_));
178   CHECK(params.get());
179   gfx::Size max_size = window->size_constraints().GetMaximumSize();
180   max_size.set_height(params->max_height.get() ?
181       *(params->max_height) : kUnboundedSize);
182   window->SetMaximumSize(max_size);
183   return true;
184 }
185
186 bool AppCurrentWindowInternalSetIconFunction::RunWithWindow(
187     ShellWindow* window) {
188   if (GetCurrentChannel() > chrome::VersionInfo::CHANNEL_DEV &&
189       GetExtension()->location() != extensions::Manifest::COMPONENT) {
190     error_ = kDevChannelOnly;
191     return false;
192   }
193
194   scoped_ptr<SetIcon::Params> params(SetIcon::Params::Create(*args_));
195   CHECK(params.get());
196   // The |icon_url| parameter may be a blob url (e.g. an image fetched with an
197   // XMLHttpRequest) or a resource url.
198   GURL url(params->icon_url);
199   if (!url.is_valid())
200     url = GetExtension()->GetResourceURL(params->icon_url);
201
202   window->SetAppIconUrl(url);
203   return true;
204 }
205
206 bool AppCurrentWindowInternalSetInputRegionFunction::RunWithWindow(
207     ShellWindow* window) {
208
209   const char* whitelist[] = {
210     "EBA908206905323CECE6DC4B276A58A0F4AC573F",
211     "2775E568AC98F9578791F1EAB65A1BF5F8CEF414",
212     "4AA3C5D69A4AECBD236CAD7884502209F0F5C169",
213     "E410CDAB2C6E6DD408D731016CECF2444000A912",
214     "9E930B2B5EABA6243AE6C710F126E54688E8FAF6"
215   };
216   if (GetCurrentChannel() > chrome::VersionInfo::CHANNEL_DEV &&
217       !SimpleFeature::IsIdInWhitelist(
218           GetExtension()->id(),
219           std::set<std::string>(whitelist,
220                                 whitelist + arraysize(whitelist)))) {
221     error_ = kDevChannelOnly;
222     return false;
223   }
224
225   scoped_ptr<SetInputRegion::Params> params(
226       SetInputRegion::Params::Create(*args_));
227   const Region& inputRegion = params->region;
228
229   // Build a region from the supplied list of rects.
230   // If |rects| is missing, then the input region is removed. This clears the
231   // input region so that the entire window accepts input events.
232   // To specify an empty input region (so the window ignores all input),
233   // |rects| should be an empty list.
234   scoped_ptr<SkRegion> region(new SkRegion);
235   if (inputRegion.rects) {
236     for (std::vector<linked_ptr<RegionRect> >::const_iterator i =
237              inputRegion.rects->begin();
238          i != inputRegion.rects->end();
239          ++i) {
240       const RegionRect& inputRect = **i;
241       int32_t x = inputRect.left;
242       int32_t y = inputRect.top;
243       int32_t width = inputRect.width;
244       int32_t height = inputRect.height;
245
246       SkIRect rect = SkIRect::MakeXYWH(x, y, width, height);
247       region->op(rect, SkRegion::kUnion_Op);
248     }
249   } else {
250     region.reset(NULL);
251   }
252
253   window->UpdateInputRegion(region.Pass());
254
255   return true;
256 }
257
258 bool AppCurrentWindowInternalSetAlwaysOnTopFunction::RunWithWindow(
259     ShellWindow* window) {
260   scoped_ptr<SetAlwaysOnTop::Params> params(
261       SetAlwaysOnTop::Params::Create(*args_));
262   CHECK(params.get());
263   window->GetBaseWindow()->SetAlwaysOnTop(params->always_on_top);
264   return true;
265 }
266
267 }  // namespace extensions