OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 <string> |
| 6 #include <vector> |
| 7 |
5 #include "chrome/browser/extensions/api/tabs/windows_util.h" | 8 #include "chrome/browser/extensions/api/tabs/windows_util.h" |
6 | 9 |
7 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
8 #include "chrome/browser/extensions/api/tabs/tabs_constants.h" | 11 #include "chrome/browser/extensions/api/tabs/tabs_constants.h" |
9 #include "chrome/browser/extensions/chrome_extension_function.h" | 12 #include "chrome/browser/extensions/chrome_extension_function.h" |
10 #include "chrome/browser/extensions/chrome_extension_function_details.h" | 13 #include "chrome/browser/extensions/chrome_extension_function_details.h" |
| 14 #include "chrome/browser/extensions/extension_util.h" |
11 #include "chrome/browser/extensions/window_controller.h" | 15 #include "chrome/browser/extensions/window_controller.h" |
12 #include "chrome/browser/extensions/window_controller_list.h" | 16 #include "chrome/browser/extensions/window_controller_list.h" |
13 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
14 #include "extensions/browser/extension_function.h" | 18 #include "extensions/browser/extension_function.h" |
15 #include "extensions/browser/extension_function_dispatcher.h" | 19 #include "extensions/browser/extension_function_dispatcher.h" |
16 #include "extensions/common/constants.h" | 20 #include "extensions/common/constants.h" |
17 #include "extensions/common/error_utils.h" | 21 #include "extensions/common/error_utils.h" |
| 22 #include "extensions/common/extension.h" |
| 23 |
| 24 namespace keys = extensions::tabs_constants; |
| 25 namespace windows = extensions::api::windows; |
18 | 26 |
19 namespace windows_util { | 27 namespace windows_util { |
20 | 28 |
| 29 extensions::WindowTypeFilter WindowTypeFilterFromWindowTypes( |
| 30 const std::vector<extensions::api::windows::WindowType>& types) { |
| 31 extensions::WindowTypeFilter filter = 0; |
| 32 for (unsigned int i = 0; i < types.size(); i++) |
| 33 filter |= 1 << types[i]; |
| 34 return filter; |
| 35 } |
| 36 |
21 bool GetWindowFromWindowID(UIThreadExtensionFunction* function, | 37 bool GetWindowFromWindowID(UIThreadExtensionFunction* function, |
22 int window_id, | 38 int window_id, |
| 39 extensions::WindowTypeFilter filter, |
23 extensions::WindowController** controller) { | 40 extensions::WindowController** controller) { |
24 if (window_id == extension_misc::kCurrentWindowId) { | 41 if (window_id == extension_misc::kCurrentWindowId) { |
25 extensions::WindowController* extension_window_controller = | 42 extensions::WindowController* extension_window_controller = |
26 function->dispatcher()->GetExtensionWindowController(); | 43 function->dispatcher()->GetExtensionWindowController(); |
27 // If there is a window controller associated with this extension, use that. | 44 // If there is a window controller associated with this extension, use that. |
28 if (extension_window_controller) { | 45 if (extension_window_controller) { |
29 *controller = extension_window_controller; | 46 *controller = extension_window_controller; |
30 } else { | 47 } else { |
31 // Otherwise get the focused or most recently added window. | 48 // Otherwise get the focused or most recently added window. |
32 *controller = extensions::WindowControllerList::GetInstance() | 49 *controller = extensions::WindowControllerList::GetInstance() |
33 ->CurrentWindowForFunction(function); | 50 ->CurrentWindowForFunction(function); |
34 } | 51 } |
35 if (!(*controller)) { | 52 if (!(*controller)) { |
36 function->SetError(extensions::tabs_constants::kNoCurrentWindowError); | 53 function->SetError(extensions::tabs_constants::kNoCurrentWindowError); |
37 return false; | 54 return false; |
38 } | 55 } |
39 } else { | 56 } else { |
40 *controller = extensions::WindowControllerList::GetInstance() | 57 *controller = |
41 ->FindWindowForFunctionById(function, window_id); | 58 extensions::WindowControllerList::GetInstance() |
| 59 ->FindWindowForFunctionByIdWithFilter(function, window_id, filter); |
42 if (!(*controller)) { | 60 if (!(*controller)) { |
43 function->SetError(extensions::ErrorUtils::FormatErrorMessage( | 61 function->SetError(extensions::ErrorUtils::FormatErrorMessage( |
44 extensions::tabs_constants::kWindowNotFoundError, | 62 extensions::tabs_constants::kWindowNotFoundError, |
45 base::IntToString(window_id))); | 63 base::IntToString(window_id))); |
46 return false; | 64 return false; |
47 } | 65 } |
48 } | 66 } |
49 return true; | 67 return true; |
50 } | 68 } |
51 | 69 |
52 bool CanOperateOnWindow(const UIThreadExtensionFunction* function, | 70 bool CanOperateOnWindow(const UIThreadExtensionFunction* function, |
53 const extensions::WindowController* controller) { | 71 const extensions::WindowController* controller, |
54 if (function->extension() != NULL && | 72 extensions::WindowTypeFilter filter) { |
55 !controller->IsVisibleToExtension(function->extension())) { | 73 if (!controller->MatchesFilter(filter)) |
56 return false; | 74 return false; |
57 } | |
58 | 75 |
59 if (function->browser_context() == controller->profile()) | 76 if (function->browser_context() == controller->profile()) |
60 return true; | 77 return true; |
61 | 78 |
62 if (!function->include_incognito()) | 79 if (!function->include_incognito()) |
63 return false; | 80 return false; |
64 | 81 |
65 Profile* profile = Profile::FromBrowserContext(function->browser_context()); | 82 Profile* profile = Profile::FromBrowserContext(function->browser_context()); |
66 return profile->HasOffTheRecordProfile() && | 83 return profile->HasOffTheRecordProfile() && |
67 profile->GetOffTheRecordProfile() == controller->profile(); | 84 profile->GetOffTheRecordProfile() == controller->profile(); |
68 } | 85 } |
69 | 86 |
70 } // namespace windows_util | 87 } // namespace windows_util |
OLD | NEW |