Home | History | Annotate | Download | only in extensions
      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.
     17 DISABLE_RUNNABLE_METHOD_REFCOUNT(ExtensionErrorReporter);
     18 
     19 ExtensionErrorReporter* ExtensionErrorReporter::instance_ = NULL;
     20 
     21 // static
     22 void ExtensionErrorReporter::Init(bool enable_noisy_errors) {
     23   if (!instance_) {
     24     instance_ = new ExtensionErrorReporter(enable_noisy_errors);
     25   }
     26 }
     27 
     28 // static
     29 ExtensionErrorReporter* ExtensionErrorReporter::GetInstance() {
     30   CHECK(instance_) << "Init() was never called";
     31   return instance_;
     32 }
     33 
     34 ExtensionErrorReporter::ExtensionErrorReporter(bool enable_noisy_errors)
     35     : ui_loop_(MessageLoop::current()),
     36       enable_noisy_errors_(enable_noisy_errors) {
     37 }
     38 
     39 ExtensionErrorReporter::~ExtensionErrorReporter() {}
     40 
     41 void ExtensionErrorReporter::ReportError(const std::string& message,
     42                                          bool be_noisy) {
     43   // NOTE: There won't be a ui_loop_ in the unit test environment.
     44   if (ui_loop_ && MessageLoop::current() != ui_loop_) {
     45     ui_loop_->PostTask(FROM_HERE,
     46         NewRunnableMethod(this, &ExtensionErrorReporter::ReportError, message,
     47                           be_noisy));
     48     return;
     49   }
     50 
     51   errors_.push_back(message);
     52 
     53   // TODO(aa): Print the error message out somewhere better. I think we are
     54   // going to need some sort of 'extension inspector'.
     55   LOG(ERROR) << "Extension error: " << message;
     56 
     57   if (enable_noisy_errors_ && be_noisy) {
     58     platform_util::SimpleErrorBox(NULL,
     59                                   UTF8ToUTF16("Extension error"),
     60                                   UTF8ToUTF16(message));
     61   }
     62 }
     63 
     64 const std::vector<std::string>* ExtensionErrorReporter::GetErrors() {
     65   return &errors_;
     66 }
     67 
     68 void ExtensionErrorReporter::ClearErrors() {
     69   errors_.clear();
     70 }
     71