Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(6996)

Unified Diff: chrome/browser/ui/webui/extensions/extension_error_handler.cc

Issue 22938005: Add ErrorConsole UI for Extension Install Warnings (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@dc_ec_install_warnings
Patch Set: Rebase to Master Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..ee39196cefc78548d9a5a1e215bd9845f5999e73
--- /dev/null
+++ b/chrome/browser/ui/webui/extensions/extension_error_handler.cc
@@ -0,0 +1,273 @@
+// 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 "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));
+}
+
+// Find the end of a feature in the manifest string. See also
+// FindFeatureBounds().
+// |i| points to the start location of the feature, which is defined as the
+// first character after the name of the feature.
+size_t FindFeatureEnd(const std::string& manifest, size_t i) {
+ size_t levels = 0;
+ bool quoted = false;
+
+ // If the feature is a string, we may need to account for the ending quote.
+ if (manifest[i] == '"')
+ ++i;
+
+ char c = manifest[i];
+
+ // We loop until we find the end of a feature. We have found the end of a
+ // feature when we are at the initial level, are not quoted, and have found
+ // a terminating character (a comma or ending bracket). Also be sure to not
+ // run off the end.
+ while (!(levels == 0 && !quoted && (c == ',' || c == '}' || c == ']')) &&
+ i < manifest.size() - 1) {
+ if (!quoted && (c == '{' || c == '['))
+ ++levels;
+ else if (!quoted && (c == '}' || c == ']'))
+ --levels;
+ else if (c == '"')
+ quoted = !quoted;
+ c = manifest[++i];
+ }
+ return i;
+}
+
+// Find the bounds of a feature in the manifest (for highlighting purposes), and
+// populate |start| and |end| accordingly. Return true on success and false on
+// failure.
+// A feature can be at any level in the hierarchy. The "start" of a feature is
+// the first character of the feature name, or the beginning quote of the name,
+// if present. The "end" of a feature is wherever the next item at the same
+// level starts.
+// For instance, the bounds for the 'permissions' feature at the top level could
+// be '"permissions": { "tabs", "history", "downloads" }', but the feature for
+// 'tabs' within 'permissions' would just be '"tabs"'.
+// We can't use JSON to do this, because we want to display the actual manifest,
+// and once we parse it into JSON, we lose any formatting the user may have had.
+//
+// |manifest| is the string containing the manifest content.
+// |feature| is the name of the feature we are searching for.
+// |enforce_at_top_level| means that the feature is guaranteed to be at the top
+// level of the manifest (relative to |start|), so we can (and should) skip
+// any nested occurrences. This is useful in case, e.g., we are looking for
+// the "permissions" feature, and the manifest also includes a "permissions"
+// resource file.
+// |start| can point to a location at which we start our search, and will be
+// populated with the start of the feature.
+// |end| can point to a location which we should not pass, and will be populated
+// with the end of the feature.
+bool FindFeatureBounds(const std::string& manifest,
+ const std::string& feature,
+ bool enforce_at_top_level,
+ size_t* start,
+ size_t* end) {
+ for (size_t i = *start;
+ i < *end && i < manifest.size() && i != std::string::npos; ++i) {
+ if (manifest.substr(i, feature.size()) == feature) {
+ if (manifest[i - 1] == '"')
+ *start = i - 1; // include the quote at the beginning of the feature.
+
+ *end = FindFeatureEnd(manifest, i + feature.size());
+ return true;
+ } else if (manifest[i] == '{' && enforce_at_top_level) {
+ i = manifest.find('}', i);
+ } else if (manifest[i] == '[' && enforce_at_top_level) {
+ i = manifest.find(']', i);
+ }
+ }
+ return false;
+}
+
+} // 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) {
+ // We initially start at the opening of the manifest dictionary, and end at
+ // it's termination.
+ size_t feature_start = contents->find('{');
+ size_t feature_end = contents->rfind('}');
+
+ // Find the bounds of the feature in order to highlight them.
+ FindFeatureBounds(*contents, key, true, &feature_start, &feature_end);
+
+ // If we were given a specific location in the feature, find it to highlight.
+ if (!specific.empty())
+ FindFeatureBounds(*contents, specific, false, &feature_start, &feature_end);
+
+ // We may have found trailing whitespace. Don't use base::TrimWhitespace,
+ // because we want to keep any whitespace we find - just not highlight it.
+ size_t trim = contents->find_last_not_of(" \t\n\r", feature_end - 1);
+ if (trim < feature_end && trim > feature_start)
+ feature_end = trim + 1;
+
+ // Break contents into three sections - unhighlighted before, highlighted, and
+ // unhighlighted after. The highlighted portion should be the feature (or
+ // specific portion of the feature).
+ // If we never found bounds for the feature, then there will be no highlighted
Yoyo Zhou 2013/08/16 00:17:26 It looks like we might highlight everything.
Devlin 2013/08/16 18:07:49 D'oh. Fixed.
+ // portion.
+ AddHighlightedToDictionaryForOverlay(
+ results,
+ contents->substr(0, feature_start), // before highlight
+ contents->substr(feature_start,
+ feature_end - feature_start), // highlight
+ contents->substr(feature_end) /* after highlight */ );
+
+ web_ui()->CallJavascriptFunction(
+ "ExtensionErrorOverlay.requestFileSourceResponse", *results);
+}
+
+} // namespace extensions

Powered by Google App Engine
This is Rietveld 408576698