1 // Copyright 2013 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/ui/webui/extensions/extension_error_handler.h"
6
7 #include "base/bind.h"
8 #include "base/strings/string16.h"
9 #include "base/threading/sequenced_worker_pool.h"
10 #include "base/values.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/ui/webui/extensions/extension_error_ui_util.h"
13 #include "chrome/grit/generated_resources.h"
14 #include "content/public/browser/web_ui.h"
15 #include "content/public/browser/web_ui_data_source.h"
16 #include "extensions/common/extension.h"
17 #include "ui/base/l10n/l10n_util.h"
18
19 namespace extensions {
20
ExtensionErrorHandler(Profile * profile)21 ExtensionErrorHandler::ExtensionErrorHandler(Profile* profile)
22 : profile_(profile),
23 weak_ptr_factory_(this) {
24 }
25
~ExtensionErrorHandler()26 ExtensionErrorHandler::~ExtensionErrorHandler() {
27 }
28
GetLocalizedValues(content::WebUIDataSource * source)29 void ExtensionErrorHandler::GetLocalizedValues(
30 content::WebUIDataSource* source) {
31 source->AddString(
32 "extensionErrorsShowMore",
33 l10n_util::GetStringUTF16(IDS_EXTENSIONS_ERRORS_SHOW_MORE));
34 source->AddString(
35 "extensionErrorsShowFewer",
36 l10n_util::GetStringUTF16(IDS_EXTENSIONS_ERRORS_SHOW_FEWER));
37 source->AddString(
38 "extensionErrorViewDetails",
39 l10n_util::GetStringUTF16(IDS_EXTENSIONS_ERROR_VIEW_DETAILS));
40 source->AddString(
41 "extensionErrorViewManifest",
42 l10n_util::GetStringUTF16(IDS_EXTENSIONS_ERROR_VIEW_MANIFEST));
43 source->AddString("extensionErrorOverlayDone",
44 l10n_util::GetStringUTF16(IDS_DONE));
45 source->AddString(
46 "extensionErrorOverlayContextUrl",
47 l10n_util::GetStringUTF16(IDS_EXTENSIONS_ERROR_CONTEXT));
48 source->AddString(
49 "extensionErrorOverlayStackTrace",
50 l10n_util::GetStringUTF16(IDS_EXTENSIONS_ERROR_STACK_TRACE));
51 source->AddString(
52 "extensionErrorOverlayAnonymousFunction",
53 l10n_util::GetStringUTF16(IDS_EXTENSIONS_ERROR_ANONYMOUS_FUNCTION));
54 source->AddString(
55 "extensionErrorOverlayLaunchDevtools",
56 l10n_util::GetStringUTF16(IDS_EXTENSIONS_ERROR_LAUNCH_DEVTOOLS));
57 source->AddString(
58 "extensionErrorOverlayContextUnknown",
59 l10n_util::GetStringUTF16(IDS_EXTENSIONS_ERROR_CONTEXT_UNKNOWN));
60 source->AddString(
61 "extensionErrorOverlayNoCodeToDisplay",
62 l10n_util::GetStringUTF16(IDS_EXTENSIONS_ERROR_NO_CODE_TO_DISPLAY));
63 }
64
RegisterMessages()65 void ExtensionErrorHandler::RegisterMessages() {
66 web_ui()->RegisterMessageCallback(
67 "extensionErrorRequestFileSource",
68 base::Bind(&ExtensionErrorHandler::HandleRequestFileSource,
69 weak_ptr_factory_.GetWeakPtr()));
70 web_ui()->RegisterMessageCallback(
71 "extensionErrorOpenDevTools",
72 base::Bind(&ExtensionErrorHandler::HandleOpenDevTools,
73 weak_ptr_factory_.GetWeakPtr()));
74 }
75
HandleRequestFileSource(const base::ListValue * args)76 void ExtensionErrorHandler::HandleRequestFileSource(
77 const base::ListValue* args) {
78 // There should only be one argument, a dictionary. Use this instead of a list
79 // because it's more descriptive, harder to accidentally break with minor
80 // modifications, and supports optional arguments more easily.
81 CHECK_EQ(1u, args->GetSize());
82
83 const base::DictionaryValue* dict = NULL;
84
85 // Three required arguments: extension_id, path_suffix, and error_message.
86 std::string extension_id;
87 base::FilePath::StringType path_suffix_string;
88 base::string16 error_message;
89
90 if (!args->GetDictionary(0, &dict)) {
91 NOTREACHED();
92 return;
93 }
94
95 error_ui_util::HandleRequestFileSource(
96 dict,
97 Profile::FromWebUI(web_ui()),
98 base::Bind(&ExtensionErrorHandler::OnFileSourceHandled,
99 weak_ptr_factory_.GetWeakPtr()));
100 }
101
OnFileSourceHandled(const base::DictionaryValue & source)102 void ExtensionErrorHandler::OnFileSourceHandled(
103 const base::DictionaryValue& source) {
104 web_ui()->CallJavascriptFunction(
105 "extensions.ExtensionErrorOverlay.requestFileSourceResponse", source);
106 }
107
HandleOpenDevTools(const base::ListValue * args)108 void ExtensionErrorHandler::HandleOpenDevTools(const base::ListValue* args) {
109 CHECK_EQ(1U, args->GetSize());
110 const base::DictionaryValue* dict = NULL;
111
112 if (!args->GetDictionary(0, &dict)) {
113 NOTREACHED();
114 return;
115 }
116
117 error_ui_util::HandleOpenDevTools(dict);
118 }
119
120 } // namespace extensions
121