Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / ui / app_list / demo / app_list_demo_views.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 "base/bind.h"
6 #include "base/command_line.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/run_loop.h"
9 #include "content/public/browser/web_contents.h"
10 #include "content/public/common/content_switches.h"
11 #include "grit/ui_resources.h"
12 #include "ui/app_list/test/app_list_test_model.h"
13 #include "ui/app_list/test/app_list_test_view_delegate.h"
14 #include "ui/app_list/views/app_list_view.h"
15 #include "ui/base/resource/resource_bundle.h"
16 #include "ui/views/controls/webview/webview.h"
17 #include "ui/views_content_client/views_content_client.h"
18
19 #if defined(OS_WIN)
20 #include "content/public/app/startup_helper_win.h"
21 #include "sandbox/win/src/sandbox_types.h"
22 #endif
23
24 namespace {
25
26 class AppListDemoService;
27
28 // Number of dummy apps to populate in the app list.
29 const int kInitialItems = 20;
30
31 // Extends the test AppListViewDelegate to quit the run loop when the launcher
32 // window is closed, and to close the window if it is simply dismissed.
33 class DemoAppListViewDelegate : public app_list::test::AppListTestViewDelegate {
34  public:
35   explicit DemoAppListViewDelegate(content::BrowserContext* browser_context)
36       : view_(NULL), browser_context_(browser_context) {}
37   virtual ~DemoAppListViewDelegate() {}
38
39   app_list::AppListView* InitView(gfx::NativeWindow window_context);
40
41   // Overridden from AppListViewDelegate:
42   virtual void Dismiss() OVERRIDE;
43   virtual void ViewClosing() OVERRIDE;
44   virtual views::View* CreateStartPageWebView(const gfx::Size& size) OVERRIDE;
45
46  private:
47   app_list::AppListView* view_;  // Weak. Owns this.
48   content::BrowserContext* browser_context_;
49   scoped_ptr<content::WebContents> web_contents_;
50
51   DISALLOW_COPY_AND_ASSIGN(DemoAppListViewDelegate);
52 };
53
54 app_list::AppListView* DemoAppListViewDelegate::InitView(
55     gfx::NativeWindow window_context) {
56   gfx::NativeView container = NULL;
57   // On Ash, the app list is placed into an aura::Window container. For the demo
58   // use the root window context as the parent. This only works on Aura since an
59   // aura::Window is also a NativeView.
60 #if defined(USE_AURA)
61   container = window_context;
62 #endif
63
64   // Note AppListView takes ownership of |this| on the next line.
65   view_ = new app_list::AppListView(this);
66   view_->InitAsBubbleAtFixedLocation(container,
67                                      0,
68                                      gfx::Point(300, 300),
69                                      views::BubbleBorder::FLOAT,
70                                      false /* border_accepts_events */);
71
72   // Populate some apps.
73   GetTestModel()->PopulateApps(kInitialItems);
74   app_list::AppListItemList* item_list = GetTestModel()->top_level_item_list();
75   ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
76   gfx::Image test_image = rb.GetImageNamed(IDR_DEFAULT_FAVICON_32);
77   for (size_t i = 0; i < item_list->item_count(); ++i) {
78     app_list::AppListItem* item = item_list->item_at(i);
79     // Alternate images with shadows and images without.
80     item->SetIcon(*test_image.ToImageSkia(), i & 1);
81   }
82   return view_;
83 }
84
85 void DemoAppListViewDelegate::Dismiss() {
86   view_->GetWidget()->Close();
87 }
88
89 void DemoAppListViewDelegate::ViewClosing() {
90   web_contents_.reset();
91   view_ = NULL;
92   base::MessageLoopForUI::current()->Quit();
93 }
94
95 views::View* DemoAppListViewDelegate::CreateStartPageWebView(
96     const gfx::Size& size) {
97   web_contents_.reset(content::WebContents::Create(
98       content::WebContents::CreateParams(browser_context_)));
99   web_contents_->GetController().LoadURL(GURL("http://www.google.com/"),
100                                          content::Referrer(),
101                                          content::PAGE_TRANSITION_AUTO_TOPLEVEL,
102                                          std::string());
103   views::WebView* web_view = new views::WebView(
104       web_contents_->GetBrowserContext());
105   web_view->SetPreferredSize(size);
106   web_view->SetWebContents(web_contents_.get());
107   return web_view;
108 }
109
110 void ShowAppList(content::BrowserContext* browser_context,
111                  gfx::NativeWindow window_context) {
112   DemoAppListViewDelegate* delegate =
113       new DemoAppListViewDelegate(browser_context);
114   app_list::AppListView* view = delegate->InitView(window_context);
115   view->GetWidget()->Show();
116   view->GetWidget()->Activate();
117 }
118
119 }  // namespace
120
121 #if defined(OS_WIN)
122 int APIENTRY wWinMain(HINSTANCE instance, HINSTANCE, wchar_t*, int) {
123   sandbox::SandboxInterfaceInfo sandbox_info = {0};
124   content::InitializeSandboxInfo(&sandbox_info);
125   ui::ViewsContentClient views_content_client(instance, &sandbox_info);
126 #else
127 int main(int argc, const char** argv) {
128   ui::ViewsContentClient views_content_client(argc, argv);
129 #endif
130
131   views_content_client.set_task(base::Bind(&ShowAppList));
132   return views_content_client.RunMain();
133 }