extension_error_reporter.cc revision c407dc5cd9bdc5668497f21b26b09d988ab439de
1// Copyright (c) 2006-2009 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/extensions/extension_error_reporter.h"
6
7#include "build/build_config.h"
8
9#include "base/logging.h"
10#include "base/message_loop.h"
11#include "base/utf_string_conversions.h"
12#include "chrome/browser/platform_util.h"
13
14// No AddRef required when using ExtensionErrorReporter with RunnableMethod.
15// This is okay since the ExtensionErrorReporter is a singleton that lives until
16// the end of the process.
17DISABLE_RUNNABLE_METHOD_REFCOUNT(ExtensionErrorReporter);
18
19ExtensionErrorReporter* ExtensionErrorReporter::instance_ = NULL;
20
21// static
22void ExtensionErrorReporter::Init(bool enable_noisy_errors) {
23  if (!instance_) {
24    instance_ = new ExtensionErrorReporter(enable_noisy_errors);
25  }
26}
27
28// static
29ExtensionErrorReporter* ExtensionErrorReporter::GetInstance() {
30  CHECK(instance_) << "Init() was never called";
31  return instance_;
32}
33
34ExtensionErrorReporter::ExtensionErrorReporter(bool enable_noisy_errors)
35    : ui_loop_(MessageLoop::current()),
36      enable_noisy_errors_(enable_noisy_errors) {
37}
38
39void ExtensionErrorReporter::ReportError(const std::string& message,
40                                         bool be_noisy) {
41  // NOTE: There won't be a ui_loop_ in the unit test environment.
42  if (ui_loop_ && MessageLoop::current() != ui_loop_) {
43    ui_loop_->PostTask(FROM_HERE,
44        NewRunnableMethod(this, &ExtensionErrorReporter::ReportError, message,
45                          be_noisy));
46    return;
47  }
48
49  errors_.push_back(message);
50
51  // TODO(aa): Print the error message out somewhere better. I think we are
52  // going to need some sort of 'extension inspector'.
53  LOG(ERROR) << "Extension error: " << message;
54
55  if (enable_noisy_errors_ && be_noisy) {
56    platform_util::SimpleErrorBox(NULL,
57                                  UTF8ToUTF16("Extension error"),
58                                  UTF8ToUTF16(message));
59  }
60}
61
62const std::vector<std::string>* ExtensionErrorReporter::GetErrors() {
63  return &errors_;
64}
65
66void ExtensionErrorReporter::ClearErrors() {
67  errors_.clear();
68}
69