diff options
Diffstat (limited to 'ui/base/clipboard')
-rw-r--r-- | ui/base/clipboard/clipboard_aurax11.cc | 174 |
1 files changed, 82 insertions, 92 deletions
diff --git a/ui/base/clipboard/clipboard_aurax11.cc b/ui/base/clipboard/clipboard_aurax11.cc index 8809d26..287941d 100644 --- a/ui/base/clipboard/clipboard_aurax11.cc +++ b/ui/base/clipboard/clipboard_aurax11.cc @@ -239,7 +239,7 @@ class Clipboard::AuraX11Details : public base::MessagePumpDispatcher { SelectionRequestor* GetSelectionRequestorForBuffer(Buffer buffer); // Finds the SelectionFormatMap for the incoming selection atom. - SelectionFormatMap* LookupStorageForAtom(::Atom atom); + const SelectionFormatMap& LookupStorageForAtom(::Atom atom); // As we need to collect all the data types before we tell X11 that we own a // particular selection, we create a temporary clipboard mapping that @@ -248,7 +248,8 @@ class Clipboard::AuraX11Details : public base::MessagePumpDispatcher { void CreateNewClipboardData(); // Inserts a mapping into clipboard_data_. - void InsertMapping(const std::string& key, char* data, size_t data_len); + void InsertMapping(const std::string& key, + const scoped_refptr<base::RefCountedMemory>& memory); // Moves the temporary |clipboard_data_| to the long term data storage for // |buffer|. @@ -262,9 +263,8 @@ class Clipboard::AuraX11Details : public base::MessagePumpDispatcher { // selection holder is some other window, we spin up a nested message loop // and do the asynchronous dance with whatever application is holding the // selection. - scoped_ptr<SelectionData> RequestAndWaitForTypes( - Buffer buffer, - const std::vector< ::Atom>& types); + ui::SelectionData RequestAndWaitForTypes(Buffer buffer, + const std::vector< ::Atom>& types); // Retrieves the list of possible data types the current clipboard owner has. // @@ -299,7 +299,7 @@ class Clipboard::AuraX11Details : public base::MessagePumpDispatcher { SelectionRequestor primary_requestor_; // Temporary target map that we write to during DispatchObects. - scoped_ptr<SelectionFormatMap> clipboard_data_; + SelectionFormatMap clipboard_data_; // Objects which offer selection data to other windows. SelectionOwner clipboard_owner_; @@ -345,18 +345,17 @@ Clipboard::AuraX11Details::~AuraX11Details() { Buffer buffer) const { if (buffer == BUFFER_STANDARD) return atom_cache_.GetAtom(kClipboard); - else - return XA_PRIMARY; + + return XA_PRIMARY; } -SelectionFormatMap* Clipboard::AuraX11Details::LookupStorageForAtom( +const SelectionFormatMap& Clipboard::AuraX11Details::LookupStorageForAtom( ::Atom atom) { if (atom == XA_PRIMARY) return primary_owner_.selection_format_map(); - else if (atom == atom_cache_.GetAtom(kClipboard)) - return clipboard_owner_.selection_format_map(); - else - return NULL; + + DCHECK_EQ(atom_cache_.GetAtom(kClipboard), atom); + return clipboard_owner_.selection_format_map(); } ui::SelectionRequestor* @@ -368,42 +367,37 @@ Clipboard::AuraX11Details::GetSelectionRequestorForBuffer(Buffer buffer) { } void Clipboard::AuraX11Details::CreateNewClipboardData() { - clipboard_data_.reset(new SelectionFormatMap); + clipboard_data_ = SelectionFormatMap(); } -void Clipboard::AuraX11Details::InsertMapping(const std::string& key, - char* data, - size_t data_len) { +void Clipboard::AuraX11Details::InsertMapping( + const std::string& key, + const scoped_refptr<base::RefCountedMemory>& memory) { ::Atom atom_key = atom_cache_.GetAtom(key.c_str()); - clipboard_data_->Insert(atom_key, data, data_len); + clipboard_data_.Insert(atom_key, memory); } void Clipboard::AuraX11Details::TakeOwnershipOfSelection(Buffer buffer) { if (buffer == BUFFER_STANDARD) - return clipboard_owner_.TakeOwnershipOfSelection(clipboard_data_.Pass()); + return clipboard_owner_.TakeOwnershipOfSelection(clipboard_data_); else - return primary_owner_.TakeOwnershipOfSelection(clipboard_data_.Pass()); + return primary_owner_.TakeOwnershipOfSelection(clipboard_data_); } -scoped_ptr<SelectionData> Clipboard::AuraX11Details::RequestAndWaitForTypes( +SelectionData Clipboard::AuraX11Details::RequestAndWaitForTypes( Buffer buffer, const std::vector< ::Atom>& types) { ::Atom selection_name = LookupSelectionForBuffer(buffer); if (XGetSelectionOwner(x_display_, selection_name) == x_window_) { // We can local fastpath instead of playing the nested message loop game // with the X server. - SelectionFormatMap* format_map = LookupStorageForAtom(selection_name); - DCHECK(format_map); + const SelectionFormatMap& format_map = LookupStorageForAtom(selection_name); for (std::vector< ::Atom>::const_iterator it = types.begin(); it != types.end(); ++it) { - SelectionFormatMap::const_iterator format_map_it = format_map->find(*it); - if (format_map_it != format_map->end()) { - scoped_ptr<SelectionData> data_out(new SelectionData); - data_out->Set(format_map_it->first, format_map_it->second.first, - format_map_it->second.second, false); - return data_out.Pass(); - } + SelectionFormatMap::const_iterator format_map_it = format_map.find(*it); + if (format_map_it != format_map.end()) + return SelectionData(format_map_it->first, format_map_it->second); } } else { TargetList targets = WaitAndGetTargetsList(buffer); @@ -414,7 +408,7 @@ scoped_ptr<SelectionData> Clipboard::AuraX11Details::RequestAndWaitForTypes( return receiver->RequestAndWaitForTypes(intersection); } - return scoped_ptr<SelectionData>(); + return SelectionData(); } TargetList Clipboard::AuraX11Details::WaitAndGetTargetsList( @@ -423,15 +417,14 @@ TargetList Clipboard::AuraX11Details::WaitAndGetTargetsList( std::vector< ::Atom> out; if (XGetSelectionOwner(x_display_, selection_name) == x_window_) { // We can local fastpath and return the list of local targets. - SelectionFormatMap* format_map = LookupStorageForAtom(selection_name); - DCHECK(format_map); + const SelectionFormatMap& format_map = LookupStorageForAtom(selection_name); - for (SelectionFormatMap::const_iterator it = format_map->begin(); - it != format_map->end(); ++it) { + for (SelectionFormatMap::const_iterator it = format_map.begin(); + it != format_map.end(); ++it) { out.push_back(it->first); } } else { - unsigned char* data = NULL; + scoped_refptr<base::RefCountedMemory> data; size_t out_data_items = 0; ::Atom out_type = None; @@ -441,11 +434,9 @@ TargetList Clipboard::AuraX11Details::WaitAndGetTargetsList( NULL, &out_data_items, &out_type)) { - ::Atom* atom_array = reinterpret_cast< ::Atom*>(data); + const ::Atom* atom_array = reinterpret_cast<const ::Atom*>(data->front()); for (size_t i = 0; i < out_data_items; ++i) out.push_back(atom_array[i]); - - XFree(data); } else { // There was no target list. Most Java apps doesn't offer a TARGETS list, // even though they AWT to. They will offer individual text types if you @@ -599,22 +590,20 @@ void Clipboard::ReadAvailableTypes(Buffer buffer, std::vector<string16>* types, types->push_back(UTF8ToUTF16(kMimeTypePNG)); *contains_filenames = false; - scoped_ptr<SelectionData> data(aurax11_details_->RequestAndWaitForTypes( + SelectionData data(aurax11_details_->RequestAndWaitForTypes( buffer, aurax11_details_->GetAtomsForFormat(GetWebCustomDataFormatType()))); - if (!data.get()) - return; - - ReadCustomDataTypes(data->data(), data->size(), types); + if (data.IsValid()) + ReadCustomDataTypes(data.GetData(), data.GetSize(), types); } void Clipboard::ReadText(Buffer buffer, string16* result) const { DCHECK(CalledOnValidThread()); - scoped_ptr<SelectionData> data(aurax11_details_->RequestAndWaitForTypes( + SelectionData data(aurax11_details_->RequestAndWaitForTypes( buffer, aurax11_details_->GetTextAtoms())); - if (data.get()) { - std::string text = data->GetText(); + if (data.IsValid()) { + std::string text = data.GetText(); *result = UTF8ToUTF16(text); } } @@ -622,10 +611,10 @@ void Clipboard::ReadText(Buffer buffer, string16* result) const { void Clipboard::ReadAsciiText(Buffer buffer, std::string* result) const { DCHECK(CalledOnValidThread()); - scoped_ptr<SelectionData> data(aurax11_details_->RequestAndWaitForTypes( + SelectionData data(aurax11_details_->RequestAndWaitForTypes( buffer, aurax11_details_->GetTextAtoms())); - if (data.get()) - result->assign(data->GetText()); + if (data.IsValid()) + result->assign(data.GetText()); } // TODO(estade): handle different charsets. @@ -642,25 +631,24 @@ void Clipboard::ReadHTML(Buffer buffer, *fragment_start = 0; *fragment_end = 0; - scoped_ptr<SelectionData> data(aurax11_details_->RequestAndWaitForTypes( + SelectionData data(aurax11_details_->RequestAndWaitForTypes( buffer, aurax11_details_->GetAtomsForFormat(GetHtmlFormatType()))); - if (!data.get()) - return; + if (data.IsValid()) { + *markup = data.GetHtml(); - *markup = data->GetHtml(); - - *fragment_start = 0; - DCHECK(markup->length() <= kuint32max); - *fragment_end = static_cast<uint32>(markup->length()); + *fragment_start = 0; + DCHECK(markup->length() <= kuint32max); + *fragment_end = static_cast<uint32>(markup->length()); + } } void Clipboard::ReadRTF(Buffer buffer, std::string* result) const { DCHECK(CalledOnValidThread()); - scoped_ptr<SelectionData> data(aurax11_details_->RequestAndWaitForTypes( + SelectionData data(aurax11_details_->RequestAndWaitForTypes( buffer, aurax11_details_->GetAtomsForFormat(GetRtfFormatType()))); - if (data.get()) - data->AssignTo(result); + if (data.IsValid()) + data.AssignTo(result); } SkBitmap Clipboard::ReadImage(Buffer buffer) const { @@ -674,13 +662,11 @@ void Clipboard::ReadCustomData(Buffer buffer, string16* result) const { DCHECK(CalledOnValidThread()); - scoped_ptr<SelectionData> data(aurax11_details_->RequestAndWaitForTypes( + SelectionData data(aurax11_details_->RequestAndWaitForTypes( buffer, aurax11_details_->GetAtomsForFormat(GetWebCustomDataFormatType()))); - if (!data.get()) - return; - - ReadCustomDataForType(data->data(), data->size(), type, result); + if (data.IsValid()) + ReadCustomDataForType(data.GetData(), data.GetSize(), type, result); } void Clipboard::ReadBookmark(string16* title, std::string* url) const { @@ -692,10 +678,10 @@ void Clipboard::ReadBookmark(string16* title, std::string* url) const { void Clipboard::ReadData(const FormatType& format, std::string* result) const { DCHECK(CalledOnValidThread()); - scoped_ptr<SelectionData> data(aurax11_details_->RequestAndWaitForTypes( + SelectionData data(aurax11_details_->RequestAndWaitForTypes( BUFFER_STANDARD, aurax11_details_->GetAtomsForFormat(format))); - if (data.get()) - data->AssignTo(result); + if (data.IsValid()) + data.AssignTo(result); } uint64 Clipboard::GetSequenceNumber(Buffer buffer) { @@ -707,13 +693,14 @@ uint64 Clipboard::GetSequenceNumber(Buffer buffer) { } void Clipboard::WriteText(const char* text_data, size_t text_len) { - char* data = new char[text_len]; - memcpy(data, text_data, text_len); + std::string str(text_data, text_len); + scoped_refptr<base::RefCountedMemory> mem( + base::RefCountedString::TakeString(&str)); - aurax11_details_->InsertMapping(kMimeTypeText, data, text_len); - aurax11_details_->InsertMapping(kText, data, text_len); - aurax11_details_->InsertMapping(kString, data, text_len); - aurax11_details_->InsertMapping(kUtf8String, data, text_len); + aurax11_details_->InsertMapping(kMimeTypeText, mem); + aurax11_details_->InsertMapping(kText, mem); + aurax11_details_->InsertMapping(kString, mem); + aurax11_details_->InsertMapping(kUtf8String, mem); } void Clipboard::WriteHTML(const char* markup_data, @@ -723,16 +710,14 @@ void Clipboard::WriteHTML(const char* markup_data, // TODO(estade): We need to expand relative links with |url_data|. static const char* html_prefix = "<meta http-equiv=\"content-type\" " "content=\"text/html; charset=utf-8\">"; - size_t html_prefix_len = strlen(html_prefix); - size_t total_len = html_prefix_len + markup_len + 1; - - char* data = new char[total_len]; - snprintf(data, total_len, "%s", html_prefix); - memcpy(data + html_prefix_len, markup_data, markup_len); + std::string data = html_prefix; + data += std::string(markup_data, markup_len); // Some programs expect NULL-terminated data. See http://crbug.com/42624 - data[total_len - 1] = '\0'; + data += '\0'; - aurax11_details_->InsertMapping(kMimeTypeHTML, data, total_len); + scoped_refptr<base::RefCountedMemory> mem( + base::RefCountedString::TakeString(&data)); + aurax11_details_->InsertMapping(kMimeTypeHTML, mem); } void Clipboard::WriteRTF(const char* rtf_data, size_t data_len) { @@ -746,18 +731,21 @@ void Clipboard::WriteBookmark(const char* title_data, // Write as a mozilla url (UTF16: URL, newline, title). string16 url = UTF8ToUTF16(std::string(url_data, url_len) + "\n"); string16 title = UTF8ToUTF16(std::string(title_data, title_len)); - int data_len = 2 * (title.length() + url.length()); - char* data = new char[data_len]; - memcpy(data, url.data(), 2 * url.length()); - memcpy(data + 2 * url.length(), title.data(), 2 * title.length()); - aurax11_details_->InsertMapping(kMimeTypeMozillaURL, data, data_len); + std::vector<unsigned char> data; + ui::AddString16ToVector(url, &data); + ui::AddString16ToVector(title, &data); + scoped_refptr<base::RefCountedMemory> mem( + base::RefCountedBytes::TakeVector(&data)); + + aurax11_details_->InsertMapping(kMimeTypeMozillaURL, mem); } // Write an extra flavor that signifies WebKit was the last to modify the // pasteboard. This flavor has no data. void Clipboard::WriteWebSmartPaste() { - aurax11_details_->InsertMapping(kMimeTypeWebkitSmartPaste, NULL, 0); + aurax11_details_->InsertMapping(kMimeTypeWebkitSmartPaste, + scoped_refptr<base::RefCountedMemory>()); } void Clipboard::WriteBitmap(const char* pixel_data, const char* size_data) { @@ -774,9 +762,11 @@ void Clipboard::WriteData(const FormatType& format, // Therefore we must upkeep their integrity. if (format.Equals(GetBitmapFormatType())) return; - char* data = new char[data_len]; - memcpy(data, data_data, data_len); - aurax11_details_->InsertMapping(format.ToString(), data, data_len); + + std::vector<unsigned char> bytes(data_data, data_data + data_len); + scoped_refptr<base::RefCountedMemory> mem( + base::RefCountedBytes::TakeVector(&bytes)); + aurax11_details_->InsertMapping(format.ToString(), mem); } // static |