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.
5 #include "chrome/browser/extensions/api/tabs/windows_util.h"
7 #include "base/strings/string_number_conversions.h"
8 #include "chrome/browser/extensions/api/tabs/tabs_constants.h"
9 #include "chrome/browser/extensions/chrome_extension_function.h"
10 #include "chrome/browser/extensions/chrome_extension_function_details.h"
11 #include "chrome/browser/extensions/window_controller.h"
12 #include "chrome/browser/extensions/window_controller_list.h"
13 #include "extensions/browser/extension_function_dispatcher.h"
14 #include "extensions/common/constants.h"
15 #include "extensions/common/error_utils.h"
17 namespace windows_util {
19 bool GetWindowFromWindowID(ChromeUIThreadExtensionFunction* function,
21 extensions::WindowController** controller) {
22 ChromeExtensionFunctionDetails function_details(function);
23 if (window_id == extension_misc::kCurrentWindowId) {
24 extensions::WindowController* extension_window_controller =
25 function->dispatcher()->delegate()->GetExtensionWindowController();
26 // If there is a window controller associated with this extension, use that.
27 if (extension_window_controller) {
28 *controller = extension_window_controller;
30 // Otherwise get the focused or most recently added window.
31 *controller = extensions::WindowControllerList::GetInstance()
32 ->CurrentWindowForFunction(function_details);
35 function->SetError(extensions::tabs_constants::kNoCurrentWindowError);
39 *controller = extensions::WindowControllerList::GetInstance()
40 ->FindWindowForFunctionById(function_details, window_id);
42 function->SetError(extensions::ErrorUtils::FormatErrorMessage(
43 extensions::tabs_constants::kWindowNotFoundError,
44 base::IntToString(window_id)));
51 } // namespace windows_util