Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / extensions / api / experience_sampling_private / experience_sampling.cc
blob8a62c9c3aa569652be45353d6739325b90ab54ed
1 // Copyright 2014 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.
5 #include "chrome/browser/extensions/api/experience_sampling_private/experience_sampling.h"
7 #include "base/values.h"
8 #include "chrome/browser/browser_process.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/profiles/profile_manager.h"
11 #include "chrome/common/extensions/api/experience_sampling_private.h"
12 #include "extensions/browser/event_router.h"
13 #include "url/gurl.h"
15 namespace extensions {
17 // static
18 const char ExperienceSamplingEvent::kProceed[] = "proceed";
19 const char ExperienceSamplingEvent::kDeny[] = "deny";
20 const char ExperienceSamplingEvent::kIgnore[] = "ignore";
21 const char ExperienceSamplingEvent::kCancel[] = "cancel";
22 const char ExperienceSamplingEvent::kReload[] = "reload";
24 // static
25 const char ExperienceSamplingEvent::kMaliciousDownload[] =
26 "download_warning_malicious";
27 const char ExperienceSamplingEvent::kDangerousDownload[] =
28 "download_warning_dangerous";
29 const char ExperienceSamplingEvent::kDownloadDangerPrompt[] =
30 "download_danger_prompt";
31 const char ExperienceSamplingEvent::kExtensionInstallDialog[] =
32 "extension_install_dialog_";
34 // static
35 scoped_ptr<ExperienceSamplingEvent> ExperienceSamplingEvent::Create(
36 const std::string& element_name,
37 const GURL& destination,
38 const GURL& referrer) {
39 Profile* profile = NULL;
40 if (g_browser_process->profile_manager())
41 profile = g_browser_process->profile_manager()->GetLastUsedProfile();
42 if (!profile)
43 return scoped_ptr<ExperienceSamplingEvent>();
44 return scoped_ptr<ExperienceSamplingEvent>(new ExperienceSamplingEvent(
45 element_name, destination, referrer, profile));
48 // static
49 scoped_ptr<ExperienceSamplingEvent> ExperienceSamplingEvent::Create(
50 const std::string& element_name) {
51 return ExperienceSamplingEvent::Create(element_name, GURL(), GURL());
54 ExperienceSamplingEvent::ExperienceSamplingEvent(
55 const std::string& element_name,
56 const GURL& destination,
57 const GURL& referrer,
58 content::BrowserContext* browser_context)
59 : has_viewed_details_(false),
60 has_viewed_learn_more_(false),
61 browser_context_(browser_context) {
62 ui_element_.name = element_name;
63 ui_element_.destination = destination.GetAsReferrer().possibly_invalid_spec();
64 ui_element_.referrer = referrer.GetAsReferrer().possibly_invalid_spec();
65 ui_element_.time = base::Time::Now().ToJsTime();
68 ExperienceSamplingEvent::~ExperienceSamplingEvent() {
71 void ExperienceSamplingEvent::CreateUserDecisionEvent(
72 const std::string& decision_name) {
73 // Check if this is from an incognito context. If it is, don't create and send
74 // any events.
75 if (browser_context_ && browser_context_->IsOffTheRecord())
76 return;
77 api::experience_sampling_private::UserDecision decision;
78 decision.name = decision_name;
79 decision.learn_more = has_viewed_learn_more();
80 decision.details = has_viewed_details();
81 decision.time = base::Time::Now().ToJsTime();
83 scoped_ptr<base::ListValue> args(new base::ListValue());
84 args->Append(ui_element_.ToValue().release());
85 args->Append(decision.ToValue().release());
86 scoped_ptr<Event> event(new Event(
87 events::EXPERIENCE_SAMPLING_PRIVATE_ON_DECISION,
88 api::experience_sampling_private::OnDecision::kEventName, args.Pass()));
89 EventRouter* router = EventRouter::Get(browser_context_);
90 if (router)
91 router->BroadcastEvent(event.Pass());
94 void ExperienceSamplingEvent::CreateOnDisplayedEvent() {
95 // Check if this is from an incognito context. If it is, don't create and send
96 // any events.
97 if (browser_context_ && browser_context_->IsOffTheRecord())
98 return;
99 scoped_ptr<base::ListValue> args(new base::ListValue());
100 args->Append(ui_element_.ToValue().release());
101 scoped_ptr<Event> event(new Event(
102 events::EXPERIENCE_SAMPLING_PRIVATE_ON_DISPLAYED,
103 api::experience_sampling_private::OnDisplayed::kEventName, args.Pass()));
104 EventRouter* router = EventRouter::Get(browser_context_);
105 if (router)
106 router->BroadcastEvent(event.Pass());
109 } // namespace extensions