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.
5 #include "chrome/browser/extensions/suggest_permission_util.h"
7 #include "chrome/browser/extensions/extension_system.h"
8 #include "chrome/common/extensions/extension_messages.h"
9 #include "content/public/browser/render_view_host.h"
10 #include "content/public/common/console_message_level.h"
11 #include "extensions/browser/process_manager.h"
12 #include "extensions/common/extension.h"
13 #include "extensions/common/permissions/permissions_info.h"
15 using content::CONSOLE_MESSAGE_LEVEL_WARNING
;
16 using content::RenderViewHost
;
18 const char kPermissionsHelpURLForExtensions
[] =
19 "http://developer.chrome.com/extensions/manifest.html#permissions";
20 const char kPermissionsHelpURLForApps
[] =
21 "http://developer.chrome.com/apps/declare_permissions.html";
23 namespace extensions
{
25 void SuggestAPIPermissionInDevToolsConsole(APIPermission::ID permission
,
26 const Extension
* extension
,
27 content::RenderViewHost
* host
) {
28 if (!extension
|| !host
)
31 const APIPermissionInfo
* permission_info
=
32 PermissionsInfo::GetInstance()->GetByID(permission
);
33 CHECK(permission_info
);
35 // Note, intentionally not internationalizing this string, as it is output
36 // as a log message to developers in the developer tools console.
37 std::string message
= base::StringPrintf(
38 "Is the '%s' permission appropriate? See %s.",
39 permission_info
->name(),
40 extension
->is_platform_app() ?
41 kPermissionsHelpURLForApps
: kPermissionsHelpURLForExtensions
);
43 host
->Send(new ExtensionMsg_AddMessageToConsole(
44 host
->GetRoutingID(), CONSOLE_MESSAGE_LEVEL_WARNING
, message
));
47 void SuggestAPIPermissionInDevToolsConsole(APIPermission::ID permission
,
48 const Extension
* extension
,
50 extensions::ProcessManager
* process_manager
=
51 extensions::ExtensionSystem::Get(profile
)->process_manager();
53 std::set
<content::RenderViewHost
*> views
=
54 process_manager
->GetRenderViewHostsForExtension(extension
->id());
56 for (std::set
<RenderViewHost
*>::const_iterator iter
= views
.begin();
57 iter
!= views
.end(); ++iter
) {
58 RenderViewHost
* host
= *iter
;
59 SuggestAPIPermissionInDevToolsConsole(permission
, extension
, host
);
63 bool IsExtensionWithPermissionOrSuggestInConsole(
64 APIPermission::ID permission
,
65 const Extension
* extension
,
66 content::RenderViewHost
* host
) {
67 if (extension
&& extension
->HasAPIPermission(permission
))
71 SuggestAPIPermissionInDevToolsConsole(permission
, extension
, host
);
76 bool IsExtensionWithPermissionOrSuggestInConsole(
77 APIPermission::ID permission
,
78 const Extension
* extension
,
80 if (extension
&& extension
->HasAPIPermission(permission
))
84 SuggestAPIPermissionInDevToolsConsole(permission
, extension
, profile
);
89 } // namespace extensions