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