Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(26)

Unified Diff: chrome/browser/media_gallery/media_device_notifications_linux_unittest.cc

Issue 10105037: Replace string copy code and scoped_arrays() with const_casts in MediaDeviceNotificationsLinuxTest. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/browser/media_gallery/media_device_notifications_linux_unittest.cc
===================================================================
--- chrome/browser/media_gallery/media_device_notifications_linux_unittest.cc (revision 132699)
+++ chrome/browser/media_gallery/media_device_notifications_linux_unittest.cc (working copy)
@@ -39,19 +39,6 @@
const char kMountPointA[] = "mnt_a";
const char kMountPointB[] = "mnt_b";
-// TODO(thestig) Move this into base/string_util.h, or replace this with
-// strndup_with_new() if we find more uses for it.
-// Duplicate the content of |str| into a new char array. Caller takes ownership
-// of the allocated array. Unlike std::string::c_str(), this returns a char*
-// instead of a const char*.
-char* copy_string(const std::string& str) {
- const size_t len = str.length();
- char* ret = new char[len + 1];
- str.copy(ret, len, 0);
- ret[len] = '\0';
- return ret;
-}
-
class MediaDeviceNotificationsLinuxTestWrapper
: public MediaDeviceNotificationsLinux {
public:
@@ -198,18 +185,27 @@
FILE* file = setmntent(mtab_file_.value().c_str(), overwrite ? "w" : "a");
ASSERT_TRUE(file);
+ // Due to the glibc *mntent() design, which is out of our control, the
+ // mtnent struct has several char* fields, even though we do not write to
vandebo (ex-Chrome) 2012/04/18 00:01:50 nit: we -> it
+ // them in the calls below. To make the compiler happy while avoiding making
+ // additional copies of strings, we just const_cast() the strings' c_str()s.
+ // This is safe since addmntent() does not write to the char* fields.
+ // In case there is an addmntent() implementation that does change the
+ // fields in question, it will crash if it tries to wrote to the mnt_opts
vandebo (ex-Chrome) 2012/04/18 00:01:50 nit: wrote -> write
+ // field. If such an implementation writes to the other char* fields, that
+ // is ok because each MtabTestData struct is used only once. All the fields
+ // in the MtabTestData structs are constructed from C strings and not from
+ // other std::strings, so there is no std::string COW issues to worry about
+ // either.
mntent entry;
- scoped_array<char> mount_opts(copy_string("rw"));
- entry.mnt_opts = mount_opts.get();
+ static const char kMountOpts[] = "rw";
+ entry.mnt_opts = const_cast<char*>(kMountOpts);
entry.mnt_freq = 0;
entry.mnt_passno = 0;
for (size_t i = 0; i < data_size; ++i) {
- scoped_array<char> mount_device(copy_string(data[i].mount_device));
- scoped_array<char> mount_point(copy_string(data[i].mount_point));
- scoped_array<char> mount_type(copy_string(data[i].mount_type));
- entry.mnt_fsname = mount_device.get();
- entry.mnt_dir = mount_point.get();
- entry.mnt_type = mount_type.get();
+ entry.mnt_fsname = const_cast<char*>(data[i].mount_device.c_str());
+ entry.mnt_dir = const_cast<char*>(data[i].mount_point.c_str());
+ entry.mnt_type = const_cast<char*>(data[i].mount_type.c_str());
ASSERT_EQ(0, addmntent(file, &entry));
}
ASSERT_EQ(1, endmntent(file));
@@ -337,9 +333,13 @@
// Detach |kDevice1| from |kMountPointB|.
// kDevice1 -> kMountPointA
// kDevice2 -> kMountPointB
+ MtabTestData test_data5[] = {
+ MtabTestData(kDevice1, test_path_a.value(), kValidFS),
+ MtabTestData(kDevice2, test_path_b.value(), kValidFS),
+ };
EXPECT_CALL(observer(), OnMediaDeviceAttached(_, _, _)).Times(2);
EXPECT_CALL(observer(), OnMediaDeviceDetached(_)).Times(1);
- OverwriteMtabAndRunLoop(test_data1, arraysize(test_data1));
+ OverwriteMtabAndRunLoop(test_data5, arraysize(test_data5));
// Detach all devices.
EXPECT_CALL(observer(), OnMediaDeviceAttached(_, _, _)).Times(0);
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698