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; |
18 | 26 |
19 namespace windows_util { | 27 namespace windows_util { |
20 | 28 |
| 29 namespace { |
| 30 |
| 31 std::vector<windows::WindowType> CreateTypeFilter( |
| 32 int length, |
| 33 const windows::WindowType types[]) { |
| 34 std::vector<windows::WindowType> filter; |
| 35 for (int i = 0; i < length; i++) |
| 36 filter.push_back(types[i]); |
| 37 return filter; |
| 38 } |
| 39 |
| 40 } // namespace |
| 41 |
| 42 const std::vector<windows::WindowType>& GetDefaultWindowTypeFilters() { |
| 43 static const windows::WindowType types[] = {windows::WINDOW_TYPE_NORMAL, |
| 44 windows::WINDOW_TYPE_PANEL, |
| 45 windows::WINDOW_TYPE_POPUP}; |
| 46 CR_DEFINE_STATIC_LOCAL(std::vector<windows::WindowType>, filter, |
| 47 (CreateTypeFilter(arraysize(types), types))); |
| 48 return filter; |
| 49 } |
| 50 |
| 51 const std::vector<windows::WindowType>& GetNoWindowTypeFilters() { |
| 52 static const windows::WindowType types[] = { |
| 53 windows::WINDOW_TYPE_APP, windows::WINDOW_TYPE_DEVTOOLS, |
| 54 windows::WINDOW_TYPE_NORMAL, windows::WINDOW_TYPE_PANEL, |
| 55 windows::WINDOW_TYPE_POPUP}; |
| 56 CR_DEFINE_STATIC_LOCAL(std::vector<windows::WindowType>, filter, |
| 57 (CreateTypeFilter(arraysize(types), types))); |
| 58 return filter; |
| 59 } |
| 60 |
| 61 bool WindowMatchesTypeFilter(const extensions::WindowController* controller, |
| 62 const std::vector<windows::WindowType>& filters) { |
| 63 int window_type = extensions::api::windows::ParseWindowType( |
| 64 controller->GetWindowTypeText()); |
| 65 for (const auto& iter : filters) { |
| 66 if (window_type == iter) |
| 67 return true; |
| 68 } |
| 69 return false; |
| 70 } |
| 71 |
21 bool GetWindowFromWindowID(UIThreadExtensionFunction* function, | 72 bool GetWindowFromWindowID(UIThreadExtensionFunction* function, |
22 int window_id, | 73 int window_id, |
| 74 const std::vector<windows::WindowType>& type_filter, |
23 extensions::WindowController** controller) { | 75 extensions::WindowController** controller) { |
24 if (window_id == extension_misc::kCurrentWindowId) { | 76 if (window_id == extension_misc::kCurrentWindowId) { |
25 extensions::WindowController* extension_window_controller = | 77 extensions::WindowController* extension_window_controller = |
26 function->dispatcher()->GetExtensionWindowController(); | 78 function->dispatcher()->GetExtensionWindowController(); |
27 // If there is a window controller associated with this extension, use that. | 79 // If there is a window controller associated with this extension, use that. |
28 if (extension_window_controller) { | 80 if (extension_window_controller) { |
29 *controller = extension_window_controller; | 81 *controller = extension_window_controller; |
30 } else { | 82 } else { |
31 // Otherwise get the focused or most recently added window. | 83 // Otherwise get the focused or most recently added window. |
32 *controller = extensions::WindowControllerList::GetInstance() | 84 *controller = extensions::WindowControllerList::GetInstance() |
33 ->CurrentWindowForFunction(function); | 85 ->CurrentWindowForFunction(function); |
34 } | 86 } |
35 if (!(*controller)) { | 87 if (!(*controller)) { |
36 function->SetError(extensions::tabs_constants::kNoCurrentWindowError); | 88 function->SetError(extensions::tabs_constants::kNoCurrentWindowError); |
37 return false; | 89 return false; |
38 } | 90 } |
39 } else { | 91 } else { |
40 *controller = extensions::WindowControllerList::GetInstance() | 92 *controller = extensions::WindowControllerList::GetInstance() |
41 ->FindWindowForFunctionById(function, window_id); | 93 ->FindWindowForFunctionByIdWithFilter(function, window_id, |
| 94 type_filter); |
42 if (!(*controller)) { | 95 if (!(*controller)) { |
43 function->SetError(extensions::ErrorUtils::FormatErrorMessage( | 96 function->SetError(extensions::ErrorUtils::FormatErrorMessage( |
44 extensions::tabs_constants::kWindowNotFoundError, | 97 extensions::tabs_constants::kWindowNotFoundError, |
45 base::IntToString(window_id))); | 98 base::IntToString(window_id))); |
46 return false; | 99 return false; |
47 } | 100 } |
48 } | 101 } |
49 return true; | 102 return true; |
50 } | 103 } |
51 | 104 |
| 105 bool ExtensionCanOperateOnWindow( |
| 106 const extensions::Extension* extension, |
| 107 content::BrowserContext* browser_context, |
| 108 const extensions::WindowController* controller, |
| 109 const std::vector<windows::WindowType>& type_filter) { |
| 110 if (!WindowMatchesTypeFilter(controller, type_filter)) |
| 111 return false; |
| 112 |
| 113 if (browser_context == controller->profile()) |
| 114 return true; |
| 115 |
| 116 if (!extensions::util::CanCrossIncognito(extension, browser_context)) |
| 117 return false; |
| 118 |
| 119 Profile* profile = Profile::FromBrowserContext(browser_context); |
| 120 return profile->HasOffTheRecordProfile() && |
| 121 profile->GetOffTheRecordProfile() == controller->profile(); |
| 122 } |
| 123 |
52 bool CanOperateOnWindow(const UIThreadExtensionFunction* function, | 124 bool CanOperateOnWindow(const UIThreadExtensionFunction* function, |
53 const extensions::WindowController* controller) { | 125 const extensions::WindowController* controller, |
54 if (function->extension() != NULL && | 126 const std::vector<windows::WindowType>& type_filter) { |
55 !controller->IsVisibleToExtension(function->extension())) { | 127 if (!WindowMatchesTypeFilter(controller, type_filter)) |
56 return false; | 128 return false; |
57 } | |
58 | 129 |
59 if (function->browser_context() == controller->profile()) | 130 if (function->browser_context() == controller->profile()) |
60 return true; | 131 return true; |
61 | 132 |
62 if (!function->include_incognito()) | 133 if (!function->include_incognito()) |
63 return false; | 134 return false; |
64 | 135 |
65 Profile* profile = Profile::FromBrowserContext(function->browser_context()); | 136 Profile* profile = Profile::FromBrowserContext(function->browser_context()); |
66 return profile->HasOffTheRecordProfile() && | 137 return profile->HasOffTheRecordProfile() && |
67 profile->GetOffTheRecordProfile() == controller->profile(); | 138 profile->GetOffTheRecordProfile() == controller->profile(); |
68 } | 139 } |
69 | 140 |
70 } // namespace windows_util | 141 } // namespace windows_util |
OLD | NEW |