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 "chrome/browser/sync/glue/synced_session.h"
6
7#include "base/stl_util.h"
8#include "chrome/common/url_constants.h"
9#include "content/public/browser/navigation_entry.h"
10#include "content/public/common/url_constants.h"
11
12namespace browser_sync {
13
14SyncedSession::SyncedSession() : session_tag("invalid"),
15                                 device_type(TYPE_UNSET) {
16}
17
18SyncedSession::~SyncedSession() {
19  STLDeleteContainerPairSecondPointers(windows.begin(), windows.end());
20}
21
22sync_pb::SessionHeader SyncedSession::ToSessionHeader() const {
23  sync_pb::SessionHeader header;
24  SyncedWindowMap::const_iterator iter;
25  for (iter = windows.begin(); iter != windows.end(); ++iter) {
26    sync_pb::SessionWindow* w = header.add_window();
27    w->CopyFrom(iter->second->ToSyncData());
28  }
29  header.set_client_name(session_name);
30  switch (device_type) {
31    case SyncedSession::TYPE_WIN:
32      header.set_device_type(sync_pb::SyncEnums_DeviceType_TYPE_WIN);
33      break;
34    case SyncedSession::TYPE_MACOSX:
35      header.set_device_type(sync_pb::SyncEnums_DeviceType_TYPE_MAC);
36      break;
37    case SyncedSession::TYPE_LINUX:
38      header.set_device_type(sync_pb::SyncEnums_DeviceType_TYPE_LINUX);
39      break;
40    case SyncedSession::TYPE_CHROMEOS:
41      header.set_device_type(sync_pb::SyncEnums_DeviceType_TYPE_CROS);
42      break;
43    case SyncedSession::TYPE_PHONE:
44      header.set_device_type(sync_pb::SyncEnums_DeviceType_TYPE_PHONE);
45      break;
46    case SyncedSession::TYPE_TABLET:
47      header.set_device_type(sync_pb::SyncEnums_DeviceType_TYPE_TABLET);
48      break;
49    case SyncedSession::TYPE_OTHER:
50      // Intentionally fall-through
51    default:
52      header.set_device_type(sync_pb::SyncEnums_DeviceType_TYPE_OTHER);
53      break;
54  }
55  return header;
56}
57
58// Note: if you modify this, make sure you modify
59// SessionModelAssociator::ShouldSyncTab to ensure the logic matches.
60bool ShouldSyncSessionTab(const SessionTab& tab) {
61  if (tab.navigations.empty())
62    return false;
63  bool found_valid_url = false;
64  for (size_t i = 0; i < tab.navigations.size(); ++i) {
65    const GURL& virtual_url = tab.navigations.at(i).virtual_url();
66    if (virtual_url.is_valid() &&
67        !virtual_url.SchemeIs(content::kChromeUIScheme) &&
68        !virtual_url.SchemeIs(chrome::kChromeNativeScheme) &&
69        !virtual_url.SchemeIsFile()) {
70      found_valid_url = true;
71      break;
72    }
73  }
74  return found_valid_url;
75}
76
77bool SessionWindowHasNoTabsToSync(const SessionWindow& window) {
78  int num_populated = 0;
79  for (std::vector<SessionTab*>::const_iterator i = window.tabs.begin();
80      i != window.tabs.end(); ++i) {
81    const SessionTab* tab = *i;
82    if (ShouldSyncSessionTab(*tab))
83      num_populated++;
84  }
85  return (num_populated == 0);
86}
87
88}  // namespace browser_sync
89