OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/extensions/extension_error_reporter.h" | 5 #include "chrome/browser/extensions/extension_error_reporter.h" |
6 | 6 |
7 #include "build/build_config.h" | 7 #include "build/build_config.h" |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/message_loop/message_loop.h" | |
14 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
15 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
| 15 #include "base/threading/thread_task_runner_handle.h" |
16 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
17 #include "chrome/browser/ui/simple_message_box.h" | 17 #include "chrome/browser/ui/simple_message_box.h" |
18 #include "content/public/browser/notification_service.h" | 18 #include "content/public/browser/notification_service.h" |
19 #include "extensions/browser/notification_types.h" | 19 #include "extensions/browser/notification_types.h" |
20 #include "grit/generated_resources.h" | 20 #include "grit/generated_resources.h" |
21 #include "ui/base/l10n/l10n_util.h" | 21 #include "ui/base/l10n/l10n_util.h" |
22 | 22 |
23 ExtensionErrorReporter* ExtensionErrorReporter::instance_ = NULL; | 23 ExtensionErrorReporter* ExtensionErrorReporter::instance_ = NULL; |
24 | 24 |
25 // static | 25 // static |
26 void ExtensionErrorReporter::Init(bool enable_noisy_errors) { | 26 void ExtensionErrorReporter::Init(bool enable_noisy_errors) { |
27 if (!instance_) { | 27 if (!instance_) { |
28 instance_ = new ExtensionErrorReporter(enable_noisy_errors); | 28 instance_ = new ExtensionErrorReporter(enable_noisy_errors); |
29 } | 29 } |
30 } | 30 } |
31 | 31 |
32 // static | 32 // static |
33 ExtensionErrorReporter* ExtensionErrorReporter::GetInstance() { | 33 ExtensionErrorReporter* ExtensionErrorReporter::GetInstance() { |
34 CHECK(instance_) << "Init() was never called"; | 34 CHECK(instance_) << "Init() was never called"; |
35 return instance_; | 35 return instance_; |
36 } | 36 } |
37 | 37 |
38 ExtensionErrorReporter::ExtensionErrorReporter(bool enable_noisy_errors) | 38 ExtensionErrorReporter::ExtensionErrorReporter(bool enable_noisy_errors) |
39 : ui_loop_(base::MessageLoop::current()), | 39 : enable_noisy_errors_(enable_noisy_errors) { |
40 enable_noisy_errors_(enable_noisy_errors) { | 40 if (base::ThreadTaskRunnerHandle::IsSet()) |
| 41 ui_task_runner_ = base::ThreadTaskRunnerHandle::Get(); |
41 } | 42 } |
42 | 43 |
43 ExtensionErrorReporter::~ExtensionErrorReporter() {} | 44 ExtensionErrorReporter::~ExtensionErrorReporter() {} |
44 | 45 |
45 void ExtensionErrorReporter::ReportLoadError( | 46 void ExtensionErrorReporter::ReportLoadError( |
46 const base::FilePath& extension_path, | 47 const base::FilePath& extension_path, |
47 const std::string& error, | 48 const std::string& error, |
48 content::BrowserContext* browser_context, | 49 content::BrowserContext* browser_context, |
49 bool be_noisy) { | 50 bool be_noisy) { |
50 content::NotificationService::current()->Notify( | 51 content::NotificationService::current()->Notify( |
51 extensions::NOTIFICATION_EXTENSION_LOAD_ERROR, | 52 extensions::NOTIFICATION_EXTENSION_LOAD_ERROR, |
52 content::Source<Profile>(Profile::FromBrowserContext(browser_context)), | 53 content::Source<Profile>(Profile::FromBrowserContext(browser_context)), |
53 content::Details<const std::string>(&error)); | 54 content::Details<const std::string>(&error)); |
54 | 55 |
55 std::string path_str = base::UTF16ToUTF8(extension_path.LossyDisplayName()); | 56 std::string path_str = base::UTF16ToUTF8(extension_path.LossyDisplayName()); |
56 base::string16 message = base::UTF8ToUTF16(base::StringPrintf( | 57 base::string16 message = base::UTF8ToUTF16(base::StringPrintf( |
57 "%s %s. %s", | 58 "%s %s. %s", |
58 l10n_util::GetStringUTF8(IDS_EXTENSIONS_LOAD_ERROR_MESSAGE).c_str(), | 59 l10n_util::GetStringUTF8(IDS_EXTENSIONS_LOAD_ERROR_MESSAGE).c_str(), |
59 path_str.c_str(), | 60 path_str.c_str(), |
60 error.c_str())); | 61 error.c_str())); |
61 ReportError(message, be_noisy); | 62 ReportError(message, be_noisy); |
62 FOR_EACH_OBSERVER(Observer, | 63 FOR_EACH_OBSERVER(Observer, |
63 observers_, | 64 observers_, |
64 OnLoadFailure(browser_context, extension_path, error)); | 65 OnLoadFailure(browser_context, extension_path, error)); |
65 } | 66 } |
66 | 67 |
67 void ExtensionErrorReporter::ReportError(const base::string16& message, | 68 void ExtensionErrorReporter::ReportError(const base::string16& message, |
68 bool be_noisy) { | 69 bool be_noisy) { |
69 // NOTE: There won't be a ui_loop_ in the unit test environment. | 70 // NOTE: There won't be a |ui_task_runner_| in the unit test environment. |
70 if (ui_loop_) { | 71 CHECK(!ui_task_runner_ || ui_task_runner_->BelongsToCurrentThread()) |
71 CHECK(base::MessageLoop::current() == ui_loop_) | 72 << "ReportError can only be called from the UI thread."; |
72 << "ReportError can only be called from the UI thread."; | |
73 } | |
74 | 73 |
75 errors_.push_back(message); | 74 errors_.push_back(message); |
76 | 75 |
77 // TODO(aa): Print the error message out somewhere better. I think we are | 76 // TODO(aa): Print the error message out somewhere better. I think we are |
78 // going to need some sort of 'extension inspector'. | 77 // going to need some sort of 'extension inspector'. |
79 LOG(WARNING) << "Extension error: " << message; | 78 LOG(WARNING) << "Extension error: " << message; |
80 | 79 |
81 if (enable_noisy_errors_ && be_noisy) { | 80 if (enable_noisy_errors_ && be_noisy) { |
82 chrome::ShowWarningMessageBox( | 81 chrome::ShowWarningMessageBox( |
83 NULL, l10n_util::GetStringUTF16(IDS_EXTENSIONS_LOAD_ERROR_HEADING), | 82 NULL, l10n_util::GetStringUTF16(IDS_EXTENSIONS_LOAD_ERROR_HEADING), |
84 message); | 83 message); |
85 } | 84 } |
86 } | 85 } |
87 | 86 |
88 const std::vector<base::string16>* ExtensionErrorReporter::GetErrors() { | 87 const std::vector<base::string16>* ExtensionErrorReporter::GetErrors() { |
89 return &errors_; | 88 return &errors_; |
90 } | 89 } |
91 | 90 |
92 void ExtensionErrorReporter::ClearErrors() { | 91 void ExtensionErrorReporter::ClearErrors() { |
93 errors_.clear(); | 92 errors_.clear(); |
94 } | 93 } |
95 | 94 |
96 void ExtensionErrorReporter::AddObserver(Observer* observer) { | 95 void ExtensionErrorReporter::AddObserver(Observer* observer) { |
97 observers_.AddObserver(observer); | 96 observers_.AddObserver(observer); |
98 } | 97 } |
99 | 98 |
100 void ExtensionErrorReporter::RemoveObserver(Observer* observer) { | 99 void ExtensionErrorReporter::RemoveObserver(Observer* observer) { |
101 observers_.RemoveObserver(observer); | 100 observers_.RemoveObserver(observer); |
102 } | 101 } |
OLD | NEW |