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"
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/chrome_notification_types.h"
17 #include "chrome/browser/ui/simple_message_box.h"
18 #include "content/public/browser/notification_service.h"
20 ExtensionErrorReporter* ExtensionErrorReporter::instance_ = NULL;
23 void ExtensionErrorReporter::Init(bool enable_noisy_errors) {
25 instance_ = new ExtensionErrorReporter(enable_noisy_errors);
30 ExtensionErrorReporter* ExtensionErrorReporter::GetInstance() {
31 CHECK(instance_) << "Init() was never called";
35 ExtensionErrorReporter::ExtensionErrorReporter(bool enable_noisy_errors)
36 : ui_loop_(base::MessageLoop::current()),
37 enable_noisy_errors_(enable_noisy_errors) {
40 ExtensionErrorReporter::~ExtensionErrorReporter() {}
42 void ExtensionErrorReporter::ReportLoadError(
43 const base::FilePath& extension_path,
44 const std::string& error,
47 content::NotificationService::current()->Notify(
48 chrome::NOTIFICATION_EXTENSION_LOAD_ERROR,
49 content::Source<Profile>(profile),
50 content::Details<const std::string>(&error));
52 std::string path_str = base::UTF16ToUTF8(extension_path.LossyDisplayName());
53 base::string16 message = base::UTF8ToUTF16(
54 base::StringPrintf("Could not load extension from '%s'. %s",
57 ReportError(message, be_noisy);
60 void ExtensionErrorReporter::ReportError(const base::string16& message,
62 // NOTE: There won't be a ui_loop_ in the unit test environment.
64 CHECK(base::MessageLoop::current() == ui_loop_)
65 << "ReportError can only be called from the UI thread.";
68 errors_.push_back(message);
70 // TODO(aa): Print the error message out somewhere better. I think we are
71 // going to need some sort of 'extension inspector'.
72 LOG(WARNING) << "Extension error: " << message;
74 if (enable_noisy_errors_ && be_noisy) {
75 chrome::ShowMessageBox(NULL,
76 base::ASCIIToUTF16("Extension error"),
78 chrome::MESSAGE_BOX_TYPE_WARNING);
82 const std::vector<base::string16>* ExtensionErrorReporter::GetErrors() {
86 void ExtensionErrorReporter::ClearErrors() {