Upstream version 11.39.256.0
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / browser / runtime_ui_delegate.cc
1 // Copyright (c) 2014 Intel Corporation. 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 "xwalk/runtime/browser/runtime_ui_delegate.h"
6
7 #include "base/command_line.h"
8 #include "grit/xwalk_resources.h"
9 #include "ui/base/resource/resource_bundle.h"
10 #include "ui/gfx/image/image.h"
11 #include "xwalk/runtime/browser/image_util.h"
12 #include "xwalk/runtime/browser/runtime.h"
13 #include "xwalk/runtime/common/xwalk_switches.h"
14
15 namespace xwalk {
16 // FIXME : Need to figure out what code paths are used by Android and not
17 // compile the unneeded files.
18 #if !defined(OS_ANDROID)
19 namespace {
20 // The default size for web content area size.
21 const int kDefaultWidth = 840;
22 const int kDefaultHeight = 600;
23
24 NativeAppWindow* CreateWindow(
25     Runtime* runtime, const NativeAppWindow::CreateParams& params) {
26   NativeAppWindow* window = NativeAppWindow::Create(params);
27   // FIXME : Pass an App icon in params.
28   // Set the app icon if it is passed from command line.
29   CommandLine* command_line = CommandLine::ForCurrentProcess();
30   gfx::Image app_icon;
31   if (command_line->HasSwitch(switches::kAppIcon)) {
32     base::FilePath icon_file =
33         command_line->GetSwitchValuePath(switches::kAppIcon);
34     app_icon = xwalk_utils::LoadImageFromFilePath(icon_file);
35   } else {
36     // Otherwise, use the default icon for Crosswalk app.
37     ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
38     app_icon = rb.GetNativeImageNamed(IDR_XWALK_ICON_48);
39   }
40
41   window->UpdateIcon(app_icon);
42
43   unsigned int fullscreen_options = runtime->fullscreen_options();
44   if (params.state == ui::SHOW_STATE_FULLSCREEN)
45     fullscreen_options |= Runtime::FULLSCREEN_FOR_LAUNCH;
46   else
47     fullscreen_options &= ~Runtime::FULLSCREEN_FOR_LAUNCH;
48   runtime->set_fullscreen_options(fullscreen_options);
49
50   return window;
51 }
52
53 }  // namespace
54 #endif
55
56 RuntimeUIDelegate* DefaultRuntimeUIDelegate::Create(
57     Runtime* runtime, const NativeAppWindow::CreateParams& params) {
58   return new DefaultRuntimeUIDelegate(runtime, params);
59 }
60
61 DefaultRuntimeUIDelegate::DefaultRuntimeUIDelegate(
62     Runtime* runtime, const NativeAppWindow::CreateParams& params)
63   : runtime_(runtime),
64     window_params_(params),
65     window_(nullptr) {
66   DCHECK(runtime_);
67 }
68
69 DefaultRuntimeUIDelegate::~DefaultRuntimeUIDelegate() {
70 }
71
72 void DefaultRuntimeUIDelegate::Show() {
73 #if !defined(OS_ANDROID)
74   if (!window_) {
75     if (window_params_.bounds.IsEmpty())
76       window_params_.bounds = gfx::Rect(0, 0, kDefaultWidth, kDefaultHeight);
77     window_params_.delegate = this;
78     window_params_.web_contents = runtime_->web_contents();
79     window_ = CreateWindow(runtime_, window_params_);
80   }
81   window_->Show();
82 #else
83   NOTIMPLEMENTED();
84 #endif
85 }
86
87 void DefaultRuntimeUIDelegate::UpdateTitle(const base::string16& text) {
88   if (window_)
89     window_->UpdateTitle(text);
90 }
91
92 void DefaultRuntimeUIDelegate::UpdateIcon(const gfx::Image& image) {
93   if (window_)
94     window_->UpdateIcon(image);
95 }
96
97 void DefaultRuntimeUIDelegate::SetFullscreen(bool enter_fullscreen) {
98   if (window_)
99     window_->SetFullscreen(enter_fullscreen);
100 }
101
102 void DefaultRuntimeUIDelegate::Close() {
103   if (window_)
104     window_->Close();
105 }
106
107 void DefaultRuntimeUIDelegate::DeleteDelegate() {
108   runtime_ = nullptr;
109   if (window_) {
110     window_->Close();
111     return;
112   }
113   delete this;
114 }
115
116 void DefaultRuntimeUIDelegate::OnWindowDestroyed() {
117   window_ = nullptr;
118   if (runtime_) {
119     runtime_->Close();
120     return;
121   }
122   delete this;
123 }
124
125 }  // namespace xwalk