| Index: extensions/browser/api/execute_code_function.cc
|
| diff --git a/chrome/browser/extensions/api/execute_code_function.cc b/extensions/browser/api/execute_code_function.cc
|
| similarity index 66%
|
| rename from chrome/browser/extensions/api/execute_code_function.cc
|
| rename to extensions/browser/api/execute_code_function.cc
|
| index e5fd3662584491ec4ddcea0ea6a4c4e22dd57539..6df92b488f61c73ead15aa0ff200ca679745645f 100644
|
| --- a/chrome/browser/extensions/api/execute_code_function.cc
|
| +++ b/extensions/browser/api/execute_code_function.cc
|
| @@ -1,25 +1,37 @@
|
| -// Copyright (c) 2013 The Chromium Authors. All rights reserved.
|
| +// Copyright 2014 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/extensions/api/execute_code_function.h"
|
| +#ifndef EXTENSIONS_BROWSER_API_EXECUTE_CODE_FUNCTION_IMPL_H_
|
| +#define EXTENSIONS_BROWSER_API_EXECUTE_CODE_FUNCTION_IMPL_H_
|
| +
|
| +#include "extensions/browser/api/execute_code_function.h"
|
|
|
| -#include "chrome/browser/extensions/api/tabs/tabs_constants.h"
|
| -#include "chrome/common/extensions/api/i18n/default_locale_handler.h"
|
| #include "extensions/browser/component_extension_resource_manager.h"
|
| #include "extensions/browser/extensions_browser_client.h"
|
| #include "extensions/browser/file_reader.h"
|
| #include "extensions/common/error_utils.h"
|
| #include "extensions/common/extension_messages.h"
|
| #include "extensions/common/file_util.h"
|
| +#include "extensions/common/manifest_constants.h"
|
| #include "extensions/common/message_bundle.h"
|
| #include "net/base/filename_util.h"
|
| #include "ui/base/resource/resource_bundle.h"
|
|
|
| +namespace {
|
| +
|
| +// Error messages
|
| +const char kNoCodeOrFileToExecuteError[] = "No source code or file specified.";
|
| +const char kMoreThanOneValuesError[] =
|
| + "Code and file should not be specified "
|
| + "at the same time in the second argument.";
|
| +const char kLoadFileError[] = "Failed to load file: \"*\". ";
|
| +
|
| +}
|
| +
|
| namespace extensions {
|
|
|
| -namespace keys = tabs_constants;
|
| -using api::tabs::InjectDetails;
|
| +using core_api::extension_types::InjectDetails;
|
|
|
| ExecuteCodeFunction::ExecuteCodeFunction() {
|
| }
|
| @@ -27,9 +39,7 @@ ExecuteCodeFunction::ExecuteCodeFunction() {
|
| ExecuteCodeFunction::~ExecuteCodeFunction() {
|
| }
|
|
|
| -void ExecuteCodeFunction::DidLoadFile(bool success,
|
| - const std::string& data) {
|
| -
|
| +void ExecuteCodeFunction::DidLoadFile(bool success, const std::string& data) {
|
| if (!success || !details_->file) {
|
| DidLoadAndLocalizeFile(success, data);
|
| return;
|
| @@ -44,17 +54,20 @@ void ExecuteCodeFunction::DidLoadFile(bool success,
|
| if (extension()) {
|
| extension_id = extension()->id();
|
| extension_path = extension()->path();
|
| - extension_default_locale = LocaleInfo::GetDefaultLocale(extension());
|
| + extension()->manifest()->GetString(manifest_keys::kDefaultLocale,
|
| + &extension_default_locale);
|
| }
|
|
|
| content::BrowserThread::PostTask(
|
| - content::BrowserThread::FILE, FROM_HERE,
|
| - base::Bind(&ExecuteCodeFunction::GetFileURLAndLocalizeCSS, this,
|
| - script_type,
|
| - data,
|
| - extension_id,
|
| - extension_path,
|
| - extension_default_locale));
|
| + content::BrowserThread::FILE,
|
| + FROM_HERE,
|
| + base::Bind(&ExecuteCodeFunction::GetFileURLAndLocalizeCSS,
|
| + this,
|
| + script_type,
|
| + data,
|
| + extension_id,
|
| + extension_path,
|
| + extension_default_locale));
|
| }
|
|
|
| void ExecuteCodeFunction::GetFileURLAndLocalizeCSS(
|
| @@ -63,11 +76,9 @@ void ExecuteCodeFunction::GetFileURLAndLocalizeCSS(
|
| const std::string& extension_id,
|
| const base::FilePath& extension_path,
|
| const std::string& extension_default_locale) {
|
| -
|
| std::string localized_data = data;
|
| // Check if the file is CSS and needs localization.
|
| - if ((script_type == ScriptExecutor::CSS) &&
|
| - !extension_id.empty() &&
|
| + if ((script_type == ScriptExecutor::CSS) && !extension_id.empty() &&
|
| (data.find(MessageBundle::kMessageBegin) != std::string::npos)) {
|
| scoped_ptr<SubstitutionMap> localization_messages(
|
| file_util::LoadMessageBundleSubstitutionMap(
|
| @@ -75,9 +86,8 @@ void ExecuteCodeFunction::GetFileURLAndLocalizeCSS(
|
|
|
| // We need to do message replacement on the data, so it has to be mutable.
|
| std::string error;
|
| - MessageBundle::ReplaceMessagesWithExternalDictionary(*localization_messages,
|
| - &localized_data,
|
| - &error);
|
| + MessageBundle::ReplaceMessagesWithExternalDictionary(
|
| + *localization_messages, &localized_data, &error);
|
| }
|
|
|
| file_url_ = net::FilePathToFileURL(resource_.GetFilePath());
|
| @@ -86,9 +96,12 @@ void ExecuteCodeFunction::GetFileURLAndLocalizeCSS(
|
| // is always true, because if loading had failed, we wouldn't have had
|
| // anything to localize.
|
| content::BrowserThread::PostTask(
|
| - content::BrowserThread::UI, FROM_HERE,
|
| - base::Bind(&ExecuteCodeFunction::DidLoadAndLocalizeFile, this,
|
| - true, localized_data));
|
| + content::BrowserThread::UI,
|
| + FROM_HERE,
|
| + base::Bind(&ExecuteCodeFunction::DidLoadAndLocalizeFile,
|
| + this,
|
| + true,
|
| + localized_data));
|
| }
|
|
|
| void ExecuteCodeFunction::DidLoadAndLocalizeFile(bool success,
|
| @@ -99,8 +112,8 @@ void ExecuteCodeFunction::DidLoadAndLocalizeFile(bool success,
|
| } else {
|
| // TODO(viettrungluu): bug: there's no particular reason the path should be
|
| // UTF-8, in which case this may fail.
|
| - error_ = ErrorUtils::FormatErrorMessage(keys::kLoadFileError,
|
| - resource_.relative_path().AsUTF8Unsafe());
|
| + error_ = ErrorUtils::FormatErrorMessage(
|
| + kLoadFileError, resource_.relative_path().AsUTF8Unsafe());
|
| SendResponse(false);
|
| }
|
| }
|
| @@ -118,17 +131,16 @@ bool ExecuteCodeFunction::Execute(const std::string& code_string) {
|
| script_type = ScriptExecutor::CSS;
|
|
|
| ScriptExecutor::FrameScope frame_scope =
|
| - details_->all_frames.get() && *details_->all_frames ?
|
| - ScriptExecutor::ALL_FRAMES :
|
| - ScriptExecutor::TOP_FRAME;
|
| + details_->all_frames.get() && *details_->all_frames
|
| + ? ScriptExecutor::ALL_FRAMES
|
| + : ScriptExecutor::TOP_FRAME;
|
|
|
| ScriptExecutor::MatchAboutBlank match_about_blank =
|
| - details_->match_about_blank.get() && *details_->match_about_blank ?
|
| - ScriptExecutor::MATCH_ABOUT_BLANK :
|
| - ScriptExecutor::DONT_MATCH_ABOUT_BLANK;
|
| + details_->match_about_blank.get() && *details_->match_about_blank
|
| + ? ScriptExecutor::MATCH_ABOUT_BLANK
|
| + : ScriptExecutor::DONT_MATCH_ABOUT_BLANK;
|
|
|
| - UserScript::RunLocation run_at =
|
| - UserScript::UNDEFINED;
|
| + UserScript::RunLocation run_at = UserScript::UNDEFINED;
|
| switch (details_->run_at) {
|
| case InjectDetails::RUN_AT_NONE:
|
| case InjectDetails::RUN_AT_DOCUMENT_IDLE:
|
| @@ -170,11 +182,11 @@ bool ExecuteCodeFunction::RunAsync() {
|
| EXTENSION_FUNCTION_VALIDATE(Init());
|
|
|
| if (!details_->code.get() && !details_->file.get()) {
|
| - error_ = keys::kNoCodeOrFileToExecuteError;
|
| + error_ = kNoCodeOrFileToExecuteError;
|
| return false;
|
| }
|
| if (details_->code.get() && details_->file.get()) {
|
| - error_ = keys::kMoreThanOneValuesError;
|
| + error_ = kMoreThanOneValuesError;
|
| return false;
|
| }
|
|
|
| @@ -189,15 +201,16 @@ bool ExecuteCodeFunction::RunAsync() {
|
| resource_ = extension()->GetResource(*details_->file);
|
|
|
| if (resource_.extension_root().empty() || resource_.relative_path().empty()) {
|
| - error_ = keys::kNoCodeOrFileToExecuteError;
|
| + error_ = kNoCodeOrFileToExecuteError;
|
| return false;
|
| }
|
|
|
| int resource_id;
|
| - if (ExtensionsBrowserClient::Get()->GetComponentExtensionResourceManager()->
|
| - IsComponentExtensionResource(
|
| - resource_.extension_root(), resource_.relative_path(),
|
| - &resource_id)) {
|
| + if (ExtensionsBrowserClient::Get()
|
| + ->GetComponentExtensionResourceManager()
|
| + ->IsComponentExtensionResource(resource_.extension_root(),
|
| + resource_.relative_path(),
|
| + &resource_id)) {
|
| const ResourceBundle& rb = ResourceBundle::GetSharedInstance();
|
| DidLoadFile(true, rb.GetRawDataResource(resource_id).as_string());
|
| } else {
|
| @@ -209,10 +222,9 @@ bool ExecuteCodeFunction::RunAsync() {
|
| return true;
|
| }
|
|
|
| -void ExecuteCodeFunction::OnExecuteCodeFinished(
|
| - const std::string& error,
|
| - const GURL& on_url,
|
| - const base::ListValue& result) {
|
| +void ExecuteCodeFunction::OnExecuteCodeFinished(const std::string& error,
|
| + const GURL& on_url,
|
| + const base::ListValue& result) {
|
| if (!error.empty())
|
| SetError(error);
|
|
|
| @@ -220,3 +232,5 @@ void ExecuteCodeFunction::OnExecuteCodeFinished(
|
| }
|
|
|
| } // namespace extensions
|
| +
|
| +#endif // EXTENSIONS_BROWSER_API_EXECUTE_CODE_FUNCTION_IMPL_H_
|
|
|