message_center_settings_controller.cc revision 7d4cd473f85ac64c3747c96c277f9e506a0d2246
1// Copyright (c) 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/notifications/message_center_settings_controller.h"
6
7#include <algorithm>
8
9#include "base/i18n/string_compare.h"
10#include "base/strings/utf_string_conversions.h"
11#include "chrome/browser/extensions/app_icon_loader_impl.h"
12#include "chrome/browser/extensions/extension_service.h"
13#include "chrome/browser/favicon/favicon_service.h"
14#include "chrome/browser/favicon/favicon_service_factory.h"
15#include "chrome/browser/favicon/favicon_types.h"
16#include "chrome/browser/history/history_types.h"
17#include "chrome/browser/notifications/desktop_notification_service.h"
18#include "chrome/browser/notifications/desktop_notification_service_factory.h"
19#include "chrome/browser/profiles/profile.h"
20#include "chrome/browser/profiles/profile_manager.h"
21#include "chrome/common/cancelable_task_tracker.h"
22#include "chrome/common/extensions/extension_constants.h"
23#include "grit/theme_resources.h"
24#include "grit/ui_strings.h"
25#include "ui/base/l10n/l10n_util.h"
26#include "ui/base/resource/resource_bundle.h"
27#include "ui/gfx/image/image.h"
28#include "ui/message_center/message_center_style.h"
29
30using message_center::Notifier;
31
32namespace {
33
34class NotifierComparator {
35 public:
36  explicit NotifierComparator(icu::Collator* collator) : collator_(collator) {}
37
38  bool operator() (Notifier* n1, Notifier* n2) {
39    return base::i18n::CompareString16WithCollator(
40        collator_, n1->name, n2->name) == UCOL_LESS;
41  }
42
43 private:
44  icu::Collator* collator_;
45};
46
47bool SimpleCompareNotifiers(Notifier* n1, Notifier* n2) {
48  return n1->name < n2->name;
49}
50
51}  // namespace
52
53MessageCenterSettingsController::MessageCenterSettingsController()
54    : delegate_(NULL) {
55}
56
57MessageCenterSettingsController::~MessageCenterSettingsController() {
58}
59
60message_center::NotifierSettingsDelegate*
61MessageCenterSettingsController::ShowSettingsDialog(gfx::NativeView context) {
62  delegate_ = message_center::ShowSettings(this, context);
63  return delegate_;
64}
65
66void MessageCenterSettingsController::GetNotifierList(
67    std::vector<Notifier*>* notifiers) {
68  DCHECK(notifiers);
69  // TODO(mukai): Fix this for multi-profile.
70  Profile* profile = ProfileManager::GetDefaultProfile();
71  DesktopNotificationService* notification_service =
72      DesktopNotificationServiceFactory::GetForProfile(profile);
73
74  UErrorCode error;
75  scoped_ptr<icu::Collator> collator(icu::Collator::createInstance(error));
76  scoped_ptr<NotifierComparator> comparator;
77  if (!U_FAILURE(error))
78    comparator.reset(new NotifierComparator(collator.get()));
79
80  ExtensionService* extension_service = profile->GetExtensionService();
81  const ExtensionSet* extension_set = extension_service->extensions();
82  // The extension icon size has to be 32x32 at least to load bigger icons if
83  // the icon doesn't exist for the specified size, and in that case it falls
84  // back to the default icon. The fetched icon will be resized in the settings
85  // dialog. See chrome/browser/extensions/extension_icon_image.cc and
86  // crbug.com/222931
87  app_icon_loader_.reset(new extensions::AppIconLoaderImpl(
88      profile, extension_misc::EXTENSION_ICON_SMALL, this));
89  for (ExtensionSet::const_iterator iter = extension_set->begin();
90       iter != extension_set->end(); ++iter) {
91    const extensions::Extension* extension = iter->get();
92    if (!extension->HasAPIPermission(
93            extensions::APIPermission::kNotification)) {
94      continue;
95    }
96
97    notifiers->push_back(new message_center::Notifier(
98        extension->id(),
99        UTF8ToUTF16(extension->name()),
100        notification_service->IsExtensionEnabled(extension->id())));
101    app_icon_loader_->FetchImage(extension->id());
102  }
103  if (comparator)
104    std::sort(notifiers->begin(), notifiers->end(), *comparator);
105  else
106    std::sort(notifiers->begin(), notifiers->end(), SimpleCompareNotifiers);
107  int app_count = notifiers->size();
108
109  ContentSettingsForOneType settings;
110  notification_service->GetNotificationsSettings(&settings);
111  FaviconService* favicon_service = FaviconServiceFactory::GetForProfile(
112      profile, Profile::EXPLICIT_ACCESS);
113  favicon_tracker_.reset(new CancelableTaskTracker());
114  patterns_.clear();
115  for (ContentSettingsForOneType::const_iterator iter = settings.begin();
116       iter != settings.end(); ++iter) {
117    if (iter->primary_pattern == ContentSettingsPattern::Wildcard() &&
118        iter->secondary_pattern == ContentSettingsPattern::Wildcard() &&
119        iter->source != "preference") {
120      continue;
121    }
122
123    std::string url_pattern = iter->primary_pattern.ToString();
124    string16 name = UTF8ToUTF16(url_pattern);
125    GURL url(url_pattern);
126    notifiers->push_back(new message_center::Notifier(
127        url,
128        name,
129        notification_service->GetContentSetting(url) == CONTENT_SETTING_ALLOW));
130    patterns_[name] = iter->primary_pattern;
131    FaviconService::FaviconForURLParams favicon_params(
132        profile, url, chrome::FAVICON | chrome::TOUCH_ICON,
133        message_center::kSettingsIconSize);
134    // Note that favicon service obtains the favicon from history. This means
135    // that it will fail to obtain the image if there are no history data for
136    // that URL.
137    favicon_service->GetFaviconImageForURL(
138        favicon_params,
139        base::Bind(&MessageCenterSettingsController::OnFaviconLoaded,
140                   base::Unretained(this), url),
141        favicon_tracker_.get());
142  }
143
144  // Screenshot notification feature is only for ChromeOS. See crbug.com/238358
145#if defined(OS_CHROMEOS)
146  const string16 screenshot_name =
147      l10n_util::GetStringUTF16(IDS_MESSAGE_CENTER_NOTIFIER_SCREENSHOT_NAME);
148  message_center::Notifier* const screenshot_notifier =
149      new message_center::Notifier(
150          message_center::Notifier::SCREENSHOT,
151          screenshot_name,
152          notification_service->IsSystemComponentEnabled(
153              message_center::Notifier::SCREENSHOT));
154  screenshot_notifier->icon =
155      ui::ResourceBundle::GetSharedInstance().GetImageNamed(
156          IDR_SCREENSHOT_NOTIFICATION_ICON);
157  notifiers->push_back(screenshot_notifier);
158#endif
159
160  if (comparator) {
161    std::sort(notifiers->begin() + app_count, notifiers->end(), *comparator);
162  } else {
163    std::sort(notifiers->begin() + app_count, notifiers->end(),
164              SimpleCompareNotifiers);
165  }
166}
167
168void MessageCenterSettingsController::SetNotifierEnabled(
169    const Notifier& notifier,
170    bool enabled) {
171  // TODO(mukai): Fix this for multi-profile.
172  Profile* profile = ProfileManager::GetDefaultProfile();
173  DesktopNotificationService* notification_service =
174      DesktopNotificationServiceFactory::GetForProfile(profile);
175
176  switch (notifier.type) {
177    case Notifier::APPLICATION:
178      notification_service->SetExtensionEnabled(notifier.id, enabled);
179      break;
180    case Notifier::WEB_PAGE: {
181      ContentSetting default_setting =
182          notification_service->GetDefaultContentSetting(NULL);
183      DCHECK(default_setting == CONTENT_SETTING_ALLOW ||
184             default_setting == CONTENT_SETTING_BLOCK ||
185             default_setting == CONTENT_SETTING_ASK);
186      if ((enabled && default_setting != CONTENT_SETTING_ALLOW) ||
187          (!enabled && default_setting == CONTENT_SETTING_ALLOW)) {
188        if (notifier.url.is_valid()) {
189          if (enabled)
190            notification_service->GrantPermission(notifier.url);
191          else
192            notification_service->DenyPermission(notifier.url);
193        } else {
194          LOG(ERROR) << "Invalid url pattern: " << notifier.url.spec();
195        }
196      } else {
197        std::map<string16, ContentSettingsPattern>::const_iterator iter =
198            patterns_.find(notifier.name);
199        if (iter != patterns_.end()) {
200          notification_service->ClearSetting(iter->second);
201        } else {
202          LOG(ERROR) << "Invalid url pattern: " << notifier.url.spec();
203        }
204      }
205      break;
206    }
207    case message_center::Notifier::SYSTEM_COMPONENT:
208      notification_service->SetSystemComponentEnabled(
209          notifier.system_component_type, enabled);
210      break;
211  }
212}
213
214void MessageCenterSettingsController::OnNotifierSettingsClosing() {
215  delegate_ = NULL;
216  DCHECK(favicon_tracker_.get());
217  favicon_tracker_->TryCancelAll();
218  patterns_.clear();
219}
220
221void MessageCenterSettingsController::OnFaviconLoaded(
222    const GURL& url,
223    const chrome::FaviconImageResult& favicon_result) {
224  if (!delegate_)
225    return;
226  delegate_->UpdateFavicon(url, favicon_result.image);
227}
228
229
230void MessageCenterSettingsController::SetAppImage(const std::string& id,
231                                                  const gfx::ImageSkia& image) {
232  if (!delegate_)
233    return;
234  delegate_->UpdateIconImage(id, gfx::Image(image) );
235}
236