Home | History | Annotate | Download | only in experience_sampling_private
      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.
      4 
      5 #include "chrome/browser/extensions/api/experience_sampling_private/experience_sampling.h"
      6 
      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"
     14 
     15 namespace extensions {
     16 
     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";
     23 
     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_";
     33 
     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));
     46 }
     47 
     48 // static
     49 scoped_ptr<ExperienceSamplingEvent> ExperienceSamplingEvent::Create(
     50     const std::string& element_name) {
     51   return ExperienceSamplingEvent::Create(element_name, GURL(), GURL());
     52 }
     53 
     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();
     66 }
     67 
     68 ExperienceSamplingEvent::~ExperienceSamplingEvent() {
     69 }
     70 
     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();
     82 
     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       api::experience_sampling_private::OnDecision::kEventName, args.Pass()));
     88   EventRouter* router = EventRouter::Get(browser_context_);
     89   if (router)
     90     router->BroadcastEvent(event.Pass());
     91 }
     92 
     93 void ExperienceSamplingEvent::CreateOnDisplayedEvent() {
     94   // Check if this is from an incognito context. If it is, don't create and send
     95   // any events.
     96   if (browser_context_ && browser_context_->IsOffTheRecord())
     97     return;
     98   scoped_ptr<base::ListValue> args(new base::ListValue());
     99   args->Append(ui_element_.ToValue().release());
    100   scoped_ptr<Event> event(new Event(
    101       api::experience_sampling_private::OnDisplayed::kEventName, args.Pass()));
    102   EventRouter* router = EventRouter::Get(browser_context_);
    103   if (router)
    104     router->BroadcastEvent(event.Pass());
    105 }
    106 
    107 }  // namespace extensions
    108