Upstream version 10.38.222.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / suggest_permission_util.cc
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
5 #include "chrome/browser/extensions/suggest_permission_util.h"
6
7 #include "chrome/browser/profiles/profile.h"
8 #include "content/public/browser/render_view_host.h"
9 #include "content/public/common/console_message_level.h"
10 #include "extensions/browser/extension_system.h"
11 #include "extensions/browser/process_manager.h"
12 #include "extensions/common/extension.h"
13 #include "extensions/common/extension_messages.h"
14 #include "extensions/common/permissions/permissions_data.h"
15 #include "extensions/common/permissions/permissions_info.h"
16
17 using content::CONSOLE_MESSAGE_LEVEL_WARNING;
18 using content::RenderViewHost;
19
20 const char kPermissionsHelpURLForExtensions[] =
21     "http://developer.chrome.com/extensions/manifest.html#permissions";
22 const char kPermissionsHelpURLForApps[] =
23     "http://developer.chrome.com/apps/declare_permissions.html";
24
25 namespace extensions {
26
27 void SuggestAPIPermissionInDevToolsConsole(APIPermission::ID permission,
28                                            const Extension* extension,
29                                            content::RenderViewHost* host) {
30   if (!extension || !host)
31     return;
32
33   const APIPermissionInfo* permission_info =
34       PermissionsInfo::GetInstance()->GetByID(permission);
35   CHECK(permission_info);
36
37   // Note, intentionally not internationalizing this string, as it is output
38   // as a log message to developers in the developer tools console.
39   std::string message = base::StringPrintf(
40       "Is the '%s' permission appropriate? See %s.",
41       permission_info->name(),
42       extension->is_platform_app() ?
43           kPermissionsHelpURLForApps : kPermissionsHelpURLForExtensions);
44
45   host->Send(new ExtensionMsg_AddMessageToConsole(
46       host->GetRoutingID(), CONSOLE_MESSAGE_LEVEL_WARNING, message));
47 }
48
49 bool IsExtensionWithPermissionOrSuggestInConsole(
50     APIPermission::ID permission,
51     const Extension* extension,
52     content::RenderViewHost* host) {
53   if (extension && extension->permissions_data()->HasAPIPermission(permission))
54     return true;
55
56   if (extension)
57     SuggestAPIPermissionInDevToolsConsole(permission, extension, host);
58
59   return false;
60 }
61
62 } // namespace extensions