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" |
18 #include "chrome/common/extensions/api/windows.h" | |
14 #include "extensions/browser/extension_function.h" | 19 #include "extensions/browser/extension_function.h" |
15 #include "extensions/browser/extension_function_dispatcher.h" | 20 #include "extensions/browser/extension_function_dispatcher.h" |
16 #include "extensions/common/constants.h" | 21 #include "extensions/common/constants.h" |
17 #include "extensions/common/error_utils.h" | 22 #include "extensions/common/error_utils.h" |
23 #include "extensions/common/extension.h" | |
24 | |
25 namespace keys = extensions::tabs_constants; | |
26 namespace windows = extensions::api::windows; | |
18 | 27 |
19 namespace windows_util { | 28 namespace windows_util { |
20 | 29 |
30 namespace { | |
31 | |
32 extensions::WindowTypeFilter CreateTypeFilter( | |
33 int length, | |
34 const windows::WindowType types[]) { | |
35 extensions::WindowTypeFilter filter; | |
36 for (int i = 0; i < length; i++) | |
37 filter.push_back(types[i]); | |
38 return filter; | |
39 } | |
40 | |
41 } // namespace | |
42 | |
43 const extensions::WindowTypeFilter& GetDefaultWindowTypeFilters() { | |
44 static const windows::WindowType types[] = {windows::WINDOW_TYPE_NORMAL, | |
45 windows::WINDOW_TYPE_PANEL, | |
46 windows::WINDOW_TYPE_POPUP}; | |
not at google - send to devlin
2015/07/31 21:48:03
There are so few of these, you could model it as a
llandwerlin-old
2015/08/03 10:11:54
Switching to a bitmask.
| |
47 CR_DEFINE_STATIC_LOCAL(extensions::WindowTypeFilter, filter, | |
48 (CreateTypeFilter(arraysize(types), types))); | |
49 return filter; | |
50 } | |
51 | |
52 const extensions::WindowTypeFilter& GetNoWindowTypeFilters() { | |
not at google - send to devlin
2015/07/31 21:48:03
I don't understand this naming... looks like it ma
llandwerlin-old
2015/08/03 10:11:54
Done.
| |
53 static const windows::WindowType types[] = { | |
54 windows::WINDOW_TYPE_APP, windows::WINDOW_TYPE_DEVTOOLS, | |
55 windows::WINDOW_TYPE_NORMAL, windows::WINDOW_TYPE_PANEL, | |
56 windows::WINDOW_TYPE_POPUP}; | |
57 CR_DEFINE_STATIC_LOCAL(extensions::WindowTypeFilter, filter, | |
58 (CreateTypeFilter(arraysize(types), types))); | |
59 return filter; | |
60 } | |
61 | |
62 bool WindowMatchesTypeFilter(const extensions::WindowController* controller, | |
63 const extensions::WindowTypeFilter& filters) { | |
64 int window_type = extensions::api::windows::ParseWindowType( | |
not at google - send to devlin
2015/07/31 21:48:03
this should be a WindowType not an int.
llandwerlin-old
2015/08/03 10:11:54
Done.
| |
65 controller->GetWindowTypeText()); | |
66 for (const auto& iter : filters) { | |
67 if (window_type == iter) | |
68 return true; | |
69 } | |
70 return false; | |
71 } | |
72 | |
21 bool GetWindowFromWindowID(UIThreadExtensionFunction* function, | 73 bool GetWindowFromWindowID(UIThreadExtensionFunction* function, |
22 int window_id, | 74 int window_id, |
75 const extensions::WindowTypeFilter& type_filter, | |
23 extensions::WindowController** controller) { | 76 extensions::WindowController** controller) { |
24 if (window_id == extension_misc::kCurrentWindowId) { | 77 if (window_id == extension_misc::kCurrentWindowId) { |
25 extensions::WindowController* extension_window_controller = | 78 extensions::WindowController* extension_window_controller = |
26 function->dispatcher()->GetExtensionWindowController(); | 79 function->dispatcher()->GetExtensionWindowController(); |
27 // If there is a window controller associated with this extension, use that. | 80 // If there is a window controller associated with this extension, use that. |
28 if (extension_window_controller) { | 81 if (extension_window_controller) { |
29 *controller = extension_window_controller; | 82 *controller = extension_window_controller; |
30 } else { | 83 } else { |
31 // Otherwise get the focused or most recently added window. | 84 // Otherwise get the focused or most recently added window. |
32 *controller = extensions::WindowControllerList::GetInstance() | 85 *controller = extensions::WindowControllerList::GetInstance() |
33 ->CurrentWindowForFunction(function); | 86 ->CurrentWindowForFunction(function); |
34 } | 87 } |
35 if (!(*controller)) { | 88 if (!(*controller)) { |
36 function->SetError(extensions::tabs_constants::kNoCurrentWindowError); | 89 function->SetError(extensions::tabs_constants::kNoCurrentWindowError); |
37 return false; | 90 return false; |
38 } | 91 } |
39 } else { | 92 } else { |
40 *controller = extensions::WindowControllerList::GetInstance() | 93 *controller = extensions::WindowControllerList::GetInstance() |
41 ->FindWindowForFunctionById(function, window_id); | 94 ->FindWindowForFunctionByIdWithFilter(function, window_id, |
95 type_filter); | |
42 if (!(*controller)) { | 96 if (!(*controller)) { |
43 function->SetError(extensions::ErrorUtils::FormatErrorMessage( | 97 function->SetError(extensions::ErrorUtils::FormatErrorMessage( |
44 extensions::tabs_constants::kWindowNotFoundError, | 98 extensions::tabs_constants::kWindowNotFoundError, |
45 base::IntToString(window_id))); | 99 base::IntToString(window_id))); |
46 return false; | 100 return false; |
47 } | 101 } |
48 } | 102 } |
49 return true; | 103 return true; |
50 } | 104 } |
51 | 105 |
106 bool ExtensionCanOperateOnWindow( | |
107 const extensions::Extension* extension, | |
108 content::BrowserContext* browser_context, | |
109 const extensions::WindowController* controller, | |
110 const extensions::WindowTypeFilter& type_filter) { | |
111 if (!WindowMatchesTypeFilter(controller, type_filter)) | |
112 return false; | |
113 | |
114 if (browser_context == controller->profile()) | |
115 return true; | |
116 | |
117 if (!extensions::util::CanCrossIncognito(extension, browser_context)) | |
not at google - send to devlin
2015/07/31 21:48:03
The other implementation uses function->include_in
llandwerlin-old
2015/08/03 10:11:54
It looks like the remaining of a previous iteratio
| |
118 return false; | |
119 | |
120 Profile* profile = Profile::FromBrowserContext(browser_context); | |
121 return profile->HasOffTheRecordProfile() && | |
122 profile->GetOffTheRecordProfile() == controller->profile(); | |
123 } | |
124 | |
52 bool CanOperateOnWindow(const UIThreadExtensionFunction* function, | 125 bool CanOperateOnWindow(const UIThreadExtensionFunction* function, |
53 const extensions::WindowController* controller) { | 126 const extensions::WindowController* controller, |
54 if (function->extension() != NULL && | 127 const extensions::WindowTypeFilter& type_filter) { |
55 !controller->IsVisibleToExtension(function->extension())) { | 128 if (!WindowMatchesTypeFilter(controller, type_filter)) |
56 return false; | 129 return false; |
57 } | |
58 | 130 |
59 if (function->browser_context() == controller->profile()) | 131 if (function->browser_context() == controller->profile()) |
60 return true; | 132 return true; |
61 | 133 |
62 if (!function->include_incognito()) | 134 if (!function->include_incognito()) |
63 return false; | 135 return false; |
64 | 136 |
65 Profile* profile = Profile::FromBrowserContext(function->browser_context()); | 137 Profile* profile = Profile::FromBrowserContext(function->browser_context()); |
66 return profile->HasOffTheRecordProfile() && | 138 return profile->HasOffTheRecordProfile() && |
67 profile->GetOffTheRecordProfile() == controller->profile(); | 139 profile->GetOffTheRecordProfile() == controller->profile(); |
68 } | 140 } |
69 | 141 |
70 } // namespace windows_util | 142 } // namespace windows_util |
OLD | NEW |