Skip to content

Commit

Permalink
Use ServiceIPCServer::MessageHandler in CloudPrintMessageHandler.
Browse files Browse the repository at this point in the history
* Moves cloud print IPC message handling from ServiceIPCServer to CloudPrintMessageHandler
* Removes the last 4 uses of g_service_process from ServiceIPCServer.
* Add unit test for ServiceIPCServer

BUG=402456

Review URL: https://codereview.chromium.org/1249513003

Cr-Commit-Position: refs/heads/master@{#340664}
  • Loading branch information
mvano authored and Commit bot committed Jul 28, 2015
1 parent d200aa5 commit 807bd33
Show file tree
Hide file tree
Showing 11 changed files with 434 additions and 51 deletions.
2 changes: 2 additions & 0 deletions chrome/chrome.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,8 @@
'service/cloud_print/cloud_print_auth.h',
'service/cloud_print/cloud_print_connector.cc',
'service/cloud_print/cloud_print_connector.h',
'service/cloud_print/cloud_print_message_handler.cc',
'service/cloud_print/cloud_print_message_handler.h',
'service/cloud_print/cloud_print_proxy.cc',
'service/cloud_print/cloud_print_proxy.h',
'service/cloud_print/cloud_print_proxy_backend.cc',
Expand Down
1 change: 1 addition & 0 deletions chrome/chrome_tests_unit.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -1093,6 +1093,7 @@
'service/cloud_print/connector_settings_unittest.cc',
'service/cloud_print/printer_job_handler_unittest.cc',
'service/cloud_print/printer_job_queue_handler_unittest.cc',
'service/service_ipc_server_unittest.cc',
'service/service_process_prefs_unittest.cc',
],
'chrome_unit_tests_captive_portal_sources': [
Expand Down
2 changes: 2 additions & 0 deletions chrome/service/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ static_library("service") {
"cloud_print/cloud_print_auth.h",
"cloud_print/cloud_print_connector.cc",
"cloud_print/cloud_print_connector.h",
"cloud_print/cloud_print_message_handler.cc",
"cloud_print/cloud_print_message_handler.h",
"cloud_print/cloud_print_proxy.cc",
"cloud_print/cloud_print_proxy.h",
"cloud_print/cloud_print_proxy_backend.cc",
Expand Down
65 changes: 65 additions & 0 deletions chrome/service/cloud_print/cloud_print_message_handler.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright 2015 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/service/cloud_print/cloud_print_message_handler.h"

#include <vector>

#include "chrome/common/service_messages.h"
#include "ipc/ipc_sender.h"

namespace cloud_print {

CloudPrintMessageHandler::CloudPrintMessageHandler(
IPC::Sender* ipc_sender,
CloudPrintProxy::Provider* proxy_provider)
: ipc_sender_(ipc_sender), proxy_provider_(proxy_provider) {
DCHECK(ipc_sender);
DCHECK(proxy_provider);
}

CloudPrintMessageHandler::~CloudPrintMessageHandler() {
}

bool CloudPrintMessageHandler::HandleMessage(const IPC::Message& message) {
bool handled = true;
IPC_BEGIN_MESSAGE_MAP(CloudPrintMessageHandler, message)
IPC_MESSAGE_HANDLER(ServiceMsg_EnableCloudPrintProxyWithRobot,
OnEnableCloudPrintProxyWithRobot)
IPC_MESSAGE_HANDLER(ServiceMsg_DisableCloudPrintProxy,
OnDisableCloudPrintProxy)
IPC_MESSAGE_HANDLER(ServiceMsg_GetCloudPrintProxyInfo,
OnGetCloudPrintProxyInfo)
IPC_MESSAGE_HANDLER(ServiceMsg_GetPrinters, OnGetPrinters)
IPC_MESSAGE_UNHANDLED(handled = false)
IPC_END_MESSAGE_MAP()
return handled;
}

void CloudPrintMessageHandler::OnEnableCloudPrintProxyWithRobot(
const std::string& robot_auth_code,
const std::string& robot_email,
const std::string& user_email,
const base::DictionaryValue& user_settings) {
proxy_provider_->GetCloudPrintProxy()->EnableForUserWithRobot(
robot_auth_code, robot_email, user_email, user_settings);
}

void CloudPrintMessageHandler::OnGetCloudPrintProxyInfo() {
CloudPrintProxyInfo info;
proxy_provider_->GetCloudPrintProxy()->GetProxyInfo(&info);
ipc_sender_->Send(new ServiceHostMsg_CloudPrintProxy_Info(info));
}

void CloudPrintMessageHandler::OnGetPrinters() {
std::vector<std::string> printers;
proxy_provider_->GetCloudPrintProxy()->GetPrinters(&printers);
ipc_sender_->Send(new ServiceHostMsg_Printers(printers));
}

void CloudPrintMessageHandler::OnDisableCloudPrintProxy() {
proxy_provider_->GetCloudPrintProxy()->UnregisterPrintersAndDisableForUser();
}

} // namespace cloud_print
54 changes: 54 additions & 0 deletions chrome/service/cloud_print/cloud_print_message_handler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright 2015 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_SERVICE_CLOUD_PRINT_CLOUD_PRINT_MESSAGE_HANDLER_H_
#define CHROME_SERVICE_CLOUD_PRINT_CLOUD_PRINT_MESSAGE_HANDLER_H_

#include <string>

#include "base/macros.h"
#include "chrome/service/cloud_print/cloud_print_proxy.h"
#include "chrome/service/service_ipc_server.h"

namespace base {
class DictionaryValue;
}

namespace IPC {
class Message;
class Sender;
}

namespace cloud_print {

// Handles IPC messages for Cloud Print. Lives on the main thread.
class CloudPrintMessageHandler : public ServiceIPCServer::MessageHandler {
public:
CloudPrintMessageHandler(IPC::Sender* ipc_sender,
CloudPrintProxy::Provider* proxy_provider);
~CloudPrintMessageHandler() override;

// ServiceIPCServer::MessageHandler implementation.
bool HandleMessage(const IPC::Message& message) override;

private:
// IPC message handlers.
void OnEnableCloudPrintProxyWithRobot(
const std::string& robot_auth_code,
const std::string& robot_email,
const std::string& user_email,
const base::DictionaryValue& user_settings);
void OnGetCloudPrintProxyInfo();
void OnGetPrinters();
void OnDisableCloudPrintProxy();

IPC::Sender* ipc_sender_; // Owned by our owner.
CloudPrintProxy::Provider* proxy_provider_; // Owned by our owner.

DISALLOW_COPY_AND_ASSIGN(CloudPrintMessageHandler);
};

} // namespace cloud_print

#endif // CHROME_SERVICE_CLOUD_PRINT_CLOUD_PRINT_MESSAGE_HANDLER_H_
6 changes: 6 additions & 0 deletions chrome/service/cloud_print/cloud_print_proxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ class CloudPrintProxy : public CloudPrintProxyFrontend,
CloudPrintProxy();
~CloudPrintProxy() override;

// Provides a CloudPrintProxy instance, which may be lazily instantiated.
class Provider {
public:
virtual CloudPrintProxy* GetCloudPrintProxy() = 0;
};

// Initializes the object. This should be called every time an object of this
// class is constructed.
void Initialize(ServiceProcessPrefs* service_prefs, Client* client);
Expand Down
50 changes: 14 additions & 36 deletions chrome/service/service_ipc_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

#include "base/metrics/histogram_delta_serialization.h"
#include "chrome/common/service_messages.h"
#include "chrome/service/cloud_print/cloud_print_proxy.h"
#include "chrome/service/service_process.h"
#include "ipc/ipc_logging.h"

ServiceIPCServer::ServiceIPCServer(
Expand Down Expand Up @@ -87,6 +85,10 @@ bool ServiceIPCServer::Send(IPC::Message* msg) {
return channel_->Send(msg);
}

void ServiceIPCServer::AddMessageHandler(scoped_ptr<MessageHandler> handler) {
message_handlers_.push_back(handler.release());
}

bool ServiceIPCServer::OnMessageReceived(const IPC::Message& msg) {
bool handled = true;
// When we get a message, always mark the IPC client as connected. The
Expand All @@ -95,34 +97,23 @@ bool ServiceIPCServer::OnMessageReceived(const IPC::Message& msg) {
// again on subsequent connections.
ipc_client_connected_ = true;
IPC_BEGIN_MESSAGE_MAP(ServiceIPCServer, msg)
IPC_MESSAGE_HANDLER(ServiceMsg_EnableCloudPrintProxyWithRobot,
OnEnableCloudPrintProxyWithRobot)
IPC_MESSAGE_HANDLER(ServiceMsg_DisableCloudPrintProxy,
OnDisableCloudPrintProxy)
IPC_MESSAGE_HANDLER(ServiceMsg_GetCloudPrintProxyInfo,
OnGetCloudPrintProxyInfo)
IPC_MESSAGE_HANDLER(ServiceMsg_GetHistograms, OnGetHistograms)
IPC_MESSAGE_HANDLER(ServiceMsg_GetPrinters, OnGetPrinters)
IPC_MESSAGE_HANDLER(ServiceMsg_Shutdown, OnShutdown);
IPC_MESSAGE_HANDLER(ServiceMsg_UpdateAvailable, OnUpdateAvailable);
IPC_MESSAGE_UNHANDLED(handled = false)
IPC_END_MESSAGE_MAP()
return handled;
}

void ServiceIPCServer::OnEnableCloudPrintProxyWithRobot(
const std::string& robot_auth_code,
const std::string& robot_email,
const std::string& user_email,
const base::DictionaryValue& user_settings) {
g_service_process->GetCloudPrintProxy()->EnableForUserWithRobot(
robot_auth_code, robot_email, user_email, user_settings);
}
if (!handled) {
// Make a copy of the handlers to prevent modification during iteration.
std::vector<MessageHandler*> temp_handlers = message_handlers_.get();
for (const auto& handler : temp_handlers) {
handled = handler->HandleMessage(msg);
if (handled)
break;
}
}

void ServiceIPCServer::OnGetCloudPrintProxyInfo() {
cloud_print::CloudPrintProxyInfo info;
g_service_process->GetCloudPrintProxy()->GetProxyInfo(&info);
channel_->Send(new ServiceHostMsg_CloudPrintProxy_Info(info));
return handled;
}

void ServiceIPCServer::OnGetHistograms() {
Expand All @@ -135,19 +126,6 @@ void ServiceIPCServer::OnGetHistograms() {
channel_->Send(new ServiceHostMsg_Histograms(deltas));
}

void ServiceIPCServer::OnGetPrinters() {
std::vector<std::string> printers;
g_service_process->GetCloudPrintProxy()->GetPrinters(&printers);
channel_->Send(new ServiceHostMsg_Printers(printers));
}

void ServiceIPCServer::OnDisableCloudPrintProxy() {
// User disabled CloudPrint proxy explicitly. Delete printers
// registered from this proxy and disable proxy.
g_service_process->GetCloudPrintProxy()->
UnregisterPrintersAndDisableForUser();
}

void ServiceIPCServer::OnShutdown() {
client_->OnShutdown();
}
Expand Down
27 changes: 16 additions & 11 deletions chrome/service/service_ipc_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@
#ifndef CHROME_SERVICE_SERVICE_IPC_SERVER_H_
#define CHROME_SERVICE_SERVICE_IPC_SERVER_H_

#include <string>
#include <vector>

#include "base/memory/scoped_ptr.h"
#include "base/memory/scoped_vector.h"
#include "ipc/ipc_channel_handle.h"
#include "ipc/ipc_listener.h"
#include "ipc/ipc_sync_channel.h"
#include "ipc/ipc_sender.h"

namespace base {

class DictionaryValue;
class HistogramDeltaSerialization;
class WaitableEvent;

Expand All @@ -25,6 +24,13 @@ class WaitableEvent;
// This class handles IPC commands for the service process.
class ServiceIPCServer : public IPC::Listener, public IPC::Sender {
public:
class MessageHandler {
public:
virtual ~MessageHandler() {}
// Must return true if the message is handled.
virtual bool HandleMessage(const IPC::Message& message) = 0;
};

class Client {
public:
virtual ~Client() {}
Expand Down Expand Up @@ -52,9 +58,16 @@ class ServiceIPCServer : public IPC::Listener, public IPC::Sender {
// IPC::Sender implementation.
bool Send(IPC::Message* msg) override;

// Registers a MessageHandler with the ServiceIPCServer. When an IPC message
// is received that is not handled by the ServiceIPCServer itself, the
// handlers will be called to handle the message in first-add first-call order
// until it is handled or there are no more handlers.
void AddMessageHandler(scoped_ptr<MessageHandler> handler);

bool is_ipc_client_connected() const { return ipc_client_connected_; }

private:
friend class ServiceIPCServerTest;
friend class MockServiceIPCServer;

// IPC::Listener implementation.
Expand All @@ -63,16 +76,7 @@ class ServiceIPCServer : public IPC::Listener, public IPC::Sender {
void OnChannelError() override;

// IPC message handlers.
void OnEnableCloudPrintProxyWithRobot(
const std::string& robot_auth_code,
const std::string& robot_email,
const std::string& user_email,
const base::DictionaryValue& user_settings);
void OnGetCloudPrintProxyInfo();
void OnGetHistograms();
void OnGetPrinters();
void OnDisableCloudPrintProxy();

void OnShutdown();
void OnUpdateAvailable();

Expand All @@ -84,6 +88,7 @@ class ServiceIPCServer : public IPC::Listener, public IPC::Sender {
IPC::ChannelHandle channel_handle_;
scoped_ptr<IPC::SyncChannel> channel_;
base::WaitableEvent* shutdown_event_;
ScopedVector<MessageHandler> message_handlers_;

// Indicates whether an IPC client is currently connected to the channel.
bool ipc_client_connected_;
Expand Down
Loading

0 comments on commit 807bd33

Please sign in to comment.