| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/extensions/api/messaging/message_service.h" | 5 #include "chrome/browser/extensions/api/messaging/message_service.h" |
| 6 | 6 |
| 7 #include "base/atomic_sequence_num.h" | 7 #include "base/atomic_sequence_num.h" |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/callback.h" | 9 #include "base/callback.h" |
| 10 #include "base/json/json_writer.h" | 10 #include "base/json/json_writer.h" |
| 11 #include "base/lazy_instance.h" | 11 #include "base/lazy_instance.h" |
| 12 #include "base/metrics/histogram.h" | 12 #include "base/metrics/histogram.h" |
| 13 #include "base/stl_util.h" | 13 #include "base/stl_util.h" |
| 14 #include "base/values.h" | 14 #include "base/values.h" |
| 15 #include "chrome/browser/chrome_notification_types.h" | 15 #include "chrome/browser/chrome_notification_types.h" |
| 16 #include "chrome/browser/extensions/api/messaging/extension_message_port.h" | 16 #include "chrome/browser/extensions/api/messaging/extension_message_port.h" |
| 17 #include "chrome/browser/extensions/api/messaging/native_message_port.h" | 17 #include "chrome/browser/extensions/api/messaging/native_message_port.h" |
| 18 #include "chrome/browser/extensions/extension_host.h" | 18 #include "chrome/browser/extensions/extension_host.h" |
| 19 #include "chrome/browser/extensions/extension_process_manager.h" | 19 #include "chrome/browser/extensions/extension_process_manager.h" |
| 20 #include "chrome/browser/extensions/extension_service.h" | 20 #include "chrome/browser/extensions/extension_service.h" |
| 21 #include "chrome/browser/extensions/extension_system.h" | 21 #include "chrome/browser/extensions/extension_system.h" |
| 22 #include "chrome/browser/extensions/extension_tab_util.h" | 22 #include "chrome/browser/extensions/extension_tab_util.h" |
| 23 #include "chrome/browser/extensions/lazy_background_task_queue.h" | 23 #include "chrome/browser/extensions/lazy_background_task_queue.h" |
| 24 #include "chrome/browser/extensions/process_map.h" | 24 #include "chrome/browser/extensions/process_map.h" |
| 25 #include "chrome/browser/profiles/profile.h" | 25 #include "chrome/browser/profiles/profile.h" |
| 26 #include "chrome/browser/tab_contents/tab_util.h" | 26 #include "chrome/browser/tab_contents/tab_util.h" |
| 27 #include "chrome/common/extensions/background_info.h" | 27 #include "chrome/common/extensions/background_info.h" |
| 28 #include "chrome/common/extensions/extension.h" | 28 #include "chrome/common/extensions/extension.h" |
| 29 #include "chrome/common/extensions/extension_manifest_constants.h" | |
| 30 #include "chrome/common/extensions/extension_messages.h" | 29 #include "chrome/common/extensions/extension_messages.h" |
| 31 #include "chrome/common/extensions/incognito_handler.h" | 30 #include "chrome/common/extensions/incognito_handler.h" |
| 32 #include "chrome/common/extensions/manifest_handlers/externally_connectable.h" | 31 #include "chrome/common/extensions/manifest_handlers/externally_connectable.h" |
| 33 #include "content/public/browser/browser_thread.h" | 32 #include "content/public/browser/browser_thread.h" |
| 34 #include "content/public/browser/notification_service.h" | 33 #include "content/public/browser/notification_service.h" |
| 35 #include "content/public/browser/render_process_host.h" | 34 #include "content/public/browser/render_process_host.h" |
| 36 #include "content/public/browser/render_view_host.h" | 35 #include "content/public/browser/render_view_host.h" |
| 37 #include "content/public/browser/render_widget_host.h" | 36 #include "content/public/browser/render_widget_host.h" |
| 38 #include "content/public/browser/render_widget_host_view.h" | 37 #include "content/public/browser/render_widget_host_view.h" |
| 39 #include "content/public/browser/site_instance.h" | 38 #include "content/public/browser/site_instance.h" |
| 40 #include "content/public/browser/web_contents.h" | 39 #include "content/public/browser/web_contents.h" |
| 40 #include "extensions/common/manifest_constants.h" |
| 41 #include "url/gurl.h" | 41 #include "url/gurl.h" |
| 42 | 42 |
| 43 using content::SiteInstance; | 43 using content::SiteInstance; |
| 44 using content::WebContents; | 44 using content::WebContents; |
| 45 | 45 |
| 46 // Since we have 2 ports for every channel, we just index channels by half the | 46 // Since we have 2 ports for every channel, we just index channels by half the |
| 47 // port ID. | 47 // port ID. |
| 48 #define GET_CHANNEL_ID(port_id) ((port_id) / 2) | 48 #define GET_CHANNEL_ID(port_id) ((port_id) / 2) |
| 49 #define GET_CHANNEL_OPENER_ID(channel_id) ((channel_id) * 2) | 49 #define GET_CHANNEL_OPENER_ID(channel_id) ((channel_id) * 2) |
| 50 #define GET_CHANNEL_RECEIVERS_ID(channel_id) ((channel_id) * 2 + 1) | 50 #define GET_CHANNEL_RECEIVERS_ID(channel_id) ((channel_id) * 2 + 1) |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 106 }; | 106 }; |
| 107 | 107 |
| 108 namespace { | 108 namespace { |
| 109 | 109 |
| 110 static base::StaticAtomicSequenceNumber g_next_channel_id; | 110 static base::StaticAtomicSequenceNumber g_next_channel_id; |
| 111 static base::StaticAtomicSequenceNumber g_channel_id_overflow_count; | 111 static base::StaticAtomicSequenceNumber g_channel_id_overflow_count; |
| 112 | 112 |
| 113 static content::RenderProcessHost* GetExtensionProcess( | 113 static content::RenderProcessHost* GetExtensionProcess( |
| 114 Profile* profile, const std::string& extension_id) { | 114 Profile* profile, const std::string& extension_id) { |
| 115 SiteInstance* site_instance = | 115 SiteInstance* site_instance = |
| 116 extensions::ExtensionSystem::Get(profile)->process_manager()-> | 116 ExtensionSystem::Get(profile)->process_manager()-> |
| 117 GetSiteInstanceForURL( | 117 GetSiteInstanceForURL( |
| 118 Extension::GetBaseURLFromExtensionId(extension_id)); | 118 Extension::GetBaseURLFromExtensionId(extension_id)); |
| 119 | 119 |
| 120 if (!site_instance->HasProcess()) | 120 if (!site_instance->HasProcess()) |
| 121 return NULL; | 121 return NULL; |
| 122 | 122 |
| 123 return site_instance->GetProcess(); | 123 return site_instance->GetProcess(); |
| 124 } | 124 } |
| 125 | 125 |
| 126 } // namespace | 126 } // namespace |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 199 | 199 |
| 200 const Extension* target_extension = ExtensionSystem::Get(profile)-> | 200 const Extension* target_extension = ExtensionSystem::Get(profile)-> |
| 201 extension_service()->extensions()->GetByID(target_extension_id); | 201 extension_service()->extensions()->GetByID(target_extension_id); |
| 202 if (!target_extension) { | 202 if (!target_extension) { |
| 203 DispatchOnDisconnect( | 203 DispatchOnDisconnect( |
| 204 source, receiver_port_id, kReceivingEndDoesntExistError); | 204 source, receiver_port_id, kReceivingEndDoesntExistError); |
| 205 return; | 205 return; |
| 206 } | 206 } |
| 207 | 207 |
| 208 ExtensionService* extension_service = | 208 ExtensionService* extension_service = |
| 209 extensions::ExtensionSystem::Get(profile)->extension_service(); | 209 ExtensionSystem::Get(profile)->extension_service(); |
| 210 | 210 |
| 211 if (profile->IsOffTheRecord() && | 211 if (profile->IsOffTheRecord() && |
| 212 !extension_service->IsIncognitoEnabled(target_extension_id)) { | 212 !extension_service->IsIncognitoEnabled(target_extension_id)) { |
| 213 DispatchOnDisconnect( | 213 DispatchOnDisconnect( |
| 214 source, receiver_port_id, kReceivingEndDoesntExistError); | 214 source, receiver_port_id, kReceivingEndDoesntExistError); |
| 215 return; | 215 return; |
| 216 } | 216 } |
| 217 | 217 |
| 218 if (source_extension_id != target_extension_id) { | 218 if (source_extension_id != target_extension_id) { |
| 219 // It's an external connection. Check the externally_connectable manifest | 219 // It's an external connection. Check the externally_connectable manifest |
| 220 // key if it's present. If it's not, we allow connection from any extension | 220 // key if it's present. If it's not, we allow connection from any extension |
| 221 // but not webpages. | 221 // but not webpages. |
| 222 ExternallyConnectableInfo* externally_connectable = | 222 ExternallyConnectableInfo* externally_connectable = |
| 223 static_cast<ExternallyConnectableInfo*>( | 223 static_cast<ExternallyConnectableInfo*>( |
| 224 target_extension->GetManifestData( | 224 target_extension->GetManifestData( |
| 225 extension_manifest_keys::kExternallyConnectable)); | 225 manifest_keys::kExternallyConnectable)); |
| 226 bool is_externally_connectable = false; | 226 bool is_externally_connectable = false; |
| 227 | 227 |
| 228 if (externally_connectable) { | 228 if (externally_connectable) { |
| 229 if (source_extension_id.empty()) { | 229 if (source_extension_id.empty()) { |
| 230 // No source extension ID so the source was a web page. Check that the | 230 // No source extension ID so the source was a web page. Check that the |
| 231 // URL matches. | 231 // URL matches. |
| 232 is_externally_connectable = | 232 is_externally_connectable = |
| 233 externally_connectable->matches.MatchesURL(source_url); | 233 externally_connectable->matches.MatchesURL(source_url); |
| 234 } else { | 234 } else { |
| 235 // Source extension ID so the source was an extension. Check that the | 235 // Source extension ID so the source was an extension. Check that the |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 298 const std::string& source_extension_id, | 298 const std::string& source_extension_id, |
| 299 const std::string& native_app_name) { | 299 const std::string& native_app_name) { |
| 300 content::RenderProcessHost* source = | 300 content::RenderProcessHost* source = |
| 301 content::RenderProcessHost::FromID(source_process_id); | 301 content::RenderProcessHost::FromID(source_process_id); |
| 302 if (!source) | 302 if (!source) |
| 303 return; | 303 return; |
| 304 | 304 |
| 305 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_LINUX) | 305 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_LINUX) |
| 306 Profile* profile = Profile::FromBrowserContext(source->GetBrowserContext()); | 306 Profile* profile = Profile::FromBrowserContext(source->GetBrowserContext()); |
| 307 ExtensionService* extension_service = | 307 ExtensionService* extension_service = |
| 308 extensions::ExtensionSystem::Get(profile)->extension_service(); | 308 ExtensionSystem::Get(profile)->extension_service(); |
| 309 bool has_permission = false; | 309 bool has_permission = false; |
| 310 if (extension_service) { | 310 if (extension_service) { |
| 311 const Extension* extension = | 311 const Extension* extension = |
| 312 extension_service->GetExtensionById(source_extension_id, false); | 312 extension_service->GetExtensionById(source_extension_id, false); |
| 313 has_permission = extension && extension->HasAPIPermission( | 313 has_permission = extension && extension->HasAPIPermission( |
| 314 APIPermission::kNativeMessaging); | 314 APIPermission::kNativeMessaging); |
| 315 } | 315 } |
| 316 | 316 |
| 317 if (!has_permission) { | 317 if (!has_permission) { |
| 318 DispatchOnDisconnect(source, receiver_port_id, kMissingPermissionError); | 318 DispatchOnDisconnect(source, receiver_port_id, kMissingPermissionError); |
| (...skipping 284 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 603 } | 603 } |
| 604 | 604 |
| 605 void MessageService::DispatchOnDisconnect(content::RenderProcessHost* source, | 605 void MessageService::DispatchOnDisconnect(content::RenderProcessHost* source, |
| 606 int port_id, | 606 int port_id, |
| 607 const std::string& error_message) { | 607 const std::string& error_message) { |
| 608 ExtensionMessagePort port(source, MSG_ROUTING_CONTROL, ""); | 608 ExtensionMessagePort port(source, MSG_ROUTING_CONTROL, ""); |
| 609 port.DispatchOnDisconnect(GET_OPPOSITE_PORT_ID(port_id), error_message); | 609 port.DispatchOnDisconnect(GET_OPPOSITE_PORT_ID(port_id), error_message); |
| 610 } | 610 } |
| 611 | 611 |
| 612 } // namespace extensions | 612 } // namespace extensions |
| OLD | NEW |