Home | History | Annotate | Download | only in extensions
      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/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"
     14 
     15 using content::CONSOLE_MESSAGE_LEVEL_WARNING;
     16 using content::RenderViewHost;
     17 
     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";
     22 
     23 namespace extensions {
     24 
     25 void SuggestAPIPermissionInDevToolsConsole(APIPermission::ID permission,
     26                                            const Extension* extension,
     27                                            content::RenderViewHost* host) {
     28   if (!extension || !host)
     29     return;
     30 
     31   const APIPermissionInfo* permission_info =
     32       PermissionsInfo::GetInstance()->GetByID(permission);
     33   CHECK(permission_info);
     34 
     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);
     42 
     43   host->Send(new ExtensionMsg_AddMessageToConsole(
     44       host->GetRoutingID(), CONSOLE_MESSAGE_LEVEL_WARNING, message));
     45 }
     46 
     47 void SuggestAPIPermissionInDevToolsConsole(APIPermission::ID permission,
     48                                            const Extension* extension,
     49                                            Profile* profile) {
     50   extensions::ProcessManager* process_manager =
     51       extensions::ExtensionSystem::Get(profile)->process_manager();
     52 
     53   std::set<content::RenderViewHost*> views =
     54       process_manager->GetRenderViewHostsForExtension(extension->id());
     55 
     56   for (std::set<RenderViewHost*>::const_iterator iter = views.begin();
     57        iter != views.end(); ++iter) {
     58     RenderViewHost* host = *iter;
     59     SuggestAPIPermissionInDevToolsConsole(permission, extension, host);
     60   }
     61 }
     62 
     63 bool IsExtensionWithPermissionOrSuggestInConsole(
     64     APIPermission::ID permission,
     65     const Extension* extension,
     66     content::RenderViewHost* host) {
     67   if (extension && extension->HasAPIPermission(permission))
     68     return true;
     69 
     70   if (extension)
     71     SuggestAPIPermissionInDevToolsConsole(permission, extension, host);
     72 
     73   return false;
     74 }
     75 
     76 bool IsExtensionWithPermissionOrSuggestInConsole(
     77     APIPermission::ID permission,
     78     const Extension* extension,
     79     Profile* profile) {
     80   if (extension && extension->HasAPIPermission(permission))
     81     return true;
     82 
     83   if (extension)
     84     SuggestAPIPermissionInDevToolsConsole(permission, extension, profile);
     85 
     86   return false;
     87 }
     88 
     89 } // namespace extensions
     90