OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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 "chrome/browser/views/extensions/browser_action_drag_data.h" |
| 6 |
| 7 #include "base/file_path.h" |
| 8 #include "base/logging.h" |
| 9 #include "base/pickle.h" |
| 10 #include "base/string_util.h" |
| 11 #include "chrome/browser/profile.h" |
| 12 |
| 13 const char* BrowserActionDragData::kClipboardFormatString = |
| 14 "chromium/x-browser-actions"; |
| 15 |
| 16 BrowserActionDragData::BrowserActionDragData() |
| 17 : index_(-1) { |
| 18 } |
| 19 |
| 20 BrowserActionDragData::BrowserActionDragData( |
| 21 const std::string& id, int index) |
| 22 : id_(id), |
| 23 index_(index) { |
| 24 } |
| 25 |
| 26 bool BrowserActionDragData::IsFromProfile(Profile* profile) const { |
| 27 // An empty path means the data is not associated with any profile. |
| 28 return (!profile_path_.empty() && |
| 29 profile_path_ == profile->GetPath().value()); |
| 30 } |
| 31 |
| 32 #if defined(TOOLKIT_VIEWS) |
| 33 void BrowserActionDragData::Write( |
| 34 Profile* profile, OSExchangeData* data) const { |
| 35 DCHECK(data); |
| 36 Pickle data_pickle; |
| 37 WriteToPickle(profile, &data_pickle); |
| 38 data->SetPickledData(GetBrowserActionCustomFormat(), data_pickle); |
| 39 } |
| 40 |
| 41 bool BrowserActionDragData::Read(const OSExchangeData& data) { |
| 42 if (!data.HasCustomFormat(GetBrowserActionCustomFormat())) |
| 43 return false; |
| 44 |
| 45 Pickle drag_data_pickle; |
| 46 if (!data.GetPickledData(GetBrowserActionCustomFormat(), &drag_data_pickle)) |
| 47 return false; |
| 48 |
| 49 if (!ReadFromPickle(&drag_data_pickle)) |
| 50 return false; |
| 51 |
| 52 return true; |
| 53 } |
| 54 |
| 55 // static |
| 56 OSExchangeData::CustomFormat |
| 57 BrowserActionDragData::GetBrowserActionCustomFormat() { |
| 58 static OSExchangeData::CustomFormat format; |
| 59 static bool format_valid = false; |
| 60 |
| 61 if (!format_valid) { |
| 62 format_valid = true; |
| 63 format = OSExchangeData::RegisterCustomFormat( |
| 64 BrowserActionDragData::kClipboardFormatString); |
| 65 } |
| 66 return format; |
| 67 } |
| 68 #endif |
| 69 |
| 70 void BrowserActionDragData::WriteToPickle( |
| 71 Profile* profile, Pickle* pickle) const { |
| 72 FilePath::WriteStringTypeToPickle(pickle, profile->GetPath().value()); |
| 73 pickle->WriteString(id_); |
| 74 pickle->WriteInt(index_); |
| 75 } |
| 76 |
| 77 bool BrowserActionDragData::ReadFromPickle(Pickle* pickle) { |
| 78 void* data_iterator = NULL; |
| 79 if (!FilePath::ReadStringTypeFromPickle(pickle, &data_iterator, |
| 80 &profile_path_)) { |
| 81 return false; |
| 82 } |
| 83 |
| 84 if (!pickle->ReadString(&data_iterator, &id_)) |
| 85 return false; |
| 86 |
| 87 int index; |
| 88 if (!pickle->ReadInt(&data_iterator, &index)) |
| 89 return false; |
| 90 |
| 91 index_ = index; |
| 92 return true; |
| 93 } |
OLD | NEW |