OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/execute_code_in_tab_function.h" | 5 #include "chrome/browser/extensions/execute_code_in_tab_function.h" |
6 | 6 |
7 #include "base/callback.h" | 7 #include "base/callback.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/extension_tabs_module.h" | 10 #include "chrome/browser/extensions/extension_tabs_module.h" |
11 #include "chrome/browser/extensions/extension_tabs_module_constants.h" | 11 #include "chrome/browser/extensions/extension_tabs_module_constants.h" |
12 #include "chrome/browser/extensions/extensions_service.h" | 12 #include "chrome/browser/extensions/extensions_service.h" |
13 #include "chrome/browser/extensions/file_reader.h" | 13 #include "chrome/browser/extensions/file_reader.h" |
14 #include "chrome/browser/profile.h" | 14 #include "chrome/browser/profile.h" |
15 #include "chrome/browser/tab_contents/tab_contents.h" | 15 #include "chrome/browser/tab_contents/tab_contents.h" |
| 16 #include "chrome/browser/tab_contents_wrapper.h" |
16 #include "chrome/browser/ui/browser.h" | 17 #include "chrome/browser/ui/browser.h" |
17 #include "chrome/common/extensions/extension.h" | 18 #include "chrome/common/extensions/extension.h" |
18 #include "chrome/common/extensions/extension_constants.h" | 19 #include "chrome/common/extensions/extension_constants.h" |
19 #include "chrome/common/extensions/extension_error_utils.h" | 20 #include "chrome/common/extensions/extension_error_utils.h" |
20 #include "chrome/common/notification_service.h" | 21 #include "chrome/common/notification_service.h" |
21 | 22 |
22 namespace keys = extension_tabs_module_constants; | 23 namespace keys = extension_tabs_module_constants; |
23 | 24 |
24 ExecuteCodeInTabFunction::ExecuteCodeInTabFunction() | 25 ExecuteCodeInTabFunction::ExecuteCodeInTabFunction() |
25 : execute_tab_id_(-1), | 26 : execute_tab_id_(-1), |
(...skipping 17 matching lines...) Expand all Loading... |
43 error_ = keys::kMoreThanOneValuesError; | 44 error_ = keys::kMoreThanOneValuesError; |
44 return false; | 45 return false; |
45 } else if (!has_code && !has_file) { | 46 } else if (!has_code && !has_file) { |
46 error_ = keys::kNoCodeOrFileToExecuteError; | 47 error_ = keys::kNoCodeOrFileToExecuteError; |
47 return false; | 48 return false; |
48 } | 49 } |
49 } | 50 } |
50 | 51 |
51 execute_tab_id_ = -1; | 52 execute_tab_id_ = -1; |
52 Browser* browser = NULL; | 53 Browser* browser = NULL; |
53 TabContents* contents = NULL; | 54 TabContentsWrapper* contents = NULL; |
54 | 55 |
55 // If |tab_id| is specified, look for it. Otherwise default to selected tab | 56 // If |tab_id| is specified, look for it. Otherwise default to selected tab |
56 // in the current window. | 57 // in the current window. |
57 Value* tab_value = NULL; | 58 Value* tab_value = NULL; |
58 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &tab_value)); | 59 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &tab_value)); |
59 if (tab_value->IsType(Value::TYPE_NULL)) { | 60 if (tab_value->IsType(Value::TYPE_NULL)) { |
60 browser = GetCurrentBrowser(); | 61 browser = GetCurrentBrowser(); |
61 if (!browser) { | 62 if (!browser) { |
62 error_ = keys::kNoCurrentWindowError; | 63 error_ = keys::kNoCurrentWindowError; |
63 return false; | 64 return false; |
(...skipping 11 matching lines...) Expand all Loading... |
75 | 76 |
76 DCHECK(browser); | 77 DCHECK(browser); |
77 DCHECK(contents); | 78 DCHECK(contents); |
78 | 79 |
79 // NOTE: This can give the wrong answer due to race conditions, but it is OK, | 80 // NOTE: This can give the wrong answer due to race conditions, but it is OK, |
80 // we check again in the renderer. | 81 // we check again in the renderer. |
81 const Extension* extension = GetExtension(); | 82 const Extension* extension = GetExtension(); |
82 const std::vector<URLPattern> host_permissions = | 83 const std::vector<URLPattern> host_permissions = |
83 extension->host_permissions(); | 84 extension->host_permissions(); |
84 if (!Extension::CanExecuteScriptOnPage( | 85 if (!Extension::CanExecuteScriptOnPage( |
85 contents->GetURL(), | 86 contents->tab_contents()->GetURL(), |
86 extension->CanExecuteScriptEverywhere(), | 87 extension->CanExecuteScriptEverywhere(), |
87 &host_permissions, | 88 &host_permissions, |
88 NULL, | 89 NULL, |
89 &error_)) { | 90 &error_)) { |
90 return false; | 91 return false; |
91 } | 92 } |
92 | 93 |
93 if (script_info->HasKey(keys::kAllFramesKey)) { | 94 if (script_info->HasKey(keys::kAllFramesKey)) { |
94 if (!script_info->GetBoolean(keys::kAllFramesKey, &all_frames_)) | 95 if (!script_info->GetBoolean(keys::kAllFramesKey, &all_frames_)) |
95 return false; | 96 return false; |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 #elif defined(OS_WIN) | 140 #elif defined(OS_WIN) |
140 error_ = ExtensionErrorUtils::FormatErrorMessage(keys::kLoadFileError, | 141 error_ = ExtensionErrorUtils::FormatErrorMessage(keys::kLoadFileError, |
141 WideToUTF8(resource_.relative_path().value())); | 142 WideToUTF8(resource_.relative_path().value())); |
142 #endif // OS_WIN | 143 #endif // OS_WIN |
143 SendResponse(false); | 144 SendResponse(false); |
144 } | 145 } |
145 Release(); // Balance the AddRef taken in RunImpl | 146 Release(); // Balance the AddRef taken in RunImpl |
146 } | 147 } |
147 | 148 |
148 bool ExecuteCodeInTabFunction::Execute(const std::string& code_string) { | 149 bool ExecuteCodeInTabFunction::Execute(const std::string& code_string) { |
149 TabContents* contents = NULL; | 150 TabContentsWrapper* contents = NULL; |
150 Browser* browser = NULL; | 151 Browser* browser = NULL; |
151 | 152 |
152 bool success = ExtensionTabUtil::GetTabById( | 153 bool success = ExtensionTabUtil::GetTabById( |
153 execute_tab_id_, profile(), include_incognito(), &browser, NULL, | 154 execute_tab_id_, profile(), include_incognito(), &browser, NULL, |
154 &contents, NULL) && contents && browser; | 155 &contents, NULL) && contents && browser; |
155 | 156 |
156 if (!success) { | 157 if (!success) { |
157 SendResponse(false); | 158 SendResponse(false); |
158 return false; | 159 return false; |
159 } | 160 } |
160 | 161 |
161 const Extension* extension = GetExtension(); | 162 const Extension* extension = GetExtension(); |
162 if (!extension) { | 163 if (!extension) { |
163 SendResponse(false); | 164 SendResponse(false); |
164 return false; | 165 return false; |
165 } | 166 } |
166 | 167 |
167 bool is_js_code = true; | 168 bool is_js_code = true; |
168 std::string function_name = name(); | 169 std::string function_name = name(); |
169 if (function_name == TabsInsertCSSFunction::function_name()) { | 170 if (function_name == TabsInsertCSSFunction::function_name()) { |
170 is_js_code = false; | 171 is_js_code = false; |
171 } else if (function_name != TabsExecuteScriptFunction::function_name()) { | 172 } else if (function_name != TabsExecuteScriptFunction::function_name()) { |
172 DCHECK(false); | 173 DCHECK(false); |
173 } | 174 } |
174 if (!contents->ExecuteCode(request_id(), extension->id(), | 175 if (!contents->tab_contents()->ExecuteCode(request_id(), extension->id(), |
175 is_js_code, code_string, all_frames_)) { | 176 is_js_code, code_string, all_frames_)) { |
176 SendResponse(false); | 177 SendResponse(false); |
177 return false; | 178 return false; |
178 } | 179 } |
179 registrar_.Add(this, NotificationType::TAB_CODE_EXECUTED, | 180 registrar_.Add(this, NotificationType::TAB_CODE_EXECUTED, |
180 NotificationService::AllSources()); | 181 NotificationService::AllSources()); |
181 AddRef(); // balanced in Observe() | 182 AddRef(); // balanced in Observe() |
182 return true; | 183 return true; |
183 } | 184 } |
184 | 185 |
185 void ExecuteCodeInTabFunction::Observe(NotificationType type, | 186 void ExecuteCodeInTabFunction::Observe(NotificationType type, |
186 const NotificationSource& source, | 187 const NotificationSource& source, |
187 const NotificationDetails& details) { | 188 const NotificationDetails& details) { |
188 std::pair<int, bool>* result_details = | 189 std::pair<int, bool>* result_details = |
189 Details<std::pair<int, bool> >(details).ptr(); | 190 Details<std::pair<int, bool> >(details).ptr(); |
190 if (result_details->first == request_id()) { | 191 if (result_details->first == request_id()) { |
191 SendResponse(result_details->second); | 192 SendResponse(result_details->second); |
192 Release(); // balanced in Execute() | 193 Release(); // balanced in Execute() |
193 } | 194 } |
194 } | 195 } |
OLD | NEW |