Rename GetIconID to GetIconId
[chromium-blink-merge.git] / chrome / browser / extensions / extension_error_reporter.cc
blob898642347d1754777ce31401a03097371ebdab0a
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.
5 #include "chrome/browser/extensions/extension_error_reporter.h"
7 #include "build/build_config.h"
9 #include "base/bind.h"
10 #include "base/bind_helpers.h"
11 #include "base/files/file_path.h"
12 #include "base/logging.h"
13 #include "base/message_loop/message_loop.h"
14 #include "base/strings/stringprintf.h"
15 #include "base/strings/utf_string_conversions.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/ui/simple_message_box.h"
18 #include "content/public/browser/notification_service.h"
19 #include "extensions/browser/notification_types.h"
20 #include "grit/generated_resources.h"
21 #include "ui/base/l10n/l10n_util.h"
23 ExtensionErrorReporter* ExtensionErrorReporter::instance_ = NULL;
25 // static
26 void ExtensionErrorReporter::Init(bool enable_noisy_errors) {
27 if (!instance_) {
28 instance_ = new ExtensionErrorReporter(enable_noisy_errors);
32 // static
33 ExtensionErrorReporter* ExtensionErrorReporter::GetInstance() {
34 CHECK(instance_) << "Init() was never called";
35 return instance_;
38 ExtensionErrorReporter::ExtensionErrorReporter(bool enable_noisy_errors)
39 : ui_loop_(base::MessageLoop::current()),
40 enable_noisy_errors_(enable_noisy_errors) {
43 ExtensionErrorReporter::~ExtensionErrorReporter() {}
45 void ExtensionErrorReporter::ReportLoadError(
46 const base::FilePath& extension_path,
47 const std::string& error,
48 content::BrowserContext* browser_context,
49 bool be_noisy) {
50 content::NotificationService::current()->Notify(
51 extensions::NOTIFICATION_EXTENSION_LOAD_ERROR,
52 content::Source<Profile>(Profile::FromBrowserContext(browser_context)),
53 content::Details<const std::string>(&error));
55 std::string path_str = base::UTF16ToUTF8(extension_path.LossyDisplayName());
56 base::string16 message = base::UTF8ToUTF16(base::StringPrintf(
57 "%s %s. %s",
58 l10n_util::GetStringUTF8(IDS_EXTENSIONS_LOAD_ERROR_MESSAGE).c_str(),
59 path_str.c_str(),
60 error.c_str()));
61 ReportError(message, be_noisy);
62 FOR_EACH_OBSERVER(Observer,
63 observers_,
64 OnLoadFailure(browser_context, extension_path, error));
67 void ExtensionErrorReporter::ReportError(const base::string16& message,
68 bool be_noisy) {
69 // NOTE: There won't be a ui_loop_ in the unit test environment.
70 if (ui_loop_) {
71 CHECK(base::MessageLoop::current() == ui_loop_)
72 << "ReportError can only be called from the UI thread.";
75 errors_.push_back(message);
77 // TODO(aa): Print the error message out somewhere better. I think we are
78 // going to need some sort of 'extension inspector'.
79 LOG(WARNING) << "Extension error: " << message;
81 if (enable_noisy_errors_ && be_noisy) {
82 chrome::ShowMessageBox(
83 NULL,
84 l10n_util::GetStringUTF16(IDS_EXTENSIONS_LOAD_ERROR_HEADING),
85 message,
86 chrome::MESSAGE_BOX_TYPE_WARNING);
90 const std::vector<base::string16>* ExtensionErrorReporter::GetErrors() {
91 return &errors_;
94 void ExtensionErrorReporter::ClearErrors() {
95 errors_.clear();
98 void ExtensionErrorReporter::AddObserver(Observer* observer) {
99 observers_.AddObserver(observer);
102 void ExtensionErrorReporter::RemoveObserver(Observer* observer) {
103 observers_.RemoveObserver(observer);