app_list_view_delegate.cc revision a93a17c8d99d686bd4a1511e5504e5e6cc9fcadf
169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal// Copyright (c) 2012 The Chromium Authors. All rights reserved.
269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal// Use of this source code is governed by a BSD-style license that can be
369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal// found in the LICENSE file.
469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal#include "chrome/browser/ui/app_list/app_list_view_delegate.h"
6
7#include "chrome/browser/browser_process.h"
8#include "chrome/browser/extensions/extension_service.h"
9#include "chrome/browser/feedback/feedback_util.h"
10#include "chrome/browser/profiles/profile_manager.h"
11#include "chrome/browser/ui/app_list/app_list_controller_delegate.h"
12#include "chrome/browser/ui/app_list/apps_model_builder.h"
13#include "chrome/browser/ui/app_list/chrome_app_list_item.h"
14#include "chrome/browser/ui/app_list/chrome_signin_delegate.h"
15#include "chrome/browser/ui/app_list/search_builder.h"
16#include "chrome/browser/ui/browser_finder.h"
17#include "chrome/browser/ui/chrome_pages.h"
18#include "chrome/browser/ui/host_desktop.h"
19#include "chrome/common/extensions/extension_constants.h"
20#include "content/public/browser/user_metrics.h"
21
22#if defined(USE_ASH)
23#include "chrome/browser/ui/ash/app_list/app_sync_ui_state_watcher.h"
24#endif
25
26AppListViewDelegate::AppListViewDelegate(AppListControllerDelegate* controller,
27                                         Profile* profile)
28    : controller_(controller),
29      profile_(profile) {}
30
31AppListViewDelegate::~AppListViewDelegate() {}
32
33void AppListViewDelegate::SetModel(app_list::AppListModel* model) {
34  if (model) {
35    apps_builder_.reset(new AppsModelBuilder(profile_,
36                                             model->apps(),
37                                             controller_.get()));
38    apps_builder_->Build();
39
40    search_builder_.reset(new SearchBuilder(profile_,
41                                            model->search_box(),
42                                            model->results(),
43                                            apps_builder_.get(),
44                                            controller_.get()));
45
46    signin_delegate_.reset(new ChromeSigninDelegate(profile_));
47
48#if defined(USE_ASH)
49    app_sync_ui_state_watcher_.reset(new AppSyncUIStateWatcher(profile_,
50                                                               model));
51#endif
52  } else {
53    apps_builder_.reset();
54    search_builder_.reset();
55#if defined(USE_ASH)
56    app_sync_ui_state_watcher_.reset();
57#endif
58  }
59}
60
61app_list::SigninDelegate* AppListViewDelegate::GetSigninDelegate() {
62  return signin_delegate_.get();
63}
64
65void AppListViewDelegate::ActivateAppListItem(
66    app_list::AppListItemModel* item,
67    int event_flags) {
68  content::RecordAction(content::UserMetricsAction("AppList_ClickOnApp"));
69  static_cast<ChromeAppListItem*>(item)->Activate(event_flags);
70}
71
72void AppListViewDelegate::StartSearch() {
73  if (search_builder_.get())
74    search_builder_->StartSearch();
75}
76
77void AppListViewDelegate::StopSearch() {
78  if (search_builder_.get())
79    search_builder_->StopSearch();
80}
81
82void AppListViewDelegate::OpenSearchResult(
83    const app_list::SearchResult& result,
84    int event_flags) {
85  if (search_builder_.get())
86    search_builder_->OpenResult(result, event_flags);
87}
88
89void AppListViewDelegate::InvokeSearchResultAction(
90    const app_list::SearchResult& result,
91    int action_index,
92    int event_flags) {
93  if (search_builder_.get())
94    search_builder_->InvokeResultAction(result, action_index, event_flags);
95}
96
97void AppListViewDelegate::Dismiss()  {
98  controller_->DismissView();
99}
100
101void AppListViewDelegate::ViewClosing() {
102  controller_->ViewClosing();
103}
104
105void AppListViewDelegate::ViewActivationChanged(bool active) {
106  controller_->ViewActivationChanged(active);
107}
108
109gfx::ImageSkia AppListViewDelegate::GetWindowIcon() {
110  return controller_->GetWindowIcon();
111}
112
113string16 AppListViewDelegate::GetCurrentUserName() {
114  ProfileInfoCache& cache =
115      g_browser_process->profile_manager()->GetProfileInfoCache();
116  size_t profile_index = cache.GetIndexOfProfileWithPath(profile_->GetPath());
117  if (profile_index != std::string::npos)
118    return cache.GetNameOfProfileAtIndex(profile_index);
119
120  return string16();
121}
122
123string16 AppListViewDelegate::GetCurrentUserEmail()  {
124  ProfileInfoCache& cache =
125      g_browser_process->profile_manager()->GetProfileInfoCache();
126  size_t profile_index = cache.GetIndexOfProfileWithPath(profile_->GetPath());
127  if (profile_index != std::string::npos)
128    return cache.GetUserNameOfProfileAtIndex(profile_index);
129
130  return string16();
131}
132
133void AppListViewDelegate::OpenSettings() {
134  ExtensionService* service = profile_->GetExtensionService();
135  DCHECK(service);
136  const extensions::Extension* extension = service->GetInstalledExtension(
137      extension_misc::kSettingsAppId);
138  DCHECK(extension);
139  controller_->ActivateApp(profile_, extension, 0);
140}
141
142void AppListViewDelegate::OpenFeedback() {
143  chrome::HostDesktopType desktop = chrome::GetHostDesktopTypeForNativeWindow(
144      controller_->GetAppListWindow());
145  Browser* browser = chrome::FindOrCreateTabbedBrowser(
146      profile_, desktop);
147  chrome::ShowFeedbackPage(browser, std::string(),
148                           chrome::kAppLauncherCategoryTag);
149}
150