summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/api/experience_sampling_private/experience_sampling.cc
blob: bda78e1dddfa091579d16a78c94eb1cdc26c7631 (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
96
97
98
99
100
// Copyright 2014 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/extensions/api/experience_sampling_private/experience_sampling.h"

#include "base/values.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/common/extensions/api/experience_sampling_private.h"
#include "extensions/browser/event_router.h"
#include "url/gurl.h"

namespace extensions {

// static
const char ExperienceSamplingEvent::kProceed[] = "proceed";
const char ExperienceSamplingEvent::kDeny[] = "deny";
const char ExperienceSamplingEvent::kCancel[] = "cancel";
const char ExperienceSamplingEvent::kReload[] = "reload";

// static
const char ExperienceSamplingEvent::kExtensionInstallDialog[] =
    "extension_install_dialog_";

// static
scoped_ptr<ExperienceSamplingEvent> ExperienceSamplingEvent::Create(
    const std::string& element_name,
    const GURL& destination,
    const GURL& referrer) {
  Profile* profile = NULL;
  if (g_browser_process->profile_manager())
     profile = g_browser_process->profile_manager()->GetLastUsedProfile();
  if (!profile)
    return scoped_ptr<ExperienceSamplingEvent>();
  return scoped_ptr<ExperienceSamplingEvent>(new ExperienceSamplingEvent(
      element_name, destination, referrer, profile));
}

// static
scoped_ptr<ExperienceSamplingEvent> ExperienceSamplingEvent::Create(
    const std::string& element_name) {
  return ExperienceSamplingEvent::Create(element_name, GURL(), GURL());
}

ExperienceSamplingEvent::ExperienceSamplingEvent(
    const std::string& element_name,
    const GURL& destination,
    const GURL& referrer,
    content::BrowserContext* browser_context)
    : has_viewed_details_(false),
      has_viewed_learn_more_(false),
      browser_context_(browser_context) {
  ui_element_.name = element_name;
  ui_element_.destination = destination.GetAsReferrer().possibly_invalid_spec();
  ui_element_.referrer = referrer.GetAsReferrer().possibly_invalid_spec();
  ui_element_.time = base::Time::Now().ToJsTime();
}

ExperienceSamplingEvent::~ExperienceSamplingEvent() {
}

void ExperienceSamplingEvent::CreateUserDecisionEvent(
    const std::string& decision_name) {
  // Check if this is from an incognito context. If it is, don't create and send
  // any events.
  if (browser_context_ && browser_context_->IsOffTheRecord())
    return;
  api::experience_sampling_private::UserDecision decision;
  decision.name = decision_name;
  decision.learn_more = has_viewed_learn_more();
  decision.details = has_viewed_details();
  decision.time = base::Time::Now().ToJsTime();

  scoped_ptr<base::ListValue> args(new base::ListValue());
  args->Append(ui_element_.ToValue().release());
  args->Append(decision.ToValue().release());
  scoped_ptr<Event> event(new Event(
      api::experience_sampling_private::OnDecision::kEventName, args.Pass()));
  EventRouter* router = EventRouter::Get(browser_context_);
  if (router)
    router->BroadcastEvent(event.Pass());
}

void ExperienceSamplingEvent::CreateOnDisplayedEvent() {
  // Check if this is from an incognito context. If it is, don't create and send
  // any events.
  if (browser_context_ && browser_context_->IsOffTheRecord())
    return;
  scoped_ptr<base::ListValue> args(new base::ListValue());
  args->Append(ui_element_.ToValue().release());
  scoped_ptr<Event> event(new Event(
      api::experience_sampling_private::OnDisplayed::kEventName, args.Pass()));
  EventRouter* router = EventRouter::Get(browser_context_);
  if (router)
    router->BroadcastEvent(event.Pass());
}

}  // namespace extensions