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