[email protected] | eb4832a | 2012-12-08 01:57:52 | [diff] [blame] | 1 | // Copyright (c) 2012 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 | |
[email protected] | 84aa73c | 2014-08-19 07:12:19 | [diff] [blame] | 5 | #include "extensions/browser/suggest_permission_util.h" |
[email protected] | eb4832a | 2012-12-08 01:57:52 | [diff] [blame] | 6 | |
rdevlin.cronin | b2cec91 | 2015-06-24 20:36:01 | [diff] [blame] | 7 | #include "base/strings/stringprintf.h" |
| 8 | #include "content/public/browser/render_frame_host.h" |
| 9 | #include "content/public/browser/web_contents.h" |
[email protected] | eb4832a | 2012-12-08 01:57:52 | [diff] [blame] | 10 | #include "content/public/common/console_message_level.h" |
[email protected] | e4452d3 | 2013-11-15 23:07:41 | [diff] [blame] | 11 | #include "extensions/common/extension.h" |
[email protected] | 076ebeda | 2014-06-06 21:47:26 | [diff] [blame] | 12 | #include "extensions/common/permissions/permissions_data.h" |
[email protected] | 793964a | 2013-10-08 00:47:19 | [diff] [blame] | 13 | #include "extensions/common/permissions/permissions_info.h" |
[email protected] | eb4832a | 2012-12-08 01:57:52 | [diff] [blame] | 14 | |
| 15 | using content::CONSOLE_MESSAGE_LEVEL_WARNING; |
[email protected] | eb4832a | 2012-12-08 01:57:52 | [diff] [blame] | 16 | |
[email protected] | 84aa73c | 2014-08-19 07:12:19 | [diff] [blame] | 17 | namespace extensions { |
| 18 | |
| 19 | namespace { |
| 20 | |
[email protected] | eb4832a | 2012-12-08 01:57:52 | [diff] [blame] | 21 | const char kPermissionsHelpURLForExtensions[] = |
| 22 | "http://developer.chrome.com/extensions/manifest.html#permissions"; |
| 23 | const char kPermissionsHelpURLForApps[] = |
| 24 | "http://developer.chrome.com/apps/declare_permissions.html"; |
| 25 | |
rdevlin.cronin | b2cec91 | 2015-06-24 20:36:01 | [diff] [blame] | 26 | void SuggestAPIPermissionInDevToolsConsole( |
| 27 | APIPermission::ID permission, |
| 28 | const Extension* extension, |
| 29 | content::RenderFrameHost* render_frame_host) { |
[email protected] | eb4832a | 2012-12-08 01:57:52 | [diff] [blame] | 30 | const APIPermissionInfo* permission_info = |
| 31 | PermissionsInfo::GetInstance()->GetByID(permission); |
[email protected] | e44fd8f | 2013-02-05 21:55:20 | [diff] [blame] | 32 | CHECK(permission_info); |
[email protected] | eb4832a | 2012-12-08 01:57:52 | [diff] [blame] | 33 | |
| 34 | // Note, intentionally not internationalizing this string, as it is output |
| 35 | // as a log message to developers in the developer tools console. |
| 36 | std::string message = base::StringPrintf( |
| 37 | "Is the '%s' permission appropriate? See %s.", |
| 38 | permission_info->name(), |
| 39 | extension->is_platform_app() ? |
| 40 | kPermissionsHelpURLForApps : kPermissionsHelpURLForExtensions); |
| 41 | |
rdevlin.cronin | b2cec91 | 2015-06-24 20:36:01 | [diff] [blame] | 42 | // Only the main frame handles dev tools messages. |
| 43 | content::WebContents::FromRenderFrameHost(render_frame_host) |
| 44 | ->GetMainFrame() |
| 45 | ->AddMessageToConsole(CONSOLE_MESSAGE_LEVEL_WARNING, message); |
[email protected] | eb4832a | 2012-12-08 01:57:52 | [diff] [blame] | 46 | } |
| 47 | |
[email protected] | 84aa73c | 2014-08-19 07:12:19 | [diff] [blame] | 48 | } // namespace |
| 49 | |
[email protected] | eb4832a | 2012-12-08 01:57:52 | [diff] [blame] | 50 | bool IsExtensionWithPermissionOrSuggestInConsole( |
| 51 | APIPermission::ID permission, |
| 52 | const Extension* extension, |
rdevlin.cronin | b2cec91 | 2015-06-24 20:36:01 | [diff] [blame] | 53 | content::RenderFrameHost* render_frame_host) { |
[email protected] | 076ebeda | 2014-06-06 21:47:26 | [diff] [blame] | 54 | if (extension && extension->permissions_data()->HasAPIPermission(permission)) |
[email protected] | eb4832a | 2012-12-08 01:57:52 | [diff] [blame] | 55 | return true; |
| 56 | |
rdevlin.cronin | b2cec91 | 2015-06-24 20:36:01 | [diff] [blame] | 57 | if (extension && render_frame_host) { |
| 58 | SuggestAPIPermissionInDevToolsConsole(permission, extension, |
| 59 | render_frame_host); |
| 60 | } |
[email protected] | eb4832a | 2012-12-08 01:57:52 | [diff] [blame] | 61 | |
| 62 | return false; |
| 63 | } |
| 64 | |
[email protected] | 84aa73c | 2014-08-19 07:12:19 | [diff] [blame] | 65 | } // namespace extensions |