Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / messaging / message_service.cc
index 9c54e78..1de7c59 100644 (file)
 #include "chrome/browser/extensions/api/messaging/incognito_connectability.h"
 #include "chrome/browser/extensions/api/messaging/native_message_port.h"
 #include "chrome/browser/extensions/api/tabs/tabs_constants.h"
-#include "chrome/browser/extensions/extension_host.h"
 #include "chrome/browser/extensions/extension_service.h"
 #include "chrome/browser/extensions/extension_tab_util.h"
 #include "chrome/browser/extensions/extension_util.h"
 #include "chrome/browser/profiles/profile.h"
 #include "chrome/browser/tab_contents/tab_util.h"
-#include "chrome/common/extensions/extension_messages.h"
-#include "chrome/common/extensions/manifest_handlers/externally_connectable.h"
 #include "content/public/browser/notification_service.h"
 #include "content/public/browser/render_process_host.h"
 #include "content/public/browser/render_view_host.h"
 #include "content/public/browser/render_widget_host_view.h"
 #include "content/public/browser/site_instance.h"
 #include "content/public/browser/web_contents.h"
+#include "extensions/browser/extension_host.h"
 #include "extensions/browser/extension_system.h"
+#include "extensions/browser/extensions_browser_client.h"
 #include "extensions/browser/lazy_background_task_queue.h"
 #include "extensions/browser/process_manager.h"
 #include "extensions/common/extension.h"
+#include "extensions/common/extension_messages.h"
 #include "extensions/common/manifest_constants.h"
 #include "extensions/common/manifest_handlers/background_info.h"
+#include "extensions/common/manifest_handlers/externally_connectable.h"
 #include "extensions/common/manifest_handlers/incognito_info.h"
+#include "extensions/common/permissions/permissions_data.h"
 #include "net/base/completion_callback.h"
 #include "url/gurl.h"
 
+using content::BrowserContext;
 using content::SiteInstance;
 using content::WebContents;
 
@@ -118,9 +121,10 @@ static base::StaticAtomicSequenceNumber g_next_channel_id;
 static base::StaticAtomicSequenceNumber g_channel_id_overflow_count;
 
 static content::RenderProcessHost* GetExtensionProcess(
-    Profile* profile, const std::string& extension_id) {
+    BrowserContext* context,
+    const std::string& extension_id) {
   SiteInstance* site_instance =
-      ExtensionSystem::Get(profile)->process_manager()->GetSiteInstanceForURL(
+      ExtensionSystem::Get(context)->process_manager()->GetSiteInstanceForURL(
           Extension::GetBaseURLFromExtensionId(extension_id));
   return site_instance->HasProcess() ? site_instance->GetProcess() : NULL;
 }
@@ -159,9 +163,9 @@ void MessageService::AllocatePortIdPair(int* port1, int* port2) {
   *port2 = port2_id;
 }
 
-MessageService::MessageService(Profile* profile)
+MessageService::MessageService(BrowserContext* context)
     : lazy_background_task_queue_(
-          ExtensionSystem::Get(profile)->lazy_background_task_queue()),
+          ExtensionSystem::Get(context)->lazy_background_task_queue()),
       weak_factory_(this) {
   registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
                  content::NotificationService::AllBrowserContextsAndSources());
@@ -174,18 +178,18 @@ MessageService::~MessageService() {
   channels_.clear();
 }
 
-static base::LazyInstance<ProfileKeyedAPIFactory<MessageService> >
-g_factory = LAZY_INSTANCE_INITIALIZER;
+static base::LazyInstance<BrowserContextKeyedAPIFactory<MessageService> >
+    g_factory = LAZY_INSTANCE_INITIALIZER;
 
 // static
-ProfileKeyedAPIFactory<MessageService>* MessageService::GetFactoryInstance() {
+BrowserContextKeyedAPIFactory<MessageService>*
+MessageService::GetFactoryInstance() {
   return g_factory.Pointer();
 }
 
 // static
-MessageService* MessageService::Get(content::BrowserContext* context) {
-  Profile* profile = Profile::FromBrowserContext(context);
-  return ProfileKeyedAPIFactory<MessageService>::GetForProfile(profile);
+MessageService* MessageService::Get(BrowserContext* context) {
+  return BrowserContextKeyedAPIFactory<MessageService>::Get(context);
 }
 
 void MessageService::OpenChannelToExtension(
@@ -199,9 +203,9 @@ void MessageService::OpenChannelToExtension(
       content::RenderProcessHost::FromID(source_process_id);
   if (!source)
     return;
-  Profile* profile = Profile::FromBrowserContext(source->GetBrowserContext());
+  BrowserContext* context = source->GetBrowserContext();
 
-  ExtensionSystem* extension_system = ExtensionSystem::Get(profile);
+  ExtensionSystem* extension_system = ExtensionSystem::Get(context);
   DCHECK(extension_system);
   const Extension* target_extension = extension_system->extension_service()->
       extensions()->GetByID(target_extension_id);
@@ -211,15 +215,6 @@ void MessageService::OpenChannelToExtension(
     return;
   }
 
-  // Only running ephemeral apps can receive messages. Idle cached ephemeral
-  // apps are invisible and should not be connectable.
-  if (target_extension->is_ephemeral() &&
-      util::IsExtensionIdle(target_extension_id, profile)) {
-    DispatchOnDisconnect(
-        source, receiver_port_id, kReceivingEndDoesntExistError);
-    return;
-  }
-
   bool is_web_connection = false;
 
   if (source_extension_id != target_extension_id) {
@@ -267,17 +262,21 @@ void MessageService::OpenChannelToExtension(
   WebContents* source_contents = tab_util::GetWebContentsByID(
       source_process_id, source_routing_id);
 
-  if (profile->IsOffTheRecord() &&
-      !util::IsIncognitoEnabled(target_extension_id, profile)) {
-    // Give the user a chance to accept an incognito connection if they haven't
-    // already - but only for spanning-mode incognito. We don't want the
-    // complication of spinning up an additional process here which might need
-    // to do some setup that we're not expecting.
-    if (!is_web_connection ||
-        IncognitoInfo::IsSplitMode(target_extension) ||
-        !IncognitoConnectability::Get(profile)->Query(target_extension,
-                                                      source_contents,
-                                                      source_url)) {
+  if (context->IsOffTheRecord() &&
+      !util::IsIncognitoEnabled(target_extension_id, context)) {
+    // Give the user a chance to accept an incognito connection from the web if
+    // they haven't already, with the conditions:
+    // - Only for spanning-mode incognito. We don't want the complication of
+    //   spinning up an additional process here which might need to do some
+    //   setup that we're not expecting.
+    // - Only for extensions that can't normally be enabled in incognito, since
+    //   that surface (e.g. chrome://extensions) should be the only one for
+    //   enabling in incognito. In practice this means platform apps only.
+    if (!is_web_connection || IncognitoInfo::IsSplitMode(target_extension) ||
+        target_extension->can_be_incognito_enabled() ||
+        // This check may show a dialog.
+        !IncognitoConnectability::Get(context)
+             ->Query(target_extension, source_contents, source_url)) {
       DispatchOnDisconnect(
           source, receiver_port_id, kReceivingEndDoesntExistError);
       return;
@@ -288,7 +287,8 @@ void MessageService::OpenChannelToExtension(
   // process, we will use the incognito EPM to find the right extension process,
   // which depends on whether the extension uses spanning or split mode.
   MessagePort* receiver = new ExtensionMessagePort(
-      GetExtensionProcess(profile, target_extension_id), MSG_ROUTING_CONTROL,
+      GetExtensionProcess(context, target_extension_id),
+      MSG_ROUTING_CONTROL,
       target_extension_id);
 
   // Include info about the opener's tab (if it was a tab).
@@ -320,10 +320,10 @@ void MessageService::OpenChannelToExtension(
   if (include_tls_channel_id) {
     pending_tls_channel_id_channels_[GET_CHANNEL_ID(params->receiver_port_id)]
         = PendingMessagesQueue();
-    property_provider_.GetDomainBoundCert(
-        profile,
+    property_provider_.GetChannelID(
+        Profile::FromBrowserContext(context),
         source_url,
-        base::Bind(&MessageService::GotDomainBoundCert,
+        base::Bind(&MessageService::GotChannelID,
                    weak_factory_.GetWeakPtr(),
                    base::Passed(make_scoped_ptr(params))));
     return;
@@ -332,9 +332,8 @@ void MessageService::OpenChannelToExtension(
   // The target might be a lazy background page. In that case, we have to check
   // if it is loaded and ready, and if not, queue up the task and load the
   // page.
-  if (MaybeAddPendingLazyBackgroundPageOpenChannelTask(profile,
-                                                       target_extension,
-                                                       params)) {
+  if (MaybeAddPendingLazyBackgroundPageOpenChannelTask(
+          context, target_extension, params)) {
     return;
   }
 
@@ -360,8 +359,9 @@ void MessageService::OpenChannelToNativeApp(
   if (extension_service) {
     const Extension* extension =
         extension_service->GetExtensionById(source_extension_id, false);
-    has_permission = extension && extension->HasAPIPermission(
-        APIPermission::kNativeMessaging);
+    has_permission = extension &&
+                     extension->permissions_data()->HasAPIPermission(
+                         APIPermission::kNativeMessaging);
   }
 
   if (!has_permission) {
@@ -654,7 +654,7 @@ void MessageService::DispatchMessage(int source_port_id,
 }
 
 bool MessageService::MaybeAddPendingLazyBackgroundPageOpenChannelTask(
-    Profile* profile,
+    BrowserContext* context,
     const Extension* extension,
     OpenChannelParams* params) {
   if (!BackgroundInfo::HasLazyBackgroundPage(extension))
@@ -664,24 +664,27 @@ bool MessageService::MaybeAddPendingLazyBackgroundPageOpenChannelTask(
   // using the original profile since that is what the extension process
   // will use.
   if (!IncognitoInfo::IsSplitMode(extension))
-    profile = profile->GetOriginalProfile();
+    context = ExtensionsBrowserClient::Get()->GetOriginalContext(context);
 
-  if (!lazy_background_task_queue_->ShouldEnqueueTask(profile, extension))
+  if (!lazy_background_task_queue_->ShouldEnqueueTask(context, extension))
     return false;
 
-  pending_lazy_background_page_channels_[
-      GET_CHANNEL_ID(params->receiver_port_id)] =
-      PendingLazyBackgroundPageChannel(profile, extension->id());
+  pending_lazy_background_page_channels_
+      [GET_CHANNEL_ID(params->receiver_port_id)] =
+          PendingLazyBackgroundPageChannel(context, extension->id());
   scoped_ptr<OpenChannelParams> scoped_params(params);
-  lazy_background_task_queue_->AddPendingTask(profile, extension->id(),
+  lazy_background_task_queue_->AddPendingTask(
+      context,
+      extension->id(),
       base::Bind(&MessageService::PendingLazyBackgroundPageOpenChannel,
-                 weak_factory_.GetWeakPtr(), base::Passed(&scoped_params),
+                 weak_factory_.GetWeakPtr(),
+                 base::Passed(&scoped_params),
                  params->source->GetID()));
   return true;
 }
 
-void MessageService::GotDomainBoundCert(scoped_ptr<OpenChannelParams> params,
-                                        const std::string& tls_channel_id) {
+void MessageService::GotChannelID(scoped_ptr<OpenChannelParams> params,
+                                  const std::string& tls_channel_id) {
   params->tls_channel_id.assign(tls_channel_id);
   int channel_id = GET_CHANNEL_ID(params->receiver_port_id);
 
@@ -692,11 +695,11 @@ void MessageService::GotDomainBoundCert(scoped_ptr<OpenChannelParams> params,
     return;
   }
 
-  Profile* profile = Profile::FromBrowserContext(
-      params->source->GetBrowserContext());
+  BrowserContext* context = params->source->GetBrowserContext();
 
-  const Extension* target_extension = ExtensionSystem::Get(profile)->
-      extension_service()->extensions()->GetByID(params->target_extension_id);
+  const Extension* target_extension =
+      ExtensionSystem::Get(context)->extension_service()->extensions()->GetByID(
+          params->target_extension_id);
   if (!target_extension) {
     pending_tls_channel_id_channels_.erase(channel_id);
     DispatchOnDisconnect(
@@ -705,9 +708,8 @@ void MessageService::GotDomainBoundCert(scoped_ptr<OpenChannelParams> params,
     return;
   }
   PendingMessagesQueue& pending_messages = pending_for_tls_channel_id->second;
-  if (MaybeAddPendingLazyBackgroundPageOpenChannelTask(profile,
-                                                       target_extension,
-                                                       params.get())) {
+  if (MaybeAddPendingLazyBackgroundPageOpenChannelTask(
+          context, target_extension, params.get())) {
     // Lazy background queue took ownership. Release ours.
     ignore_result(params.release());
     // Messages queued up waiting for the TLS channel ID now need to be queued