Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / extensions / api / tabs / windows_util.cc
blob1c4e6495916e6d034aaf3b63ebdc3bc847a43dc2
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 <string>
6 #include <vector>
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,
27 int window_id,
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;
36 } else {
37 // Otherwise get the focused or most recently added window.
38 *controller = extensions::WindowControllerList::GetInstance()
39 ->CurrentWindowForFunctionWithFilter(function, filter);
41 if (!(*controller)) {
42 function->SetError(extensions::tabs_constants::kNoCurrentWindowError);
43 return false;
45 } else {
46 *controller =
47 extensions::WindowControllerList::GetInstance()
48 ->FindWindowForFunctionByIdWithFilter(function, window_id, filter);
49 if (!(*controller)) {
50 function->SetError(extensions::ErrorUtils::FormatErrorMessage(
51 extensions::tabs_constants::kWindowNotFoundError,
52 base::IntToString(window_id)));
53 return false;
56 return true;
59 bool CanOperateOnWindow(const UIThreadExtensionFunction* function,
60 const extensions::WindowController* controller,
61 extensions::WindowController::TypeFilter filter) {
62 if (filter && !controller->MatchesFilter(filter))
63 return false;
65 if (!filter && function->extension() &&
66 !controller->IsVisibleToExtension(function->extension()))
67 return false;
69 if (function->browser_context() == controller->profile())
70 return true;
72 if (!function->include_incognito())
73 return false;
75 Profile* profile = Profile::FromBrowserContext(function->browser_context());
76 return profile->HasOffTheRecordProfile() &&
77 profile->GetOffTheRecordProfile() == controller->profile();
80 } // namespace windows_util