summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/extension_warning_set.cc
blob: 95225aec8666d5d958552852301b69d7255525d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/extensions/extension_warning_set.h"

#include "chrome/browser/browser_process.h"
#include "chrome/browser/extensions/extension_global_error_badge.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/ui/global_error_service.h"
#include "chrome/browser/ui/global_error_service_factory.h"
#include "chrome/common/chrome_notification_types.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/notification_service.h"
#include "grit/chromium_strings.h"
#include "grit/generated_resources.h"
#include "ui/base/l10n/l10n_util.h"

using content::BrowserThread;

// This class is used to represent warnings if extensions misbehave.
class ExtensionWarning {
 public:
  // Default constructor for storing ExtensionServiceWarning in STL containers
  // do not use.
  ExtensionWarning();

  // Constructs a warning of type |type| for extension |extension_id|. This
  // could be for example the fact that an extension conflicted with others.
  ExtensionWarning(ExtensionWarningSet::WarningType type,
                   const std::string& extension_id);

  ~ExtensionWarning();

  // Returns the specific warning type.
  ExtensionWarningSet::WarningType warning_type() const { return type_; }

  // Returns the id of the extension for which this warning is valid.
  const std::string& extension_id() const { return extension_id_; }

 private:
  ExtensionWarningSet::WarningType type_;
  std::string extension_id_;

  // Allow implicit copy and assign operator.
};

ExtensionWarning::ExtensionWarning() : type_(ExtensionWarningSet::kInvalid) {
}

ExtensionWarning::ExtensionWarning(
    ExtensionWarningSet::WarningType type,
    const std::string& extension_id)
    : type_(type), extension_id_(extension_id) {
  // These are invalid here because they do not have corresponding warning
  // messages in the UI.
  CHECK(type != ExtensionWarningSet::kInvalid);
  CHECK(type != ExtensionWarningSet::kMaxWarningType);
}

ExtensionWarning::~ExtensionWarning() {
}

bool operator<(const ExtensionWarning& a, const ExtensionWarning& b) {
  if (a.warning_type() == b.warning_type())
    return a.extension_id() < b.extension_id();
  return a.warning_type() < b.warning_type();
}

// Static
string16 ExtensionWarningSet::GetLocalizedWarning(
    ExtensionWarningSet::WarningType warning_type) {
  switch (warning_type) {
    case kInvalid:
    case kMaxWarningType:
      NOTREACHED();
      return string16();
    case kNetworkDelay:
      return l10n_util::GetStringFUTF16(
          IDS_EXTENSION_WARNINGS_NETWORK_DELAY,
          l10n_util::GetStringUTF16(IDS_PRODUCT_NAME));
    case kNetworkConflict:
      return l10n_util::GetStringUTF16(IDS_EXTENSION_WARNINGS_NETWORK_CONFLICT);
    case kRepeatedCacheFlushes:
      return l10n_util::GetStringFUTF16(
          IDS_EXTENSION_WARNINGS_NETWORK_DELAY,
          l10n_util::GetStringUTF16(IDS_PRODUCT_NAME));
  }
  NOTREACHED();  // Switch statement has no default branch.
  return string16();
}

ExtensionWarningSet::ExtensionWarningSet(Profile* profile) : profile_(profile) {
}

ExtensionWarningSet::~ExtensionWarningSet() {
}

void ExtensionWarningSet::SetWarning(ExtensionWarningSet::WarningType type,
                                     const std::string& extension_id) {
  ExtensionWarning warning(type, extension_id);
  bool inserted = warnings_.insert(warning).second;
  if (inserted) {
    NotifyWarningsChanged();
    UpdateWarningBadge();
  }
}

void ExtensionWarningSet::ClearWarnings(
    const std::set<ExtensionWarningSet::WarningType>& types) {
  bool deleted_anything = false;
  for (iterator i = warnings_.begin(); i != warnings_.end();) {
    if (types.find(i->warning_type()) != types.end()) {
      deleted_anything = true;
      warnings_.erase(i++);
    } else {
      ++i;
    }
  }

  if (deleted_anything) {
    NotifyWarningsChanged();
    UpdateWarningBadge();
  }
}

void ExtensionWarningSet::GetWarningsAffectingExtension(
    const std::string& extension_id,
    std::set<ExtensionWarningSet::WarningType>* result) const {
  result->clear();
  for (const_iterator i = warnings_.begin(); i != warnings_.end(); ++i) {
    if (i->extension_id() == extension_id)
      result->insert(i->warning_type());
  }
}

// static
void ExtensionWarningSet::NotifyWarningsOnUI(
    void* profile_id,
    std::set<std::string> extension_ids,
    WarningType warning_type) {
  CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  Profile* profile = reinterpret_cast<Profile*>(profile_id);
  if (!profile ||
      !g_browser_process->profile_manager() ||
      !g_browser_process->profile_manager()->IsValidProfile(profile)) {
    return;
  }

  ExtensionWarningSet* warnings =
      profile->GetExtensionService()->extension_warnings();

  for (std::set<std::string>::const_iterator i = extension_ids.begin();
       i != extension_ids.end(); ++i) {
    warnings->SetWarning(warning_type, *i);
  }
}

void ExtensionWarningSet::SuppressBadgeForCurrentWarnings() {
  badge_suppressions_.insert(warnings_.begin(), warnings_.end());
  UpdateWarningBadge();
}

void ExtensionWarningSet::NotifyWarningsChanged() {
  content::NotificationService::current()->Notify(
      chrome::NOTIFICATION_EXTENSION_WARNING_CHANGED,
      content::Source<Profile>(profile_),
      content::NotificationService::NoDetails());
}

void ExtensionWarningSet::UpdateWarningBadge() {
  // We need a badge if a warning exists that has not been suppressed.
  bool need_warning_badge = false;
  for (const_iterator i = warnings_.begin(); i != warnings_.end(); ++i) {
    if (badge_suppressions_.find(*i) == badge_suppressions_.end()) {
      need_warning_badge = true;
      break;
    }
  }

  GlobalErrorService* service =
      GlobalErrorServiceFactory::GetForProfile(profile_);
  GlobalError* error = service->GetGlobalErrorByMenuItemCommandID(
      ExtensionGlobalErrorBadge::GetMenuItemCommandID());

  // Activate or hide the warning badge in case the current state is incorrect.
  if (error && !need_warning_badge) {
    service->RemoveGlobalError(error);
    delete error;
  } else if (!error && need_warning_badge) {
    service->AddGlobalError(new ExtensionGlobalErrorBadge);
  }
}