Takashi Toyoshima | 6957907 | 2018-11-19 07:10:50 | [diff] [blame] | 1 | // Copyright 2018 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. |
| 4 | |
| 5 | #include "chrome/browser/extensions/permissions_test_util.h" |
| 6 | |
Jan Wilken Dörrie | 4a498d8c | 2021-01-20 10:19:39 | [diff] [blame] | 7 | #include "base/logging.h" |
Takashi Toyoshima | 6957907 | 2018-11-19 07:10:50 | [diff] [blame] | 8 | #include "base/run_loop.h" |
Matt Siembor | 39462b3 | 2019-05-07 20:20:44 | [diff] [blame] | 9 | #include "chrome/common/webui_url_constants.h" |
Takashi Toyoshima | 6957907 | 2018-11-19 07:10:50 | [diff] [blame] | 10 | #include "content/public/browser/browser_context.h" |
| 11 | #include "extensions/common/extension.h" |
| 12 | #include "extensions/common/permissions/permission_set.h" |
| 13 | |
| 14 | namespace extensions { |
| 15 | namespace permissions_test_util { |
| 16 | |
Devlin Cronin | e342f4d | 2018-11-21 21:59:11 | [diff] [blame] | 17 | std::vector<std::string> GetPatternsAsStrings(const URLPatternSet& patterns) { |
| 18 | std::vector<std::string> pattern_strings; |
| 19 | pattern_strings.reserve(patterns.size()); |
| 20 | for (const auto& pattern : patterns) { |
| 21 | // chrome://favicon/ is automatically added as a pattern when the extension |
| 22 | // requests access to <all_urls>, but isn't really a host pattern (it allows |
| 23 | // the extension to retrieve a favicon for a given URL). Just ignore it when |
| 24 | // generating host sets. |
| 25 | std::string pattern_string = pattern.GetAsString(); |
Matt Siembor | 39462b3 | 2019-05-07 20:20:44 | [diff] [blame] | 26 | if (pattern_string != std::string(chrome::kChromeUIFaviconURL) + "*") |
Devlin Cronin | e342f4d | 2018-11-21 21:59:11 | [diff] [blame] | 27 | pattern_strings.push_back(pattern_string); |
| 28 | } |
| 29 | |
| 30 | return pattern_strings; |
| 31 | } |
| 32 | |
Takashi Toyoshima | 6957907 | 2018-11-19 07:10:50 | [diff] [blame] | 33 | void GrantOptionalPermissionsAndWaitForCompletion( |
| 34 | content::BrowserContext* browser_context, |
| 35 | const Extension& extension, |
| 36 | const PermissionSet& permissions) { |
| 37 | base::RunLoop run_loop; |
| 38 | PermissionsUpdater(browser_context) |
| 39 | .GrantOptionalPermissions(extension, permissions, run_loop.QuitClosure()); |
| 40 | run_loop.Run(); |
| 41 | } |
| 42 | |
| 43 | void GrantRuntimePermissionsAndWaitForCompletion( |
| 44 | content::BrowserContext* browser_context, |
| 45 | const Extension& extension, |
| 46 | const PermissionSet& permissions) { |
| 47 | base::RunLoop run_loop; |
| 48 | PermissionsUpdater(browser_context) |
| 49 | .GrantRuntimePermissions(extension, permissions, run_loop.QuitClosure()); |
| 50 | run_loop.Run(); |
| 51 | } |
| 52 | |
| 53 | void RevokeOptionalPermissionsAndWaitForCompletion( |
| 54 | content::BrowserContext* browser_context, |
| 55 | const Extension& extension, |
| 56 | const PermissionSet& permissions, |
| 57 | PermissionsUpdater::RemoveType remove_type) { |
| 58 | base::RunLoop run_loop; |
| 59 | PermissionsUpdater(browser_context) |
| 60 | .RevokeOptionalPermissions(extension, permissions, remove_type, |
| 61 | run_loop.QuitClosure()); |
| 62 | run_loop.Run(); |
| 63 | } |
| 64 | |
| 65 | void RevokeRuntimePermissionsAndWaitForCompletion( |
| 66 | content::BrowserContext* browser_context, |
| 67 | const Extension& extension, |
| 68 | const PermissionSet& permissions) { |
| 69 | base::RunLoop run_loop; |
| 70 | PermissionsUpdater(browser_context) |
| 71 | .RevokeRuntimePermissions(extension, permissions, run_loop.QuitClosure()); |
| 72 | run_loop.Run(); |
| 73 | } |
| 74 | |
Devlin Cronin | e342f4d | 2018-11-21 21:59:11 | [diff] [blame] | 75 | } // namespace permissions_test_util |
Takashi Toyoshima | 6957907 | 2018-11-19 07:10:50 | [diff] [blame] | 76 | } // namespace extensions |