-// Copyright (c) 2013 The Chromium Authors. All rights reserved.
+// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <map>
-#include "base/macros.h"
+#include "base/memory/raw_ptr.h"
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "dbus/object_path.h"
// override {
// Properties* properties = new Properties(
// object_proxy, interface_name,
-// base::Bind(&PropertyChanged,
-// weak_ptr_factory_.GetWeakPtr(),
-// object_path));
+// base::BindRepeating(&PropertyChanged,
+// weak_ptr_factory_.GetWeakPtr(),
+// object_path));
// return static_cast<dbus::PropertySet*>(properties);
// }
//
// ObjectManager implements both the D-Bus client components of the D-Bus
// Object Manager interface, as internal methods, and a public API for
// client classes to utilize.
-class CHROME_DBUS_EXPORT ObjectManager
+class CHROME_DBUS_EXPORT ObjectManager final
: public base::RefCountedThreadSafe<ObjectManager> {
-public:
+ public:
// ObjectManager::Interface must be implemented by any class wishing to have
// its remote objects managed by an ObjectManager.
class Interface {
};
// Client code should use Bus::GetObjectManager() instead of this constructor.
- ObjectManager(Bus* bus,
- const std::string& service_name,
- const ObjectPath& object_path);
+ static scoped_refptr<ObjectManager> Create(Bus* bus,
+ const std::string& service_name,
+ const ObjectPath& object_path);
+
+ ObjectManager(const ObjectManager&) = delete;
+ ObjectManager& operator=(const ObjectManager&) = delete;
// Register a client implementation class |interface| for the given D-Bus
// interface named in |interface_name|. That object's CreateProperties()
// method will be used to create instances of dbus::PropertySet* when
// required.
- virtual void RegisterInterface(const std::string& interface_name,
- Interface* interface);
+ void RegisterInterface(const std::string& interface_name,
+ Interface* interface);
// Unregister the implementation class for the D-Bus interface named in
// |interface_name|, objects and properties of this interface will be
// ignored.
- virtual void UnregisterInterface(const std::string& interface_name);
+ void UnregisterInterface(const std::string& interface_name);
+
+ // Checks whether an interface is registered.
+ bool IsInterfaceRegisteredForTesting(const std::string& interface_name) const;
// Returns a list of object paths, in an undefined order, of objects known
// to this manager.
- virtual std::vector<ObjectPath> GetObjects();
+ std::vector<ObjectPath> GetObjects();
// Returns the list of object paths, in an undefined order, of objects
// implementing the interface named in |interface_name| known to this manager.
- virtual std::vector<ObjectPath> GetObjectsWithInterface(
+ std::vector<ObjectPath> GetObjectsWithInterface(
const std::string& interface_name);
// Returns a ObjectProxy pointer for the given |object_path|. Unlike
// the equivalent method on Bus this will return NULL if the object
// manager has not been informed of that object's existence.
- virtual ObjectProxy* GetObjectProxy(const ObjectPath& object_path);
+ ObjectProxy* GetObjectProxy(const ObjectPath& object_path);
// Returns a PropertySet* pointer for the given |object_path| and
// |interface_name|, or NULL if the object manager has not been informed of
// that object's existence or the interface's properties. The caller should
// cast the returned pointer to the appropriate type, e.g.:
// static_cast<Properties*>(GetProperties(object_path, my_interface));
- virtual PropertySet* GetProperties(const ObjectPath& object_path,
- const std::string& interface_name);
+ PropertySet* GetProperties(const ObjectPath& object_path,
+ const std::string& interface_name);
// Instructs the object manager to refresh its list of managed objects;
// automatically called by the D-Bus thread manager, there should never be
// BLOCKING CALL.
void CleanUp();
- protected:
- virtual ~ObjectManager();
-
private:
friend class base::RefCountedThreadSafe<ObjectManager>;
+ ObjectManager(Bus* bus,
+ const std::string& service_name,
+ const ObjectPath& object_path);
+ ~ObjectManager();
+
// Called from the constructor to add a match rule for PropertiesChanged
// signals on the D-Bus thread and set up a corresponding filter function.
bool SetupMatchRuleAndFilter();
void NameOwnerChanged(const std::string& old_owner,
const std::string& new_owner);
- Bus* bus_;
+ // Write |new_owner| to |service_name_owner_|. This method makes sure write
+ // happens on the DBus thread, which is the sole writer to
+ // |service_name_owner_|.
+ void UpdateServiceNameOwner(const std::string& new_owner);
+
+ // Valid in between the constructor and `CleanUp()`.
+ // After Cleanup(), `this` lifetime might exceed Bus's one.
+ raw_ptr<Bus> bus_;
std::string service_name_;
std::string service_name_owner_;
std::string match_rule_;
ObjectPath object_path_;
- ObjectProxy* object_proxy_;
- bool setup_success_;
- bool cleanup_called_;
+ raw_ptr<ObjectProxy, AcrossTasksDanglingUntriaged> object_proxy_;
+ bool setup_success_ = false;
+ bool cleanup_called_ = false;
// Maps the name of an interface to the implementation class used for
// instantiating PropertySet structures for that interface's properties.
Object();
~Object();
- ObjectProxy* object_proxy;
+ raw_ptr<ObjectProxy, AcrossTasksDanglingUntriaged> object_proxy;
// Maps the name of an interface to the specific PropertySet structure
// of that interface's properties.
// than we do.
// Note: This should remain the last member so it'll be destroyed and
// invalidate its weak pointers before any other members are destroyed.
- base::WeakPtrFactory<ObjectManager> weak_ptr_factory_;
-
- DISALLOW_COPY_AND_ASSIGN(ObjectManager);
+ base::WeakPtrFactory<ObjectManager> weak_ptr_factory_{this};
};
} // namespace dbus