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/performance_monitor/performance_monitor_util.h"
6
7#include "base/json/json_writer.h"
8#include "base/memory/scoped_ptr.h"
9#include "base/strings/string_number_conversions.h"
10#include "base/threading/sequenced_worker_pool.h"
11#include "base/time/time.h"
12#include "chrome/browser/performance_monitor/events.h"
13#include "content/public/browser/browser_thread.h"
14
15namespace performance_monitor {
16namespace util {
17
18bool PostTaskToDatabaseThreadAndReply(
19    const tracked_objects::Location& from_here,
20    const base::Closure& request,
21    const base::Closure& reply) {
22  base::SequencedWorkerPool* pool = content::BrowserThread::GetBlockingPool();
23  base::SequencedWorkerPool::SequenceToken token =
24      pool->GetNamedSequenceToken(Database::kDatabaseSequenceToken);
25  return pool->GetSequencedTaskRunner(token)->PostTaskAndReply(
26      from_here, request, reply);
27}
28
29scoped_ptr<Event> CreateExtensionEvent(const EventType type,
30                                       const base::Time& time,
31                                       const std::string& id,
32                                       const std::string& name,
33                                       const std::string& url,
34                                       const int location,
35                                       const std::string& version,
36                                       const std::string& description) {
37  events::ExtensionEvent event;
38  event.event_type = type;
39  event.time = static_cast<double>(time.ToInternalValue());
40  event.extension_id = id;
41  event.extension_name = name;
42  event.extension_url = url;
43  event.extension_location = location;
44  event.extension_version = version;
45  event.extension_description = description;
46  scoped_ptr<base::DictionaryValue> value = event.ToValue();
47  return scoped_ptr<Event>(new Event(
48      type, time, value.Pass()));
49}
50
51scoped_ptr<Event> CreateRendererFailureEvent(const base::Time& time,
52                                             const EventType& type,
53                                             const std::string& url) {
54  events::RendererFailure event;
55  event.event_type = type;
56  event.time = static_cast<double>(time.ToInternalValue());
57  event.url = url;
58  scoped_ptr<base::DictionaryValue> value = event.ToValue();
59  return scoped_ptr<Event>(new Event(type, time, value.Pass()));
60}
61
62scoped_ptr<Event> CreateUncleanExitEvent(const base::Time& time,
63                                         const std::string& profile_name) {
64  events::UncleanExit event;
65  event.event_type = EVENT_UNCLEAN_EXIT;
66  event.time = static_cast<double>(time.ToInternalValue());
67  event.profile_name = profile_name;
68  scoped_ptr<base::DictionaryValue> value = event.ToValue();
69  return scoped_ptr<Event>(new Event(
70      EVENT_UNCLEAN_EXIT, time, value.Pass()));
71}
72
73scoped_ptr<Event> CreateChromeUpdateEvent(const base::Time& time,
74                                          const std::string& previous_version,
75                                          const std::string& current_version) {
76  events::ChromeUpdate event;
77  event.event_type = EVENT_CHROME_UPDATE;
78  event.time = static_cast<double>(time.ToInternalValue());
79  event.previous_version = previous_version;
80  event.current_version = current_version;
81  scoped_ptr<base::DictionaryValue> value = event.ToValue();
82  return scoped_ptr<Event>(new Event(
83      EVENT_CHROME_UPDATE, time, value.Pass()));
84}
85
86}  // namespace util
87}  // namespace performance_monitor
88