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