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