Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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/file_util.h" | |
| 9 #include "base/files/file_path.h" | |
| 10 #include "base/location.h" | |
| 11 #include "base/strings/utf_string_conversions.h" | |
|
Dan Beam
2013/08/21 23:41:03
#include "base/strings/string16.h" (sorry, see bel
Devlin
2013/08/22 18:29:06
Done.
| |
| 12 #include "base/values.h" | |
| 13 #include "chrome/browser/extensions/extension_service.h" | |
| 14 #include "chrome/browser/extensions/extension_system.h" | |
| 15 #include "chrome/browser/profiles/profile.h" | |
| 16 #include "chrome/common/extensions/extension.h" | |
| 17 #include "content/public/browser/browser_thread.h" | |
| 18 #include "content/public/browser/web_ui.h" | |
| 19 #include "content/public/browser/web_ui_data_source.h" | |
| 20 #include "extensions/browser/manifest_highlighter.h" | |
| 21 #include "grit/generated_resources.h" | |
| 22 #include "ui/base/l10n/l10n_util.h" | |
| 23 | |
| 24 namespace extensions { | |
| 25 | |
| 26 namespace { | |
| 27 | |
| 28 // Keys for objects passed to and from extension error UI. | |
| 29 const char kExtensionIdKey[] = "extensionId"; | |
| 30 const char kPathSuffixKey[] = "pathSuffix"; | |
| 31 const char kErrorMessageKey[] = "errorMessage"; | |
| 32 const char kFileTypeKey[] = "fileType"; | |
| 33 const char kFeatureKey[] = "feature"; | |
| 34 const char kSpecificKey[] = "specific"; | |
| 35 const char kManifestFileType[] = "manifest"; | |
| 36 const char kTitleKey[] = "title"; | |
| 37 const char kBeforeHighlightKey[] = "beforeHighlight"; | |
| 38 const char kHighlightKey[] = "highlight"; | |
| 39 const char kAfterHighlightKey[] = "afterHighlight"; | |
| 40 | |
| 41 // Populate a DictionaryValue with the highlighted portions for the callback to | |
| 42 // ExtensionErrorOverlay, given the components. | |
| 43 void AddHighlightedToDictionaryForOverlay(base::DictionaryValue* dict, | |
|
Dan Beam
2013/08/21 23:41:03
nit: HighlightDictionary
Devlin
2013/08/22 18:29:06
Done.
| |
| 44 const std::string& before_highlight, | |
| 45 const std::string& highlight, | |
| 46 const std::string& after_highlight) { | |
|
Dan Beam
2013/08/21 23:41:03
nit: just pass a const ManifestHighlighter& highli
Devlin
2013/08/22 18:29:06
Done.
| |
| 47 if (!before_highlight.empty()) | |
| 48 dict->SetString(kBeforeHighlightKey, base::UTF8ToUTF16(before_highlight)); | |
|
Dan Beam
2013/08/21 23:41:03
nit: \n
Devlin
2013/08/22 18:29:06
Done.
| |
| 49 if (!highlight.empty()) | |
| 50 dict->SetString(kHighlightKey, base::UTF8ToUTF16(highlight)); | |
|
Dan Beam
2013/08/21 23:41:03
nit: \n
Devlin
2013/08/22 18:29:06
Done.
| |
| 51 if (!after_highlight.empty()) | |
| 52 dict->SetString(kAfterHighlightKey, base::UTF8ToUTF16(after_highlight)); | |
| 53 } | |
| 54 | |
| 55 } // namespace | |
| 56 | |
| 57 ExtensionErrorHandler::ExtensionErrorHandler(Profile* profile) | |
| 58 : profile_(profile) { | |
| 59 } | |
| 60 | |
| 61 ExtensionErrorHandler::~ExtensionErrorHandler() { | |
| 62 } | |
| 63 | |
| 64 void ExtensionErrorHandler::GetLocalizedValues( | |
| 65 content::WebUIDataSource* source) { | |
| 66 source->AddString( | |
| 67 "extensionErrorsManifestErrors", | |
| 68 l10n_util::GetStringUTF16(IDS_EXTENSIONS_ERRORS_MANIFEST_ERRORS)); | |
| 69 source->AddString( | |
| 70 "extensionErrorsShowMore", | |
| 71 l10n_util::GetStringUTF16(IDS_EXTENSIONS_ERRORS_SHOW_MORE)); | |
| 72 source->AddString( | |
| 73 "extensionErrorsShowFewer", | |
| 74 l10n_util::GetStringUTF16(IDS_EXTENSIONS_ERRORS_SHOW_FEWER)); | |
| 75 source->AddString( | |
| 76 "extensionErrorViewSource", | |
| 77 l10n_util::GetStringUTF16(IDS_EXTENSIONS_ERROR_VIEW_SOURCE)); | |
| 78 } | |
| 79 | |
| 80 void ExtensionErrorHandler::RegisterMessages() { | |
| 81 web_ui()->RegisterMessageCallback( | |
| 82 "extensionErrorRequestFileSource", | |
| 83 base::Bind(&ExtensionErrorHandler::HandleRequestFileSource, | |
| 84 base::Unretained(this))); | |
| 85 } | |
| 86 | |
| 87 void ExtensionErrorHandler::HandleRequestFileSource( | |
| 88 const base::ListValue* args) { | |
| 89 // There should only be one argument, a dictionary. Use this instead of a list | |
| 90 // because it's more descriptive, harder to accidentally break with minor | |
| 91 // modifications, and supports optional arguments more easily. | |
| 92 CHECK_EQ(1u, args->GetSize()); | |
| 93 | |
| 94 const base::DictionaryValue* dict = NULL; | |
| 95 | |
| 96 // Four required arguments: extension_id, path_suffix, error_message, and | |
| 97 // file_type. | |
| 98 std::string extension_id; | |
| 99 base::FilePath::StringType path_suffix; | |
| 100 base::string16 error_message; | |
|
Dan Beam
2013/08/21 23:41:03
^ i lied, you do need to include string16, sorry..
Devlin
2013/08/22 18:29:06
haha no worries. Done.
| |
| 101 std::string file_type; | |
| 102 | |
| 103 if (!args->GetDictionary(0, &dict) || | |
| 104 !dict->GetString(kExtensionIdKey, &extension_id) || | |
| 105 !dict->GetString(kPathSuffixKey, &path_suffix) || | |
| 106 !dict->GetString(kErrorMessageKey, &error_message) || | |
| 107 !dict->GetString(kFileTypeKey, &file_type)) { | |
| 108 NOTREACHED(); | |
| 109 return; | |
| 110 } | |
| 111 | |
| 112 const Extension* extension = | |
| 113 ExtensionSystem::Get(Profile::FromWebUI(web_ui()))-> | |
| 114 extension_service()->GetExtensionById(extension_id, | |
| 115 true /* include disabled */ ); | |
| 116 base::FilePath path = extension->path().Append(path_suffix); | |
| 117 | |
| 118 // Setting the title and the error message is the same for all file types. | |
| 119 scoped_ptr<base::DictionaryValue> results(new base::DictionaryValue); | |
| 120 results->SetString(kTitleKey, | |
| 121 base::UTF8ToUTF16(extension->name()) + | |
| 122 base::ASCIIToUTF16(": ") + | |
| 123 path.BaseName().LossyDisplayName()); | |
| 124 results->SetString(kErrorMessageKey, error_message); | |
| 125 | |
| 126 base::Closure closure; | |
| 127 std::string* contents = NULL; | |
| 128 | |
| 129 if (file_type == kManifestFileType) { | |
| 130 std::string feature; | |
| 131 std::string specific; | |
| 132 if (!dict->GetString(kFeatureKey, &feature)) { | |
| 133 NOTREACHED(); | |
| 134 return; | |
| 135 } | |
| 136 // A "specific" location is optional. | |
| 137 dict->GetString(kSpecificKey, &specific); | |
| 138 | |
| 139 contents = new std::string; // Owned by GetManifestFileCallback() | |
| 140 closure = base::Bind(&ExtensionErrorHandler::GetManifestFileCallback, | |
| 141 base::Unretained(this), | |
| 142 base::Owned(results.release()), | |
| 143 feature, | |
| 144 specific, | |
| 145 base::Owned(contents)); | |
| 146 } else { | |
| 147 // currently, only manifest file types supported. | |
| 148 NOTREACHED(); | |
| 149 return; | |
| 150 } | |
| 151 | |
| 152 content::BrowserThread::PostBlockingPoolTaskAndReply( | |
| 153 FROM_HERE, | |
| 154 base::Bind(base::IgnoreResult(&file_util::ReadFileToString), | |
| 155 path, | |
| 156 contents), | |
| 157 closure); | |
| 158 } | |
| 159 | |
| 160 void ExtensionErrorHandler::GetManifestFileCallback( | |
| 161 base::DictionaryValue* results, | |
| 162 const std::string& key, | |
| 163 const std::string& specific, | |
| 164 std::string* contents) { | |
| 165 ManifestHighlighter highlighter(*contents, key, specific); | |
| 166 | |
| 167 AddHighlightedToDictionaryForOverlay( | |
| 168 results, | |
| 169 highlighter.GetBeforeFeature(), | |
| 170 highlighter.GetFeature(), | |
| 171 highlighter.GetAfterFeature()); | |
| 172 | |
| 173 web_ui()->CallJavascriptFunction( | |
| 174 "extensions.ExtensionErrorOverlay.requestFileSourceResponse", *results); | |
| 175 } | |
| 176 | |
| 177 } // namespace extensions | |
| OLD | NEW |