summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos
diff options
context:
space:
mode:
authorsky@chromium.org <sky@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-11-11 01:10:45 +0000
committersky@chromium.org <sky@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-11-11 01:10:45 +0000
commitb4037d13ad107e6fc72e1149ed4f760e08aedd9a (patch)
tree370e5a60e694bab5d7e6dc04010c5c2a966fd6d0 /chrome/browser/chromeos
parente896a4d2af4d4c15aeeeb66fb2b18950f777f103 (diff)
downloadchromium_src-b4037d13ad107e6fc72e1149ed4f760e08aedd9a.zip
chromium_src-b4037d13ad107e6fc72e1149ed4f760e08aedd9a.tar.gz
chromium_src-b4037d13ad107e6fc72e1149ed4f760e08aedd9a.tar.bz2
Lands http://codereview.chromium.org/379007 for Dave:
Fixes leak in BackingStore as shown by valgrind. XFree doesn't free the data of the image, where as XDestroyImage does. BUG=none TEST=none Review URL: http://codereview.chromium.org/391008 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@31633 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/chromeos')
-rw-r--r--chrome/browser/chromeos/mount_library.cc98
-rw-r--r--chrome/browser/chromeos/mount_library.h91
-rw-r--r--chrome/browser/chromeos/usb_mount_observer.cc91
-rw-r--r--chrome/browser/chromeos/usb_mount_observer.h61
4 files changed, 341 insertions, 0 deletions
diff --git a/chrome/browser/chromeos/mount_library.cc b/chrome/browser/chromeos/mount_library.cc
new file mode 100644
index 0000000..1c99b97
--- /dev/null
+++ b/chrome/browser/chromeos/mount_library.cc
@@ -0,0 +1,98 @@
+// Copyright (c) 2009 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/mount_library.h"
+
+#include "base/message_loop.h"
+#include "base/string_util.h"
+#include "chrome/browser/chrome_thread.h"
+#include "chrome/browser/chromeos/cros_library.h"
+
+// Allows InvokeLater without adding refcounting. This class is a Singleton and
+// won't be deleted until it's last InvokeLater is run.
+template <>
+struct RunnableMethodTraits<chromeos::MountLibrary> {
+ void RetainCallee(chromeos::MountLibrary* obj) {}
+ void ReleaseCallee(chromeos::MountLibrary* obj) {}
+};
+
+namespace chromeos {
+
+// static
+MountLibrary* MountLibrary::Get() {
+ return Singleton<MountLibrary>::get();
+}
+
+// static
+bool MountLibrary::loaded() {
+ return CrosLibrary::loaded();
+}
+
+void MountLibrary::AddObserver(Observer* observer) {
+ observers_.AddObserver(observer);
+}
+
+void MountLibrary::RemoveObserver(Observer* observer) {
+ observers_.RemoveObserver(observer);
+}
+
+void MountLibrary::ParseDisks(const MountStatus& status) {
+ disks_.clear();
+ for (int i = 0; i < status.size; i++) {
+ std::string path;
+ std::string mountpath;
+ if (status.disks[i].path != NULL) {
+ path = status.disks[i].path;
+ }
+ if (status.disks[i].mountpath != NULL) {
+ mountpath = status.disks[i].mountpath;
+ }
+ disks_.push_back(Disk(path, mountpath));
+ }
+}
+
+MountLibrary::MountLibrary() {
+ if (CrosLibrary::loaded()) {
+ Init();
+ } else {
+ LOG(ERROR) << "Cros Library has not been loaded";
+ }
+}
+
+MountLibrary::~MountLibrary() {
+ if (CrosLibrary::loaded()) {
+ DisconnectMountStatus(mount_status_connection_);
+ }
+}
+
+// static
+void MountLibrary::MountStatusChangedHandler(void* object,
+ const MountStatus& status,
+ MountEventType evt,
+ const char* path) {
+ MountLibrary* mount = static_cast<MountLibrary*>(object);
+ std::string devicepath = path;
+ mount->ParseDisks(status);
+ mount->UpdateMountStatus(status, evt, devicepath);
+}
+
+void MountLibrary::Init() {
+ // Getting the monitor status so that the daemon starts up.
+ MountStatus* mount = RetrieveMountInformation();
+ FreeMountStatus(mount);
+
+ mount_status_connection_ = MonitorMountStatus(
+ &MountStatusChangedHandler, this);
+}
+
+void MountLibrary::UpdateMountStatus(const MountStatus& status,
+ MountEventType evt,
+ const std::string& path) {
+ // Make sure we run on UI thread.
+ DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
+
+ FOR_EACH_OBSERVER(Observer, observers_, MountChanged(this, evt, path));
+}
+
+} // namespace chromeos
diff --git a/chrome/browser/chromeos/mount_library.h b/chrome/browser/chromeos/mount_library.h
new file mode 100644
index 0000000..91b6059
--- /dev/null
+++ b/chrome/browser/chromeos/mount_library.h
@@ -0,0 +1,91 @@
+// Copyright (c) 2009 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_MOUNT_LIBRARY_H_
+#define CHROME_BROWSER_CHROMEOS_MOUNT_LIBRARY_H_
+
+#include <string>
+#include <vector>
+
+#include "base/observer_list.h"
+#include "base/singleton.h"
+#include "base/time.h"
+#include "third_party/cros/chromeos_mount.h"
+
+namespace chromeos {
+
+// This class handles the interaction with the ChromeOS mount library APIs.
+// Classes can add themselves as observers. Users can get an instance of this
+// library class like this: MountLibrary::Get().
+class MountLibrary {
+ public:
+ // Used to house an instance of each found mount device.
+ struct Disk {
+ Disk() {}
+ Disk(const std::string& devicepath, const std::string& mountpath)
+ : device_path(devicepath),
+ mount_path(mountpath) {}
+ std::string device_path;
+ std::string mount_path;
+ };
+ typedef std::vector<Disk> DiskVector;
+
+ class Observer {
+ public:
+ virtual void MountChanged(MountLibrary* obj,
+ MountEventType evt,
+ const std::string& path) = 0;
+ };
+
+ // This gets the singleton MountLibrary
+ static MountLibrary* Get();
+
+ // Returns true if the ChromeOS library was loaded.
+ static bool loaded();
+
+ void AddObserver(Observer* observer);
+ void RemoveObserver(Observer* observer);
+
+ const DiskVector& disks() const { return disks_; }
+
+ private:
+ friend struct DefaultSingletonTraits<MountLibrary>;
+
+ void ParseDisks(const MountStatus& status);
+
+ MountLibrary();
+ ~MountLibrary();
+
+ // This method is called when there's a change in mount status.
+ // This method is called the UI Thread.
+ static void MountStatusChangedHandler(void* object,
+ const MountStatus& status,
+ MountEventType evt,
+ const char* path);
+
+ // This methods starts the monitoring of mount changes.
+ // It should be called on the UI Thread.
+ void Init();
+
+ // Called by the handler to update the mount status.
+ // This will notify all the Observers.
+ void UpdateMountStatus(const MountStatus& status,
+ MountEventType evt,
+ const std::string& path);
+
+ ObserverList<Observer> observers_;
+
+ // A reference to the mount api, to allow callbacks when the mount
+ // status changes.
+ MountStatusConnection mount_status_connection_;
+
+ // The list of disks found.
+ DiskVector disks_;
+
+ DISALLOW_COPY_AND_ASSIGN(MountLibrary);
+};
+
+} // namespace chromeos
+
+#endif // CHROME_BROWSER_CHROMEOS_MOUNT_LIBRARY_H_
diff --git a/chrome/browser/chromeos/usb_mount_observer.cc b/chrome/browser/chromeos/usb_mount_observer.cc
new file mode 100644
index 0000000..177aaf1
--- /dev/null
+++ b/chrome/browser/chromeos/usb_mount_observer.cc
@@ -0,0 +1,91 @@
+// Copyright (c) 2009 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/browser.h"
+#include "chrome/browser/browser_window.h"
+#include "chrome/browser/chromeos/usb_mount_observer.h"
+
+namespace chromeos {
+
+void USBMountObserver::Observe(NotificationType type,
+ const NotificationSource& source,
+ const NotificationDetails& details) {
+ DCHECK(type == NotificationType::BROWSER_CLOSED);
+ for (BrowserIterator i = browsers_.begin(); i != browsers_.end();
+ ++i) {
+ if (Source<Browser>(source).ptr() == i->browser) {
+ browsers_.erase(i);
+ registrar_.Remove(this,
+ NotificationType::BROWSER_CLOSED,
+ source);
+ return;
+ }
+ }
+}
+
+void USBMountObserver::MountChanged(chromeos::MountLibrary* obj,
+ chromeos::MountEventType evt,
+ const std::string& path) {
+ if (evt == chromeos::DISK_ADDED) {
+ // Return since disk added doesn't mean anything until
+ // its mounted, which is a change event.
+ } else if (evt == chromeos::DISK_REMOVED) {
+ RemoveBrowserFromVector(path);
+ } else if (evt == chromeos::DISK_CHANGED) {
+ BrowserIterator iter = FindBrowserForPath(path);
+ if (iter == browsers_.end()) {
+ // We don't currently have this one, so it must have been
+ // mounted
+ const chromeos::MountLibrary::DiskVector& disks = obj->disks();
+ for (size_t i = 0; i < disks.size(); ++i) {
+ if (disks[i].device_path == path) {
+ if (!disks[i].mount_path.empty()) {
+ Browser* browser = Browser::CreateForPopup(profile_);
+ std::string url = "chrome://filebrowse#";
+ url += disks[i].mount_path;
+ browser->AddTabWithURL(
+ GURL(url), GURL(), PageTransition::START_PAGE,
+ true, -1, false, NULL);
+ browser->window()->SetBounds(gfx::Rect(0, 0, 250, 300));
+ registrar_.Add(this,
+ NotificationType::BROWSER_CLOSED,
+ Source<Browser>(browser));
+ browser->window()->Show();
+ BrowserWithPath new_browser;
+ new_browser.browser = browser;
+ new_browser.device_path = disks[i].device_path;
+ browsers_.push_back(new_browser);
+ }
+ return;
+ }
+ }
+ }
+ }
+}
+
+USBMountObserver::BrowserIterator USBMountObserver::FindBrowserForPath(
+ const std::string& path) {
+ for (BrowserIterator i = browsers_.begin();i != browsers_.end();
+ ++i) {
+ if (i->device_path == path) {
+ return i;
+ }
+ }
+ return browsers_.end();
+}
+
+void USBMountObserver::RemoveBrowserFromVector(const std::string& path) {
+ BrowserIterator i = FindBrowserForPath(path);
+ if (i != browsers_.end()) {
+ registrar_.Remove(this,
+ NotificationType::BROWSER_CLOSED,
+ Source<Browser>(i->browser));
+ if (i->browser->window()) {
+ i->browser->window()->Close();
+ }
+ browsers_.erase(i);
+ }
+}
+
+} // namespace chromeos
diff --git a/chrome/browser/chromeos/usb_mount_observer.h b/chrome/browser/chromeos/usb_mount_observer.h
new file mode 100644
index 0000000..c6cb2b9
--- /dev/null
+++ b/chrome/browser/chromeos/usb_mount_observer.h
@@ -0,0 +1,61 @@
+// Copyright (c) 2009 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_USB_MOUNT_OBSERVER_H_
+#define CHROME_BROWSER_CHROMEOS_USB_MOUNT_OBSERVER_H_
+
+#include <string>
+#include <vector>
+
+#include "chrome/browser/chromeos/mount_library.h"
+#include "chrome/common/notification_observer.h"
+#include "chrome/common/notification_source.h"
+#include "chrome/common/notification_type.h"
+#include "chrome/common/notification_registrar.h"
+
+class Browser;
+class Profile;
+
+namespace chromeos { // NOLINT
+
+// Used to monitor mount changes and popup a new window when
+// a new mounted usb device is found.
+class USBMountObserver : public chromeos::MountLibrary::Observer,
+ public NotificationObserver {
+ public:
+ struct BrowserWithPath {
+ Browser* browser;
+ std::string device_path;
+ };
+
+ USBMountObserver() {}
+ ~USBMountObserver() {}
+
+ void set_profile(Profile* profile) { profile_ = profile; }
+
+ static USBMountObserver* Get() {
+ return Singleton<USBMountObserver>::get();
+ }
+ void Observe(NotificationType type,
+ const NotificationSource& source,
+ const NotificationDetails& details);
+
+ void MountChanged(chromeos::MountLibrary* obj,
+ chromeos::MountEventType evt,
+ const std::string& path);
+ private:
+ typedef std::vector<BrowserWithPath>::iterator BrowserIterator;
+ BrowserIterator FindBrowserForPath(const std::string& path);
+
+ void RemoveBrowserFromVector(const std::string& path);
+
+ Profile* profile_;
+ std::vector<BrowserWithPath> browsers_;
+ NotificationRegistrar registrar_;
+
+ DISALLOW_COPY_AND_ASSIGN(USBMountObserver);
+};
+
+} // namespace chromeos
+#endif // CHROME_BROWSER_CHROMEOS_USB_MOUNT_OBSERVER_H_