app_list_view_delegate.cc revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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/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/profiles/profile_manager.h"
10#include "chrome/browser/ui/app_list/app_list_controller_delegate.h"
11#include "chrome/browser/ui/app_list/apps_model_builder.h"
12#include "chrome/browser/ui/app_list/chrome_app_list_item.h"
13#include "chrome/browser/ui/app_list/chrome_signin_delegate.h"
14#include "chrome/browser/ui/app_list/search_builder.h"
15#include "chrome/browser/ui/browser_finder.h"
16#include "chrome/browser/ui/chrome_pages.h"
17#include "chrome/browser/ui/host_desktop.h"
18#include "chrome/browser/ui/webui/ntp/app_launcher_handler.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                                            controller_.get()));
44
45    signin_delegate_.reset(new ChromeSigninDelegate(profile_));
46
47#if defined(USE_ASH)
48    app_sync_ui_state_watcher_.reset(new AppSyncUIStateWatcher(profile_,
49                                                               model));
50#endif
51  } else {
52    apps_builder_.reset();
53    search_builder_.reset();
54#if defined(USE_ASH)
55    app_sync_ui_state_watcher_.reset();
56#endif
57  }
58}
59
60app_list::SigninDelegate* AppListViewDelegate::GetSigninDelegate() {
61  return signin_delegate_.get();
62}
63
64void AppListViewDelegate::ActivateAppListItem(
65    app_list::AppListItemModel* item,
66    int event_flags) {
67  content::RecordAction(content::UserMetricsAction("AppList_ClickOnApp"));
68  static_cast<ChromeAppListItem*>(item)->Activate(event_flags);
69}
70
71void AppListViewDelegate::StartSearch() {
72  if (search_builder_.get())
73    search_builder_->StartSearch();
74}
75
76void AppListViewDelegate::StopSearch() {
77  if (search_builder_.get())
78    search_builder_->StopSearch();
79}
80
81void AppListViewDelegate::OpenSearchResult(
82    const app_list::SearchResult& result,
83    int event_flags) {
84  if (search_builder_.get())
85    search_builder_->OpenResult(result, event_flags);
86}
87
88void AppListViewDelegate::InvokeSearchResultAction(
89    const app_list::SearchResult& result,
90    int action_index,
91    int event_flags) {
92  if (search_builder_.get())
93    search_builder_->InvokeResultAction(result, action_index, event_flags);
94}
95
96void AppListViewDelegate::Dismiss()  {
97  controller_->DismissView();
98}
99
100void AppListViewDelegate::ViewClosing() {
101  controller_->ViewClosing();
102}
103
104void AppListViewDelegate::ViewActivationChanged(bool active) {
105  controller_->ViewActivationChanged(active);
106}
107
108gfx::ImageSkia AppListViewDelegate::GetWindowIcon() {
109  return controller_->GetWindowIcon();
110}
111
112string16 AppListViewDelegate::GetCurrentUserName() {
113  ProfileInfoCache& cache =
114      g_browser_process->profile_manager()->GetProfileInfoCache();
115  size_t profile_index = cache.GetIndexOfProfileWithPath(profile_->GetPath());
116  if (profile_index != std::string::npos)
117    return cache.GetNameOfProfileAtIndex(profile_index);
118
119  return string16();
120}
121
122string16 AppListViewDelegate::GetCurrentUserEmail()  {
123  ProfileInfoCache& cache =
124      g_browser_process->profile_manager()->GetProfileInfoCache();
125  size_t profile_index = cache.GetIndexOfProfileWithPath(profile_->GetPath());
126  if (profile_index != std::string::npos)
127    return cache.GetUserNameOfProfileAtIndex(profile_index);
128
129  return string16();
130}
131
132void AppListViewDelegate::OpenSettings() {
133  ExtensionService* service = profile_->GetExtensionService();
134  DCHECK(service);
135  const extensions::Extension* extension = service->GetInstalledExtension(
136      extension_misc::kSettingsAppId);
137  DCHECK(extension);
138  controller_->ActivateApp(profile_, extension, 0);
139}
140
141void AppListViewDelegate::OpenFeedback() {
142  chrome::HostDesktopType desktop = chrome::GetHostDesktopTypeForNativeWindow(
143      controller_->GetAppListWindow());
144  Browser* browser = chrome::FindOrCreateTabbedBrowser(
145      profile_, desktop);
146  chrome::ShowFeedbackPage(browser, std::string(), std::string());
147}
148