| Index: chrome/browser/ui/webui/extensions/extension_error_handler.cc
|
| diff --git a/chrome/browser/ui/webui/extensions/extension_error_handler.cc b/chrome/browser/ui/webui/extensions/extension_error_handler.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..530801193f68689f368794c73a600b004dca7b45
|
| --- /dev/null
|
| +++ b/chrome/browser/ui/webui/extensions/extension_error_handler.cc
|
| @@ -0,0 +1,175 @@
|
| +// Copyright 2013 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "chrome/browser/ui/webui/extensions/extension_error_handler.h"
|
| +
|
| +#include "base/bind.h"
|
| +#include "base/file_util.h"
|
| +#include "base/files/file_path.h"
|
| +#include "base/strings/utf_string_conversions.h"
|
| +#include "base/values.h"
|
| +#include "chrome/browser/extensions/extension_service.h"
|
| +#include "chrome/browser/extensions/extension_system.h"
|
| +#include "chrome/browser/profiles/profile.h"
|
| +#include "chrome/common/extensions/extension.h"
|
| +#include "content/public/browser/browser_thread.h"
|
| +#include "content/public/browser/web_ui.h"
|
| +#include "content/public/browser/web_ui_data_source.h"
|
| +#include "extensions/browser/manifest_highlighter.h"
|
| +#include "grit/generated_resources.h"
|
| +#include "ui/base/l10n/l10n_util.h"
|
| +
|
| +namespace extensions {
|
| +
|
| +namespace {
|
| +
|
| +// Keys for objects passed to and from extension error UI.
|
| +const char kExtensionIdKey[] = "extensionId";
|
| +const char kPathSuffixKey[] = "pathSuffix";
|
| +const char kErrorMessageKey[] = "errorMessage";
|
| +const char kFileTypeKey[] = "fileType";
|
| +const char kFeatureKey[] = "feature";
|
| +const char kSpecificKey[] = "specific";
|
| +const char kManifestFileType[] = "manifest";
|
| +const char kTitleKey[] = "title";
|
| +const char kBeforeHighlightKey[] = "beforeHighlight";
|
| +const char kHighlightKey[] = "highlight";
|
| +const char kAfterHighlightKey[] = "afterHighlight";
|
| +
|
| +// Populate a DictionaryValue with the highlighted portions for the callback to
|
| +// ExtensionErrorOverlay, given the components.
|
| +void AddHighlightedToDictionaryForOverlay(base::DictionaryValue* dict,
|
| + const std::string& before_highlight,
|
| + const std::string& highlight,
|
| + const std::string& after_highlight) {
|
| + if (!before_highlight.empty())
|
| + dict->SetString(kBeforeHighlightKey, base::UTF8ToUTF16(before_highlight));
|
| + if (!highlight.empty())
|
| + dict->SetString(kHighlightKey, base::UTF8ToUTF16(highlight));
|
| + if (!after_highlight.empty())
|
| + dict->SetString(kAfterHighlightKey, base::UTF8ToUTF16(after_highlight));
|
| +}
|
| +
|
| +} // namespace
|
| +
|
| +ExtensionErrorHandler::ExtensionErrorHandler(Profile* profile)
|
| + : profile_(profile) {
|
| +}
|
| +
|
| +ExtensionErrorHandler::~ExtensionErrorHandler() {
|
| +}
|
| +
|
| +void ExtensionErrorHandler::GetLocalizedValues(
|
| + content::WebUIDataSource* source) {
|
| + source->AddString(
|
| + "extensionErrorsManifestErrors",
|
| + l10n_util::GetStringUTF16(IDS_EXTENSIONS_ERRORS_MANIFEST_ERRORS));
|
| + source->AddString(
|
| + "extensionErrorsShowMore",
|
| + l10n_util::GetStringUTF16(IDS_EXTENSIONS_ERRORS_SHOW_MORE));
|
| + source->AddString(
|
| + "extensionErrorsShowFewer",
|
| + l10n_util::GetStringUTF16(IDS_EXTENSIONS_ERRORS_SHOW_FEWER));
|
| + source->AddString(
|
| + "extensionErrorViewSource",
|
| + l10n_util::GetStringUTF16(IDS_EXTENSIONS_ERROR_VIEW_SOURCE));
|
| +}
|
| +
|
| +void ExtensionErrorHandler::RegisterMessages() {
|
| + web_ui()->RegisterMessageCallback(
|
| + "extensionErrorRequestFileSource",
|
| + base::Bind(&ExtensionErrorHandler::HandleRequestFileSource,
|
| + base::Unretained(this)));
|
| +}
|
| +
|
| +void ExtensionErrorHandler::HandleRequestFileSource(
|
| + const base::ListValue* args) {
|
| + // There should only be one argument, a dictionary. Use this instead of a list
|
| + // because it's more descriptive, harder to accidentally break with minor
|
| + // modifications, and supports optional arguments more easily.
|
| + CHECK(args->GetSize() == 1);
|
| +
|
| + const base::DictionaryValue* dict = NULL;
|
| +
|
| + // Four required arguments: extension_id, path_suffix, error_message, and
|
| + // file_type.
|
| + std::string extension_id;
|
| + base::FilePath::StringType path_suffix;
|
| + base::string16 error_message;
|
| + std::string file_type;
|
| +
|
| + if (!args->GetDictionary(0, &dict) ||
|
| + !dict->GetString(kExtensionIdKey, &extension_id) ||
|
| + !dict->GetString(kPathSuffixKey, &path_suffix) ||
|
| + !dict->GetString(kErrorMessageKey, &error_message) ||
|
| + !dict->GetString(kFileTypeKey, &file_type)) {
|
| + NOTREACHED();
|
| + return;
|
| + }
|
| +
|
| + const Extension* extension =
|
| + ExtensionSystem::Get(Profile::FromWebUI(web_ui()))->
|
| + extension_service()->GetExtensionById(extension_id,
|
| + true /* include disabled */ );
|
| + base::FilePath path = extension->path().Append(path_suffix);
|
| +
|
| + // Setting the title and the error message is the same for all file types.
|
| + scoped_ptr<base::DictionaryValue> results(new base::DictionaryValue);
|
| + results->SetString(kTitleKey,
|
| + base::UTF8ToUTF16(extension->name()) +
|
| + base::ASCIIToUTF16(": ") +
|
| + path.BaseName().LossyDisplayName());
|
| + results->SetString(kErrorMessageKey, error_message);
|
| +
|
| + base::Closure closure;
|
| + std::string* contents = NULL;
|
| +
|
| + if (file_type == kManifestFileType) {
|
| + std::string feature;
|
| + std::string specific;
|
| + if (!dict->GetString(kFeatureKey, &feature)) {
|
| + NOTREACHED();
|
| + return;
|
| + }
|
| + // A "specific" location is optional.
|
| + dict->GetString(kSpecificKey, &specific);
|
| +
|
| + contents = new std::string; // Owned by GetManifestFileCallback()
|
| + closure = base::Bind(&ExtensionErrorHandler::GetManifestFileCallback,
|
| + base::Unretained(this),
|
| + base::Owned(results.release()),
|
| + feature,
|
| + specific,
|
| + base::Owned(contents));
|
| + } else { // currently, only manifest file types supported.
|
| + NOTREACHED();
|
| + return;
|
| + }
|
| +
|
| + content::BrowserThread::PostBlockingPoolTaskAndReply(
|
| + FROM_HERE,
|
| + base::Bind(base::IgnoreResult(&file_util::ReadFileToString),
|
| + path,
|
| + contents),
|
| + closure);
|
| +}
|
| +
|
| +void ExtensionErrorHandler::GetManifestFileCallback(
|
| + base::DictionaryValue* results,
|
| + const std::string& key,
|
| + const std::string& specific,
|
| + std::string* contents) {
|
| + ManifestHighlighter highlighter(*contents, key, specific);
|
| +
|
| + AddHighlightedToDictionaryForOverlay(
|
| + results,
|
| + highlighter.GetBeforeFeature(),
|
| + highlighter.GetFeature(),
|
| + highlighter.GetAfterFeature());
|
| +
|
| + web_ui()->CallJavascriptFunction(
|
| + "extensions.ExtensionErrorOverlay.requestFileSourceResponse", *results);
|
| +}
|
| +
|
| +} // namespace extensions
|
|
|