Commit 43f8b0a6 authored by miket@chromium.org's avatar miket@chromium.org

Eliminate ApiResourceEventNotifier.

This work fell out of a notification API change where I finally
figured out that the EventNotifier wasn't the best way to send
events back to the caller. We're now using a more standard Event
scheme (which, in my defense, might not have existed back when
AREN was conceived). Dead code eliminated.

BUG=164249
TBR=ben@chromium.org

Review URL: https://chromiumcodereview.appspot.com/11636050

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@175905 0039d316-1c4b-4281-b951-d872f2087c98
parent 72e124bf
...@@ -5,7 +5,6 @@ ...@@ -5,7 +5,6 @@
#include "chrome/browser/extensions/api/api_function.h" #include "chrome/browser/extensions/api/api_function.h"
#include "base/bind.h" #include "base/bind.h"
#include "chrome/browser/extensions/api/api_resource_event_notifier.h"
#include "chrome/browser/extensions/extension_system.h" #include "chrome/browser/extensions/extension_system.h"
#include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile.h"
...@@ -19,22 +18,6 @@ ApiFunction::ApiFunction() { ...@@ -19,22 +18,6 @@ ApiFunction::ApiFunction() {
ApiFunction::~ApiFunction() { ApiFunction::~ApiFunction() {
} }
int ApiFunction::ExtractSrcId(const DictionaryValue* options) {
int src_id = -1;
if (options) {
if (options->HasKey(kSrcIdKey))
EXTENSION_FUNCTION_VALIDATE(options->GetInteger(kSrcIdKey, &src_id));
}
return src_id;
}
ApiResourceEventNotifier* ApiFunction::CreateEventNotifier(int src_id) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
return new ApiResourceEventNotifier(
extensions::ExtensionSystem::Get(profile())->event_router(), profile(),
extension_id(), src_id, source_url());
}
// AsyncApiFunction // AsyncApiFunction
AsyncApiFunction::AsyncApiFunction() AsyncApiFunction::AsyncApiFunction()
: work_thread_id_(BrowserThread::IO) { : work_thread_id_(BrowserThread::IO) {
......
...@@ -10,19 +10,13 @@ ...@@ -10,19 +10,13 @@
namespace extensions { namespace extensions {
class ApiResourceEventNotifier; // Base class for API functions. TODO(miket): there isn't much here anymore
// since the removal of ApiResourceEventRouter. Should we promote all its
// subclasses to UIThreadExtensionFunctions?
class ApiFunction : public UIThreadExtensionFunction { class ApiFunction : public UIThreadExtensionFunction {
protected: protected:
ApiFunction(); ApiFunction();
virtual ~ApiFunction(); virtual ~ApiFunction();
// Looks for a kSrcId key that might have been added to a create method's
// options object.
int ExtractSrcId(const DictionaryValue* options);
// Utility.
ApiResourceEventNotifier* CreateEventNotifier(int src_id);
}; };
// AsyncApiFunction provides convenient thread management for APIs that need to // AsyncApiFunction provides convenient thread management for APIs that need to
......
...@@ -3,25 +3,16 @@ ...@@ -3,25 +3,16 @@
// found in the LICENSE file. // found in the LICENSE file.
#include "chrome/browser/extensions/api/api_resource.h" #include "chrome/browser/extensions/api/api_resource.h"
#include "chrome/browser/extensions/api/api_resource_event_notifier.h"
namespace extensions { namespace extensions {
ApiResource::ApiResource(const std::string& owner_extension_id, ApiResource::ApiResource(const std::string& owner_extension_id)
ApiResourceEventNotifier* event_notifier) : owner_extension_id_(owner_extension_id) {
: owner_extension_id_(owner_extension_id),
event_notifier_(event_notifier) {
CHECK(!owner_extension_id_.empty()); CHECK(!owner_extension_id_.empty());
if (event_notifier)
CHECK(event_notifier->src_extension_id() == owner_extension_id_);
// scoped_refptr<> constructor does the initial AddRef() for us on
// event_notifier_.
} }
ApiResource::~ApiResource() { ApiResource::~ApiResource() {
// scoped_refptr<> constructor calls Release() for us on event_notifier_.
} }
} }
...@@ -11,8 +11,6 @@ ...@@ -11,8 +11,6 @@
namespace extensions { namespace extensions {
class ApiResourceEventNotifier;
// An ApiResource represents something that an extension API manages, such as a // An ApiResource represents something that an extension API manages, such as a
// socket or a serial-port connection. Typically, an ApiResourceManager will // socket or a serial-port connection. Typically, an ApiResourceManager will
// control the lifetime of all ApiResources of a specific derived type. // control the lifetime of all ApiResources of a specific derived type.
...@@ -25,20 +23,12 @@ class ApiResource { ...@@ -25,20 +23,12 @@ class ApiResource {
} }
protected: protected:
ApiResource(const std::string& owner_extension_id, explicit ApiResource(const std::string& owner_extension_id);
ApiResourceEventNotifier* event_notifier);
private: private:
// The extension that owns this resource. This could be derived from // The extension that owns this resource.
// event_notifier, but that's a little too cute; to make future code
// maintenance easier, we'll require callers to explicitly specify the owner,
// and for now we'll assert that the owner implied by the event_notifier is
// consistent with the explicit one.
const std::string& owner_extension_id_; const std::string& owner_extension_id_;
// The object that lets this resource report events to the owner application.
scoped_refptr<ApiResourceEventNotifier> event_notifier_;
DISALLOW_COPY_AND_ASSIGN(ApiResource); DISALLOW_COPY_AND_ASSIGN(ApiResource);
}; };
......
// Copyright (c) 2012 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/extensions/api/api_resource_event_notifier.h"
#include "base/bind.h"
#include "base/json/json_writer.h"
#include "base/values.h"
#include "chrome/browser/extensions/event_router.h"
#include "chrome/browser/profiles/profile.h"
#include "content/public/browser/browser_thread.h"
using content::BrowserThread;
namespace extensions {
const char kEventTypeKey[] = "type";
const char kSrcIdKey[] = "srcId";
const char kIsFinalEventKey[] = "isFinalEvent";
const char kResultCodeKey[] = "resultCode";
ApiResourceEventNotifier::ApiResourceEventNotifier(
EventRouter* router,
Profile* profile,
const std::string& src_extension_id,
int src_id,
const GURL& src_url)
: router_(router),
profile_(profile),
src_extension_id_(src_extension_id),
src_id_(src_id),
src_url_(src_url) {
}
// static
std::string ApiResourceEventNotifier::ApiResourceEventTypeToString(
ApiResourceEventType event_type) {
NOTREACHED();
return std::string();
}
ApiResourceEventNotifier::~ApiResourceEventNotifier() {}
void ApiResourceEventNotifier::DispatchEvent(
const std::string& event_name, DictionaryValue* args) {
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE,
base::Bind(
&ApiResourceEventNotifier::DispatchEventOnUIThread, this,
event_name, args));
}
void ApiResourceEventNotifier::DispatchEventOnUIThread(
const std::string& event_name, DictionaryValue* args) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
scoped_ptr<ListValue> arguments(new ListValue());
arguments->Set(0, args);
scoped_ptr<Event> event(new Event(event_name, arguments.Pass()));
event->restrict_to_profile = profile_;
event->event_url = src_url_;
router_->DispatchEventToExtension(src_extension_id_, event.Pass());
}
DictionaryValue* ApiResourceEventNotifier::CreateApiResourceEvent(
ApiResourceEventType event_type) {
DictionaryValue* event = new DictionaryValue();
event->SetString(kEventTypeKey, ApiResourceEventTypeToString(event_type));
event->SetInteger(kSrcIdKey, src_id_);
// TODO(miket): Signal that it's OK to clean up onEvent listeners. This is
// the framework we'll use, but we need to start using it.
event->SetBoolean(kIsFinalEventKey, false);
// The caller owns the created event, which typically is then given to a
// ListValue to dispose of.
return event;
}
} // namespace extensions
// Copyright (c) 2012 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_EXTENSIONS_API_API_RESOURCE_EVENT_NOTIFIER_H_
#define CHROME_BROWSER_EXTENSIONS_API_API_RESOURCE_EVENT_NOTIFIER_H_
#include <string>
#include "base/basictypes.h"
#include "base/memory/ref_counted.h"
#include "base/values.h"
#include "chrome/browser/usb/usb_device.h"
#include "googleurl/src/gurl.h"
class Profile;
namespace base {
class ListValue;
}
namespace extensions {
class EventRouter;
enum ApiResourceEventType {
};
extern const char kSrcIdKey[];
// ApiResourceEventNotifier knows how to send an event to a specific app's
// onEvent handler.
class ApiResourceEventNotifier
: public base::RefCountedThreadSafe<ApiResourceEventNotifier> {
public:
ApiResourceEventNotifier(EventRouter* router, Profile* profile,
const std::string& src_extension_id, int src_id,
const GURL& src_url);
static std::string ApiResourceEventTypeToString(
ApiResourceEventType event_type);
const std::string& src_extension_id() const { return src_extension_id_; }
private:
friend class base::RefCountedThreadSafe<ApiResourceEventNotifier>;
friend class MockApiResourceEventNotifier;
virtual ~ApiResourceEventNotifier();
void DispatchEvent(const std::string& event_name, DictionaryValue* args);
void DispatchEventOnUIThread(const std::string& event_name,
DictionaryValue* args);
DictionaryValue* CreateApiResourceEvent(ApiResourceEventType event_type);
EventRouter* router_;
Profile* profile_;
std::string src_extension_id_;
int src_id_;
GURL src_url_;
DISALLOW_COPY_AND_ASSIGN(ApiResourceEventNotifier);
};
} // namespace extensions
#endif // CHROME_BROWSER_EXTENSIONS_API_API_RESOURCE_EVENT_NOTIFIER_H_
...@@ -12,7 +12,6 @@ ...@@ -12,7 +12,6 @@
#include "chrome/test/base/browser_with_test_window_test.h" #include "chrome/test/base/browser_with_test_window_test.h"
#include "content/public/browser/browser_thread.h" #include "content/public/browser/browser_thread.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
#include "chrome/browser/extensions/api/api_resource_event_notifier.h"
#include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile.h"
#include "googleurl/src/gurl.h" #include "googleurl/src/gurl.h"
...@@ -31,9 +30,8 @@ class ApiResourceManagerUnitTest : public BrowserWithTestWindowTest { ...@@ -31,9 +30,8 @@ class ApiResourceManagerUnitTest : public BrowserWithTestWindowTest {
class FakeApiResource : public ApiResource { class FakeApiResource : public ApiResource {
public: public:
FakeApiResource(const std::string& owner_extension_id, FakeApiResource(const std::string& owner_extension_id) :
ApiResourceEventNotifier* event_notifier) : ApiResource(owner_extension_id) {}
ApiResource(owner_extension_id, event_notifier) {}
~FakeApiResource() {} ~FakeApiResource() {}
}; };
...@@ -48,19 +46,8 @@ TEST_F(ApiResourceManagerUnitTest, TwoAppsCannotShareResources) { ...@@ -48,19 +46,8 @@ TEST_F(ApiResourceManagerUnitTest, TwoAppsCannotShareResources) {
const std::string extension_one_id(extension_one->id()); const std::string extension_one_id(extension_one->id());
const std::string extension_two_id(extension_two->id()); const std::string extension_two_id(extension_two->id());
GURL url_one("url-one"); int resource_one_id = manager->Add(new FakeApiResource(extension_one_id));
GURL url_two("url-two"); int resource_two_id = manager->Add(new FakeApiResource(extension_two_id));
scoped_refptr<ApiResourceEventNotifier> event_notifier_one(
new ApiResourceEventNotifier(
NULL, NULL, extension_one_id, 1111, url_one));
scoped_refptr<ApiResourceEventNotifier> event_notifier_two(
new ApiResourceEventNotifier(
NULL, NULL, extension_two_id, 2222, url_two));
int resource_one_id = manager->Add(new FakeApiResource(
extension_one_id, event_notifier_one.get()));
int resource_two_id = manager->Add(new FakeApiResource(
extension_two_id, event_notifier_two.get()));
CHECK(resource_one_id); CHECK(resource_one_id);
CHECK(resource_two_id); CHECK(resource_two_id);
......
...@@ -13,12 +13,9 @@ namespace extensions { ...@@ -13,12 +13,9 @@ namespace extensions {
const char kSerialConnectionNotFoundError[] = "Serial connection not found"; const char kSerialConnectionNotFoundError[] = "Serial connection not found";
SerialConnection::SerialConnection(const std::string& port, SerialConnection::SerialConnection(const std::string& port, int bitrate,
int bitrate,
const std::string& owner_extension_id) const std::string& owner_extension_id)
: ApiResource(owner_extension_id, NULL), : ApiResource(owner_extension_id), port_(port), bitrate_(bitrate),
port_(port),
bitrate_(bitrate),
file_(base::kInvalidPlatformFileValue) { file_(base::kInvalidPlatformFileValue) {
CHECK(bitrate >= 0); CHECK(bitrate >= 0);
} }
...@@ -56,8 +53,7 @@ void SerialConnection::Close() { ...@@ -56,8 +53,7 @@ void SerialConnection::Close() {
int SerialConnection::Read(scoped_refptr<net::IOBufferWithSize> io_buffer) { int SerialConnection::Read(scoped_refptr<net::IOBufferWithSize> io_buffer) {
DCHECK(io_buffer->data()); DCHECK(io_buffer->data());
return base::ReadPlatformFileAtCurrentPos(file_, return base::ReadPlatformFileAtCurrentPos(file_, io_buffer->data(),
io_buffer->data(),
io_buffer->size()); io_buffer->size());
} }
...@@ -65,8 +61,8 @@ int SerialConnection::Write(scoped_refptr<net::IOBuffer> io_buffer, ...@@ -65,8 +61,8 @@ int SerialConnection::Write(scoped_refptr<net::IOBuffer> io_buffer,
int byte_count) { int byte_count) {
DCHECK(io_buffer->data()); DCHECK(io_buffer->data());
DCHECK(byte_count >= 0); DCHECK(byte_count >= 0);
return base::WritePlatformFileAtCurrentPos(file_, return base::WritePlatformFileAtCurrentPos(file_, io_buffer->data(),
io_buffer->data(), byte_count); byte_count);
} }
void SerialConnection::Flush() { void SerialConnection::Flush() {
......
...@@ -16,8 +16,7 @@ namespace extensions { ...@@ -16,8 +16,7 @@ namespace extensions {
const char kSocketTypeNotSupported[] = "Socket type does not support this API"; const char kSocketTypeNotSupported[] = "Socket type does not support this API";
Socket::Socket(const std::string& owner_extension_id) Socket::Socket(const std::string& owner_extension_id)
: ApiResource(owner_extension_id, NULL), : ApiResource(owner_extension_id), is_connected_(false) {
is_connected_(false) {
} }
Socket::~Socket() { Socket::~Socket() {
......
...@@ -295,7 +295,8 @@ void UsbFindDevicesFunction::AsyncWorkStart() { ...@@ -295,7 +295,8 @@ void UsbFindDevicesFunction::AsyncWorkStart() {
if (device_for_test_) { if (device_for_test_) {
UsbDeviceResource* const resource = new UsbDeviceResource( UsbDeviceResource* const resource = new UsbDeviceResource(
extension_->id(), NULL, device_for_test_); extension_->id(),
device_for_test_);
Device device; Device device;
result->Append(PopulateDevice(manager_->Add(resource), 0, 0)); result->Append(PopulateDevice(manager_->Add(resource), 0, 0));
...@@ -327,7 +328,7 @@ void UsbFindDevicesFunction::AsyncWorkStart() { ...@@ -327,7 +328,7 @@ void UsbFindDevicesFunction::AsyncWorkStart() {
for (size_t i = 0; i < devices.size(); ++i) { for (size_t i = 0; i < devices.size(); ++i) {
UsbDevice* const device = devices[i]; UsbDevice* const device = devices[i];
UsbDeviceResource* const resource = new UsbDeviceResource( UsbDeviceResource* const resource = new UsbDeviceResource(
extension_->id(), NULL, device); extension_->id(), device);
Device js_device; Device js_device;
result->Append(PopulateDevice(manager_->Add(resource), result->Append(PopulateDevice(manager_->Add(resource),
......
...@@ -18,7 +18,6 @@ class UsbDevice; ...@@ -18,7 +18,6 @@ class UsbDevice;
namespace extensions { namespace extensions {
class ApiResourceEventNotifier;
class UsbDeviceResource; class UsbDeviceResource;
class UsbAsyncApiFunction : public AsyncApiFunction { class UsbAsyncApiFunction : public AsyncApiFunction {
......
...@@ -10,7 +10,6 @@ ...@@ -10,7 +10,6 @@
#include "base/bind.h" #include "base/bind.h"
#include "base/bind_helpers.h" #include "base/bind_helpers.h"
#include "base/synchronization/lock.h" #include "base/synchronization/lock.h"
#include "chrome/browser/extensions/api/api_resource_event_notifier.h"
#include "chrome/browser/extensions/api/api_resource.h" #include "chrome/browser/extensions/api/api_resource.h"
#include "chrome/browser/usb/usb_device.h" #include "chrome/browser/usb/usb_device.h"
#include "chrome/common/extensions/api/usb.h" #include "chrome/common/extensions/api/usb.h"
...@@ -18,9 +17,8 @@ ...@@ -18,9 +17,8 @@
namespace extensions { namespace extensions {
UsbDeviceResource::UsbDeviceResource(const std::string& owner_extension_id, UsbDeviceResource::UsbDeviceResource(const std::string& owner_extension_id,
ApiResourceEventNotifier* notifier,
scoped_refptr<UsbDevice> device) scoped_refptr<UsbDevice> device)
: ApiResource(owner_extension_id, notifier), device_(device) {} : ApiResource(owner_extension_id), device_(device) {}
UsbDeviceResource::~UsbDeviceResource() {} UsbDeviceResource::~UsbDeviceResource() {}
......
...@@ -23,13 +23,10 @@ class IOBuffer; ...@@ -23,13 +23,10 @@ class IOBuffer;
namespace extensions { namespace extensions {
class ApiResourceEventNotifier;
// A UsbDeviceResource is an ApiResource wrapper for a UsbDevice. // A UsbDeviceResource is an ApiResource wrapper for a UsbDevice.
class UsbDeviceResource : public ApiResource { class UsbDeviceResource : public ApiResource {
public: public:
UsbDeviceResource(const std::string& owner_extension_id, UsbDeviceResource(const std::string& owner_extension_id,
ApiResourceEventNotifier* notifier,
scoped_refptr<UsbDevice> device); scoped_refptr<UsbDevice> device);
virtual ~UsbDeviceResource(); virtual ~UsbDeviceResource();
......
...@@ -67,8 +67,6 @@ ...@@ -67,8 +67,6 @@
'browser/extensions/api/api_resource.cc', 'browser/extensions/api/api_resource.cc',
'browser/extensions/api/api_resource.h', 'browser/extensions/api/api_resource.h',
'browser/extensions/api/api_resource_manager.h', 'browser/extensions/api/api_resource_manager.h',
'browser/extensions/api/api_resource_event_notifier.cc',
'browser/extensions/api/api_resource_event_notifier.h',
'browser/extensions/api/alarms/alarm_manager.cc', 'browser/extensions/api/alarms/alarm_manager.cc',
'browser/extensions/api/alarms/alarm_manager.h', 'browser/extensions/api/alarms/alarm_manager.h',
'browser/extensions/api/alarms/alarms_api.cc', 'browser/extensions/api/alarms/alarms_api.cc',
......
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