diff options
author | akalin@chromium.org <akalin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-05-20 04:27:11 +0000 |
---|---|---|
committer | akalin@chromium.org <akalin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-05-20 04:27:11 +0000 |
commit | c291f3ffac92b611e1eff1fcb9037bfbb0950dab (patch) | |
tree | 44b20dd0aa820025b8f54be57bf7f1f6fad26c0d /chrome/common/net | |
parent | e86bec3da344eb5c0c2d3d3ee228944f40ba1f81 (diff) | |
download | chromium_src-c291f3ffac92b611e1eff1fcb9037bfbb0950dab.zip chromium_src-c291f3ffac92b611e1eff1fcb9037bfbb0950dab.tar.gz chromium_src-c291f3ffac92b611e1eff1fcb9037bfbb0950dab.tar.bz2 |
Removed tons of unused code in chrome/common/net/notifier.
BUG=none
TEST=trybots
Review URL: http://codereview.chromium.org/2076008
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@47774 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/common/net')
-rw-r--r-- | chrome/common/net/notifier/base/posix/time_posix.cc | 14 | ||||
-rw-r--r-- | chrome/common/net/notifier/base/time.cc | 30 | ||||
-rw-r--r-- | chrome/common/net/notifier/base/time.h | 10 | ||||
-rw-r--r-- | chrome/common/net/notifier/base/utils.h | 90 | ||||
-rw-r--r-- | chrome/common/net/notifier/base/win/time_win32.cc | 71 | ||||
-rw-r--r-- | chrome/common/net/notifier/communicator/auto_reconnect.cc | 16 | ||||
-rw-r--r-- | chrome/common/net/notifier/communicator/auto_reconnect.h | 1 | ||||
-rw-r--r-- | chrome/common/net/notifier/communicator/login.cc | 4 | ||||
-rw-r--r-- | chrome/common/net/notifier/communicator/login.h | 2 | ||||
-rw-r--r-- | chrome/common/net/notifier/communicator/mailbox.cc | 677 | ||||
-rw-r--r-- | chrome/common/net/notifier/communicator/mailbox.h | 166 | ||||
-rw-r--r-- | chrome/common/net/notifier/communicator/mailbox_unittest.cc | 119 | ||||
-rw-r--r-- | chrome/common/net/notifier/communicator/xmpp_log.cc | 112 | ||||
-rw-r--r-- | chrome/common/net/notifier/communicator/xmpp_log.h | 46 |
14 files changed, 0 insertions, 1358 deletions
diff --git a/chrome/common/net/notifier/base/posix/time_posix.cc b/chrome/common/net/notifier/base/posix/time_posix.cc index a3d63aa..778b096 100644 --- a/chrome/common/net/notifier/base/posix/time_posix.cc +++ b/chrome/common/net/notifier/base/posix/time_posix.cc @@ -23,18 +23,4 @@ time64 GetCurrent100NSTime() { return retval; } -time64 TmToTime64(const struct tm& tm) { - struct tm tm_temp; - memcpy(&tm_temp, &tm, sizeof(struct tm)); - time_t t = timegm(&tm_temp); - return t * kSecsTo100ns; -} - -bool Time64ToTm(time64 t, struct tm* tm) { - DCHECK(tm); - time_t secs = t / kSecsTo100ns; - gmtime_r(&secs, tm); - return true; -} - } // namespace notifier diff --git a/chrome/common/net/notifier/base/time.cc b/chrome/common/net/notifier/base/time.cc deleted file mode 100644 index 1f07ebe..0000000 --- a/chrome/common/net/notifier/base/time.cc +++ /dev/null @@ -1,30 +0,0 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "chrome/common/net/notifier/base/time.h" - -#include <string> -#include <time.h> - -#include "chrome/common/net/notifier/base/utils.h" -#include "talk/base/common.h" -#include "talk/base/logging.h" - -namespace notifier { - -char* GetLocalTimeAsString() { - time64 long_time = GetCurrent100NSTime(); - struct tm now; - Time64ToTm(long_time, &now); - char* time_string = asctime(&now); - if (time_string) { - int time_len = strlen(time_string); - if (time_len > 0) { - time_string[time_len - 1] = 0; // trim off terminating \n. - } - } - return time_string; -} - -} // namespace notifier diff --git a/chrome/common/net/notifier/base/time.h b/chrome/common/net/notifier/base/time.h index 77a68d4..2de8b6d 100644 --- a/chrome/common/net/notifier/base/time.h +++ b/chrome/common/net/notifier/base/time.h @@ -43,16 +43,6 @@ namespace notifier { // the value since Jan 1, 1601. time64 GetCurrent100NSTime(); -// Convert from struct tm to time64. -time64 TmToTime64(const struct tm& tm); - -// Convert from time64 to struct tm. -bool Time64ToTm(time64 t, struct tm* tm); - -// Returns the local time as a string suitable for logging. -// Note: This is *not* threadsafe, so only call it from the main thread. -char* GetLocalTimeAsString(); - } // namespace notifier #endif // CHROME_COMMON_NET_NOTIFIER_BASE_TIME_H_ diff --git a/chrome/common/net/notifier/base/utils.h b/chrome/common/net/notifier/base/utils.h deleted file mode 100644 index fc12943..0000000 --- a/chrome/common/net/notifier/base/utils.h +++ /dev/null @@ -1,90 +0,0 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. -// -// Utility functions. - -#ifndef CHROME_COMMON_NET_NOTIFIER_BASE_UTILS_H_ -#define CHROME_COMMON_NET_NOTIFIER_BASE_UTILS_H_ - -#include <map> -#include <string> - -#include "chrome/common/net/notifier/base/static_assert.h" - -// Return error if the first argument evaluates to false. -#define RET_IF_FALSE(x) do { if (!(x)) return false; } while (false) - -// Protocol constants. -const char kHttpProto[] = "http://"; -const char kHttpsProto[] = "https://"; - -// Initialize a POD to zero. Using this function requires discipline. Don't -// use for types that have a v-table or virtual bases. -template <typename T> -inline void SetZero(T& p) { - // Guard against the easy mistake of - // foo(int *p) { SetZero(p); } instead of - // SetZero(*p); - // which it should be. - STATIC_ASSERT(sizeof(T) != sizeof(void*)); - - // A POD (plain old data) object has one of these data types: - // a fundamental type, union, struct, array, - // or class--with no constructor. PODs don't have virtual functions or - // virtual bases. - - // Test to see if the type has constructors. - union CtorTest { - T t; - int i; - }; - - // TODO(sync) There might be a way to test if the type has virtuals. - // For now, if we zero a type with virtuals by mistake, it is going to crash - // predictable at run-time when the virtuals are called. - memset(&p, 0, sizeof(T)); -} - -// Used to delete each element in a vector<T*>/deque<T*> (and then empty the -// sequence). -template <class T> -void CleanupSequence(T* items) { - for (typename T::iterator it(items->begin()); it != items->end(); ++it) { - delete (*it); - } - items->clear(); -} - -// Typically used to clean up values used in a hash_map that had Type* as -// values. -// -// WARNING: This function assumes that T::clear will not access the values -// (or the keys if they are the same as the values). This is true for -// hash_map. -template <class T> -void CleanupMap(T* items) { - // This is likely slower than a for loop, but we have to do it this way. In - // some of the maps we use, deleting it->second causes it->first to be - // deleted as well, and that seems to send the iterator in a tizzy. - typename T::iterator it = items->begin(); - while (it != items->end()) { - items->erase(it->first); - delete it->second; - it = items->begin(); - } -} - -// Get the value of an element in the map with the specified name. -template <class T> -void GetMapElement(const std::map<const std::string, const T>& m, - const char* name, - T* value) { - typename std::map<const std::string, const T>::const_iterator iter( - m.find(name)); - if (iter != m.end()) { - *value = iter->second; - } -} - -#endif // CHROME_COMMON_NET_NOTIFIER_BASE_UTILS_H_ diff --git a/chrome/common/net/notifier/base/win/time_win32.cc b/chrome/common/net/notifier/base/win/time_win32.cc index b93f01c..520742bd 100644 --- a/chrome/common/net/notifier/base/win/time_win32.cc +++ b/chrome/common/net/notifier/base/win/time_win32.cc @@ -9,10 +9,7 @@ #include "chrome/common/net/notifier/base/time.h" -#include "base/logging.h" -#include "chrome/common/net/notifier/base/utils.h" #include "talk/base/common.h" -#include "talk/base/logging.h" namespace notifier { @@ -21,43 +18,6 @@ time64 FileTimeToTime64(const FILETIME& file_time) { file_time.dwLowDateTime; } -void Time64ToFileTime(const time64& time, FILETIME* ft) { - DCHECK(ft); - - ft->dwHighDateTime = static_cast<DWORD>(time >> 32); - ft->dwLowDateTime = static_cast<DWORD>(time & 0xffffffff); -} - -void TmTimeToSystemTime(const struct tm& tm, SYSTEMTIME* sys_time) { - DCHECK(sys_time); - - SetZero(*sys_time); - // tm's year is 1900 based, systemtime's year is absolute. - sys_time->wYear = tm.tm_year + 1900; - // tm's month is 0 based, but systemtime's month is 1 based. - sys_time->wMonth = tm.tm_mon + 1; - sys_time->wDay = tm.tm_mday; - sys_time->wDayOfWeek = tm.tm_wday; - sys_time->wHour = tm.tm_hour; - sys_time->wMinute = tm.tm_min; - sys_time->wSecond = tm.tm_sec; -} - -void SystemTimeToTmTime(const SYSTEMTIME& sys_time, struct tm* tm) { - DCHECK(tm); - - SetZero(*tm); - // tm's year is 1900 based, systemtime's year is absolute. - tm->tm_year = sys_time.wYear - 1900; - // tm's month is 0 based, but systemtime's month is 1 based. - tm->tm_mon = sys_time.wMonth - 1; - tm->tm_mday = sys_time.wDay; - tm->tm_wday = sys_time.wDayOfWeek; - tm->tm_hour = sys_time.wHour; - tm->tm_min = sys_time.wMinute; - tm->tm_sec = sys_time.wSecond; -} - time64 GetCurrent100NSTime() { // In order to get the 100ns time we shouldn't use SystemTime as it's // granularity is 1 ms. Below is the correct implementation. On the other @@ -71,35 +31,4 @@ time64 GetCurrent100NSTime() { return time; } -time64 TmToTime64(const struct tm& tm) { - SYSTEMTIME sys_time; - TmTimeToSystemTime(tm, &sys_time); - - FILETIME file_time; - SetZero(file_time); - if (!::SystemTimeToFileTime(&sys_time, &file_time)) { - return 0; - } - - return FileTimeToTime64(file_time); -} - -bool Time64ToTm(time64 t, struct tm* tm) { - DCHECK(tm); - - FILETIME file_time; - SetZero(file_time); - Time64ToFileTime(t, &file_time); - - SYSTEMTIME sys_time; - SetZero(sys_time); - if (!::FileTimeToSystemTime(&file_time, &sys_time)) { - return false; - } - - SystemTimeToTmTime(sys_time, tm); - - return true; -} - } // namespace notifier diff --git a/chrome/common/net/notifier/communicator/auto_reconnect.cc b/chrome/common/net/notifier/communicator/auto_reconnect.cc index 393866b..54f0a80 100644 --- a/chrome/common/net/notifier/communicator/auto_reconnect.cc +++ b/chrome/common/net/notifier/communicator/auto_reconnect.cc @@ -4,7 +4,6 @@ #include "chrome/common/net/notifier/communicator/auto_reconnect.h" -#include "chrome/common/net/notifier/base/time.h" #include "chrome/common/net/notifier/base/timer.h" #include "talk/base/common.h" @@ -29,21 +28,6 @@ void AutoReconnect::NetworkStateChanged(bool is_alive) { } } -int AutoReconnect::seconds_until() const { - if (!is_retrying() || !reconnect_timer_->get_timeout_time()) { - return 0; - } - int64 time_until_100ns = - reconnect_timer_->get_timeout_time() - GetCurrent100NSTime(); - if (time_until_100ns < 0) { - return 0; - } - - // Do a ceiling on the value (to avoid returning before its time). - int64 result = (time_until_100ns + kSecsTo100ns - 1) / kSecsTo100ns; - return static_cast<int>(result); -} - void AutoReconnect::StartReconnectTimer() { StartReconnectTimerWithInterval(reconnect_interval_ns_); } diff --git a/chrome/common/net/notifier/communicator/auto_reconnect.h b/chrome/common/net/notifier/communicator/auto_reconnect.h index f7e3582..24043ed 100644 --- a/chrome/common/net/notifier/communicator/auto_reconnect.h +++ b/chrome/common/net/notifier/communicator/auto_reconnect.h @@ -40,7 +40,6 @@ class AutoReconnect : public sigslot::has_slots<> { return reconnect_timer_ != NULL; } - int seconds_until() const; sigslot::signal0<> SignalTimerStartStop; sigslot::signal0<> SignalStartConnection; diff --git a/chrome/common/net/notifier/communicator/login.cc b/chrome/common/net/notifier/communicator/login.cc index 0e55eef..4c382ae 100644 --- a/chrome/common/net/notifier/communicator/login.cc +++ b/chrome/common/net/notifier/communicator/login.cc @@ -239,10 +239,6 @@ buzz::XmppClient* Login::xmpp_client() { return single_attempt_->xmpp_client(); } -int Login::seconds_until_reconnect() const { - return auto_reconnect_->seconds_until(); -} - void Login::UseNextConnection() { if (!single_attempt_) { // Just in case, there is an obscure case that causes this to get called diff --git a/chrome/common/net/notifier/communicator/login.h b/chrome/common/net/notifier/communicator/login.h index b0e841a..4d4cafa 100644 --- a/chrome/common/net/notifier/communicator/login.h +++ b/chrome/common/net/notifier/communicator/login.h @@ -99,8 +99,6 @@ class Login : public net::NetworkChangeNotifier::Observer, // Do not call until StartConnection has been called. const talk_base::ProxyInfo& proxy() const; - int seconds_until_reconnect() const; - virtual void OnIPAddressChanged(); // SignalClientStateChange(ConnectionState new_state); diff --git a/chrome/common/net/notifier/communicator/mailbox.cc b/chrome/common/net/notifier/communicator/mailbox.cc deleted file mode 100644 index 86104bf..0000000 --- a/chrome/common/net/notifier/communicator/mailbox.cc +++ /dev/null @@ -1,677 +0,0 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "chrome/common/net/notifier/communicator/mailbox.h" - -#include <assert.h> -#include <stdlib.h> - -#include <stack> -#include <vector> - -#include "base/logging.h" -#include "chrome/common/net/notifier/base/string.h" -#include "chrome/common/net/notifier/base/utils.h" -#include "chrome/common/net/notifier/communicator/xml_parse_helpers.h" -#include "talk/base/basictypes.h" -#include "talk/base/common.h" -#include "talk/base/stringutils.h" -#include "talk/xmllite/xmlelement.h" -#include "talk/xmpp/constants.h" - -namespace notifier { - -// Labels are a list of strings seperated by a '|' character. The '|' character -// is escaped with a backslash ('\\') and the backslash is also escaped with a -// backslash. -static void ParseLabelSet(const std::string& text, - MessageThread::StringSet* labels) { - const char* input_cur = text.c_str(); - const char* input_end = input_cur + text.size(); - char* result = new char[text.size() + 1]; - char* next_write = result; - - while (input_cur < input_end) { - if (*input_cur == '|') { - if (next_write != result) { - *next_write = '\0'; - labels->insert(std::string(result)); - next_write = result; - } - input_cur++; - continue; - } - - if (*input_cur == '\\') { - // Skip a character in the input and break if we are at the end. - input_cur++; - if (input_cur >= input_end) - break; - } - *next_write = *input_cur; - next_write++; - input_cur++; - } - - if (next_write != result) { - *next_write = '\0'; - labels->insert(std::string(result)); - } - - delete [] result; -} - -// ----------------------------------------------------------------------------- - -std::string MailAddress::safe_name() const { - if (!name().empty()) { - return name(); - } - - if (!address().empty()) { - size_t at = address().find('@'); - if (at == std::string::npos) { - return address(); - } - - if (at != 0) { - return address().substr(0, at); - } - } - - return std::string("(unknown)"); -} - -// ----------------------------------------------------------------------------- -MessageThread::~MessageThread() { - Clear(); -} - -void MessageThread::Clear() { - delete labels_; - labels_ = NULL; - - delete senders_; - senders_ = NULL; -} - -MessageThread& MessageThread::operator=(const MessageThread& r) { - if (&r != this) { - Clear(); - // Copy everything. - r.AssertValid(); - thread_id_ = r.thread_id_; - date64_ = r.date64_; - message_count_ = r.message_count_; - personal_level_ = r.personal_level_; - subject_ = r.subject_; - snippet_ = r.snippet_; - - if (r.labels_) - labels_ = new StringSet(*r.labels_); - else - labels_ = new StringSet; - if (r.senders_) - senders_ = new MailSenderList(*r.senders_); - else - senders_ = new MailSenderList; - } - AssertValid(); - return *this; -} - -MessageThread* MessageThread::CreateFromXML( - const buzz::XmlElement* src) { - MessageThread* info = new MessageThread(); - if (!info || !info->InitFromXml(src)) { - delete info; - return NULL; - } - return info; -} - -// Init from a chunk of XML. -bool MessageThread::InitFromXml(const buzz::XmlElement* src) { - labels_ = new StringSet; - senders_ = new MailSenderList; - - if (src->Name() != buzz::kQnMailThreadInfo) - return false; - - if (!ParseInt64Attr(src, buzz::kQnMailTid, &thread_id_)) - return false; - if (!ParseInt64Attr(src, buzz::kQnMailDate, &date64_)) - return false; - if (!ParseIntAttr(src, buzz::kQnMailMessages, &message_count_)) - return false; - if (!ParseIntAttr(src, buzz::kQnMailParticipation, &personal_level_)) - return false; - - const buzz::XmlElement* senders = src->FirstNamed(buzz::kQnMailSenders); - if (!senders) - return false; - for (const buzz::XmlElement* child = senders->FirstElement(); - child != NULL; - child = child->NextElement()) { - if (child->Name() != buzz::kQnMailSender) - continue; - std::string address; - if (!ParseStringAttr(child, buzz::kQnMailAddress, &address)) - continue; - std::string name; - ParseStringAttr(child, buzz::kQnMailName, &name); - bool originator = false; - ParseBoolAttr(child, buzz::kQnMailOriginator, &originator); - bool unread = false; - ParseBoolAttr(child, buzz::kQnMailUnread, &unread); - - senders_->push_back(MailSender(name, address, unread, originator)); - } - - const buzz::XmlElement* labels = src->FirstNamed(buzz::kQnMailLabels); - if (!labels) - return false; - ParseLabelSet(labels->BodyText(), labels_); - - const buzz::XmlElement* subject = src->FirstNamed(buzz::kQnMailSubject); - if (!subject) - return false; - subject_ = subject->BodyText(); - - const buzz::XmlElement* snippet = src->FirstNamed(buzz::kQnMailSnippet); - if (!snippet) - return false; - snippet_ = snippet->BodyText(); - - AssertValid(); - return true; -} - -bool MessageThread::starred() const { - return (labels_->find("^t") != labels_->end()); -} - -bool MessageThread::unread() const { - return (labels_->find("^u") != labels_->end()); -} - -#if defined(DEBUG) -// Non-debug version is inline and empty. -void MessageThread::AssertValid() const { - assert(thread_id_ != 0); - assert(senders_ != NULL); - // In some (odd) cases, gmail may send email with no sender. - // assert(!senders_->empty()); - assert(message_count_ > 0); - assert(labels_ != NULL); -} -#endif - -MailBox* MailBox::CreateFromXML(const buzz::XmlElement* src) { - MailBox* mail_box = new MailBox(); - if (!mail_box || !mail_box->InitFromXml(src)) { - delete mail_box; - return NULL; - } - return mail_box; -} - -// Init from a chunk of XML. -bool MailBox::InitFromXml(const buzz::XmlElement* src) { - if (src->Name() != buzz::kQnMailBox) - return false; - - if (!ParseIntAttr(src, buzz::kQnMailTotalMatched, &mailbox_size_)) - return false; - - estimate_ = false; - ParseBoolAttr(src, buzz::kQnMailTotalEstimate, &estimate_); - - first_index_ = 0; - ParseIntAttr(src, buzz::kQnMailFirstIndex, &first_index_); - - result_time_ = 0; - ParseInt64Attr(src, buzz::kQnMailResultTime, &result_time_); - - highest_thread_id_ = 0; - - const buzz::XmlElement* thread_element = - src->FirstNamed(buzz::kQnMailThreadInfo); - while (thread_element) { - MessageThread* thread = MessageThread::CreateFromXML(thread_element); - if (thread) { - if (thread->thread_id() > highest_thread_id_) - highest_thread_id_ = thread->thread_id(); - threads_.push_back(MessageThreadPointer(thread)); - } - thread_element = thread_element->NextNamed(buzz::kQnMailThreadInfo); - } - return true; -} - -const size_t kMaxShortnameLength = 12; - -// Tip: If you extend this list of chars, do not include '-'. -const char name_delim[] = " ,.:;\'\"()[]{}<>*@"; - -class SenderFormatter { - public: - // sender should not be deleted while this class is in use. - SenderFormatter(const MailSender& sender, - const std::string& me_address) - : sender_(sender), - visible_(false), - short_format_(true), - space_(kMaxShortnameLength) { - me_ = talk_base::ascicmp(me_address.c_str(), - sender.address().c_str()) == 0; - } - - bool visible() const { - return visible_; - } - - bool is_unread() const { - return sender_.unread(); - } - - const std::string name() const { - return name_; - } - - void set_short_format(bool short_format) { - short_format_ = short_format; - UpdateName(); - } - - void set_visible(bool visible) { - visible_ = visible; - UpdateName(); - } - - void set_space(size_t space) { - space_ = space; - UpdateName(); - } - - private: - // Attempt to shorten to the first word in a person's name We could revisit - // and do better at international punctuation, but this is what cricket did, - // and it should be removed soon when gmail does the notification instead of - // us forming it on the client. - static void ShortenName(std::string* name) { - size_t start = name->find_first_not_of(name_delim); - if (start != std::string::npos && start > 0) { - name->erase(0, start); - } - start = name->find_first_of(name_delim); - if (start != std::string::npos) { - name->erase(start); - } - } - - void UpdateName() { - // Update the name if is going to be used. - if (!visible_) { - return; - } - - if (me_) { - name_ = "me"; - return; - } - - if (sender_.name().empty() && sender_.address().empty()) { - name_ = ""; - return; - } - - name_ = sender_.name(); - // Handle the case of no name or a name looks like an email address. When - // mail is sent to "Quality@example.com" <quality-team@example.com>, we - // shouldn't show "Quality@example.com" as the name. Instead, use the email - // address (without the @...) - if (name_.empty() || name_.find_first_of("@") != std::string::npos) { - name_ = sender_.address(); - size_t at_index = name_.find_first_of("@"); - if (at_index != std::string::npos) { - name_.erase(at_index); - } - } else if (short_format_) { - ShortenName(&name_); - } - - if (name_.empty()) { - name_ = "(unknown)"; - } - - // Abbreviate if too long. - if (name_.size() > space_) { - name_.replace(space_ - 1, std::string::npos, "."); - } - } - - const MailSender& sender_; - std::string name_; - bool visible_; - bool short_format_; - size_t space_; - bool me_; - DISALLOW_COPY_AND_ASSIGN(SenderFormatter); -}; - -const char kNormalSeparator[] = ", "; -const char kEllidedSeparator[] = " .."; - -std::string FormatName(const std::string& name, bool bold) { - std::string formatted_name; - if (bold) { - formatted_name.append("<b>"); - } - formatted_name.append(HtmlEncode(name)); - if (bold) { - formatted_name.append("</b>"); - } - return formatted_name; -} - -class SenderFormatterList { - public: - // sender_list must not be deleted while this class is being used. - SenderFormatterList(const MailSenderList& sender_list, - const std::string& me_address) - : state_(INITIAL_STATE), - are_any_read_(false), - index_(-1), - first_unread_index_(-1) { - // Add all read messages. - const MailSender* originator = NULL; - bool any_unread = false; - for (size_t i = 0; i < sender_list.size(); ++i) { - if (sender_list[i].originator()) { - originator = &sender_list[i]; - } - if (sender_list[i].unread()) { - any_unread = true; - continue; - } - are_any_read_ = true; - if (!sender_list[i].originator()) { - senders_.push_back(new SenderFormatter(sender_list[i], - me_address)); - } - } - - // There may not be an orignator (if there no senders). - if (originator) { - senders_.insert(senders_.begin(), new SenderFormatter(*originator, - me_address)); - } - - // Add all unread messages. - if (any_unread) { - // It should be rare, but there may be cases when all of the senders - // appear to have read the message. - first_unread_index_ = is_first_unread() ? 0 : senders_.size(); - for (size_t i = 0; i < sender_list.size(); ++i) { - if (!sender_list[i].unread()) { - continue; - } - // Don't add the originator if it is already at the start of the - // "unread" list. - if (sender_list[i].originator() && is_first_unread()) { - continue; - } - senders_.push_back(new SenderFormatter(sender_list[i], me_address)); - } - } - } - - ~SenderFormatterList() { - CleanupSequence(&senders_); - } - - std::string GetHtml(int space) { - index_ = -1; - state_ = INITIAL_STATE; - while (!added_.empty()) { - added_.pop(); - } - - // If there is only one sender, let it take up all of the space. - if (senders_.size() == 1) { - senders_[0]->set_space(space); - senders_[0]->set_short_format(false); - } - - int length = 1; - // Add as many senders as we can in the given space. Computes the visible - // length at each iteration, but does not construct the actual html. - while (length < space && AddNextSender()) { - int new_length = ConstructHtml(is_first_unread(), NULL); - // Remove names to avoid truncating - // * if there will be at least 2 left or - // * if the spacing <= 2 characters per sender and there - // is at least one left. - if ((new_length > space && - (visible_count() > 2 || - (ComputeSpacePerSender(space) <= 2 && visible_count() > 1)))) { - RemoveLastAddedSender(); - break; - } - length = new_length; - } - - if (length > space) { - int max = ComputeSpacePerSender(space); - for (size_t i = 0; i < senders_.size(); ++i) { - if (senders_[i]->visible()) { - senders_[i]->set_space(max); - } - } - } - - // Now construct the actual html. - std::string html_list; - length = ConstructHtml(is_first_unread(), &html_list); - if (length > space) { - LOG(LS_WARNING) << "LENGTH: " << length << " exceeds " - << space << " " << html_list; - } - return html_list; - } - - private: - int ComputeSpacePerSender(int space) const { - // Why the "- 2"? To allow for the " .. " which may occur after the names, - // and no matter what always allow at least 2 characters per sender. - return talk_base::_max<int>(space / visible_count() - 2, 2); - } - - // Finds the next sender that should be added to the "from" list and sets it - // to visible. - // - // This method may be called until it returns false or until - // RemoveLastAddedSender is called. - bool AddNextSender() { - // The progression is: - // 1. Add the person who started the thread, which is the first message. - // 2. Add the first unread message (unless it has already been added). - // 3. Add the last message (unless it has already been added). - // 4. Add the message that is just before the last message processed - // (unless it has already been added). - // If there is no message (i.e. at index -1), return false. - // - // Typically, this method is called until it returns false or all of the - // space available is used. - switch (state_) { - case INITIAL_STATE: - state_ = FIRST_MESSAGE; - index_ = 0; - // If the server behaves odd and doesn't send us any senders, do - // something graceful. - if (senders_.size() == 0) { - return false; - } - break; - - case FIRST_MESSAGE: - if (first_unread_index_ >= 0) { - state_ = FIRST_UNREAD_MESSAGE; - index_ = first_unread_index_; - break; - } - // Fall through. - case FIRST_UNREAD_MESSAGE: - state_ = LAST_MESSAGE; - index_ = senders_.size() - 1; - break; - - case LAST_MESSAGE: - case PREVIOUS_MESSAGE: - state_ = PREVIOUS_MESSAGE; - index_--; - break; - - case REMOVED_MESSAGE: - default: - LOG(DFATAL) << "invalid state: " << state_; - return false; - } - - if (index_ < 0) { - return false; - } - - if (!senders_[index_]->visible()) { - added_.push(index_); - senders_[index_]->set_visible(true); - } - return true; - } - - // Makes the last added sender not visible. - // - // May be called while visible_count() > 0. - void RemoveLastAddedSender() { - state_ = REMOVED_MESSAGE; - int index = added_.top(); - added_.pop(); - senders_[index]->set_visible(false); - } - - // Constructs the html of the SenderList and returns the length of the - // visible text. - // - // The algorithm simply walks down the list of Senders, appending the html - // for each visible sender, and adding ellipsis or commas in between, - // whichever is appropriate. - // - // html Filled with html. Maybe NULL if the html doesn't need to be - // constructed yet (useful for simply determining the length of the - // visible text). - // - // returns The approximate visible length of the html. - int ConstructHtml(bool first_is_unread, - std::string* html) const { - if (senders_.empty()) { - return 0; - } - - int length = 0; - - // The first is always visible. - const SenderFormatter* sender = senders_[0]; - const std::string& originator_name = sender->name(); - length += originator_name.length(); - if (html) { - html->append(FormatName(originator_name, first_is_unread)); - } - - bool elided = false; - const char* between = ""; - for (size_t i = 1; i < senders_.size(); i++) { - sender = senders_[i]; - - if (sender->visible()) { - // Handle the separator. - between = elided ? " " : kNormalSeparator; - // Ignore the , for length because it is so narrow, so in both cases - // above the space is the only things that counts for spaces. - length++; - - // Handle the name. - const std::string name = sender->name(); - length += name.size(); - - // Construct the html. - if (html) { - html->append(between); - html->append(FormatName(name, sender->is_unread())); - } - elided = false; - } else if (!elided) { - between = kEllidedSeparator; - length += 2; // ".." is narrow. - if (html) { - html->append(between); - } - elided = true; - } - } - return length; - } - - bool is_first_unread() const { - return !are_any_read_; - } - - size_t visible_count() const { - return added_.size(); - } - - enum MessageState { - INITIAL_STATE, - FIRST_MESSAGE, - FIRST_UNREAD_MESSAGE, - LAST_MESSAGE, - PREVIOUS_MESSAGE, - REMOVED_MESSAGE, - }; - - // What state we were in during the last "stateful" function call. - MessageState state_; - bool are_any_read_; - std::vector<SenderFormatter*> senders_; - std::stack<int> added_; - int index_; - int first_unread_index_; - DISALLOW_COPY_AND_ASSIGN(SenderFormatterList); -}; - - -std::string GetSenderHtml(const MailSenderList& sender_list, - int message_count, - const std::string& me_address, - int space) { - // There has to be at least 9 spaces to show something reasonable. - DCHECK_GE(space, 10); - std::string count_html; - if (message_count > 1) { - std::string count(IntToString(message_count)); - space -= (count.size()); - count_html.append(" ("); - count_html.append(count); - count_html.append(")"); - // Reduce space due to parenthesis and . - space -= 2; - } - - SenderFormatterList senders(sender_list, me_address); - std::string html_list(senders.GetHtml(space)); - html_list.append(count_html); - return html_list; -} - -} // namespace notifier diff --git a/chrome/common/net/notifier/communicator/mailbox.h b/chrome/common/net/notifier/communicator/mailbox.h deleted file mode 100644 index 9aea0c7..0000000 --- a/chrome/common/net/notifier/communicator/mailbox.h +++ /dev/null @@ -1,166 +0,0 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef CHROME_COMMON_NET_NOTIFIER_COMMUNICATOR_MAILBOX_H_ -#define CHROME_COMMON_NET_NOTIFIER_COMMUNICATOR_MAILBOX_H_ - -#include <set> -#include <string> -#include <vector> - -#include "talk/base/basictypes.h" -#include "talk/base/linked_ptr.h" - -namespace buzz { -class XmlElement; -} - -namespace notifier { -// ----------------------------------------------------------------------------- -class MailAddress { - public: - MailAddress(const std::string& name, const std::string& address) - : name_(name), - address_(address) { - } - const std::string& name() const { return name_; } - const std::string& address() const { return address_; } - std::string safe_name() const; // Will return *something*. - private: - std::string name_; - std::string address_; -}; - -// ----------------------------------------------------------------------------- -class MailSender : public MailAddress { - public: - MailSender(const std::string& name, const std::string& address, bool unread, - bool originator) - : MailAddress(name, address), - unread_(unread), - originator_(originator) { - } - - MailSender(const MailSender& r) - : MailAddress(r.name(), r.address()) { - unread_ = r.unread_; - originator_ = r.originator_; - } - - bool unread() const { return unread_; } - bool originator() const { return originator_; } - - private: - bool unread_; - bool originator_; -}; - -typedef std::vector<MailSender> MailSenderList; - -// ----------------------------------------------------------------------------- -// MessageThread: everything there is to know about a mail thread. -class MessageThread { - public: - MessageThread(const MessageThread& r) { - labels_ = NULL; - senders_ = NULL; - *this = r; - } - - ~MessageThread(); - - // Try to parse the XML to create a MessageThreadInfo. If NULL is returned - // then we either ran out of memory or there was an error in parsing the XML. - static MessageThread* CreateFromXML(const buzz::XmlElement* src); - - MessageThread& operator=(const MessageThread& r); - - // SameThreadAs : name is self evident. - bool SameThreadAs(const MessageThread& r) { - AssertValid(); - r.AssertValid(); - return (thread_id_ == r.thread_id_); - } - - // SameAs : true if thread has same id and messages. - // Assumes that messages don't disappear from threads. - bool SameAs(const MessageThread& r) { - AssertValid(); - r.AssertValid(); - return SameThreadAs(r) && - message_count_ == r.message_count_; - } - - typedef std::set<std::string> StringSet; - - int64 thread_id() const { return thread_id_; } - const StringSet* labels() const { return labels_; } - int64 date64() const { return date64_; } - MailSenderList* senders() const { return senders_; } - int personal_level() const { return personal_level_; } - int message_count() const { return message_count_; } - const std::string& subject() const { return subject_; } - const std::string& snippet() const { return snippet_; } - bool starred() const; - bool unread() const; - -#if defined(DEBUG) - void AssertValid() const; -#else - inline void AssertValid() const {} -#endif - - private: - void Clear(); - - private: - MessageThread() : senders_(NULL), labels_(NULL) {} - bool InitFromXml(const buzz::XmlElement* src); - - int64 thread_id_; - int64 date64_; - int message_count_; - int personal_level_; - std::string subject_; - std::string snippet_; - MailSenderList* senders_; - StringSet* labels_; -}; - -typedef talk_base::linked_ptr<MessageThread> MessageThreadPointer; -typedef std::vector<MessageThreadPointer> MessageThreadVector; - -// ----------------------------------------------------------------------------- -class MailBox { - public: - static MailBox* CreateFromXML(const buzz::XmlElement* src); - - const MessageThreadVector& threads() const { return threads_; } - int mailbox_size() const { return mailbox_size_; } - int first_index() const { return first_index_; } - bool estimate() const { return estimate_; } - int64 result_time() const { return result_time_; } - int64 highest_thread_id() const { return highest_thread_id_; } - - private: - MailBox() {} - bool InitFromXml(const buzz::XmlElement* src); - - MessageThreadVector threads_; - - int mailbox_size_; - int first_index_; - bool estimate_; - int64 result_time_; - int64 highest_thread_id_; -}; - -std::string GetSenderHtml(const MailSenderList& sender_list, - int message_count, - const std::string& me_address, - int space); - -} // namespace notifier - -#endif // CHROME_COMMON_NET_NOTIFIER_COMMUNICATOR_MAILBOX_H_ diff --git a/chrome/common/net/notifier/communicator/mailbox_unittest.cc b/chrome/common/net/notifier/communicator/mailbox_unittest.cc deleted file mode 100644 index 7230766..0000000 --- a/chrome/common/net/notifier/communicator/mailbox_unittest.cc +++ /dev/null @@ -1,119 +0,0 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "chrome/common/net/notifier/communicator/mailbox.h" - -namespace notifier { - -TEST_NOTIFIER_F(MailBoxTest); - -TEST_F(MailBoxTest, SingleSenderHtml) { - std::string me_address("random@company.com"); - MailSenderList sender_list; - sender_list.push_back(MailSender("Alex Smith", "a@a.com", true, true)); - std::string sender_html = GetSenderHtml(sender_list, 1, me_address, 25); - ASSERT_STREQ("<b>Alex Smith</b>", sender_html.c_str()); -} - -TEST_F(MailBoxTest, TruncatedSingleSenderHtml) { - std::string me_address("random@company.com"); - MailSenderList sender_list; - sender_list.push_back(MailSender( - "Alex Smith AReallyLongLastNameThatWillBeTruncated", - "a@a.com", - true, - true)); - std::string sender_html = GetSenderHtml(sender_list, 1, me_address, 25); - ASSERT_STREQ("<b>Alex Smith AReallyLongLa.</b>", sender_html.c_str()); -} - -TEST_F(MailBoxTest, SingleSenderManyTimesHtml) { - std::string me_address("random@company.com"); - MailSenderList sender_list; - sender_list.push_back(MailSender("Alex Smith", "a@a.com", true, true)); - std::string sender_html = GetSenderHtml(sender_list, 10, me_address, 25); - ASSERT_STREQ("<b>Alex Smith</b> (10)", sender_html.c_str()); -} - -TEST_F(MailBoxTest, SenderWithMeHtml) { - std::string me_address("randOm@comPany.Com"); - MailSenderList sender_list; - sender_list.push_back( - MailSender("Alex Smith", "alex@jones.com", false, false)); - sender_list.push_back( - MailSender("Your Name Goes Here", "raNdom@coMpany.cOm", true, true)); - std::string sender_html = GetSenderHtml(sender_list, 5, me_address, 25); - ASSERT_STREQ("me, Alex, <b>me</b> (5)", sender_html.c_str()); -} - -TEST_F(MailBoxTest, SenderHtmlWithAllUnread) { - std::string me_address("random@company.com"); - MailSenderList sender_list; - sender_list.push_back( - MailSender("Alex Smith", "alex@jones.com", true, false)); - sender_list.push_back(MailSender( - "Your Name Goes Here", - "foo@company.com", - true, - true)); - sender_list.push_back(MailSender("", "bob@davis.com", true, false)); - std::string sender_html = GetSenderHtml(sender_list, 100, me_address, 25); - ASSERT_STREQ("<b>Your</b>, <b>Alex</b>, <b>bob</b> (100)", - sender_html.c_str()); -} - -TEST_F(MailBoxTest, SenderHtmlWithTruncatedNames) { - std::string me_address("random@company.com"); - MailSenderList sender_list; - sender_list.push_back(MailSender( - "ReallyLongName Smith", - "alex@jones.com", - true, - false)); - sender_list.push_back(MailSender( - "AnotherVeryLongFirstNameIsHere", - "foo@company.com", - true, - true)); - std::string sender_html = GetSenderHtml(sender_list, 2, me_address, 25); - ASSERT_STREQ("<b>AnotherV.</b>, <b>ReallyLo.</b> (2)", - sender_html.c_str()); -} - -TEST_F(MailBoxTest, SenderWithTwoSendersShowing) { - std::string me_address("random@company.com"); - MailSenderList sender_list; - sender_list.push_back( - MailSender("ALongishName Smith", "alex@jones.com", false, false)); - sender_list.push_back( - MailSender("AnotherBigName", "no@company.com", true, false)); - sender_list.push_back( - MailSender("Person1", "no1@company.com", true, false)); - sender_list.push_back( - MailSender("Person2", "no2@company.com", false, true)); - std::string sender_html = GetSenderHtml(sender_list, 6, me_address, 25); - ASSERT_STREQ("Person2 .. <b>AnotherB.</b> .. (6)", - sender_html.c_str()); -} - -TEST_F(MailBoxTest, SenderWithThreeSendersShowing) { - std::string me_address("random@company.com"); - MailSenderList sender_list; - sender_list.push_back( - MailSender("Joe Smith", "alex@jones.com", false, false)); - sender_list.push_back( - MailSender("Bob Other", "no@company.com", true, false)); - sender_list.push_back( - MailSender("Person0", "no0@company.com", true, false)); - sender_list.push_back( - MailSender("Person1", "no1@company.com", true, false)); - sender_list.push_back( - MailSender("ted", "ted@company.com", false, true)); - std::string sender_html = GetSenderHtml(sender_list, 6, me_address, 25); - ASSERT_STREQ( - "ted .. <b>Bob</b> .. <b>Person1</b> (6)", - sender_html.c_str()); -} - -} // namespace notifier diff --git a/chrome/common/net/notifier/communicator/xmpp_log.cc b/chrome/common/net/notifier/communicator/xmpp_log.cc deleted file mode 100644 index 27b0cf96..0000000 --- a/chrome/common/net/notifier/communicator/xmpp_log.cc +++ /dev/null @@ -1,112 +0,0 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#if LOGGING - -#include "chrome/common/net/notifier/communicator/xmpp_log.h" - -#include <iomanip> -#include <string> -#include <vector> - -#include "chrome/common/net/notifier/base/time.h" -#include "talk/base/common.h" -#include "talk/base/logging.h" - -namespace notifier { - -static bool IsAuthTag(const char* str, size_t len) { - // Beware that str is not NULL terminated. - if (str[0] == '<' && - str[1] == 'a' && - str[2] == 'u' && - str[3] == 't' && - str[4] == 'h' && - str[5] <= ' ') { - std::string tag(str, len); - if (tag.find("mechanism") != std::string::npos) - return true; - } - return false; -} - -static bool IsChatText(const char* str, size_t len) { - // Beware that str is not NULL terminated. - if (str[0] == '<' && - str[1] == 'm' && - str[2] == 'e' && - str[3] == 's' && - str[4] == 's' && - str[5] == 'a' && - str[6] == 'g' && - str[7] == 'e' && - str[8] <= ' ') { - std::string tag(str, len); - if (tag.find("chat") != std::string::npos) - return true; - } - return false; -} - -void XmppLog::XmppPrint(bool output) { - std::vector<char>* buffer = output ? - &xmpp_output_buffer_ : &xmpp_input_buffer_; - const bool log_chat = LOG_CHECK_LEVEL(LS_SENSITIVE); - if (buffer->size()) { - char* time_string = GetLocalTimeAsString(); - LOG(INFO) << (output ? "SEND >>>>>>>>>>>>>>>>>>>>>>>>>" : - "RECV <<<<<<<<<<<<<<<<<<<<<<<<<") - << " : " << time_string; - - int start = 0; - int nest = 3; - for (int i = 0; i < static_cast<int>(buffer->size()); ++i) { - if ((*buffer)[i] == '>') { - bool indent; - if ((i > 0) && ((*buffer)[i - 1] == '/')) { - indent = false; - } else if ((start + 1 < static_cast<int>(buffer->size())) && - ((*buffer)[start + 1] == '/')) { - indent = false; - nest -= 2; - } else { - indent = true; - } - - // Output a tag - LOG(INFO) << std::setw(nest) << " " - << std::string(&((*buffer)[start]), i + 1 - start); - - if (indent) - nest += 2; - - // Note if it's a PLAIN auth tag - if (IsAuthTag(&((*buffer)[start]), i + 1 - start)) { - censor_password_ = true; - } else if (!log_chat && IsChatText(&((*buffer)[start]), - i + 1 - start)) { - censor_password_ = true; - } - - start = i + 1; - } - - if ((*buffer)[i] == '<' && start < i) { - if (censor_password_) { - LOG(INFO) << std::setw(nest) << " " << "## TEXT REMOVED ##"; - censor_password_ = false; - } else { - LOG(INFO) << std::setw(nest) << " " - << std::string(&((*buffer)[start]), i - start); - } - start = i; - } - } - buffer->erase(buffer->begin(), buffer->begin() + start); - } -} - -} // namespace notifier - -#endif // if LOGGING diff --git a/chrome/common/net/notifier/communicator/xmpp_log.h b/chrome/common/net/notifier/communicator/xmpp_log.h deleted file mode 100644 index 1d6edac..0000000 --- a/chrome/common/net/notifier/communicator/xmpp_log.h +++ /dev/null @@ -1,46 +0,0 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef CHROME_COMMON_NET_NOTIFIER_COMMUNICATOR_XMPP_LOG_H_ -#define CHROME_COMMON_NET_NOTIFIER_COMMUNICATOR_XMPP_LOG_H_ - -#if LOGGING - -#include <vector> - -#include "talk/base/basictypes.h" -#include "talk/base/sigslot.h" - -namespace notifier { - -// Log the xmpp input and output. -class XmppLog : public sigslot::has_slots<> { - public: - XmppLog() : censor_password_(false) { - } - - void Input(const char* data, int len) { - xmpp_input_buffer_.insert(xmpp_input_buffer_.end(), data, data + len); - XmppPrint(false); - } - - void Output(const char* data, int len) { - xmpp_output_buffer_.insert(xmpp_output_buffer_.end(), data, data + len); - XmppPrint(true); - } - - private: - void XmppPrint(bool output); - - std::vector<char> xmpp_input_buffer_; - std::vector<char> xmpp_output_buffer_; - bool censor_password_; - DISALLOW_COPY_AND_ASSIGN(XmppLog); -}; - -} // namespace notifier - -#endif // if LOGGING - -#endif // CHROME_COMMON_NET_NOTIFIER_COMMUNICATOR_XMPP_LOG_H_ |