11320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci// Copyright 2014 The Chromium Authors. All rights reserved.
2a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)// found in the LICENSE file.
4a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
51320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#ifndef EXTENSIONS_BROWSER_API_EXECUTE_CODE_FUNCTION_IMPL_H_
61320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#define EXTENSIONS_BROWSER_API_EXECUTE_CODE_FUNCTION_IMPL_H_
71320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
81320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "extensions/browser/api/execute_code_function.h"
9a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
106d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)#include "extensions/browser/component_extension_resource_manager.h"
116d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)#include "extensions/browser/extensions_browser_client.h"
12a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)#include "extensions/browser/file_reader.h"
13a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)#include "extensions/common/error_utils.h"
14a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "extensions/common/extension_messages.h"
15c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch#include "extensions/common/file_util.h"
161320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "extensions/common/manifest_constants.h"
17c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch#include "extensions/common/message_bundle.h"
18c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch#include "net/base/filename_util.h"
193551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#include "ui/base/resource/resource_bundle.h"
20a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
211320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccinamespace {
221320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
231320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci// Error messages
241320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucciconst char kNoCodeOrFileToExecuteError[] = "No source code or file specified.";
251320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucciconst char kMoreThanOneValuesError[] =
261320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    "Code and file should not be specified "
271320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    "at the same time in the second argument.";
281320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucciconst char kLoadFileError[] = "Failed to load file: \"*\". ";
291320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
301320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
311320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
32a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)namespace extensions {
33a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
341320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucciusing core_api::extension_types::InjectDetails;
35a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
36a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)ExecuteCodeFunction::ExecuteCodeFunction() {
37a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
38a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
39a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)ExecuteCodeFunction::~ExecuteCodeFunction() {
40a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
41a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
421320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccivoid ExecuteCodeFunction::DidLoadFile(bool success, const std::string& data) {
434e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  if (!success || !details_->file) {
44a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    DidLoadAndLocalizeFile(success, data);
454e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    return;
464e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  }
474e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
484e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  ScriptExecutor::ScriptType script_type =
494e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      ShouldInsertCSS() ? ScriptExecutor::CSS : ScriptExecutor::JAVASCRIPT;
504e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
514e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  std::string extension_id;
524e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  base::FilePath extension_path;
534e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  std::string extension_default_locale;
545f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  if (extension()) {
555f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    extension_id = extension()->id();
565f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    extension_path = extension()->path();
571320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    extension()->manifest()->GetString(manifest_keys::kDefaultLocale,
581320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                       &extension_default_locale);
59a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  }
604e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
614e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  content::BrowserThread::PostTask(
621320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      content::BrowserThread::FILE,
631320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      FROM_HERE,
641320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      base::Bind(&ExecuteCodeFunction::GetFileURLAndLocalizeCSS,
651320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                 this,
661320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                 script_type,
671320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                 data,
681320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                 extension_id,
691320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                 extension_path,
701320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                 extension_default_locale));
71a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
72a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
734e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)void ExecuteCodeFunction::GetFileURLAndLocalizeCSS(
744e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    ScriptExecutor::ScriptType script_type,
75a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    const std::string& data,
76a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    const std::string& extension_id,
77a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    const base::FilePath& extension_path,
78a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    const std::string& extension_default_locale) {
794e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  std::string localized_data = data;
804e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // Check if the file is CSS and needs localization.
811320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if ((script_type == ScriptExecutor::CSS) && !extension_id.empty() &&
824e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      (data.find(MessageBundle::kMessageBegin) != std::string::npos)) {
834e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    scoped_ptr<SubstitutionMap> localization_messages(
84c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch        file_util::LoadMessageBundleSubstitutionMap(
854e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)            extension_path, extension_id, extension_default_locale));
864e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
874e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    // We need to do message replacement on the data, so it has to be mutable.
884e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    std::string error;
891320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    MessageBundle::ReplaceMessagesWithExternalDictionary(
901320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        *localization_messages, &localized_data, &error);
914e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  }
924e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
934e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  file_url_ = net::FilePathToFileURL(resource_.GetFilePath());
94a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
95a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  // Call back DidLoadAndLocalizeFile on the UI thread. The success parameter
96a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  // is always true, because if loading had failed, we wouldn't have had
97a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  // anything to localize.
98a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  content::BrowserThread::PostTask(
991320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      content::BrowserThread::UI,
1001320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      FROM_HERE,
1011320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      base::Bind(&ExecuteCodeFunction::DidLoadAndLocalizeFile,
1021320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                 this,
1031320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                 true,
1041320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                 localized_data));
105a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
106a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
107a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)void ExecuteCodeFunction::DidLoadAndLocalizeFile(bool success,
108a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)                                                 const std::string& data) {
109a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  if (success) {
110a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    if (!Execute(data))
111a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      SendResponse(false);
112a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  } else {
113a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    // TODO(viettrungluu): bug: there's no particular reason the path should be
114a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    // UTF-8, in which case this may fail.
1151320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    error_ = ErrorUtils::FormatErrorMessage(
1161320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        kLoadFileError, resource_.relative_path().AsUTF8Unsafe());
117a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    SendResponse(false);
118a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  }
119a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
120a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
121a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)bool ExecuteCodeFunction::Execute(const std::string& code_string) {
122a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  ScriptExecutor* executor = GetScriptExecutor();
123a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  if (!executor)
124a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    return false;
125a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
1265f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  if (!extension())
127a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    return false;
128a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
129a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  ScriptExecutor::ScriptType script_type = ScriptExecutor::JAVASCRIPT;
130a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  if (ShouldInsertCSS())
131a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    script_type = ScriptExecutor::CSS;
132a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
133a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  ScriptExecutor::FrameScope frame_scope =
1341320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      details_->all_frames.get() && *details_->all_frames
1351320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci          ? ScriptExecutor::ALL_FRAMES
1361320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci          : ScriptExecutor::TOP_FRAME;
137a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
138cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  ScriptExecutor::MatchAboutBlank match_about_blank =
1391320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      details_->match_about_blank.get() && *details_->match_about_blank
1401320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci          ? ScriptExecutor::MATCH_ABOUT_BLANK
1411320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci          : ScriptExecutor::DONT_MATCH_ABOUT_BLANK;
142cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
1431320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  UserScript::RunLocation run_at = UserScript::UNDEFINED;
144a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  switch (details_->run_at) {
145a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    case InjectDetails::RUN_AT_NONE:
146a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    case InjectDetails::RUN_AT_DOCUMENT_IDLE:
147a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      run_at = UserScript::DOCUMENT_IDLE;
148a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      break;
149a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    case InjectDetails::RUN_AT_DOCUMENT_START:
150a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      run_at = UserScript::DOCUMENT_START;
151a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      break;
152a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    case InjectDetails::RUN_AT_DOCUMENT_END:
153a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      run_at = UserScript::DOCUMENT_END;
154a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      break;
155a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  }
156a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  CHECK_NE(UserScript::UNDEFINED, run_at);
157a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
158a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  executor->ExecuteScript(
1595f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      extension()->id(),
160a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      script_type,
161a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      code_string,
162a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      frame_scope,
163cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      match_about_blank,
164a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      run_at,
165a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      ScriptExecutor::ISOLATED_WORLD,
166424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      IsWebView() ? ScriptExecutor::WEB_VIEW_PROCESS
167424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                  : ScriptExecutor::DEFAULT_PROCESS,
168a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch      GetWebViewSrc(),
1694e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      file_url_,
170a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      user_gesture_,
171424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      has_callback() ? ScriptExecutor::JSON_SERIALIZED_RESULT
172424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                     : ScriptExecutor::NO_RESULT,
173a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)      base::Bind(&ExecuteCodeFunction::OnExecuteCodeFinished, this));
174a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  return true;
175a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
176a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
177a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)bool ExecuteCodeFunction::HasPermission() {
178a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  return true;
179a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
180a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
181010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)bool ExecuteCodeFunction::RunAsync() {
182a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  EXTENSION_FUNCTION_VALIDATE(Init());
183a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
184a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  if (!details_->code.get() && !details_->file.get()) {
1851320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    error_ = kNoCodeOrFileToExecuteError;
186a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    return false;
187a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  }
188a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  if (details_->code.get() && details_->file.get()) {
1891320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    error_ = kMoreThanOneValuesError;
190a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    return false;
191a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  }
192a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
193a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  if (!CanExecuteScriptOnPage())
194a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    return false;
195a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
196a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  if (details_->code.get())
197a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    return Execute(*details_->code);
198a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
199a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  if (!details_->file.get())
200a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    return false;
2015f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  resource_ = extension()->GetResource(*details_->file);
202a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
203a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  if (resource_.extension_root().empty() || resource_.relative_path().empty()) {
2041320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    error_ = kNoCodeOrFileToExecuteError;
205a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    return false;
206a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  }
207a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
2083551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  int resource_id;
2091320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (ExtensionsBrowserClient::Get()
2101320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci          ->GetComponentExtensionResourceManager()
2111320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci          ->IsComponentExtensionResource(resource_.extension_root(),
2121320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                         resource_.relative_path(),
2131320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                         &resource_id)) {
2143551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    const ResourceBundle& rb = ResourceBundle::GetSharedInstance();
2153551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    DidLoadFile(true, rb.GetRawDataResource(resource_id).as_string());
2163551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  } else {
2173551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    scoped_refptr<FileReader> file_reader(new FileReader(
2183551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)        resource_, base::Bind(&ExecuteCodeFunction::DidLoadFile, this)));
2193551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    file_reader->Start();
2203551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  }
221a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
222a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  return true;
223a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
224a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
2251320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccivoid ExecuteCodeFunction::OnExecuteCodeFinished(const std::string& error,
2261320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                                const GURL& on_url,
2271320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                                const base::ListValue& result) {
228a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  if (!error.empty())
229a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)    SetError(error);
230a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
231a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)  SendResponse(error.empty());
232a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}
233a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)
234a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles)}  // namespace extensions
2351320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
2361320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#endif  // EXTENSIONS_BROWSER_API_EXECUTE_CODE_FUNCTION_IMPL_H_
237