Commit c30db006 authored by Austin Tankiang's avatar Austin Tankiang Committed by Commit Bot

Add built-in native message host for DriveFS

This allows the Drive Web interface to talk to DriveFS on Chrome OS via
the native messaging api.

Bug: 1020954
Change-Id: I450e8ad46cc601e0b0d8df2c19dd4e255f963391
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1910982
Commit-Queue: Austin Tankiang <austinct@chromium.org>
Reviewed-by: default avatarSergey Ulanov <sergeyu@chromium.org>
Reviewed-by: default avatarSergei Datsenko <dats@chromium.org>
Reviewed-by: default avatarSam McNally <sammc@chromium.org>
Cr-Commit-Position: refs/heads/master@{#716064}
parent 20c06c2f
...@@ -907,6 +907,8 @@ source_set("chromeos") { ...@@ -907,6 +907,8 @@ source_set("chromeos") {
"display/quirks_manager_delegate_impl.h", "display/quirks_manager_delegate_impl.h",
"drive/drive_integration_service.cc", "drive/drive_integration_service.cc",
"drive/drive_integration_service.h", "drive/drive_integration_service.h",
"drive/drivefs_native_message_host.cc",
"drive/drivefs_native_message_host.h",
"drive/file_system_util.cc", "drive/file_system_util.cc",
"drive/file_system_util.h", "drive/file_system_util.h",
"drive/fileapi/drivefs_async_file_util.cc", "drive/fileapi/drivefs_async_file_util.cc",
......
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/chromeos/drive/drivefs_native_message_host.h"
#include "base/bind.h"
#include "base/logging.h"
#include "base/stl_util.h"
#include "base/task/post_task.h"
#include "base/task/task_traits.h"
#include "chrome/browser/chromeos/drive/drive_integration_service.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/extensions/extension_constants.h"
#include "components/drive/file_errors.h"
namespace drive {
const char kDriveFsNativeMessageHostName[] = "com.google.drive.nativeproxy";
const char* const kDriveFsNativeMessageHostOrigins[] = {
"chrome-extension://lmjegmlicamnimmfhcmpkclmigmmcbeh/",
};
constexpr size_t kDriveFsNativeMessageHostOriginsSize =
base::size(kDriveFsNativeMessageHostOrigins);
class DriveFsNativeMessageHost : public extensions::NativeMessageHost {
public:
explicit DriveFsNativeMessageHost(Profile* profile)
: drive_service_(DriveIntegrationServiceFactory::GetForProfile(profile)) {
}
~DriveFsNativeMessageHost() override = default;
void OnMessage(const std::string& message) override {
if (!drive_service_ || !drive_service_->GetDriveFsInterface()) {
OnDriveFsResponse(FILE_ERROR_SERVICE_UNAVAILABLE, "");
return;
}
drive_service_->GetDriveFsInterface()->SendNativeMessageRequest(
message, base::Bind(&DriveFsNativeMessageHost::OnDriveFsResponse,
weak_ptr_factory_.GetWeakPtr()));
}
void Start(Client* client) override { client_ = client; }
scoped_refptr<base::SingleThreadTaskRunner> task_runner() const override {
return task_runner_;
}
private:
void OnDriveFsResponse(FileError error, const std::string& response) {
if (error == FILE_ERROR_OK) {
client_->PostMessageFromNativeHost(response);
} else {
LOG(WARNING) << "DriveFS returned error " << FileErrorToString(error);
client_->CloseChannel(FileErrorToString(error));
}
}
DriveIntegrationService* drive_service_;
Client* client_ = nullptr;
const scoped_refptr<base::SingleThreadTaskRunner> task_runner_ =
base::CreateSingleThreadTaskRunner({base::CurrentThread()});
base::WeakPtrFactory<DriveFsNativeMessageHost> weak_ptr_factory_{this};
DISALLOW_COPY_AND_ASSIGN(DriveFsNativeMessageHost);
};
std::unique_ptr<extensions::NativeMessageHost> CreateDriveFsNativeMessageHost(
content::BrowserContext* browser_context) {
return std::make_unique<DriveFsNativeMessageHost>(
Profile::FromBrowserContext(browser_context));
}
} // namespace drive
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_CHROMEOS_DRIVE_DRIVEFS_NATIVE_MESSAGE_HOST_H_
#define CHROME_BROWSER_CHROMEOS_DRIVE_DRIVEFS_NATIVE_MESSAGE_HOST_H_
#include <memory>
#include "content/public/browser/browser_context.h"
#include "extensions/browser/api/messaging/native_message_host.h"
namespace drive {
extern const char kDriveFsNativeMessageHostName[];
extern const char* const kDriveFsNativeMessageHostOrigins[];
extern const size_t kDriveFsNativeMessageHostOriginsSize;
std::unique_ptr<extensions::NativeMessageHost> CreateDriveFsNativeMessageHost(
content::BrowserContext* browser_context);
} // namespace drive
#endif // CHROME_BROWSER_CHROMEOS_DRIVE_DRIVEFS_NATIVE_MESSAGE_HOST_H_
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
#include "base/values.h" #include "base/values.h"
#include "chrome/browser/browser_process.h" #include "chrome/browser/browser_process.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/chromeos/drive/drivefs_native_message_host.h"
#include "chrome/browser/chromeos/wilco_dtc_supportd/wilco_dtc_supportd_messaging.h" #include "chrome/browser/chromeos/wilco_dtc_supportd/wilco_dtc_supportd_messaging.h"
#include "content/public/browser/browser_task_traits.h" #include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h" #include "content/public/browser/browser_thread.h"
...@@ -132,6 +133,10 @@ static const BuiltInHost kBuiltInHost[] = { ...@@ -132,6 +133,10 @@ static const BuiltInHost kBuiltInHost[] = {
chromeos::kWilcoDtcSupportdHostOrigins, chromeos::kWilcoDtcSupportdHostOrigins,
chromeos::kWilcoDtcSupportdHostOriginsSize, chromeos::kWilcoDtcSupportdHostOriginsSize,
&chromeos::CreateExtensionOwnedWilcoDtcSupportdMessageHost}, &chromeos::CreateExtensionOwnedWilcoDtcSupportdMessageHost},
{drive::kDriveFsNativeMessageHostName,
drive::kDriveFsNativeMessageHostOrigins,
drive::kDriveFsNativeMessageHostOriginsSize,
&drive::CreateDriveFsNativeMessageHost},
}; };
bool MatchesSecurityOrigin(const BuiltInHost& host, bool MatchesSecurityOrigin(const BuiltInHost& host,
......
...@@ -416,4 +416,10 @@ void FakeDriveFs::FetchAllChangeLogs() {} ...@@ -416,4 +416,10 @@ void FakeDriveFs::FetchAllChangeLogs() {}
void FakeDriveFs::FetchChangeLog( void FakeDriveFs::FetchChangeLog(
std::vector<mojom::FetchChangeLogOptionsPtr> options) {} std::vector<mojom::FetchChangeLogOptionsPtr> options) {}
void FakeDriveFs::SendNativeMessageRequest(
const std::string& request,
SendNativeMessageRequestCallback callback) {
std::move(callback).Run(drive::FILE_ERROR_SERVICE_UNAVAILABLE, "");
}
} // namespace drivefs } // namespace drivefs
...@@ -98,6 +98,10 @@ class FakeDriveFs : public drivefs::mojom::DriveFs, ...@@ -98,6 +98,10 @@ class FakeDriveFs : public drivefs::mojom::DriveFs,
void FetchChangeLog( void FetchChangeLog(
std::vector<mojom::FetchChangeLogOptionsPtr> options) override; std::vector<mojom::FetchChangeLogOptionsPtr> options) override;
void SendNativeMessageRequest(
const std::string& request,
SendNativeMessageRequestCallback callback) override;
const base::FilePath mount_path_; const base::FilePath mount_path_;
std::map<base::FilePath, FileMetadata> metadata_; std::map<base::FilePath, FileMetadata> metadata_;
......
...@@ -57,6 +57,16 @@ interface DriveFs { ...@@ -57,6 +57,16 @@ interface DriveFs {
// Fetches the changelog(s) using the provided |options|. // Fetches the changelog(s) using the provided |options|.
FetchChangeLog(array<FetchChangeLogOptions> options); FetchChangeLog(array<FetchChangeLogOptions> options);
// Send a native message |request| to DriveFS. |request| is a DriveFS
// specific message that looks like "native_opener/v2/$command_id/$message",
// where $command_id is an integer and $message is the base64 encoding of a
// JSPB encoded proto message.
//
// |response| is only filled out if |error| is kOk, and returns a JSPB
// encoded proto message.
SendNativeMessageRequest(string request) => (
FileError error, string response);
}; };
// Implemented by Chrome, used from DriveFS. // Implemented by Chrome, used from DriveFS.
......
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