Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / views / app_list / linux / app_list_service_linux.cc
1 // Copyright 2013 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/ui/views/app_list/linux/app_list_service_linux.h"
6
7 #include "base/memory/singleton.h"
8 #include "chrome/browser/shell_integration.h"
9 #include "chrome/browser/shell_integration_linux.h"
10 #include "chrome/browser/ui/app_list/app_list_controller_delegate.h"
11 #include "chrome/browser/ui/app_list/app_list_factory.h"
12 #include "chrome/browser/ui/app_list/app_list_shower.h"
13 #include "chrome/browser/ui/app_list/app_list_view_delegate.h"
14 #include "chrome/browser/ui/app_list/keep_alive_service_impl.h"
15 #include "chrome/browser/ui/views/app_list/linux/app_list_controller_delegate_linux.h"
16 #include "chrome/browser/ui/views/app_list/linux/app_list_linux.h"
17 #include "content/public/browser/browser_thread.h"
18 #include "grit/chromium_strings.h"
19 #include "grit/google_chrome_strings.h"
20 #include "ui/app_list/app_list_constants.h"
21 #include "ui/app_list/views/app_list_view.h"
22 #include "ui/base/l10n/l10n_util.h"
23 #include "ui/gfx/screen.h"
24
25 namespace {
26
27 class AppListFactoryLinux : public AppListFactory {
28  public:
29   explicit AppListFactoryLinux(AppListServiceLinux* service)
30       : service_(service) {}
31   virtual ~AppListFactoryLinux() {}
32
33   virtual AppList* CreateAppList(
34       Profile* profile,
35       AppListService* service,
36       const base::Closure& on_should_dismiss) OVERRIDE {
37     // The view delegate will be owned by the app list view. The app list view
38     // manages it's own lifetime.
39     AppListViewDelegate* view_delegate = new AppListViewDelegate(
40         profile, service->GetControllerDelegate());
41     app_list::AppListView* view = new app_list::AppListView(view_delegate);
42     gfx::Point cursor = gfx::Screen::GetNativeScreen()->GetCursorScreenPoint();
43     view->InitAsBubbleAtFixedLocation(NULL,
44                                       &pagination_model_,
45                                       cursor,
46                                       views::BubbleBorder::FLOAT,
47                                       false /* border_accepts_events */);
48     return new AppListLinux(view, on_should_dismiss);
49   }
50
51  private:
52   // PaginationModel that is shared across all views.
53   app_list::PaginationModel pagination_model_;
54   AppListServiceLinux* service_;
55
56   DISALLOW_COPY_AND_ASSIGN(AppListFactoryLinux);
57 };
58
59 void CreateShortcuts() {
60   std::string app_list_title =
61       l10n_util::GetStringUTF8(IDS_APP_LIST_SHORTCUT_NAME);
62
63   if (!ShellIntegrationLinux::CreateAppListDesktopShortcut(
64            app_list::kAppListWMClass,
65            app_list_title)) {
66     LOG(WARNING) << "Unable to create App Launcher shortcut.";
67   }
68 }
69
70 }  // namespace
71
72 AppListServiceLinux::~AppListServiceLinux() {}
73
74 // static
75 AppListServiceLinux* AppListServiceLinux::GetInstance() {
76   return Singleton<AppListServiceLinux,
77                    LeakySingletonTraits<AppListServiceLinux> >::get();
78 }
79
80 void AppListServiceLinux::set_can_close(bool can_close) {
81   shower_->set_can_close(can_close);
82 }
83
84 void AppListServiceLinux::OnAppListClosing() {
85   shower_->CloseAppList();
86 }
87
88 void AppListServiceLinux::Init(Profile* initial_profile) {
89   HandleCommandLineFlags(initial_profile);
90 }
91
92 void AppListServiceLinux::CreateForProfile(Profile* requested_profile) {
93   shower_->CreateViewForProfile(requested_profile);
94 }
95
96 void AppListServiceLinux::ShowForProfile(Profile* requested_profile) {
97   DCHECK(requested_profile);
98   if (requested_profile->IsManaged())
99     return;
100
101   ScopedKeepAlive show_app_list_keepalive;
102
103   // TODO(mgiuca): Call SetDidRunForNDayActiveStats.
104
105   InvalidatePendingProfileLoads();
106   SetProfilePath(requested_profile->GetPath());
107   shower_->ShowForProfile(requested_profile);
108   RecordAppListLaunch();
109 }
110
111 void AppListServiceLinux::DismissAppList() {
112   shower_->DismissAppList();
113 }
114
115 bool AppListServiceLinux::IsAppListVisible() const {
116   return shower_->IsAppListVisible();
117 }
118
119 gfx::NativeWindow AppListServiceLinux::GetAppListWindow() {
120   return shower_->GetWindow();
121 }
122
123 Profile* AppListServiceLinux::GetCurrentAppListProfile() {
124   return shower_->profile();
125 }
126
127 AppListControllerDelegate* AppListServiceLinux::GetControllerDelegate() {
128   return controller_delegate_.get();
129 }
130
131 void AppListServiceLinux::CreateShortcut() {
132   content::BrowserThread::PostTask(
133       content::BrowserThread::FILE, FROM_HERE, base::Bind(&CreateShortcuts));
134 }
135
136 AppListServiceLinux::AppListServiceLinux()
137     : shower_(new AppListShower(
138           scoped_ptr<AppListFactory>(new AppListFactoryLinux(this)),
139           scoped_ptr<KeepAliveService>(new KeepAliveServiceImpl),
140           this)),
141       controller_delegate_(new AppListControllerDelegateLinux(this)) {
142 }
143
144 // static
145 AppListService* AppListService::Get(chrome::HostDesktopType desktop_type) {
146   return AppListServiceLinux::GetInstance();
147 }
148
149 // static
150 void AppListService::InitAll(Profile* initial_profile) {
151   AppListServiceLinux::GetInstance()->Init(initial_profile);
152 }