Commit 80b441cd authored by Austin Tankiang's avatar Austin Tankiang Committed by Commit Bot

Pass BrowserContext to built-in native message hosts

This is needed to support upcoming work to allow the Drive Web interface
to talk to DriveFS on Chrome OS via the native messaging api. A new
built-in native message host will be added to bridge the gap between
Drive Web and DriveFS, and that message host will need a profile object
in order to connect to DriveFS. This profile object will be obtained
from the passed in BrowserContext.

Bug: 1020954
Change-Id: I0bbd39107e5b0189c2f8f69fcafe488aa7ab57aa
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1905035
Commit-Queue: Austin Tankiang <austinct@chromium.org>
Reviewed-by: default avatarOleh Lamzin <lamzin@google.com>
Reviewed-by: default avatarHidehiko Abe <hidehiko@chromium.org>
Reviewed-by: default avatarSergey Ulanov <sergeyu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#716058}
parent 55f44515
...@@ -23,7 +23,8 @@ const char* const ArcSupportMessageHost::kHostOrigin[] = { ...@@ -23,7 +23,8 @@ const char* const ArcSupportMessageHost::kHostOrigin[] = {
"chrome-extension://cnbgggchhmkkdmeppjobngjoejnihlei/"}; "chrome-extension://cnbgggchhmkkdmeppjobngjoejnihlei/"};
// static // static
std::unique_ptr<extensions::NativeMessageHost> ArcSupportMessageHost::Create() { std::unique_ptr<extensions::NativeMessageHost> ArcSupportMessageHost::Create(
content::BrowserContext* browser_context) {
return std::unique_ptr<NativeMessageHost>(new ArcSupportMessageHost()); return std::unique_ptr<NativeMessageHost>(new ArcSupportMessageHost());
} }
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include "base/macros.h" #include "base/macros.h"
#include "base/values.h" #include "base/values.h"
#include "content/public/browser/browser_context.h"
#include "extensions/browser/api/messaging/native_message_host.h" #include "extensions/browser/api/messaging/native_message_host.h"
namespace arc { namespace arc {
...@@ -32,7 +33,8 @@ class ArcSupportMessageHost : public extensions::NativeMessageHost { ...@@ -32,7 +33,8 @@ class ArcSupportMessageHost : public extensions::NativeMessageHost {
// Called when the arc_support connects the "port". Returns the // Called when the arc_support connects the "port". Returns the
// instance of ArcSupportMessageHost. // instance of ArcSupportMessageHost.
static std::unique_ptr<NativeMessageHost> Create(); static std::unique_ptr<NativeMessageHost> Create(
content::BrowserContext* browser_context);
~ArcSupportMessageHost() override; ~ArcSupportMessageHost() override;
......
...@@ -65,7 +65,7 @@ class ArcSupportMessageHostTest : public testing::Test { ...@@ -65,7 +65,7 @@ class ArcSupportMessageHostTest : public testing::Test {
void SetUp() override { void SetUp() override {
client_ = std::make_unique<TestClient>(); client_ = std::make_unique<TestClient>();
message_host_ = ArcSupportMessageHost::Create(); message_host_ = ArcSupportMessageHost::Create(nullptr);
message_host_->Start(client_.get()); message_host_->Start(client_.get());
} }
......
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
#include "base/logging.h" #include "base/logging.h"
#include "base/values.h" #include "base/values.h"
#include "chrome/browser/chromeos/arc/extensions/arc_support_message_host.h" #include "chrome/browser/chromeos/arc/extensions/arc_support_message_host.h"
#include "chrome/browser/profiles/profile.h"
namespace { namespace {
...@@ -46,7 +47,7 @@ FakeArcSupport::~FakeArcSupport() { ...@@ -46,7 +47,7 @@ FakeArcSupport::~FakeArcSupport() {
void FakeArcSupport::Open(Profile* profile) { void FakeArcSupport::Open(Profile* profile) {
DCHECK(!native_message_host_); DCHECK(!native_message_host_);
native_message_host_ = ArcSupportMessageHost::Create(); native_message_host_ = ArcSupportMessageHost::Create(profile);
native_message_host_->Start(this); native_message_host_->Start(this);
support_host_->SetMessageHost( support_host_->SetMessageHost(
static_cast<ArcSupportMessageHost*>(native_message_host_.get())); static_cast<ArcSupportMessageHost*>(native_message_host_.get()));
......
...@@ -357,7 +357,8 @@ void DeliverMessageToExtension( ...@@ -357,7 +357,8 @@ void DeliverMessageToExtension(
} // namespace } // namespace
std::unique_ptr<extensions::NativeMessageHost> std::unique_ptr<extensions::NativeMessageHost>
CreateExtensionOwnedWilcoDtcSupportdMessageHost() { CreateExtensionOwnedWilcoDtcSupportdMessageHost(
content::BrowserContext* browser_context) {
return std::make_unique<WilcoDtcSupportdExtensionOwnedMessageHost>(); return std::make_unique<WilcoDtcSupportdExtensionOwnedMessageHost>();
} }
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include <string> #include <string>
#include "base/callback_forward.h" #include "base/callback_forward.h"
#include "content/public/browser/browser_context.h"
namespace extensions { namespace extensions {
class NativeMessageHost; class NativeMessageHost;
...@@ -33,7 +34,8 @@ extern const int kWilcoDtcSupportdUiMessageMaxSize; ...@@ -33,7 +34,8 @@ extern const int kWilcoDtcSupportdUiMessageMaxSize;
// This should be used when the communication is initiated by the extension // This should be used when the communication is initiated by the extension
// (i.e., not the daemon). // (i.e., not the daemon).
std::unique_ptr<extensions::NativeMessageHost> std::unique_ptr<extensions::NativeMessageHost>
CreateExtensionOwnedWilcoDtcSupportdMessageHost(); CreateExtensionOwnedWilcoDtcSupportdMessageHost(
content::BrowserContext* browser_context);
// Delivers the UI message |json_message| from the wilco_dtc_supportd daemon to // Delivers the UI message |json_message| from the wilco_dtc_supportd daemon to
// the extensions that are allowed to receive it. The delivery is done via // the extensions that are allowed to receive it. The delivery is done via
......
...@@ -109,7 +109,7 @@ TEST(WilcoDtcSupportdMessagingOpenedByExtensionNoBridgeTest, Test) { ...@@ -109,7 +109,7 @@ TEST(WilcoDtcSupportdMessagingOpenedByExtensionNoBridgeTest, Test) {
// Create the message host. // Create the message host.
std::unique_ptr<extensions::NativeMessageHost> message_host = std::unique_ptr<extensions::NativeMessageHost> message_host =
CreateExtensionOwnedWilcoDtcSupportdMessageHost(); CreateExtensionOwnedWilcoDtcSupportdMessageHost(nullptr);
StrictMock<MockNativeMessageHostClient> message_host_client; StrictMock<MockNativeMessageHostClient> message_host_client;
// The message host will close the channel during the OnMessage() call at the // The message host will close the channel during the OnMessage() call at the
...@@ -169,7 +169,7 @@ class WilcoDtcSupportdMessagingOpenedByExtensionTest : public testing::Test { ...@@ -169,7 +169,7 @@ class WilcoDtcSupportdMessagingOpenedByExtensionTest : public testing::Test {
TEST_F(WilcoDtcSupportdMessagingOpenedByExtensionTest, NoMojoConnection) { TEST_F(WilcoDtcSupportdMessagingOpenedByExtensionTest, NoMojoConnection) {
// Create the message host. // Create the message host.
std::unique_ptr<extensions::NativeMessageHost> message_host = std::unique_ptr<extensions::NativeMessageHost> message_host =
CreateExtensionOwnedWilcoDtcSupportdMessageHost(); CreateExtensionOwnedWilcoDtcSupportdMessageHost(nullptr);
StrictMock<MockNativeMessageHostClient> message_host_client; StrictMock<MockNativeMessageHostClient> message_host_client;
message_host->Start(&message_host_client); message_host->Start(&message_host_client);
...@@ -189,7 +189,7 @@ class WilcoDtcSupportdMessagingOpenedByExtensionSingleHostTest ...@@ -189,7 +189,7 @@ class WilcoDtcSupportdMessagingOpenedByExtensionSingleHostTest
protected: protected:
WilcoDtcSupportdMessagingOpenedByExtensionSingleHostTest() { WilcoDtcSupportdMessagingOpenedByExtensionSingleHostTest() {
wilco_dtc_supportd_bridge_wrapper()->EstablishFakeMojoConnection(); wilco_dtc_supportd_bridge_wrapper()->EstablishFakeMojoConnection();
message_host_ = CreateExtensionOwnedWilcoDtcSupportdMessageHost(); message_host_ = CreateExtensionOwnedWilcoDtcSupportdMessageHost(nullptr);
message_host_->Start(&message_host_client_); message_host_->Start(&message_host_client_);
} }
......
...@@ -43,7 +43,8 @@ const char* const kEchoHostOrigins[] = { ...@@ -43,7 +43,8 @@ const char* const kEchoHostOrigins[] = {
class EchoHost : public NativeMessageHost { class EchoHost : public NativeMessageHost {
public: public:
static std::unique_ptr<NativeMessageHost> Create() { static std::unique_ptr<NativeMessageHost> Create(
content::BrowserContext* browser_context) {
return std::unique_ptr<NativeMessageHost>(new EchoHost()); return std::unique_ptr<NativeMessageHost>(new EchoHost());
} }
...@@ -90,10 +91,12 @@ struct BuiltInHost { ...@@ -90,10 +91,12 @@ struct BuiltInHost {
const char* const name; const char* const name;
const char* const* const allowed_origins; const char* const* const allowed_origins;
int allowed_origins_count; int allowed_origins_count;
std::unique_ptr<NativeMessageHost> (*create_function)(); std::unique_ptr<NativeMessageHost> (*create_function)(
content::BrowserContext*);
}; };
std::unique_ptr<NativeMessageHost> CreateIt2MeHost() { std::unique_ptr<NativeMessageHost> CreateIt2MeHost(
content::BrowserContext* browser_context) {
return remoting::CreateIt2MeNativeMessagingHostForChromeOS( return remoting::CreateIt2MeNativeMessagingHostForChromeOS(
base::CreateSingleThreadTaskRunner({content::BrowserThread::IO}), base::CreateSingleThreadTaskRunner({content::BrowserThread::IO}),
base::CreateSingleThreadTaskRunner({content::BrowserThread::UI}), base::CreateSingleThreadTaskRunner({content::BrowserThread::UI}),
...@@ -157,7 +160,7 @@ std::unique_ptr<NativeMessageHost> NativeMessageHost::Create( ...@@ -157,7 +160,7 @@ std::unique_ptr<NativeMessageHost> NativeMessageHost::Create(
std::string name(host.name); std::string name(host.name);
if (name == native_host_name) { if (name == native_host_name) {
if (MatchesSecurityOrigin(host, source_extension_id)) { if (MatchesSecurityOrigin(host, source_extension_id)) {
return (*host.create_function)(); return (*host.create_function)(browser_context);
} }
*error = kForbiddenError; *error = kForbiddenError;
return nullptr; return nullptr;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment