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 GetDefaultWindowTypeFilter() { |
| 30 return (extensions::WINDOW_TYPE_FILTER_NORMAL | |
| 31 extensions::WINDOW_TYPE_FILTER_PANEL | |
| 32 extensions::WINDOW_TYPE_FILTER_POPUP); |
| 33 } |
| 34 |
| 35 extensions::WindowTypeFilter GetAllWindowTypeFilter() { |
| 36 // extensions::WindowTypeFilterValues needs to be updated if there |
| 37 // is a change to extensions::api::windows:WindowType. |
| 38 COMPILE_ASSERT(extensions::api::windows::WINDOW_TYPE_LAST == |
| 39 extensions::api::windows::WINDOW_TYPE_DEVTOOLS, |
| 40 Update_WindowControllerList_to_match_WindowType); |
| 41 COMPILE_ASSERT((1 << extensions::api::windows::WINDOW_TYPE_DEVTOOLS) == |
| 42 extensions::WINDOW_TYPE_FILTER_DEVTOOLS, |
| 43 Update_WindowControllerList_to_match_WindowType); |
| 44 return (extensions::WINDOW_TYPE_FILTER_NORMAL | |
| 45 extensions::WINDOW_TYPE_FILTER_PANEL | |
| 46 extensions::WINDOW_TYPE_FILTER_POPUP | |
| 47 extensions::WINDOW_TYPE_FILTER_APP | |
| 48 extensions::WINDOW_TYPE_FILTER_DEVTOOLS); |
| 49 } |
| 50 |
| 51 bool WindowMatchesTypeFilter(const extensions::WindowController* controller, |
| 52 extensions::WindowTypeFilter filter) { |
| 53 extensions::WindowTypeFilter type = |
| 54 1 << extensions::api::windows::ParseWindowType( |
| 55 controller->GetWindowTypeText()); |
| 56 return (type & filter) != 0; |
| 57 } |
| 58 |
| 59 extensions::WindowTypeFilter WindowTypeFilterFromWindowTypes( |
| 60 const std::vector<extensions::api::windows::WindowType>& types) { |
| 61 extensions::WindowTypeFilter filter = 0; |
| 62 for (unsigned int i = 0; i < types.size(); i++) |
| 63 filter |= 1 << types[i]; |
| 64 return filter; |
| 65 } |
| 66 |
21 bool GetWindowFromWindowID(UIThreadExtensionFunction* function, | 67 bool GetWindowFromWindowID(UIThreadExtensionFunction* function, |
22 int window_id, | 68 int window_id, |
| 69 extensions::WindowTypeFilter filter, |
23 extensions::WindowController** controller) { | 70 extensions::WindowController** controller) { |
24 if (window_id == extension_misc::kCurrentWindowId) { | 71 if (window_id == extension_misc::kCurrentWindowId) { |
25 extensions::WindowController* extension_window_controller = | 72 extensions::WindowController* extension_window_controller = |
26 function->dispatcher()->GetExtensionWindowController(); | 73 function->dispatcher()->GetExtensionWindowController(); |
27 // If there is a window controller associated with this extension, use that. | 74 // If there is a window controller associated with this extension, use that. |
28 if (extension_window_controller) { | 75 if (extension_window_controller) { |
29 *controller = extension_window_controller; | 76 *controller = extension_window_controller; |
30 } else { | 77 } else { |
31 // Otherwise get the focused or most recently added window. | 78 // Otherwise get the focused or most recently added window. |
32 *controller = extensions::WindowControllerList::GetInstance() | 79 *controller = extensions::WindowControllerList::GetInstance() |
33 ->CurrentWindowForFunction(function); | 80 ->CurrentWindowForFunction(function); |
34 } | 81 } |
35 if (!(*controller)) { | 82 if (!(*controller)) { |
36 function->SetError(extensions::tabs_constants::kNoCurrentWindowError); | 83 function->SetError(extensions::tabs_constants::kNoCurrentWindowError); |
37 return false; | 84 return false; |
38 } | 85 } |
39 } else { | 86 } else { |
40 *controller = extensions::WindowControllerList::GetInstance() | 87 *controller = |
41 ->FindWindowForFunctionById(function, window_id); | 88 extensions::WindowControllerList::GetInstance() |
| 89 ->FindWindowForFunctionByIdWithFilter(function, window_id, filter); |
42 if (!(*controller)) { | 90 if (!(*controller)) { |
43 function->SetError(extensions::ErrorUtils::FormatErrorMessage( | 91 function->SetError(extensions::ErrorUtils::FormatErrorMessage( |
44 extensions::tabs_constants::kWindowNotFoundError, | 92 extensions::tabs_constants::kWindowNotFoundError, |
45 base::IntToString(window_id))); | 93 base::IntToString(window_id))); |
46 return false; | 94 return false; |
47 } | 95 } |
48 } | 96 } |
49 return true; | 97 return true; |
50 } | 98 } |
51 | 99 |
52 bool CanOperateOnWindow(const UIThreadExtensionFunction* function, | 100 bool CanOperateOnWindow(const UIThreadExtensionFunction* function, |
53 const extensions::WindowController* controller) { | 101 const extensions::WindowController* controller, |
54 if (function->extension() != NULL && | 102 extensions::WindowTypeFilter filter) { |
55 !controller->IsVisibleToExtension(function->extension())) { | 103 if (!WindowMatchesTypeFilter(controller, filter)) |
56 return false; | 104 return false; |
57 } | |
58 | 105 |
59 if (function->browser_context() == controller->profile()) | 106 if (function->browser_context() == controller->profile()) |
60 return true; | 107 return true; |
61 | 108 |
62 if (!function->include_incognito()) | 109 if (!function->include_incognito()) |
63 return false; | 110 return false; |
64 | 111 |
65 Profile* profile = Profile::FromBrowserContext(function->browser_context()); | 112 Profile* profile = Profile::FromBrowserContext(function->browser_context()); |
66 return profile->HasOffTheRecordProfile() && | 113 return profile->HasOffTheRecordProfile() && |
67 profile->GetOffTheRecordProfile() == controller->profile(); | 114 profile->GetOffTheRecordProfile() == controller->profile(); |
68 } | 115 } |
69 | 116 |
70 } // namespace windows_util | 117 } // namespace windows_util |
OLD | NEW |