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

Side by Side Diff: chrome/browser/extensions/api/tabs/execute_code_in_tab_function.cc

Issue 10815028: Make ActiveTabPermissionManager also grant the tabs permission. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 8 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/extensions/api/tabs/execute_code_in_tab_function.h" 5 #include "chrome/browser/extensions/api/tabs/execute_code_in_tab_function.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/string_util.h" 8 #include "base/string_util.h"
9 #include "base/utf_string_conversions.h" 9 #include "base/utf_string_conversions.h"
10 #include "chrome/browser/extensions/api/tabs/tabs.h" 10 #include "chrome/browser/extensions/api/tabs/tabs.h"
11 #include "chrome/browser/extensions/api/tabs/tabs_constants.h" 11 #include "chrome/browser/extensions/api/tabs/tabs_constants.h"
12 #include "chrome/browser/extensions/extension_service.h" 12 #include "chrome/browser/extensions/extension_service.h"
13 #include "chrome/browser/extensions/extension_tab_util.h" 13 #include "chrome/browser/extensions/extension_tab_util.h"
14 #include "chrome/browser/extensions/tab_helper.h" 14 #include "chrome/browser/extensions/tab_helper.h"
15 #include "chrome/browser/extensions/file_reader.h" 15 #include "chrome/browser/extensions/file_reader.h"
16 #include "chrome/browser/extensions/script_executor.h" 16 #include "chrome/browser/extensions/script_executor.h"
17 #include "chrome/browser/profiles/profile.h" 17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/browser/ui/browser.h" 18 #include "chrome/browser/ui/browser.h"
19 #include "chrome/browser/ui/tab_contents/tab_contents.h" 19 #include "chrome/browser/ui/tab_contents/tab_contents.h"
20 #include "chrome/common/extensions/api/tabs.h"
20 #include "chrome/common/extensions/extension.h" 21 #include "chrome/common/extensions/extension.h"
21 #include "chrome/common/extensions/extension_constants.h" 22 #include "chrome/common/extensions/extension_constants.h"
22 #include "chrome/common/extensions/extension_error_utils.h" 23 #include "chrome/common/extensions/extension_error_utils.h"
23 #include "chrome/common/extensions/extension_file_util.h" 24 #include "chrome/common/extensions/extension_file_util.h"
24 #include "chrome/common/extensions/extension_l10n_util.h" 25 #include "chrome/common/extensions/extension_l10n_util.h"
25 #include "chrome/common/extensions/extension_manifest_constants.h" 26 #include "chrome/common/extensions/extension_manifest_constants.h"
26 #include "chrome/common/extensions/extension_message_bundle.h" 27 #include "chrome/common/extensions/extension_message_bundle.h"
27 #include "chrome/common/extensions/extension_messages.h" 28 #include "chrome/common/extensions/extension_messages.h"
28 #include "content/public/browser/render_view_host.h" 29 #include "content/public/browser/render_view_host.h"
29 #include "content/public/browser/web_contents.h" 30 #include "content/public/browser/web_contents.h"
30 31
31 using content::BrowserThread; 32 using content::BrowserThread;
33 using extensions::api::tabs::InjectDetails;
32 using extensions::ScriptExecutor; 34 using extensions::ScriptExecutor;
35 using extensions::UserScript;
33 36
34 namespace keys = extensions::tabs_constants; 37 namespace keys = extensions::tabs_constants;
35 38
36 ExecuteCodeInTabFunction::ExecuteCodeInTabFunction() 39 ExecuteCodeInTabFunction::ExecuteCodeInTabFunction()
37 : execute_tab_id_(-1), 40 : execute_tab_id_(-1) {
38 all_frames_(false),
39 run_at_(extensions::UserScript::DOCUMENT_IDLE) {
40 } 41 }
41 42
42 ExecuteCodeInTabFunction::~ExecuteCodeInTabFunction() {} 43 ExecuteCodeInTabFunction::~ExecuteCodeInTabFunction() {}
43 44
45 bool ExecuteCodeInTabFunction::HasPermission() {
46 if (Init() &&
47 execute_tab_id_ >= 0 &&
48 extension_->HasAPIPermissionForTab(execute_tab_id_,
49 extensions::APIPermission::kTab)) {
50 return true;
51 }
52 return ExtensionFunction::HasPermission();
53 }
54
44 bool ExecuteCodeInTabFunction::RunImpl() { 55 bool ExecuteCodeInTabFunction::RunImpl() {
45 DictionaryValue* script_info; 56 EXTENSION_FUNCTION_VALIDATE(Init());
46 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &script_info)); 57
47 size_t number_of_value = script_info->size(); 58 if (!details_->code.get() && !details_->file.get()) {
48 if (number_of_value == 0) {
49 error_ = keys::kNoCodeOrFileToExecuteError; 59 error_ = keys::kNoCodeOrFileToExecuteError;
50 return false; 60 return false;
51 } else { 61 }
52 bool has_code = script_info->HasKey(keys::kCodeKey); 62 if (details_.get() && details_->file.get()) {
53 bool has_file = script_info->HasKey(keys::kFileKey); 63 error_ = keys::kMoreThanOneValuesError;
54 if (has_code && has_file) { 64 return false;
55 error_ = keys::kMoreThanOneValuesError;
56 return false;
57 } else if (!has_code && !has_file) {
58 error_ = keys::kNoCodeOrFileToExecuteError;
59 return false;
60 }
61 } 65 }
62 66
63 execute_tab_id_ = -1;
64 Browser* browser = NULL; 67 Browser* browser = NULL;
65 TabContents* contents = NULL; 68 TabContents* contents = NULL;
66 69
67 // If |tab_id| is specified, look for it. Otherwise default to selected tab 70 // If |tab_id| is specified, look for the tab. Otherwise default to selected
68 // in the current window. 71 // tab in the current window.
69 Value* tab_value = NULL; 72 if (execute_tab_id_ == -1) {
70 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &tab_value));
71 if (tab_value->IsType(Value::TYPE_NULL)) {
72 browser = GetCurrentBrowser(); 73 browser = GetCurrentBrowser();
73 if (!browser) { 74 if (!browser) {
74 error_ = keys::kNoCurrentWindowError; 75 error_ = keys::kNoCurrentWindowError;
75 return false; 76 return false;
76 } 77 }
77 if (!ExtensionTabUtil::GetDefaultTab(browser, &contents, &execute_tab_id_)) 78 if (!ExtensionTabUtil::GetDefaultTab(browser, &contents, &execute_tab_id_))
78 return false; 79 return false;
79 } else { 80 } else {
80 EXTENSION_FUNCTION_VALIDATE(tab_value->GetAsInteger(&execute_tab_id_));
81 if (!ExtensionTabUtil::GetTabById(execute_tab_id_, profile(), 81 if (!ExtensionTabUtil::GetTabById(execute_tab_id_, profile(),
82 include_incognito(), 82 include_incognito(),
83 &browser, NULL, &contents, NULL)) { 83 &browser, NULL, &contents, NULL)) {
84 return false; 84 return false;
85 } 85 }
86 } 86 }
87 87
88 // NOTE: This can give the wrong answer due to race conditions, but it is OK, 88 // NOTE: This can give the wrong answer due to race conditions, but it is OK,
89 // we check again in the renderer. 89 // we check again in the renderer.
90 CHECK(browser); 90 CHECK(browser);
91 CHECK(contents); 91 CHECK(contents);
92 if (!GetExtension()->CanExecuteScriptOnPage( 92 if (!GetExtension()->CanExecuteScriptOnPage(
93 contents->web_contents()->GetURL(), execute_tab_id_, NULL, &error_)) { 93 contents->web_contents()->GetURL(), execute_tab_id_, NULL, &error_)) {
94 return false; 94 return false;
95 } 95 }
96 96
97 if (script_info->HasKey(keys::kAllFramesKey)) {
98 if (!script_info->GetBoolean(keys::kAllFramesKey, &all_frames_))
99 return false;
100 }
101 97
102 if (script_info->HasKey(keys::kRunAtKey)) {
103 std::string run_string;
104 EXTENSION_FUNCTION_VALIDATE(script_info->GetString(
105 keys::kRunAtKey, &run_string));
106 98
107 if (run_string == extension_manifest_values::kRunAtDocumentStart) 99 if (details_->code.get())
108 run_at_ = extensions::UserScript::DOCUMENT_START; 100 return Execute(*details_->code);
109 else if (run_string == extension_manifest_values::kRunAtDocumentEnd)
110 run_at_ = extensions::UserScript::DOCUMENT_END;
111 else if (run_string == extension_manifest_values::kRunAtDocumentIdle)
112 run_at_ = extensions::UserScript::DOCUMENT_IDLE;
113 else
114 EXTENSION_FUNCTION_VALIDATE(false);
115 }
116 101
117 std::string code_string; 102 CHECK(details_->file.get());
118 if (script_info->HasKey(keys::kCodeKey)) { 103 resource_ = GetExtension()->GetResource(*details_->file);
119 if (!script_info->GetString(keys::kCodeKey, &code_string))
120 return false;
121 }
122 104
123 if (!code_string.empty())
124 return Execute(code_string);
125
126 std::string relative_path;
127 if (script_info->HasKey(keys::kFileKey)) {
128 if (!script_info->GetString(keys::kFileKey, &relative_path))
129 return false;
130 resource_ = GetExtension()->GetResource(relative_path);
131 }
132 if (resource_.extension_root().empty() || resource_.relative_path().empty()) { 105 if (resource_.extension_root().empty() || resource_.relative_path().empty()) {
133 error_ = keys::kNoCodeOrFileToExecuteError; 106 error_ = keys::kNoCodeOrFileToExecuteError;
134 return false; 107 return false;
135 } 108 }
136 109
137 scoped_refptr<FileReader> file_reader(new FileReader( 110 scoped_refptr<FileReader> file_reader(new FileReader(
138 resource_, base::Bind(&ExecuteCodeInTabFunction::DidLoadFile, this))); 111 resource_, base::Bind(&ExecuteCodeInTabFunction::DidLoadFile, this)));
139 file_reader->Start(); 112 file_reader->Start();
140 113
141 return true; 114 return true;
(...skipping 14 matching lines...) Expand all
156 void TabsExecuteScriptFunction::OnExecuteCodeFinished(bool success, 129 void TabsExecuteScriptFunction::OnExecuteCodeFinished(bool success,
157 int32 page_id, 130 int32 page_id,
158 const std::string& error, 131 const std::string& error,
159 const ListValue& result) { 132 const ListValue& result) {
160 if (error.empty()) 133 if (error.empty())
161 SetResult(result.DeepCopy()); 134 SetResult(result.DeepCopy());
162 ExecuteCodeInTabFunction::OnExecuteCodeFinished(success, page_id, error, 135 ExecuteCodeInTabFunction::OnExecuteCodeFinished(success, page_id, error,
163 result); 136 result);
164 } 137 }
165 138
139 bool ExecuteCodeInTabFunction::Init() {
140 if (details_.get())
141 return true;
142
143 // tab_id is optional so it's ok if it's not there.
144 int tab_id = -1;
145 args_->GetInteger(0, &tab_id);
146
147 // details are not optional.
148 DictionaryValue* details_value = NULL;
149 if (!args_->GetDictionary(1, &details_value))
150 return false;
151 scoped_ptr<InjectDetails> details(new InjectDetails());
152 if (!InjectDetails::Populate(*details_value, details.get()))
153 return false;
154
155 execute_tab_id_ = tab_id;
156 details_ = details.Pass();
157 return true;
158 }
159
166 void ExecuteCodeInTabFunction::DidLoadFile(bool success, 160 void ExecuteCodeInTabFunction::DidLoadFile(bool success,
167 const std::string& data) { 161 const std::string& data) {
168 std::string function_name = name(); 162 std::string function_name = name();
169 const extensions::Extension* extension = GetExtension(); 163 const extensions::Extension* extension = GetExtension();
170 164
171 // Check if the file is CSS and needs localization. 165 // Check if the file is CSS and needs localization.
172 if (success && 166 if (success &&
173 function_name == TabsInsertCSSFunction::function_name() && 167 function_name == TabsInsertCSSFunction::function_name() &&
174 extension != NULL && 168 extension != NULL &&
175 data.find(ExtensionMessageBundle::kMessageBegin) != std::string::npos) { 169 data.find(ExtensionMessageBundle::kMessageBegin) != std::string::npos) {
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
244 return false; 238 return false;
245 239
246 ScriptExecutor::ScriptType script_type = ScriptExecutor::JAVASCRIPT; 240 ScriptExecutor::ScriptType script_type = ScriptExecutor::JAVASCRIPT;
247 std::string function_name = name(); 241 std::string function_name = name();
248 if (function_name == TabsInsertCSSFunction::function_name()) { 242 if (function_name == TabsInsertCSSFunction::function_name()) {
249 script_type = ScriptExecutor::CSS; 243 script_type = ScriptExecutor::CSS;
250 } else if (function_name != TabsExecuteScriptFunction::function_name()) { 244 } else if (function_name != TabsExecuteScriptFunction::function_name()) {
251 NOTREACHED(); 245 NOTREACHED();
252 } 246 }
253 247
248 ScriptExecutor::FrameScope frame_scope =
249 details_->all_frames.get() && *details_->all_frames ?
250 ScriptExecutor::ALL_FRAMES :
251 ScriptExecutor::TOP_FRAME;
252
253 UserScript::RunLocation run_at = UserScript::UNDEFINED;
254 switch (details_->run_at) {
255 case InjectDetails::RUN_AT_NONE:
256 case InjectDetails::RUN_AT_DOCUMENT_IDLE:
257 run_at = UserScript::DOCUMENT_IDLE;
258 break;
259 case InjectDetails::RUN_AT_DOCUMENT_START:
260 run_at = UserScript::DOCUMENT_START;
261 break;
262 case InjectDetails::RUN_AT_DOCUMENT_END:
263 run_at = UserScript::DOCUMENT_END;
264 break;
265 }
266 CHECK_NE(UserScript::UNDEFINED, run_at);
267
254 contents->extension_tab_helper()->script_executor()->ExecuteScript( 268 contents->extension_tab_helper()->script_executor()->ExecuteScript(
255 extension->id(), 269 extension->id(),
256 script_type, 270 script_type,
257 code_string, 271 code_string,
258 all_frames_ ? ScriptExecutor::ALL_FRAMES : ScriptExecutor::TOP_FRAME, 272 frame_scope,
259 run_at_, 273 run_at,
260 ScriptExecutor::ISOLATED_WORLD, 274 ScriptExecutor::ISOLATED_WORLD,
261 base::Bind(&ExecuteCodeInTabFunction::OnExecuteCodeFinished, this)); 275 base::Bind(&ExecuteCodeInTabFunction::OnExecuteCodeFinished, this));
262 return true; 276 return true;
263 } 277 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698