summaryrefslogtreecommitdiffstats
path: root/chrome/browser/sync/js/js_transaction_observer.cc
blob: 1de292509b259fefe4cf8ac3a04c4462df4b3f1c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright (c) 2011 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/browser/sync/js/js_transaction_observer.h"

#include <sstream>
#include <string>

#include "base/logging.h"
#include "base/tracked.h"
#include "base/values.h"
#include "chrome/browser/sync/js/js_event_details.h"
#include "chrome/browser/sync/js/js_event_handler.h"

namespace browser_sync {

JsTransactionObserver::JsTransactionObserver() {}

JsTransactionObserver::~JsTransactionObserver() {
  DCHECK(non_thread_safe_.CalledOnValidThread());
}

void JsTransactionObserver::SetJsEventHandler(
    const WeakHandle<JsEventHandler>& event_handler) {
  event_handler_ = event_handler;
}

namespace {

std::string GetLocationString(const tracked_objects::Location& location) {
  std::ostringstream oss;
  oss << location.function_name() << "@"
      << location.file_name() << ":" << location.line_number();
  return oss.str();
}

}  // namespace

void JsTransactionObserver::OnTransactionStart(
    const tracked_objects::Location& location,
    const syncable::WriterTag& writer) {
  DCHECK(non_thread_safe_.CalledOnValidThread());
  if (!event_handler_.IsInitialized()) {
    return;
  }
  DictionaryValue details;
  details.SetString("location", GetLocationString(location));
  details.SetString("writer", syncable::WriterTagToString(writer));
  HandleJsEvent(FROM_HERE, "onTransactionStart", JsEventDetails(&details));
}

void JsTransactionObserver::OnTransactionMutate(
    const tracked_objects::Location& location,
    const syncable::WriterTag& writer,
    const syncable::EntryKernelMutationSet& mutations,
    const syncable::ModelTypeBitSet& models_with_changes) {
  DCHECK(non_thread_safe_.CalledOnValidThread());
  if (!event_handler_.IsInitialized()) {
    return;
  }
  DictionaryValue details;
  details.SetString("location", GetLocationString(location));
  details.SetString("writer", syncable::WriterTagToString(writer));
  details.Set("mutations", syncable::EntryKernelMutationSetToValue(mutations));
  details.Set("modelsWithChanges",
              syncable::ModelTypeBitSetToValue(models_with_changes));
  HandleJsEvent(FROM_HERE, "onTransactionMutate", JsEventDetails(&details));
}

void JsTransactionObserver::OnTransactionEnd(
    const tracked_objects::Location& location,
    const syncable::WriterTag& writer) {
  DCHECK(non_thread_safe_.CalledOnValidThread());
  if (!event_handler_.IsInitialized()) {
    return;
  }
  DictionaryValue details;
  details.SetString("location", GetLocationString(location));
  details.SetString("writer", syncable::WriterTagToString(writer));
  HandleJsEvent(FROM_HERE, "onTransactionEnd", JsEventDetails(&details));
}

void JsTransactionObserver::HandleJsEvent(
    const tracked_objects::Location& from_here,
    const std::string& name, const JsEventDetails& details) {
  if (!event_handler_.IsInitialized()) {
    NOTREACHED();
    return;
  }
  event_handler_.Call(from_here,
                      &JsEventHandler::HandleJsEvent, name, details);
}

}  // namespace browser_sync