1// Copyright (c) 2012 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5#include "ui/base/dragdrop/os_exchange_data_provider_aura.h" 6 7#include "base/logging.h" 8#include "base/strings/utf_string_conversions.h" 9#include "net/base/net_util.h" 10#include "ui/base/clipboard/clipboard.h" 11#include "ui/base/clipboard/scoped_clipboard_writer.h" 12 13namespace ui { 14 15OSExchangeDataProviderAura::OSExchangeDataProviderAura() 16 : formats_(0) { 17} 18 19OSExchangeDataProviderAura::~OSExchangeDataProviderAura() {} 20 21OSExchangeData::Provider* OSExchangeDataProviderAura::Clone() const { 22 OSExchangeDataProviderAura* ret = new OSExchangeDataProviderAura(); 23 ret->formats_ = formats_; 24 ret->string_ = string_; 25 ret->url_ = url_; 26 ret->title_ = title_; 27 ret->filenames_ = filenames_; 28 ret->pickle_data_ = pickle_data_; 29 // We skip copying the drag images. 30 ret->html_ = html_; 31 ret->base_url_ = base_url_; 32 33 return ret; 34} 35 36void OSExchangeDataProviderAura::SetString(const base::string16& data) { 37 string_ = data; 38 formats_ |= OSExchangeData::STRING; 39} 40 41void OSExchangeDataProviderAura::SetURL(const GURL& url, 42 const base::string16& title) { 43 url_ = url; 44 title_ = title; 45 formats_ |= OSExchangeData::URL; 46 47 SetString(UTF8ToUTF16(url.spec())); 48} 49 50void OSExchangeDataProviderAura::SetFilename(const base::FilePath& path) { 51 filenames_.clear(); 52 filenames_.push_back(OSExchangeData::FileInfo(path, base::FilePath())); 53 formats_ |= OSExchangeData::FILE_NAME; 54} 55 56void OSExchangeDataProviderAura::SetFilenames( 57 const std::vector<OSExchangeData::FileInfo>& filenames) { 58 filenames_ = filenames; 59 formats_ |= OSExchangeData::FILE_NAME; 60} 61 62void OSExchangeDataProviderAura::SetPickledData( 63 const OSExchangeData::CustomFormat& format, 64 const Pickle& data) { 65 pickle_data_[format] = data; 66 formats_ |= OSExchangeData::PICKLED_DATA; 67} 68 69bool OSExchangeDataProviderAura::GetString(base::string16* data) const { 70 if ((formats_ & OSExchangeData::STRING) == 0) 71 return false; 72 *data = string_; 73 return true; 74} 75 76bool OSExchangeDataProviderAura::GetURLAndTitle( 77 OSExchangeData::FilenameToURLPolicy policy, 78 GURL* url, 79 base::string16* title) const { 80 // TODO(dcheng): implement filename conversion. 81 if ((formats_ & OSExchangeData::URL) == 0) { 82 title->clear(); 83 return GetPlainTextURL(url); 84 } 85 86 if (!url_.is_valid()) 87 return false; 88 89 *url = url_; 90 *title = title_; 91 return true; 92} 93 94bool OSExchangeDataProviderAura::GetFilename(base::FilePath* path) const { 95 if ((formats_ & OSExchangeData::FILE_NAME) == 0) 96 return false; 97 DCHECK(!filenames_.empty()); 98 *path = filenames_[0].path; 99 return true; 100} 101 102bool OSExchangeDataProviderAura::GetFilenames( 103 std::vector<OSExchangeData::FileInfo>* filenames) const { 104 if ((formats_ & OSExchangeData::FILE_NAME) == 0) 105 return false; 106 *filenames = filenames_; 107 return true; 108} 109 110bool OSExchangeDataProviderAura::GetPickledData( 111 const OSExchangeData::CustomFormat& format, 112 Pickle* data) const { 113 PickleData::const_iterator i = pickle_data_.find(format); 114 if (i == pickle_data_.end()) 115 return false; 116 117 *data = i->second; 118 return true; 119} 120 121bool OSExchangeDataProviderAura::HasString() const { 122 return (formats_ & OSExchangeData::STRING) != 0; 123} 124 125bool OSExchangeDataProviderAura::HasURL() const { 126 if ((formats_ & OSExchangeData::URL) != 0) { 127 return true; 128 } 129 // No URL, see if we have plain text that can be parsed as a URL. 130 return GetPlainTextURL(NULL); 131} 132 133bool OSExchangeDataProviderAura::HasFile() const { 134 return (formats_ & OSExchangeData::FILE_NAME) != 0; 135} 136 137bool OSExchangeDataProviderAura::HasCustomFormat( 138 const OSExchangeData::CustomFormat& format) const { 139 return pickle_data_.find(format) != pickle_data_.end(); 140} 141 142void OSExchangeDataProviderAura::SetHtml(const base::string16& html, 143 const GURL& base_url) { 144 formats_ |= OSExchangeData::HTML; 145 html_ = html; 146 base_url_ = base_url; 147} 148 149bool OSExchangeDataProviderAura::GetHtml(base::string16* html, 150 GURL* base_url) const { 151 if ((formats_ & OSExchangeData::HTML) == 0) 152 return false; 153 *html = html_; 154 *base_url = base_url_; 155 return true; 156} 157 158bool OSExchangeDataProviderAura::HasHtml() const { 159 return ((formats_ & OSExchangeData::HTML) != 0); 160} 161 162void OSExchangeDataProviderAura::SetDragImage( 163 const gfx::ImageSkia& image, 164 const gfx::Vector2d& cursor_offset) { 165 drag_image_ = image; 166 drag_image_offset_ = cursor_offset; 167} 168 169const gfx::ImageSkia& OSExchangeDataProviderAura::GetDragImage() const { 170 return drag_image_; 171} 172 173const gfx::Vector2d& 174OSExchangeDataProviderAura::GetDragImageOffset() const { 175 return drag_image_offset_; 176} 177 178bool OSExchangeDataProviderAura::GetPlainTextURL(GURL* url) const { 179 if ((formats_ & OSExchangeData::STRING) == 0) 180 return false; 181 182 GURL test_url(string_); 183 if (!test_url.is_valid()) 184 return false; 185 186 if (url) 187 *url = test_url; 188 return true; 189} 190 191/////////////////////////////////////////////////////////////////////////////// 192// OSExchangeData, public: 193 194// static 195OSExchangeData::Provider* OSExchangeData::CreateProvider() { 196 return new OSExchangeDataProviderAura(); 197} 198 199} // namespace ui 200