summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/extension_warning_service.cc
blob: a65dfa6c688f27ec215477e996ecb01d6637aaae (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
// 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_service.h"

#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/notification_service.h"
#include "extensions/browser/extension_system.h"
#include "extensions/common/extension.h"

using content::BrowserThread;

namespace extensions {

ExtensionWarningService::ExtensionWarningService(Profile* profile)
    : profile_(profile) {
  DCHECK(CalledOnValidThread());
  if (profile_) {
    registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED,
        content::Source<Profile>(profile_->GetOriginalProfile()));
  }
}

ExtensionWarningService::~ExtensionWarningService() {}

void ExtensionWarningService::ClearWarnings(
    const std::set<ExtensionWarning::WarningType>& types) {
  DCHECK(CalledOnValidThread());
  bool deleted_anything = false;
  for (ExtensionWarningSet::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();
}

std::set<ExtensionWarning::WarningType>
ExtensionWarningService::GetWarningTypesAffectingExtension(
    const std::string& extension_id) const {
  DCHECK(CalledOnValidThread());
  std::set<ExtensionWarning::WarningType> result;
  for (ExtensionWarningSet::const_iterator i = warnings_.begin();
       i != warnings_.end(); ++i) {
    if (i->extension_id() == extension_id)
      result.insert(i->warning_type());
  }
  return result;
}

std::vector<std::string>
ExtensionWarningService::GetWarningMessagesForExtension(
    const std::string& extension_id) const {
  DCHECK(CalledOnValidThread());
  std::vector<std::string> result;

  const ExtensionService* extension_service =
      ExtensionSystem::Get(profile_)->extension_service();

  for (ExtensionWarningSet::const_iterator i = warnings_.begin();
       i != warnings_.end(); ++i) {
    if (i->extension_id() == extension_id)
      result.push_back(i->GetLocalizedMessage(extension_service->extensions()));
  }
  return result;
}

void ExtensionWarningService::AddWarnings(
    const ExtensionWarningSet& warnings) {
  DCHECK(CalledOnValidThread());
  size_t old_size = warnings_.size();

  warnings_.insert(warnings.begin(), warnings.end());

  if (old_size != warnings_.size())
    NotifyWarningsChanged();
}

// static
void ExtensionWarningService::NotifyWarningsOnUI(
    void* profile_id,
    const ExtensionWarningSet& warnings) {
  DCHECK(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;
  }

  extensions::ExtensionWarningService* warning_service =
      extensions::ExtensionSystem::Get(profile)->warning_service();

  warning_service->AddWarnings(warnings);
}

void ExtensionWarningService::AddObserver(Observer* observer) {
  observer_list_.AddObserver(observer);
}

void ExtensionWarningService::RemoveObserver(Observer* observer) {
  observer_list_.RemoveObserver(observer);
}

void ExtensionWarningService::NotifyWarningsChanged() {
  FOR_EACH_OBSERVER(Observer, observer_list_, ExtensionWarningsChanged());
}

void ExtensionWarningService::Observe(
    int type,
    const content::NotificationSource& source,
    const content::NotificationDetails& details) {
  switch (type) {
    case chrome::NOTIFICATION_EXTENSION_UNLOADED: {
      const Extension* extension =
          content::Details<extensions::UnloadedExtensionInfo>(details)->
          extension;
      // Unloading one extension might have solved the problems of others.
      // Therefore, we clear warnings of this type for all extensions.
      std::set<ExtensionWarning::WarningType> warning_types =
          GetWarningTypesAffectingExtension(extension->id());
      ClearWarnings(warning_types);
      break;
    }
    default:
      NOTREACHED();
      break;
  }
}

}  // namespace extensions