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
3 // found in the LICENSE file.
8 #include "chrome/browser/extensions/api/tabs/windows_util.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "chrome/browser/extensions/api/tabs/tabs_constants.h"
12 #include "chrome/browser/extensions/chrome_extension_function.h"
13 #include "chrome/browser/extensions/chrome_extension_function_details.h"
14 #include "chrome/browser/extensions/extension_util.h"
15 #include "chrome/browser/extensions/window_controller.h"
16 #include "chrome/browser/extensions/window_controller_list.h"
17 #include "chrome/browser/profiles/profile.h"
18 #include "extensions/browser/extension_function.h"
19 #include "extensions/browser/extension_function_dispatcher.h"
20 #include "extensions/common/constants.h"
21 #include "extensions/common/error_utils.h"
22 #include "extensions/common/extension.h"
24 namespace windows_util
{
26 bool GetWindowFromWindowID(UIThreadExtensionFunction
* function
,
28 extensions::WindowController::TypeFilter filter
,
29 extensions::WindowController
** controller
) {
30 if (window_id
== extension_misc::kCurrentWindowId
) {
31 extensions::WindowController
* extension_window_controller
=
32 function
->dispatcher()->GetExtensionWindowController();
33 // If there is a window controller associated with this extension, use that.
34 if (extension_window_controller
) {
35 *controller
= extension_window_controller
;
37 // Otherwise get the focused or most recently added window.
38 *controller
= extensions::WindowControllerList::GetInstance()
39 ->CurrentWindowForFunctionWithFilter(function
, filter
);
42 function
->SetError(extensions::tabs_constants::kNoCurrentWindowError
);
47 extensions::WindowControllerList::GetInstance()
48 ->FindWindowForFunctionByIdWithFilter(function
, window_id
, filter
);
50 function
->SetError(extensions::ErrorUtils::FormatErrorMessage(
51 extensions::tabs_constants::kWindowNotFoundError
,
52 base::IntToString(window_id
)));
59 bool CanOperateOnWindow(const UIThreadExtensionFunction
* function
,
60 const extensions::WindowController
* controller
,
61 extensions::WindowController::TypeFilter filter
) {
62 if (filter
&& !controller
->MatchesFilter(filter
))
65 if (!filter
&& function
->extension() &&
66 !controller
->IsVisibleToExtension(function
->extension()))
69 if (function
->browser_context() == controller
->profile())
72 if (!function
->include_incognito())
75 Profile
* profile
= Profile::FromBrowserContext(function
->browser_context());
76 return profile
->HasOffTheRecordProfile() &&
77 profile
->GetOffTheRecordProfile() == controller
->profile();
80 } // namespace windows_util