OLD | NEW |
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/tabs.h" | 5 #include "chrome/browser/extensions/api/tabs/tabs_api.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <limits> | 8 #include <limits> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/base64.h" | 11 #include "base/base64.h" |
12 #include "base/bind.h" | 12 #include "base/bind.h" |
13 #include "base/command_line.h" | 13 #include "base/command_line.h" |
14 #include "base/logging.h" | 14 #include "base/logging.h" |
15 #include "base/memory/ref_counted_memory.h" | 15 #include "base/memory/ref_counted_memory.h" |
16 #include "base/message_loop.h" | 16 #include "base/message_loop.h" |
17 #include "base/stl_util.h" | 17 #include "base/stl_util.h" |
18 #include "base/string16.h" | 18 #include "base/string16.h" |
19 #include "base/string_number_conversions.h" | 19 #include "base/string_number_conversions.h" |
20 #include "base/string_util.h" | 20 #include "base/string_util.h" |
21 #include "base/stringprintf.h" | 21 #include "base/stringprintf.h" |
22 #include "base/utf_string_conversions.h" | 22 #include "base/utf_string_conversions.h" |
23 #include "chrome/browser/extensions/api/tabs/tabs_constants.h" | 23 #include "chrome/browser/extensions/api/tabs/tabs_constants.h" |
24 #include "chrome/browser/extensions/extension_function_dispatcher.h" | 24 #include "chrome/browser/extensions/extension_function_dispatcher.h" |
25 #include "chrome/browser/extensions/extension_function_util.h" | 25 #include "chrome/browser/extensions/extension_function_util.h" |
26 #include "chrome/browser/extensions/extension_host.h" | 26 #include "chrome/browser/extensions/extension_host.h" |
27 #include "chrome/browser/extensions/extension_service.h" | 27 #include "chrome/browser/extensions/extension_service.h" |
28 #include "chrome/browser/extensions/extension_tab_util.h" | 28 #include "chrome/browser/extensions/extension_tab_util.h" |
| 29 #include "chrome/browser/extensions/file_reader.h" |
29 #include "chrome/browser/extensions/script_executor.h" | 30 #include "chrome/browser/extensions/script_executor.h" |
30 #include "chrome/browser/extensions/tab_helper.h" | 31 #include "chrome/browser/extensions/tab_helper.h" |
31 #include "chrome/browser/extensions/window_controller.h" | 32 #include "chrome/browser/extensions/window_controller.h" |
32 #include "chrome/browser/extensions/window_controller_list.h" | 33 #include "chrome/browser/extensions/window_controller_list.h" |
33 #include "chrome/browser/prefs/incognito_mode_prefs.h" | 34 #include "chrome/browser/prefs/incognito_mode_prefs.h" |
34 #include "chrome/browser/prefs/pref_service.h" | 35 #include "chrome/browser/prefs/pref_service.h" |
35 #include "chrome/browser/profiles/profile.h" | 36 #include "chrome/browser/profiles/profile.h" |
36 #include "chrome/browser/translate/translate_tab_helper.h" | 37 #include "chrome/browser/translate/translate_tab_helper.h" |
37 #include "chrome/browser/ui/browser.h" | 38 #include "chrome/browser/ui/browser.h" |
38 #include "chrome/browser/ui/browser_commands.h" | 39 #include "chrome/browser/ui/browser_commands.h" |
39 #include "chrome/browser/ui/browser_finder.h" | 40 #include "chrome/browser/ui/browser_finder.h" |
40 #include "chrome/browser/ui/browser_list.h" | 41 #include "chrome/browser/ui/browser_list.h" |
41 #include "chrome/browser/ui/browser_navigator.h" | 42 #include "chrome/browser/ui/browser_navigator.h" |
42 #include "chrome/browser/ui/browser_tabstrip.h" | 43 #include "chrome/browser/ui/browser_tabstrip.h" |
43 #include "chrome/browser/ui/browser_window.h" | 44 #include "chrome/browser/ui/browser_window.h" |
44 #include "chrome/browser/ui/extensions/shell_window.h" | 45 #include "chrome/browser/ui/extensions/shell_window.h" |
45 #include "chrome/browser/ui/host_desktop.h" | 46 #include "chrome/browser/ui/host_desktop.h" |
46 #include "chrome/browser/ui/panels/panel_manager.h" | 47 #include "chrome/browser/ui/panels/panel_manager.h" |
47 #include "chrome/browser/ui/snapshot_tab_helper.h" | 48 #include "chrome/browser/ui/snapshot_tab_helper.h" |
48 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 49 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
49 #include "chrome/browser/ui/window_sizer/window_sizer.h" | 50 #include "chrome/browser/ui/window_sizer/window_sizer.h" |
50 #include "chrome/browser/web_applications/web_app.h" | 51 #include "chrome/browser/web_applications/web_app.h" |
51 #include "chrome/common/chrome_notification_types.h" | 52 #include "chrome/common/chrome_notification_types.h" |
52 #include "chrome/common/chrome_switches.h" | 53 #include "chrome/common/chrome_switches.h" |
| 54 #include "chrome/common/extensions/api/tabs.h" |
53 #include "chrome/common/extensions/api/windows.h" | 55 #include "chrome/common/extensions/api/windows.h" |
54 #include "chrome/common/extensions/extension.h" | 56 #include "chrome/common/extensions/extension.h" |
| 57 #include "chrome/common/extensions/extension_constants.h" |
| 58 #include "chrome/common/extensions/extension_file_util.h" |
| 59 #include "chrome/common/extensions/extension_l10n_util.h" |
55 #include "chrome/common/extensions/extension_manifest_constants.h" | 60 #include "chrome/common/extensions/extension_manifest_constants.h" |
56 #include "chrome/common/extensions/extension_messages.h" | 61 #include "chrome/common/extensions/extension_messages.h" |
| 62 #include "chrome/common/extensions/message_bundle.h" |
57 #include "chrome/common/extensions/user_script.h" | 63 #include "chrome/common/extensions/user_script.h" |
58 #include "chrome/common/pref_names.h" | 64 #include "chrome/common/pref_names.h" |
59 #include "chrome/common/url_constants.h" | 65 #include "chrome/common/url_constants.h" |
60 #include "content/public/browser/navigation_controller.h" | 66 #include "content/public/browser/navigation_controller.h" |
61 #include "content/public/browser/navigation_entry.h" | 67 #include "content/public/browser/navigation_entry.h" |
62 #include "content/public/browser/notification_details.h" | 68 #include "content/public/browser/notification_details.h" |
63 #include "content/public/browser/notification_source.h" | 69 #include "content/public/browser/notification_source.h" |
64 #include "content/public/browser/render_view_host.h" | 70 #include "content/public/browser/render_view_host.h" |
65 #include "content/public/browser/render_widget_host_view.h" | 71 #include "content/public/browser/render_widget_host_view.h" |
66 #include "content/public/browser/web_contents.h" | 72 #include "content/public/browser/web_contents.h" |
(...skipping 13 matching lines...) Expand all Loading... |
80 #include "win8/util/win8_util.h" | 86 #include "win8/util/win8_util.h" |
81 #endif // OS_WIN | 87 #endif // OS_WIN |
82 | 88 |
83 namespace Get = extensions::api::windows::Get; | 89 namespace Get = extensions::api::windows::Get; |
84 namespace GetAll = extensions::api::windows::GetAll; | 90 namespace GetAll = extensions::api::windows::GetAll; |
85 namespace GetCurrent = extensions::api::windows::GetCurrent; | 91 namespace GetCurrent = extensions::api::windows::GetCurrent; |
86 namespace GetLastFocused = extensions::api::windows::GetLastFocused; | 92 namespace GetLastFocused = extensions::api::windows::GetLastFocused; |
87 namespace errors = extension_manifest_errors; | 93 namespace errors = extension_manifest_errors; |
88 namespace keys = extensions::tabs_constants; | 94 namespace keys = extensions::tabs_constants; |
89 | 95 |
| 96 using content::BrowserThread; |
90 using content::NavigationController; | 97 using content::NavigationController; |
91 using content::NavigationEntry; | 98 using content::NavigationEntry; |
92 using content::OpenURLParams; | 99 using content::OpenURLParams; |
93 using content::Referrer; | 100 using content::Referrer; |
94 using content::RenderViewHost; | 101 using content::RenderViewHost; |
95 using content::WebContents; | 102 using content::WebContents; |
96 using extensions::ErrorUtils; | 103 using extensions::ErrorUtils; |
97 using extensions::ScriptExecutor; | 104 using extensions::ScriptExecutor; |
| 105 using extensions::UserScript; |
98 using extensions::WindowController; | 106 using extensions::WindowController; |
99 using extensions::WindowControllerList; | 107 using extensions::WindowControllerList; |
| 108 using extensions::api::tabs::InjectDetails; |
100 | 109 |
101 const int CaptureVisibleTabFunction::kDefaultQuality = 90; | 110 const int TabsCaptureVisibleTabFunction::kDefaultQuality = 90; |
102 | 111 |
103 namespace { | 112 namespace { |
104 | 113 |
105 // |error_message| can optionally be passed in a will be set with an appropriate | 114 // |error_message| can optionally be passed in a will be set with an appropriate |
106 // message if the window cannot be found by id. | 115 // message if the window cannot be found by id. |
107 Browser* GetBrowserInProfileWithId(Profile* profile, | 116 Browser* GetBrowserInProfileWithId(Profile* profile, |
108 const int window_id, | 117 const int window_id, |
109 bool include_incognito, | 118 bool include_incognito, |
110 std::string* error_message) { | 119 std::string* error_message) { |
111 Profile* incognito_profile = | 120 Profile* incognito_profile = |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
242 | 251 |
243 if (!new_window) | 252 if (!new_window) |
244 new_window = new Browser(params); | 253 new_window = new Browser(params); |
245 return new_window; | 254 return new_window; |
246 } | 255 } |
247 | 256 |
248 } // namespace | 257 } // namespace |
249 | 258 |
250 // Windows --------------------------------------------------------------------- | 259 // Windows --------------------------------------------------------------------- |
251 | 260 |
252 bool GetWindowFunction::RunImpl() { | 261 bool WindowsGetFunction::RunImpl() { |
253 scoped_ptr<Get::Params> params(Get::Params::Create(*args_)); | 262 scoped_ptr<Get::Params> params(Get::Params::Create(*args_)); |
254 EXTENSION_FUNCTION_VALIDATE(params.get()); | 263 EXTENSION_FUNCTION_VALIDATE(params.get()); |
255 | 264 |
256 bool populate_tabs = false; | 265 bool populate_tabs = false; |
257 if (params->get_info.get() && params->get_info->populate.get()) | 266 if (params->get_info.get() && params->get_info->populate.get()) |
258 populate_tabs = *params->get_info->populate; | 267 populate_tabs = *params->get_info->populate; |
259 | 268 |
260 WindowController* controller; | 269 WindowController* controller; |
261 if (!GetWindowFromWindowID(this, params->window_id, &controller)) | 270 if (!GetWindowFromWindowID(this, params->window_id, &controller)) |
262 return false; | 271 return false; |
263 | 272 |
264 if (populate_tabs) | 273 if (populate_tabs) |
265 SetResult(controller->CreateWindowValueWithTabs(GetExtension())); | 274 SetResult(controller->CreateWindowValueWithTabs(GetExtension())); |
266 else | 275 else |
267 SetResult(controller->CreateWindowValue()); | 276 SetResult(controller->CreateWindowValue()); |
268 return true; | 277 return true; |
269 } | 278 } |
270 | 279 |
271 bool GetCurrentWindowFunction::RunImpl() { | 280 bool WindowsGetCurrentFunction::RunImpl() { |
272 scoped_ptr<GetCurrent::Params> params(GetCurrent::Params::Create(*args_)); | 281 scoped_ptr<GetCurrent::Params> params(GetCurrent::Params::Create(*args_)); |
273 EXTENSION_FUNCTION_VALIDATE(params.get()); | 282 EXTENSION_FUNCTION_VALIDATE(params.get()); |
274 | 283 |
275 bool populate_tabs = false; | 284 bool populate_tabs = false; |
276 if (params->get_info.get() && params->get_info->populate.get()) | 285 if (params->get_info.get() && params->get_info->populate.get()) |
277 populate_tabs = *params->get_info->populate; | 286 populate_tabs = *params->get_info->populate; |
278 | 287 |
279 WindowController* controller; | 288 WindowController* controller; |
280 if (!GetWindowFromWindowID(this, | 289 if (!GetWindowFromWindowID(this, |
281 extension_misc::kCurrentWindowId, | 290 extension_misc::kCurrentWindowId, |
282 &controller)) { | 291 &controller)) { |
283 return false; | 292 return false; |
284 } | 293 } |
285 if (populate_tabs) | 294 if (populate_tabs) |
286 SetResult(controller->CreateWindowValueWithTabs(GetExtension())); | 295 SetResult(controller->CreateWindowValueWithTabs(GetExtension())); |
287 else | 296 else |
288 SetResult(controller->CreateWindowValue()); | 297 SetResult(controller->CreateWindowValue()); |
289 return true; | 298 return true; |
290 } | 299 } |
291 | 300 |
292 bool GetLastFocusedWindowFunction::RunImpl() { | 301 bool WindowsGetLastFocusedFunction::RunImpl() { |
293 scoped_ptr<GetLastFocused::Params> params( | 302 scoped_ptr<GetLastFocused::Params> params( |
294 GetLastFocused::Params::Create(*args_)); | 303 GetLastFocused::Params::Create(*args_)); |
295 EXTENSION_FUNCTION_VALIDATE(params.get()); | 304 EXTENSION_FUNCTION_VALIDATE(params.get()); |
296 | 305 |
297 bool populate_tabs = false; | 306 bool populate_tabs = false; |
298 if (params->get_info.get() && params->get_info->populate.get()) | 307 if (params->get_info.get() && params->get_info->populate.get()) |
299 populate_tabs = *params->get_info->populate; | 308 populate_tabs = *params->get_info->populate; |
300 | 309 |
301 // Note: currently this returns the last active browser. If we decide to | 310 // Note: currently this returns the last active browser. If we decide to |
302 // include other window types (e.g. panels), we will need to add logic to | 311 // include other window types (e.g. panels), we will need to add logic to |
303 // WindowControllerList that mirrors the active behavior of BrowserList. | 312 // WindowControllerList that mirrors the active behavior of BrowserList. |
304 Browser* browser = chrome::FindAnyBrowser( | 313 Browser* browser = chrome::FindAnyBrowser( |
305 profile(), include_incognito(), chrome::GetActiveDesktop()); | 314 profile(), include_incognito(), chrome::GetActiveDesktop()); |
306 if (!browser || !browser->window()) { | 315 if (!browser || !browser->window()) { |
307 error_ = keys::kNoLastFocusedWindowError; | 316 error_ = keys::kNoLastFocusedWindowError; |
308 return false; | 317 return false; |
309 } | 318 } |
310 WindowController* controller = | 319 WindowController* controller = |
311 browser->extension_window_controller(); | 320 browser->extension_window_controller(); |
312 if (populate_tabs) | 321 if (populate_tabs) |
313 SetResult(controller->CreateWindowValueWithTabs(GetExtension())); | 322 SetResult(controller->CreateWindowValueWithTabs(GetExtension())); |
314 else | 323 else |
315 SetResult(controller->CreateWindowValue()); | 324 SetResult(controller->CreateWindowValue()); |
316 return true; | 325 return true; |
317 } | 326 } |
318 | 327 |
319 bool GetAllWindowsFunction::RunImpl() { | 328 bool WindowsGetAllFunction::RunImpl() { |
320 scoped_ptr<GetAll::Params> params(GetAll::Params::Create(*args_)); | 329 scoped_ptr<GetAll::Params> params(GetAll::Params::Create(*args_)); |
321 EXTENSION_FUNCTION_VALIDATE(params.get()); | 330 EXTENSION_FUNCTION_VALIDATE(params.get()); |
322 | 331 |
323 bool populate_tabs = false; | 332 bool populate_tabs = false; |
324 if (params->get_info.get() && params->get_info->populate.get()) | 333 if (params->get_info.get() && params->get_info->populate.get()) |
325 populate_tabs = *params->get_info->populate; | 334 populate_tabs = *params->get_info->populate; |
326 | 335 |
327 ListValue* window_list = new ListValue(); | 336 ListValue* window_list = new ListValue(); |
328 const WindowControllerList::ControllerList& windows = | 337 const WindowControllerList::ControllerList& windows = |
329 WindowControllerList::GetInstance()->windows(); | 338 WindowControllerList::GetInstance()->windows(); |
330 for (WindowControllerList::ControllerList::const_iterator iter = | 339 for (WindowControllerList::ControllerList::const_iterator iter = |
331 windows.begin(); | 340 windows.begin(); |
332 iter != windows.end(); ++iter) { | 341 iter != windows.end(); ++iter) { |
333 if (!this->CanOperateOnWindow(*iter)) | 342 if (!this->CanOperateOnWindow(*iter)) |
334 continue; | 343 continue; |
335 if (populate_tabs) | 344 if (populate_tabs) |
336 window_list->Append((*iter)->CreateWindowValueWithTabs(GetExtension())); | 345 window_list->Append((*iter)->CreateWindowValueWithTabs(GetExtension())); |
337 else | 346 else |
338 window_list->Append((*iter)->CreateWindowValue()); | 347 window_list->Append((*iter)->CreateWindowValue()); |
339 } | 348 } |
340 SetResult(window_list); | 349 SetResult(window_list); |
341 return true; | 350 return true; |
342 } | 351 } |
343 | 352 |
344 bool CreateWindowFunction::ShouldOpenIncognitoWindow( | 353 bool WindowsCreateFunction::ShouldOpenIncognitoWindow( |
345 const base::DictionaryValue* args, | 354 const base::DictionaryValue* args, |
346 std::vector<GURL>* urls, | 355 std::vector<GURL>* urls, |
347 bool* is_error) { | 356 bool* is_error) { |
348 *is_error = false; | 357 *is_error = false; |
349 const IncognitoModePrefs::Availability incognito_availability = | 358 const IncognitoModePrefs::Availability incognito_availability = |
350 IncognitoModePrefs::GetAvailability(profile_->GetPrefs()); | 359 IncognitoModePrefs::GetAvailability(profile_->GetPrefs()); |
351 bool incognito = false; | 360 bool incognito = false; |
352 if (args && args->HasKey(keys::kIncognitoKey)) { | 361 if (args && args->HasKey(keys::kIncognitoKey)) { |
353 EXTENSION_FUNCTION_VALIDATE(args->GetBoolean(keys::kIncognitoKey, | 362 EXTENSION_FUNCTION_VALIDATE(args->GetBoolean(keys::kIncognitoKey, |
354 &incognito)); | 363 &incognito)); |
(...skipping 29 matching lines...) Expand all Loading... |
384 if (urls->empty() && !first_url_erased.empty()) { | 393 if (urls->empty() && !first_url_erased.empty()) { |
385 error_ = ErrorUtils::FormatErrorMessage( | 394 error_ = ErrorUtils::FormatErrorMessage( |
386 keys::kURLsNotAllowedInIncognitoError, first_url_erased); | 395 keys::kURLsNotAllowedInIncognitoError, first_url_erased); |
387 *is_error = true; | 396 *is_error = true; |
388 return false; | 397 return false; |
389 } | 398 } |
390 } | 399 } |
391 return incognito; | 400 return incognito; |
392 } | 401 } |
393 | 402 |
394 bool CreateWindowFunction::RunImpl() { | 403 bool WindowsCreateFunction::RunImpl() { |
395 DictionaryValue* args = NULL; | 404 DictionaryValue* args = NULL; |
396 std::vector<GURL> urls; | 405 std::vector<GURL> urls; |
397 WebContents* contents = NULL; | 406 WebContents* contents = NULL; |
398 | 407 |
399 if (HasOptionalArgument(0)) | 408 if (HasOptionalArgument(0)) |
400 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &args)); | 409 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &args)); |
401 | 410 |
402 // Look for optional url. | 411 // Look for optional url. |
403 if (args) { | 412 if (args) { |
404 if (args->HasKey(keys::kUrlKey)) { | 413 if (args->HasKey(keys::kUrlKey)) { |
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
653 SetResult(Value::CreateNullValue()); | 662 SetResult(Value::CreateNullValue()); |
654 } else { | 663 } else { |
655 SetResult( | 664 SetResult( |
656 new_window->extension_window_controller()->CreateWindowValueWithTabs( | 665 new_window->extension_window_controller()->CreateWindowValueWithTabs( |
657 GetExtension())); | 666 GetExtension())); |
658 } | 667 } |
659 | 668 |
660 return true; | 669 return true; |
661 } | 670 } |
662 | 671 |
663 bool UpdateWindowFunction::RunImpl() { | 672 bool WindowsUpdateFunction::RunImpl() { |
664 int window_id = extension_misc::kUnknownWindowId; | 673 int window_id = extension_misc::kUnknownWindowId; |
665 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &window_id)); | 674 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &window_id)); |
666 DictionaryValue* update_props; | 675 DictionaryValue* update_props; |
667 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &update_props)); | 676 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &update_props)); |
668 | 677 |
669 WindowController* controller; | 678 WindowController* controller; |
670 if (!GetWindowFromWindowID(this, window_id, &controller)) | 679 if (!GetWindowFromWindowID(this, window_id, &controller)) |
671 return false; | 680 return false; |
672 | 681 |
673 #if defined(OS_WIN) | 682 #if defined(OS_WIN) |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
797 EXTENSION_FUNCTION_VALIDATE(update_props->GetBoolean( | 806 EXTENSION_FUNCTION_VALIDATE(update_props->GetBoolean( |
798 keys::kDrawAttentionKey, &draw_attention)); | 807 keys::kDrawAttentionKey, &draw_attention)); |
799 controller->window()->FlashFrame(draw_attention); | 808 controller->window()->FlashFrame(draw_attention); |
800 } | 809 } |
801 | 810 |
802 SetResult(controller->CreateWindowValue()); | 811 SetResult(controller->CreateWindowValue()); |
803 | 812 |
804 return true; | 813 return true; |
805 } | 814 } |
806 | 815 |
807 bool RemoveWindowFunction::RunImpl() { | 816 bool WindowsRemoveFunction::RunImpl() { |
808 int window_id = -1; | 817 int window_id = -1; |
809 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &window_id)); | 818 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &window_id)); |
810 | 819 |
811 WindowController* controller; | 820 WindowController* controller; |
812 if (!GetWindowFromWindowID(this, window_id, &controller)) | 821 if (!GetWindowFromWindowID(this, window_id, &controller)) |
813 return false; | 822 return false; |
814 | 823 |
815 #if defined(OS_WIN) | 824 #if defined(OS_WIN) |
816 // In Windows 8 metro mode, an existing Browser instance is reused for | 825 // In Windows 8 metro mode, an existing Browser instance is reused for |
817 // hosting the extension tab. We should not be closing it as we don't own it. | 826 // hosting the extension tab. We should not be closing it as we don't own it. |
818 if (win8::IsSingleWindowMetroMode()) | 827 if (win8::IsSingleWindowMetroMode()) |
819 return false; | 828 return false; |
820 #endif | 829 #endif |
821 | 830 |
822 WindowController::Reason reason; | 831 WindowController::Reason reason; |
823 if (!controller->CanClose(&reason)) { | 832 if (!controller->CanClose(&reason)) { |
824 if (reason == WindowController::REASON_NOT_EDITABLE) | 833 if (reason == WindowController::REASON_NOT_EDITABLE) |
825 error_ = keys::kTabStripNotEditableError; | 834 error_ = keys::kTabStripNotEditableError; |
826 return false; | 835 return false; |
827 } | 836 } |
828 controller->window()->Close(); | 837 controller->window()->Close(); |
829 return true; | 838 return true; |
830 } | 839 } |
831 | 840 |
832 // Tabs ------------------------------------------------------------------------ | 841 // Tabs ------------------------------------------------------------------------ |
833 | 842 |
834 bool GetSelectedTabFunction::RunImpl() { | 843 bool TabsGetSelectedFunction::RunImpl() { |
835 // windowId defaults to "current" window. | 844 // windowId defaults to "current" window. |
836 int window_id = extension_misc::kCurrentWindowId; | 845 int window_id = extension_misc::kCurrentWindowId; |
837 | 846 |
838 if (HasOptionalArgument(0)) | 847 if (HasOptionalArgument(0)) |
839 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &window_id)); | 848 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &window_id)); |
840 | 849 |
841 Browser* browser = NULL; | 850 Browser* browser = NULL; |
842 if (!GetBrowserFromWindowID(this, window_id, &browser)) | 851 if (!GetBrowserFromWindowID(this, window_id, &browser)) |
843 return false; | 852 return false; |
844 | 853 |
845 TabStripModel* tab_strip = browser->tab_strip_model(); | 854 TabStripModel* tab_strip = browser->tab_strip_model(); |
846 WebContents* contents = tab_strip->GetActiveWebContents(); | 855 WebContents* contents = tab_strip->GetActiveWebContents(); |
847 if (!contents) { | 856 if (!contents) { |
848 error_ = keys::kNoSelectedTabError; | 857 error_ = keys::kNoSelectedTabError; |
849 return false; | 858 return false; |
850 } | 859 } |
851 SetResult(ExtensionTabUtil::CreateTabValue(contents, | 860 SetResult(ExtensionTabUtil::CreateTabValue(contents, |
852 tab_strip, | 861 tab_strip, |
853 tab_strip->active_index(), | 862 tab_strip->active_index(), |
854 GetExtension())); | 863 GetExtension())); |
855 return true; | 864 return true; |
856 } | 865 } |
857 | 866 |
858 bool GetAllTabsInWindowFunction::RunImpl() { | 867 bool TabsGetAllInWindowFunction::RunImpl() { |
859 // windowId defaults to "current" window. | 868 // windowId defaults to "current" window. |
860 int window_id = extension_misc::kCurrentWindowId; | 869 int window_id = extension_misc::kCurrentWindowId; |
861 if (HasOptionalArgument(0)) | 870 if (HasOptionalArgument(0)) |
862 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &window_id)); | 871 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &window_id)); |
863 | 872 |
864 Browser* browser = NULL; | 873 Browser* browser = NULL; |
865 if (!GetBrowserFromWindowID(this, window_id, &browser)) | 874 if (!GetBrowserFromWindowID(this, window_id, &browser)) |
866 return false; | 875 return false; |
867 | 876 |
868 SetResult(ExtensionTabUtil::CreateTabList(browser, GetExtension())); | 877 SetResult(ExtensionTabUtil::CreateTabList(browser, GetExtension())); |
869 | 878 |
870 return true; | 879 return true; |
871 } | 880 } |
872 | 881 |
873 bool QueryTabsFunction::RunImpl() { | 882 bool TabsQueryFunction::RunImpl() { |
874 DictionaryValue* query = NULL; | 883 DictionaryValue* query = NULL; |
875 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &query)); | 884 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &query)); |
876 | 885 |
877 QueryArg active = ParseBoolQueryArg(query, keys::kActiveKey); | 886 QueryArg active = ParseBoolQueryArg(query, keys::kActiveKey); |
878 QueryArg pinned = ParseBoolQueryArg(query, keys::kPinnedKey); | 887 QueryArg pinned = ParseBoolQueryArg(query, keys::kPinnedKey); |
879 QueryArg selected = ParseBoolQueryArg(query, keys::kHighlightedKey); | 888 QueryArg selected = ParseBoolQueryArg(query, keys::kHighlightedKey); |
880 QueryArg current_window = ParseBoolQueryArg(query, keys::kCurrentWindowKey); | 889 QueryArg current_window = ParseBoolQueryArg(query, keys::kCurrentWindowKey); |
881 QueryArg focused_window = | 890 QueryArg focused_window = |
882 ParseBoolQueryArg(query, keys::kLastFocusedWindowKey); | 891 ParseBoolQueryArg(query, keys::kLastFocusedWindowKey); |
883 | 892 |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
976 | 985 |
977 result->Append(ExtensionTabUtil::CreateTabValue( | 986 result->Append(ExtensionTabUtil::CreateTabValue( |
978 web_contents, tab_strip, i, GetExtension())); | 987 web_contents, tab_strip, i, GetExtension())); |
979 } | 988 } |
980 } | 989 } |
981 | 990 |
982 SetResult(result); | 991 SetResult(result); |
983 return true; | 992 return true; |
984 } | 993 } |
985 | 994 |
986 bool CreateTabFunction::RunImpl() { | 995 bool TabsCreateFunction::RunImpl() { |
987 DictionaryValue* args = NULL; | 996 DictionaryValue* args = NULL; |
988 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &args)); | 997 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &args)); |
989 | 998 |
990 // windowId defaults to "current" window. | 999 // windowId defaults to "current" window. |
991 int window_id = extension_misc::kCurrentWindowId; | 1000 int window_id = extension_misc::kCurrentWindowId; |
992 if (args->HasKey(keys::kWindowIdKey)) | 1001 if (args->HasKey(keys::kWindowIdKey)) |
993 EXTENSION_FUNCTION_VALIDATE(args->GetInteger( | 1002 EXTENSION_FUNCTION_VALIDATE(args->GetInteger( |
994 keys::kWindowIdKey, &window_id)); | 1003 keys::kWindowIdKey, &window_id)); |
995 | 1004 |
996 Browser* browser = NULL; | 1005 Browser* browser = NULL; |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1110 // Return data about the newly created tab. | 1119 // Return data about the newly created tab. |
1111 if (has_callback()) { | 1120 if (has_callback()) { |
1112 SetResult(ExtensionTabUtil::CreateTabValue( | 1121 SetResult(ExtensionTabUtil::CreateTabValue( |
1113 params.target_contents, | 1122 params.target_contents, |
1114 tab_strip, new_index, GetExtension())); | 1123 tab_strip, new_index, GetExtension())); |
1115 } | 1124 } |
1116 | 1125 |
1117 return true; | 1126 return true; |
1118 } | 1127 } |
1119 | 1128 |
1120 bool DuplicateTabFunction::RunImpl() { | 1129 bool TabsDuplicateFunction::RunImpl() { |
1121 int tab_id = -1; | 1130 int tab_id = -1; |
1122 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &tab_id)); | 1131 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &tab_id)); |
1123 | 1132 |
1124 Browser* browser = NULL; | 1133 Browser* browser = NULL; |
1125 TabStripModel* tab_strip = NULL; | 1134 TabStripModel* tab_strip = NULL; |
1126 int tab_index = -1; | 1135 int tab_index = -1; |
1127 if (!GetTabById(tab_id, profile(), include_incognito(), | 1136 if (!GetTabById(tab_id, profile(), include_incognito(), |
1128 &browser, &tab_strip, NULL, &tab_index, &error_)) { | 1137 &browser, &tab_strip, NULL, &tab_index, &error_)) { |
1129 return false; | 1138 return false; |
1130 } | 1139 } |
1131 | 1140 |
1132 WebContents* new_contents = chrome::DuplicateTabAt(browser, tab_index); | 1141 WebContents* new_contents = chrome::DuplicateTabAt(browser, tab_index); |
1133 if (!has_callback()) | 1142 if (!has_callback()) |
1134 return true; | 1143 return true; |
1135 | 1144 |
1136 int new_index = tab_strip->GetIndexOfWebContents(new_contents); | 1145 int new_index = tab_strip->GetIndexOfWebContents(new_contents); |
1137 | 1146 |
1138 // Return data about the newly created tab. | 1147 // Return data about the newly created tab. |
1139 SetResult(ExtensionTabUtil::CreateTabValue( | 1148 SetResult(ExtensionTabUtil::CreateTabValue( |
1140 new_contents, | 1149 new_contents, |
1141 tab_strip, new_index, GetExtension())); | 1150 tab_strip, new_index, GetExtension())); |
1142 | 1151 |
1143 return true; | 1152 return true; |
1144 } | 1153 } |
1145 | 1154 |
1146 bool GetTabFunction::RunImpl() { | 1155 bool TabsGetFunction::RunImpl() { |
1147 int tab_id = -1; | 1156 int tab_id = -1; |
1148 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &tab_id)); | 1157 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &tab_id)); |
1149 | 1158 |
1150 TabStripModel* tab_strip = NULL; | 1159 TabStripModel* tab_strip = NULL; |
1151 WebContents* contents = NULL; | 1160 WebContents* contents = NULL; |
1152 int tab_index = -1; | 1161 int tab_index = -1; |
1153 if (!GetTabById(tab_id, profile(), include_incognito(), | 1162 if (!GetTabById(tab_id, profile(), include_incognito(), |
1154 NULL, &tab_strip, &contents, &tab_index, &error_)) | 1163 NULL, &tab_strip, &contents, &tab_index, &error_)) |
1155 return false; | 1164 return false; |
1156 | 1165 |
1157 SetResult(ExtensionTabUtil::CreateTabValue(contents, | 1166 SetResult(ExtensionTabUtil::CreateTabValue(contents, |
1158 tab_strip, | 1167 tab_strip, |
1159 tab_index, | 1168 tab_index, |
1160 GetExtension())); | 1169 GetExtension())); |
1161 return true; | 1170 return true; |
1162 } | 1171 } |
1163 | 1172 |
1164 bool GetCurrentTabFunction::RunImpl() { | 1173 bool TabsGetCurrentFunction::RunImpl() { |
1165 DCHECK(dispatcher()); | 1174 DCHECK(dispatcher()); |
1166 | 1175 |
1167 WebContents* contents = dispatcher()->delegate()->GetAssociatedWebContents(); | 1176 WebContents* contents = dispatcher()->delegate()->GetAssociatedWebContents(); |
1168 if (contents) | 1177 if (contents) |
1169 SetResult(ExtensionTabUtil::CreateTabValue(contents, GetExtension())); | 1178 SetResult(ExtensionTabUtil::CreateTabValue(contents, GetExtension())); |
1170 | 1179 |
1171 return true; | 1180 return true; |
1172 } | 1181 } |
1173 | 1182 |
1174 bool HighlightTabsFunction::RunImpl() { | 1183 bool TabsHighlightFunction::RunImpl() { |
1175 DictionaryValue* info = NULL; | 1184 DictionaryValue* info = NULL; |
1176 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &info)); | 1185 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &info)); |
1177 | 1186 |
1178 // Get the window id from the params; default to current window if omitted. | 1187 // Get the window id from the params; default to current window if omitted. |
1179 int window_id = extension_misc::kCurrentWindowId; | 1188 int window_id = extension_misc::kCurrentWindowId; |
1180 if (info->HasKey(keys::kWindowIdKey)) | 1189 if (info->HasKey(keys::kWindowIdKey)) |
1181 EXTENSION_FUNCTION_VALIDATE( | 1190 EXTENSION_FUNCTION_VALIDATE( |
1182 info->GetInteger(keys::kWindowIdKey, &window_id)); | 1191 info->GetInteger(keys::kWindowIdKey, &window_id)); |
1183 | 1192 |
1184 Browser* browser = NULL; | 1193 Browser* browser = NULL; |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1221 } | 1230 } |
1222 | 1231 |
1223 selection.set_active(active_index); | 1232 selection.set_active(active_index); |
1224 browser->tab_strip_model()->SetSelectionFromModel(selection); | 1233 browser->tab_strip_model()->SetSelectionFromModel(selection); |
1225 SetResult( | 1234 SetResult( |
1226 browser->extension_window_controller()->CreateWindowValueWithTabs( | 1235 browser->extension_window_controller()->CreateWindowValueWithTabs( |
1227 GetExtension())); | 1236 GetExtension())); |
1228 return true; | 1237 return true; |
1229 } | 1238 } |
1230 | 1239 |
1231 UpdateTabFunction::UpdateTabFunction() : web_contents_(NULL) { | 1240 TabsUpdateFunction::TabsUpdateFunction() : web_contents_(NULL) { |
1232 } | 1241 } |
1233 | 1242 |
1234 bool UpdateTabFunction::RunImpl() { | 1243 bool TabsUpdateFunction::RunImpl() { |
1235 DictionaryValue* update_props = NULL; | 1244 DictionaryValue* update_props = NULL; |
1236 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &update_props)); | 1245 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &update_props)); |
1237 | 1246 |
1238 Value* tab_value = NULL; | 1247 Value* tab_value = NULL; |
1239 if (HasOptionalArgument(0)) { | 1248 if (HasOptionalArgument(0)) { |
1240 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &tab_value)); | 1249 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &tab_value)); |
1241 } | 1250 } |
1242 | 1251 |
1243 int tab_id = -1; | 1252 int tab_id = -1; |
1244 WebContents* contents = NULL; | 1253 WebContents* contents = NULL; |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1328 tab_strip->SetOpenerOfWebContentsAt(tab_index, opener_contents); | 1337 tab_strip->SetOpenerOfWebContentsAt(tab_index, opener_contents); |
1329 } | 1338 } |
1330 | 1339 |
1331 if (!is_async) { | 1340 if (!is_async) { |
1332 PopulateResult(); | 1341 PopulateResult(); |
1333 SendResponse(true); | 1342 SendResponse(true); |
1334 } | 1343 } |
1335 return true; | 1344 return true; |
1336 } | 1345 } |
1337 | 1346 |
1338 bool UpdateTabFunction::UpdateURLIfPresent(DictionaryValue* update_props, | 1347 bool TabsUpdateFunction::UpdateURLIfPresent(DictionaryValue* update_props, |
1339 int tab_id, | 1348 int tab_id, |
1340 bool* is_async) { | 1349 bool* is_async) { |
1341 if (!update_props->HasKey(keys::kUrlKey)) | 1350 if (!update_props->HasKey(keys::kUrlKey)) |
1342 return true; | 1351 return true; |
1343 | 1352 |
1344 std::string url_string; | 1353 std::string url_string; |
1345 EXTENSION_FUNCTION_VALIDATE(update_props->GetString( | 1354 EXTENSION_FUNCTION_VALIDATE(update_props->GetString( |
1346 keys::kUrlKey, &url_string)); | 1355 keys::kUrlKey, &url_string)); |
1347 GURL url = ExtensionTabUtil::ResolvePossiblyRelativeURL( | 1356 GURL url = ExtensionTabUtil::ResolvePossiblyRelativeURL( |
1348 url_string, GetExtension()); | 1357 url_string, GetExtension()); |
(...skipping 23 matching lines...) Expand all Loading... |
1372 } | 1381 } |
1373 | 1382 |
1374 extensions::TabHelper::FromWebContents(web_contents_)-> | 1383 extensions::TabHelper::FromWebContents(web_contents_)-> |
1375 script_executor()->ExecuteScript( | 1384 script_executor()->ExecuteScript( |
1376 extension_id(), | 1385 extension_id(), |
1377 ScriptExecutor::JAVASCRIPT, | 1386 ScriptExecutor::JAVASCRIPT, |
1378 url.path(), | 1387 url.path(), |
1379 ScriptExecutor::TOP_FRAME, | 1388 ScriptExecutor::TOP_FRAME, |
1380 extensions::UserScript::DOCUMENT_IDLE, | 1389 extensions::UserScript::DOCUMENT_IDLE, |
1381 ScriptExecutor::MAIN_WORLD, | 1390 ScriptExecutor::MAIN_WORLD, |
1382 base::Bind(&UpdateTabFunction::OnExecuteCodeFinished, this)); | 1391 base::Bind(&TabsUpdateFunction::OnExecuteCodeFinished, this)); |
1383 | 1392 |
1384 *is_async = true; | 1393 *is_async = true; |
1385 return true; | 1394 return true; |
1386 } | 1395 } |
1387 | 1396 |
1388 web_contents_->GetController().LoadURL( | 1397 web_contents_->GetController().LoadURL( |
1389 url, content::Referrer(), content::PAGE_TRANSITION_LINK, std::string()); | 1398 url, content::Referrer(), content::PAGE_TRANSITION_LINK, std::string()); |
1390 | 1399 |
1391 // The URL of a tab contents never actually changes to a JavaScript URL, so | 1400 // The URL of a tab contents never actually changes to a JavaScript URL, so |
1392 // this check only makes sense in other cases. | 1401 // this check only makes sense in other cases. |
1393 if (!url.SchemeIs(chrome::kJavaScriptScheme)) | 1402 if (!url.SchemeIs(chrome::kJavaScriptScheme)) |
1394 DCHECK_EQ(url.spec(), web_contents_->GetURL().spec()); | 1403 DCHECK_EQ(url.spec(), web_contents_->GetURL().spec()); |
1395 | 1404 |
1396 return true; | 1405 return true; |
1397 } | 1406 } |
1398 | 1407 |
1399 void UpdateTabFunction::PopulateResult() { | 1408 void TabsUpdateFunction::PopulateResult() { |
1400 if (!has_callback()) | 1409 if (!has_callback()) |
1401 return; | 1410 return; |
1402 | 1411 |
1403 SetResult(ExtensionTabUtil::CreateTabValue(web_contents_, GetExtension())); | 1412 SetResult(ExtensionTabUtil::CreateTabValue(web_contents_, GetExtension())); |
1404 } | 1413 } |
1405 | 1414 |
1406 void UpdateTabFunction::OnExecuteCodeFinished(const std::string& error, | 1415 void TabsUpdateFunction::OnExecuteCodeFinished(const std::string& error, |
1407 int32 on_page_id, | 1416 int32 on_page_id, |
1408 const GURL& url, | 1417 const GURL& url, |
1409 const ListValue& script_result) { | 1418 const ListValue& script_result) { |
1410 if (error.empty()) | 1419 if (error.empty()) |
1411 PopulateResult(); | 1420 PopulateResult(); |
1412 else | 1421 else |
1413 error_ = error; | 1422 error_ = error; |
1414 SendResponse(error.empty()); | 1423 SendResponse(error.empty()); |
1415 } | 1424 } |
1416 | 1425 |
1417 bool MoveTabsFunction::RunImpl() { | 1426 bool TabsMoveFunction::RunImpl() { |
1418 Value* tab_value = NULL; | 1427 Value* tab_value = NULL; |
1419 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &tab_value)); | 1428 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &tab_value)); |
1420 | 1429 |
1421 std::vector<int> tab_ids; | 1430 std::vector<int> tab_ids; |
1422 EXTENSION_FUNCTION_VALIDATE(extensions::ReadOneOrMoreIntegers( | 1431 EXTENSION_FUNCTION_VALIDATE(extensions::ReadOneOrMoreIntegers( |
1423 tab_value, &tab_ids)); | 1432 tab_value, &tab_ids)); |
1424 | 1433 |
1425 DictionaryValue* update_props = NULL; | 1434 DictionaryValue* update_props = NULL; |
1426 int new_index; | 1435 int new_index; |
1427 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &update_props)); | 1436 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &update_props)); |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1528 if (tab_ids.size() > 1) { | 1537 if (tab_ids.size() > 1) { |
1529 SetResult(tab_values.DeepCopy()); | 1538 SetResult(tab_values.DeepCopy()); |
1530 } else if (tab_ids.size() == 1) { | 1539 } else if (tab_ids.size() == 1) { |
1531 Value* value = NULL; | 1540 Value* value = NULL; |
1532 CHECK(tab_values.Get(0, &value)); | 1541 CHECK(tab_values.Get(0, &value)); |
1533 SetResult(value->DeepCopy()); | 1542 SetResult(value->DeepCopy()); |
1534 } | 1543 } |
1535 return true; | 1544 return true; |
1536 } | 1545 } |
1537 | 1546 |
1538 bool ReloadTabFunction::RunImpl() { | 1547 bool TabsReloadFunction::RunImpl() { |
1539 bool bypass_cache = false; | 1548 bool bypass_cache = false; |
1540 if (HasOptionalArgument(1)) { | 1549 if (HasOptionalArgument(1)) { |
1541 DictionaryValue* reload_props = NULL; | 1550 DictionaryValue* reload_props = NULL; |
1542 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &reload_props)); | 1551 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &reload_props)); |
1543 | 1552 |
1544 if (reload_props->HasKey(keys::kBypassCache)) { | 1553 if (reload_props->HasKey(keys::kBypassCache)) { |
1545 EXTENSION_FUNCTION_VALIDATE(reload_props->GetBoolean( | 1554 EXTENSION_FUNCTION_VALIDATE(reload_props->GetBoolean( |
1546 keys::kBypassCache, | 1555 keys::kBypassCache, |
1547 &bypass_cache)); | 1556 &bypass_cache)); |
1548 } | 1557 } |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1583 GetCurrentBrowser()->OpenURL(params); | 1592 GetCurrentBrowser()->OpenURL(params); |
1584 } else if (bypass_cache) { | 1593 } else if (bypass_cache) { |
1585 web_contents->GetController().ReloadIgnoringCache(true); | 1594 web_contents->GetController().ReloadIgnoringCache(true); |
1586 } else { | 1595 } else { |
1587 web_contents->GetController().Reload(true); | 1596 web_contents->GetController().Reload(true); |
1588 } | 1597 } |
1589 | 1598 |
1590 return true; | 1599 return true; |
1591 } | 1600 } |
1592 | 1601 |
1593 bool RemoveTabsFunction::RunImpl() { | 1602 bool TabsRemoveFunction::RunImpl() { |
1594 Value* tab_value = NULL; | 1603 Value* tab_value = NULL; |
1595 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &tab_value)); | 1604 EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &tab_value)); |
1596 | 1605 |
1597 std::vector<int> tab_ids; | 1606 std::vector<int> tab_ids; |
1598 EXTENSION_FUNCTION_VALIDATE(extensions::ReadOneOrMoreIntegers( | 1607 EXTENSION_FUNCTION_VALIDATE(extensions::ReadOneOrMoreIntegers( |
1599 tab_value, &tab_ids)); | 1608 tab_value, &tab_ids)); |
1600 | 1609 |
1601 for (size_t i = 0; i < tab_ids.size(); ++i) { | 1610 for (size_t i = 0; i < tab_ids.size(); ++i) { |
1602 Browser* browser = NULL; | 1611 Browser* browser = NULL; |
1603 WebContents* contents = NULL; | 1612 WebContents* contents = NULL; |
1604 if (!GetTabById(tab_ids[i], profile(), include_incognito(), | 1613 if (!GetTabById(tab_ids[i], profile(), include_incognito(), |
1605 &browser, NULL, &contents, NULL, &error_)) | 1614 &browser, NULL, &contents, NULL, &error_)) |
1606 return false; | 1615 return false; |
1607 | 1616 |
1608 // Don't let the extension remove a tab if the user is dragging tabs around. | 1617 // Don't let the extension remove a tab if the user is dragging tabs around. |
1609 if (!browser->window()->IsTabStripEditable()) { | 1618 if (!browser->window()->IsTabStripEditable()) { |
1610 error_ = keys::kTabStripNotEditableError; | 1619 error_ = keys::kTabStripNotEditableError; |
1611 return false; | 1620 return false; |
1612 } | 1621 } |
1613 | 1622 |
1614 // There's a chance that the tab is being dragged, or we're in some other | 1623 // There's a chance that the tab is being dragged, or we're in some other |
1615 // nested event loop. This code path ensures that the tab is safely closed | 1624 // nested event loop. This code path ensures that the tab is safely closed |
1616 // under such circumstances, whereas |TabStripModel::CloseWebContentsAt()| | 1625 // under such circumstances, whereas |TabStripModel::CloseWebContentsAt()| |
1617 // does not. | 1626 // does not. |
1618 contents->Close(); | 1627 contents->Close(); |
1619 } | 1628 } |
1620 return true; | 1629 return true; |
1621 } | 1630 } |
1622 | 1631 |
1623 bool CaptureVisibleTabFunction::GetTabToCapture(WebContents** web_contents) { | 1632 bool TabsCaptureVisibleTabFunction::GetTabToCapture( |
| 1633 WebContents** web_contents) { |
1624 Browser* browser = NULL; | 1634 Browser* browser = NULL; |
1625 // windowId defaults to "current" window. | 1635 // windowId defaults to "current" window. |
1626 int window_id = extension_misc::kCurrentWindowId; | 1636 int window_id = extension_misc::kCurrentWindowId; |
1627 | 1637 |
1628 if (HasOptionalArgument(0)) | 1638 if (HasOptionalArgument(0)) |
1629 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &window_id)); | 1639 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &window_id)); |
1630 | 1640 |
1631 if (!GetBrowserFromWindowID(this, window_id, &browser)) | 1641 if (!GetBrowserFromWindowID(this, window_id, &browser)) |
1632 return false; | 1642 return false; |
1633 | 1643 |
1634 *web_contents = chrome::GetActiveWebContents(browser); | 1644 *web_contents = chrome::GetActiveWebContents(browser); |
1635 if (*web_contents == NULL) { | 1645 if (*web_contents == NULL) { |
1636 error_ = keys::kInternalVisibleTabCaptureError; | 1646 error_ = keys::kInternalVisibleTabCaptureError; |
1637 return false; | 1647 return false; |
1638 } | 1648 } |
1639 | 1649 |
1640 return true; | 1650 return true; |
1641 }; | 1651 }; |
1642 | 1652 |
1643 bool CaptureVisibleTabFunction::RunImpl() { | 1653 bool TabsCaptureVisibleTabFunction::RunImpl() { |
1644 PrefServiceBase* service = profile()->GetPrefs(); | 1654 PrefServiceBase* service = profile()->GetPrefs(); |
1645 if (service->GetBoolean(prefs::kDisableScreenshots)) { | 1655 if (service->GetBoolean(prefs::kDisableScreenshots)) { |
1646 error_ = keys::kScreenshotsDisabled; | 1656 error_ = keys::kScreenshotsDisabled; |
1647 return false; | 1657 return false; |
1648 } | 1658 } |
1649 | 1659 |
1650 WebContents* web_contents = NULL; | 1660 WebContents* web_contents = NULL; |
1651 if (!GetTabToCapture(&web_contents)) | 1661 if (!GetTabToCapture(&web_contents)) |
1652 return false; | 1662 return false; |
1653 | 1663 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1692 RenderViewHost* render_view_host = web_contents->GetRenderViewHost(); | 1702 RenderViewHost* render_view_host = web_contents->GetRenderViewHost(); |
1693 content::RenderWidgetHostView* view = render_view_host->GetView(); | 1703 content::RenderWidgetHostView* view = render_view_host->GetView(); |
1694 if (!view) { | 1704 if (!view) { |
1695 error_ = keys::kInternalVisibleTabCaptureError; | 1705 error_ = keys::kInternalVisibleTabCaptureError; |
1696 return false; | 1706 return false; |
1697 } | 1707 } |
1698 skia::PlatformBitmap* temp_bitmap = new skia::PlatformBitmap; | 1708 skia::PlatformBitmap* temp_bitmap = new skia::PlatformBitmap; |
1699 render_view_host->CopyFromBackingStore( | 1709 render_view_host->CopyFromBackingStore( |
1700 gfx::Rect(), | 1710 gfx::Rect(), |
1701 view->GetViewBounds().size(), | 1711 view->GetViewBounds().size(), |
1702 base::Bind(&CaptureVisibleTabFunction::CopyFromBackingStoreComplete, | 1712 base::Bind(&TabsCaptureVisibleTabFunction::CopyFromBackingStoreComplete, |
1703 this, | 1713 this, |
1704 base::Owned(temp_bitmap)), | 1714 base::Owned(temp_bitmap)), |
1705 temp_bitmap); | 1715 temp_bitmap); |
1706 return true; | 1716 return true; |
1707 } | 1717 } |
1708 | 1718 |
1709 void CaptureVisibleTabFunction::CopyFromBackingStoreComplete( | 1719 void TabsCaptureVisibleTabFunction::CopyFromBackingStoreComplete( |
1710 skia::PlatformBitmap* bitmap, | 1720 skia::PlatformBitmap* bitmap, |
1711 bool succeeded) { | 1721 bool succeeded) { |
1712 if (succeeded) { | 1722 if (succeeded) { |
1713 VLOG(1) << "captureVisibleTab() got image from backing store."; | 1723 VLOG(1) << "captureVisibleTab() got image from backing store."; |
1714 SendResultFromBitmap(bitmap->GetBitmap()); | 1724 SendResultFromBitmap(bitmap->GetBitmap()); |
1715 return; | 1725 return; |
1716 } | 1726 } |
1717 | 1727 |
1718 WebContents* web_contents = NULL; | 1728 WebContents* web_contents = NULL; |
1719 if (!GetTabToCapture(&web_contents)) { | 1729 if (!GetTabToCapture(&web_contents)) { |
1720 error_ = keys::kInternalVisibleTabCaptureError; | 1730 error_ = keys::kInternalVisibleTabCaptureError; |
1721 SendResponse(false); | 1731 SendResponse(false); |
1722 return; | 1732 return; |
1723 } | 1733 } |
1724 | 1734 |
1725 // Ask the renderer for a snapshot of the tab. | 1735 // Ask the renderer for a snapshot of the tab. |
1726 registrar_.Add(this, | 1736 registrar_.Add(this, |
1727 chrome::NOTIFICATION_TAB_SNAPSHOT_TAKEN, | 1737 chrome::NOTIFICATION_TAB_SNAPSHOT_TAKEN, |
1728 content::Source<WebContents>(web_contents)); | 1738 content::Source<WebContents>(web_contents)); |
1729 AddRef(); // Balanced in CaptureVisibleTabFunction::Observe(). | 1739 AddRef(); // Balanced in TabsCaptureVisibleTabFunction::Observe(). |
1730 SnapshotTabHelper::FromWebContents(web_contents)->CaptureSnapshot(); | 1740 SnapshotTabHelper::FromWebContents(web_contents)->CaptureSnapshot(); |
1731 } | 1741 } |
1732 | 1742 |
1733 // If a backing store was not available in CaptureVisibleTabFunction::RunImpl, | 1743 // If a backing store was not available in |
1734 // than the renderer was asked for a snapshot. Listen for a notification | 1744 // TabsCaptureVisibleTabFunction::RunImpl, than the renderer was asked for a |
1735 // that the snapshot is available. | 1745 // snapshot. Listen for a notification that the snapshot is available. |
1736 void CaptureVisibleTabFunction::Observe( | 1746 void TabsCaptureVisibleTabFunction::Observe( |
1737 int type, | 1747 int type, |
1738 const content::NotificationSource& source, | 1748 const content::NotificationSource& source, |
1739 const content::NotificationDetails& details) { | 1749 const content::NotificationDetails& details) { |
1740 DCHECK(type == chrome::NOTIFICATION_TAB_SNAPSHOT_TAKEN); | 1750 DCHECK(type == chrome::NOTIFICATION_TAB_SNAPSHOT_TAKEN); |
1741 | 1751 |
1742 const SkBitmap *screen_capture = | 1752 const SkBitmap *screen_capture = |
1743 content::Details<const SkBitmap>(details).ptr(); | 1753 content::Details<const SkBitmap>(details).ptr(); |
1744 const bool error = screen_capture->empty(); | 1754 const bool error = screen_capture->empty(); |
1745 | 1755 |
1746 if (error) { | 1756 if (error) { |
1747 error_ = keys::kInternalVisibleTabCaptureError; | 1757 error_ = keys::kInternalVisibleTabCaptureError; |
1748 SendResponse(false); | 1758 SendResponse(false); |
1749 } else { | 1759 } else { |
1750 VLOG(1) << "captureVisibleTab() got image from renderer."; | 1760 VLOG(1) << "captureVisibleTab() got image from renderer."; |
1751 SendResultFromBitmap(*screen_capture); | 1761 SendResultFromBitmap(*screen_capture); |
1752 } | 1762 } |
1753 | 1763 |
1754 Release(); // Balanced in CaptureVisibleTabFunction::RunImpl(). | 1764 Release(); // Balanced in TabsCaptureVisibleTabFunction::RunImpl(). |
1755 } | 1765 } |
1756 | 1766 |
1757 // Turn a bitmap of the screen into an image, set that image as the result, | 1767 // Turn a bitmap of the screen into an image, set that image as the result, |
1758 // and call SendResponse(). | 1768 // and call SendResponse(). |
1759 void CaptureVisibleTabFunction::SendResultFromBitmap( | 1769 void TabsCaptureVisibleTabFunction::SendResultFromBitmap( |
1760 const SkBitmap& screen_capture) { | 1770 const SkBitmap& screen_capture) { |
1761 std::vector<unsigned char> data; | 1771 std::vector<unsigned char> data; |
1762 SkAutoLockPixels screen_capture_lock(screen_capture); | 1772 SkAutoLockPixels screen_capture_lock(screen_capture); |
1763 bool encoded = false; | 1773 bool encoded = false; |
1764 std::string mime_type; | 1774 std::string mime_type; |
1765 switch (image_format_) { | 1775 switch (image_format_) { |
1766 case FORMAT_JPEG: | 1776 case FORMAT_JPEG: |
1767 encoded = gfx::JPEGCodec::Encode( | 1777 encoded = gfx::JPEGCodec::Encode( |
1768 reinterpret_cast<unsigned char*>(screen_capture.getAddr32(0, 0)), | 1778 reinterpret_cast<unsigned char*>(screen_capture.getAddr32(0, 0)), |
1769 gfx::JPEGCodec::FORMAT_SkBitmap, | 1779 gfx::JPEGCodec::FORMAT_SkBitmap, |
(...skipping 25 matching lines...) Expand all Loading... |
1795 base::StringPiece stream_as_string( | 1805 base::StringPiece stream_as_string( |
1796 reinterpret_cast<const char*>(vector_as_array(&data)), data.size()); | 1806 reinterpret_cast<const char*>(vector_as_array(&data)), data.size()); |
1797 | 1807 |
1798 base::Base64Encode(stream_as_string, &base64_result); | 1808 base::Base64Encode(stream_as_string, &base64_result); |
1799 base64_result.insert(0, base::StringPrintf("data:%s;base64,", | 1809 base64_result.insert(0, base::StringPrintf("data:%s;base64,", |
1800 mime_type.c_str())); | 1810 mime_type.c_str())); |
1801 SetResult(new StringValue(base64_result)); | 1811 SetResult(new StringValue(base64_result)); |
1802 SendResponse(true); | 1812 SendResponse(true); |
1803 } | 1813 } |
1804 | 1814 |
1805 void CaptureVisibleTabFunction::RegisterUserPrefs( | 1815 void TabsCaptureVisibleTabFunction::RegisterUserPrefs( |
1806 PrefServiceSyncable* service) { | 1816 PrefServiceSyncable* service) { |
1807 service->RegisterBooleanPref(prefs::kDisableScreenshots, false, | 1817 service->RegisterBooleanPref(prefs::kDisableScreenshots, false, |
1808 PrefServiceSyncable::UNSYNCABLE_PREF); | 1818 PrefServiceSyncable::UNSYNCABLE_PREF); |
1809 } | 1819 } |
1810 | 1820 |
1811 bool DetectTabLanguageFunction::RunImpl() { | 1821 bool TabsDetectLanguageFunction::RunImpl() { |
1812 int tab_id = 0; | 1822 int tab_id = 0; |
1813 Browser* browser = NULL; | 1823 Browser* browser = NULL; |
1814 WebContents* contents = NULL; | 1824 WebContents* contents = NULL; |
1815 | 1825 |
1816 // If |tab_id| is specified, look for it. Otherwise default to selected tab | 1826 // If |tab_id| is specified, look for it. Otherwise default to selected tab |
1817 // in the current window. | 1827 // in the current window. |
1818 if (HasOptionalArgument(0)) { | 1828 if (HasOptionalArgument(0)) { |
1819 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &tab_id)); | 1829 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &tab_id)); |
1820 if (!GetTabById(tab_id, profile(), include_incognito(), | 1830 if (!GetTabById(tab_id, profile(), include_incognito(), |
1821 &browser, NULL, &contents, NULL, &error_)) { | 1831 &browser, NULL, &contents, NULL, &error_)) { |
(...skipping 17 matching lines...) Expand all Loading... |
1839 } | 1849 } |
1840 | 1850 |
1841 AddRef(); // Balanced in GotLanguage(). | 1851 AddRef(); // Balanced in GotLanguage(). |
1842 | 1852 |
1843 TranslateTabHelper* translate_tab_helper = | 1853 TranslateTabHelper* translate_tab_helper = |
1844 TranslateTabHelper::FromWebContents(contents); | 1854 TranslateTabHelper::FromWebContents(contents); |
1845 if (!translate_tab_helper->language_state().original_language().empty()) { | 1855 if (!translate_tab_helper->language_state().original_language().empty()) { |
1846 // Delay the callback invocation until after the current JS call has | 1856 // Delay the callback invocation until after the current JS call has |
1847 // returned. | 1857 // returned. |
1848 MessageLoop::current()->PostTask(FROM_HERE, base::Bind( | 1858 MessageLoop::current()->PostTask(FROM_HERE, base::Bind( |
1849 &DetectTabLanguageFunction::GotLanguage, this, | 1859 &TabsDetectLanguageFunction::GotLanguage, this, |
1850 translate_tab_helper->language_state().original_language())); | 1860 translate_tab_helper->language_state().original_language())); |
1851 return true; | 1861 return true; |
1852 } | 1862 } |
1853 // The tab contents does not know its language yet. Let's wait until it | 1863 // The tab contents does not know its language yet. Let's wait until it |
1854 // receives it, or until the tab is closed/navigates to some other page. | 1864 // receives it, or until the tab is closed/navigates to some other page. |
1855 registrar_.Add(this, chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED, | 1865 registrar_.Add(this, chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED, |
1856 content::Source<WebContents>(contents)); | 1866 content::Source<WebContents>(contents)); |
1857 registrar_.Add( | 1867 registrar_.Add( |
1858 this, chrome::NOTIFICATION_TAB_CLOSING, | 1868 this, chrome::NOTIFICATION_TAB_CLOSING, |
1859 content::Source<NavigationController>(&(contents->GetController()))); | 1869 content::Source<NavigationController>(&(contents->GetController()))); |
1860 registrar_.Add( | 1870 registrar_.Add( |
1861 this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, | 1871 this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, |
1862 content::Source<NavigationController>(&(contents->GetController()))); | 1872 content::Source<NavigationController>(&(contents->GetController()))); |
1863 return true; | 1873 return true; |
1864 } | 1874 } |
1865 | 1875 |
1866 void DetectTabLanguageFunction::Observe( | 1876 void TabsDetectLanguageFunction::Observe( |
1867 int type, | 1877 int type, |
1868 const content::NotificationSource& source, | 1878 const content::NotificationSource& source, |
1869 const content::NotificationDetails& details) { | 1879 const content::NotificationDetails& details) { |
1870 std::string language; | 1880 std::string language; |
1871 if (type == chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED) | 1881 if (type == chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED) |
1872 language = *content::Details<std::string>(details).ptr(); | 1882 language = *content::Details<std::string>(details).ptr(); |
1873 | 1883 |
1874 registrar_.RemoveAll(); | 1884 registrar_.RemoveAll(); |
1875 | 1885 |
1876 // Call GotLanguage in all cases as we want to guarantee the callback is | 1886 // Call GotLanguage in all cases as we want to guarantee the callback is |
1877 // called for every API call the extension made. | 1887 // called for every API call the extension made. |
1878 GotLanguage(language); | 1888 GotLanguage(language); |
1879 } | 1889 } |
1880 | 1890 |
1881 void DetectTabLanguageFunction::GotLanguage(const std::string& language) { | 1891 void TabsDetectLanguageFunction::GotLanguage(const std::string& language) { |
1882 SetResult(Value::CreateStringValue(language.c_str())); | 1892 SetResult(Value::CreateStringValue(language.c_str())); |
1883 SendResponse(true); | 1893 SendResponse(true); |
1884 | 1894 |
1885 Release(); // Balanced in Run() | 1895 Release(); // Balanced in Run() |
1886 } | 1896 } |
| 1897 |
| 1898 ExecuteCodeInTabFunction::ExecuteCodeInTabFunction() |
| 1899 : execute_tab_id_(-1) { |
| 1900 } |
| 1901 |
| 1902 ExecuteCodeInTabFunction::~ExecuteCodeInTabFunction() {} |
| 1903 |
| 1904 bool ExecuteCodeInTabFunction::HasPermission() { |
| 1905 if (Init() && |
| 1906 extension_->HasAPIPermissionForTab(execute_tab_id_, |
| 1907 extensions::APIPermission::kTab)) { |
| 1908 return true; |
| 1909 } |
| 1910 return ExtensionFunction::HasPermission(); |
| 1911 } |
| 1912 |
| 1913 bool ExecuteCodeInTabFunction::RunImpl() { |
| 1914 EXTENSION_FUNCTION_VALIDATE(Init()); |
| 1915 |
| 1916 if (!details_->code.get() && !details_->file.get()) { |
| 1917 error_ = keys::kNoCodeOrFileToExecuteError; |
| 1918 return false; |
| 1919 } |
| 1920 if (details_->code.get() && details_->file.get()) { |
| 1921 error_ = keys::kMoreThanOneValuesError; |
| 1922 return false; |
| 1923 } |
| 1924 |
| 1925 content::WebContents* contents = NULL; |
| 1926 |
| 1927 // If |tab_id| is specified, look for the tab. Otherwise default to selected |
| 1928 // tab in the current window. |
| 1929 CHECK_GE(execute_tab_id_, 0); |
| 1930 if (!ExtensionTabUtil::GetTabById(execute_tab_id_, profile(), |
| 1931 include_incognito(), |
| 1932 NULL, NULL, &contents, NULL)) { |
| 1933 return false; |
| 1934 } |
| 1935 |
| 1936 // NOTE: This can give the wrong answer due to race conditions, but it is OK, |
| 1937 // we check again in the renderer. |
| 1938 CHECK(contents); |
| 1939 if (!GetExtension()->CanExecuteScriptOnPage(contents->GetURL(), |
| 1940 contents->GetURL(), |
| 1941 execute_tab_id_, |
| 1942 NULL, |
| 1943 &error_)) { |
| 1944 return false; |
| 1945 } |
| 1946 |
| 1947 if (details_->code.get()) |
| 1948 return Execute(*details_->code); |
| 1949 |
| 1950 CHECK(details_->file.get()); |
| 1951 resource_ = GetExtension()->GetResource(*details_->file); |
| 1952 |
| 1953 if (resource_.extension_root().empty() || resource_.relative_path().empty()) { |
| 1954 error_ = keys::kNoCodeOrFileToExecuteError; |
| 1955 return false; |
| 1956 } |
| 1957 |
| 1958 scoped_refptr<FileReader> file_reader(new FileReader( |
| 1959 resource_, base::Bind(&ExecuteCodeInTabFunction::DidLoadFile, this))); |
| 1960 file_reader->Start(); |
| 1961 |
| 1962 return true; |
| 1963 } |
| 1964 |
| 1965 void ExecuteCodeInTabFunction::OnExecuteCodeFinished(const std::string& error, |
| 1966 int32 on_page_id, |
| 1967 const GURL& on_url, |
| 1968 const ListValue& result) { |
| 1969 if (!error.empty()) |
| 1970 SetError(error); |
| 1971 |
| 1972 SendResponse(error.empty()); |
| 1973 } |
| 1974 |
| 1975 void TabsExecuteScriptFunction::OnExecuteCodeFinished(const std::string& error, |
| 1976 int32 on_page_id, |
| 1977 const GURL& on_url, |
| 1978 const ListValue& result) { |
| 1979 if (error.empty()) |
| 1980 SetResult(result.DeepCopy()); |
| 1981 ExecuteCodeInTabFunction::OnExecuteCodeFinished(error, on_page_id, on_url, |
| 1982 result); |
| 1983 } |
| 1984 |
| 1985 bool ExecuteCodeInTabFunction::Init() { |
| 1986 if (details_.get()) |
| 1987 return true; |
| 1988 |
| 1989 // |tab_id| is optional so it's ok if it's not there. |
| 1990 int tab_id = -1; |
| 1991 args_->GetInteger(0, &tab_id); |
| 1992 |
| 1993 // |details| are not optional. |
| 1994 DictionaryValue* details_value = NULL; |
| 1995 if (!args_->GetDictionary(1, &details_value)) |
| 1996 return false; |
| 1997 scoped_ptr<InjectDetails> details(new InjectDetails()); |
| 1998 if (!InjectDetails::Populate(*details_value, details.get())) |
| 1999 return false; |
| 2000 |
| 2001 // If the tab ID is -1 then it needs to be converted to the currently active |
| 2002 // tab's ID. |
| 2003 if (tab_id == -1) { |
| 2004 Browser* browser = GetCurrentBrowser(); |
| 2005 if (!browser) |
| 2006 return false; |
| 2007 content::WebContents* web_contents = NULL; |
| 2008 if (!ExtensionTabUtil::GetDefaultTab(browser, &web_contents, &tab_id)) |
| 2009 return false; |
| 2010 } |
| 2011 |
| 2012 execute_tab_id_ = tab_id; |
| 2013 details_ = details.Pass(); |
| 2014 return true; |
| 2015 } |
| 2016 |
| 2017 void ExecuteCodeInTabFunction::DidLoadFile(bool success, |
| 2018 const std::string& data) { |
| 2019 std::string function_name = name(); |
| 2020 const extensions::Extension* extension = GetExtension(); |
| 2021 |
| 2022 // Check if the file is CSS and needs localization. |
| 2023 if (success && |
| 2024 function_name == TabsInsertCSSFunction::function_name() && |
| 2025 extension != NULL && |
| 2026 data.find( |
| 2027 extensions::MessageBundle::kMessageBegin) != std::string::npos) { |
| 2028 BrowserThread::PostTask( |
| 2029 BrowserThread::FILE, FROM_HERE, |
| 2030 base::Bind(&ExecuteCodeInTabFunction::LocalizeCSS, this, |
| 2031 data, |
| 2032 extension->id(), |
| 2033 extension->path(), |
| 2034 extension->default_locale())); |
| 2035 } else { |
| 2036 DidLoadAndLocalizeFile(success, data); |
| 2037 } |
| 2038 } |
| 2039 |
| 2040 void ExecuteCodeInTabFunction::LocalizeCSS( |
| 2041 const std::string& data, |
| 2042 const std::string& extension_id, |
| 2043 const FilePath& extension_path, |
| 2044 const std::string& extension_default_locale) { |
| 2045 scoped_ptr<SubstitutionMap> localization_messages( |
| 2046 extension_file_util::LoadMessageBundleSubstitutionMap( |
| 2047 extension_path, extension_id, extension_default_locale)); |
| 2048 |
| 2049 // We need to do message replacement on the data, so it has to be mutable. |
| 2050 std::string css_data = data; |
| 2051 std::string error; |
| 2052 extensions::MessageBundle::ReplaceMessagesWithExternalDictionary( |
| 2053 *localization_messages, &css_data, &error); |
| 2054 |
| 2055 // Call back DidLoadAndLocalizeFile on the UI thread. The success parameter |
| 2056 // is always true, because if loading had failed, we wouldn't have had |
| 2057 // anything to localize. |
| 2058 BrowserThread::PostTask( |
| 2059 BrowserThread::UI, FROM_HERE, |
| 2060 base::Bind(&ExecuteCodeInTabFunction::DidLoadAndLocalizeFile, this, |
| 2061 true, css_data)); |
| 2062 } |
| 2063 |
| 2064 void ExecuteCodeInTabFunction::DidLoadAndLocalizeFile(bool success, |
| 2065 const std::string& data) { |
| 2066 if (success) { |
| 2067 if (!Execute(data)) |
| 2068 SendResponse(false); |
| 2069 } else { |
| 2070 #if defined(OS_POSIX) |
| 2071 // TODO(viettrungluu): bug: there's no particular reason the path should be |
| 2072 // UTF-8, in which case this may fail. |
| 2073 error_ = ErrorUtils::FormatErrorMessage(keys::kLoadFileError, |
| 2074 resource_.relative_path().value()); |
| 2075 #elif defined(OS_WIN) |
| 2076 error_ = ErrorUtils::FormatErrorMessage(keys::kLoadFileError, |
| 2077 WideToUTF8(resource_.relative_path().value())); |
| 2078 #endif // OS_WIN |
| 2079 SendResponse(false); |
| 2080 } |
| 2081 } |
| 2082 |
| 2083 bool ExecuteCodeInTabFunction::Execute(const std::string& code_string) { |
| 2084 content::WebContents* contents = NULL; |
| 2085 Browser* browser = NULL; |
| 2086 |
| 2087 bool success = ExtensionTabUtil::GetTabById( |
| 2088 execute_tab_id_, profile(), include_incognito(), &browser, NULL, |
| 2089 &contents, NULL) && contents && browser; |
| 2090 |
| 2091 if (!success) |
| 2092 return false; |
| 2093 |
| 2094 const extensions::Extension* extension = GetExtension(); |
| 2095 if (!extension) |
| 2096 return false; |
| 2097 |
| 2098 ScriptExecutor::ScriptType script_type = ScriptExecutor::JAVASCRIPT; |
| 2099 std::string function_name = name(); |
| 2100 if (function_name == TabsInsertCSSFunction::function_name()) { |
| 2101 script_type = ScriptExecutor::CSS; |
| 2102 } else if (function_name != TabsExecuteScriptFunction::function_name()) { |
| 2103 NOTREACHED(); |
| 2104 } |
| 2105 |
| 2106 ScriptExecutor::FrameScope frame_scope = |
| 2107 details_->all_frames.get() && *details_->all_frames ? |
| 2108 ScriptExecutor::ALL_FRAMES : |
| 2109 ScriptExecutor::TOP_FRAME; |
| 2110 |
| 2111 UserScript::RunLocation run_at = UserScript::UNDEFINED; |
| 2112 switch (details_->run_at) { |
| 2113 case InjectDetails::RUN_AT_NONE: |
| 2114 case InjectDetails::RUN_AT_DOCUMENT_IDLE: |
| 2115 run_at = UserScript::DOCUMENT_IDLE; |
| 2116 break; |
| 2117 case InjectDetails::RUN_AT_DOCUMENT_START: |
| 2118 run_at = UserScript::DOCUMENT_START; |
| 2119 break; |
| 2120 case InjectDetails::RUN_AT_DOCUMENT_END: |
| 2121 run_at = UserScript::DOCUMENT_END; |
| 2122 break; |
| 2123 } |
| 2124 CHECK_NE(UserScript::UNDEFINED, run_at); |
| 2125 |
| 2126 extensions::TabHelper::FromWebContents(contents)-> |
| 2127 script_executor()->ExecuteScript( |
| 2128 extension->id(), |
| 2129 script_type, |
| 2130 code_string, |
| 2131 frame_scope, |
| 2132 run_at, |
| 2133 ScriptExecutor::ISOLATED_WORLD, |
| 2134 base::Bind(&ExecuteCodeInTabFunction::OnExecuteCodeFinished, this)); |
| 2135 return true; |
| 2136 } |
OLD | NEW |