activity_log_private_api.cc revision 868fa2fe829687343ffae624259930155e16dbd8
1// Copyright 2013 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/activity_log_private/activity_log_private_api.h"
6
7#include "base/lazy_instance.h"
8#include "base/prefs/pref_service.h"
9#include "base/strings/string_number_conversions.h"
10#include "base/values.h"
11#include "chrome/browser/browser_process.h"
12#include "chrome/browser/extensions/event_router_forwarder.h"
13#include "chrome/browser/extensions/extension_service.h"
14#include "chrome/browser/extensions/extension_system.h"
15#include "chrome/browser/profiles/profile.h"
16#include "chrome/common/extensions/api/activity_log_private.h"
17#include "chrome/common/pref_names.h"
18
19namespace extensions {
20
21using api::activity_log_private::ExtensionActivity;
22
23const char kActivityLogExtensionId[] = "acldcpdepobcjbdanifkmfndkjoilgba";
24const char kActivityLogTestExtensionId[] = "ajabfgledjhbabeoojlabelaifmakodf";
25const char kNewActivityEventName[] = "activityLogPrivate.onExtensionActivity";
26
27static base::LazyInstance<ProfileKeyedAPIFactory<ActivityLogAPI> >
28    g_factory = LAZY_INSTANCE_INITIALIZER;
29
30// static
31ProfileKeyedAPIFactory<ActivityLogAPI>* ActivityLogAPI::GetFactoryInstance() {
32  return &g_factory.Get();
33}
34
35template<>
36void ProfileKeyedAPIFactory<ActivityLogAPI>::DeclareFactoryDependencies() {
37  DependsOn(ExtensionSystemFactory::GetInstance());
38  DependsOn(ActivityLogFactory::GetInstance());
39}
40
41ActivityLogAPI::ActivityLogAPI(Profile* profile)
42    : profile_(profile),
43      initialized_(false) {
44  if (!ExtensionSystem::Get(profile_)->event_router()) {  // Check for testing.
45    LOG(ERROR) << "ExtensionSystem event_router does not exist.";
46    return;
47  }
48  activity_log_ = extensions::ActivityLog::GetInstance(profile_);
49  DCHECK(activity_log_);
50  ExtensionSystem::Get(profile_)->event_router()->RegisterObserver(
51      this, kNewActivityEventName);
52  activity_log_->AddObserver(this);
53  initialized_ = true;
54}
55
56ActivityLogAPI::~ActivityLogAPI() {
57}
58
59void ActivityLogAPI::Shutdown() {
60  if (!initialized_) {  // Check for testing.
61    LOG(ERROR) << "ExtensionSystem event_router does not exist.";
62    return;
63  }
64  ExtensionSystem::Get(profile_)->event_router()->UnregisterObserver(this);
65  activity_log_->RemoveObserver(this);
66}
67
68// static
69bool ActivityLogAPI::IsExtensionWhitelisted(const std::string& extension_id) {
70  return (extension_id == kActivityLogExtensionId ||
71          extension_id == kActivityLogTestExtensionId);
72}
73
74void ActivityLogAPI::OnListenerAdded(const EventListenerInfo& details) {
75  // TODO(felt): Only observe activity_log_ events when we have a customer.
76}
77
78void ActivityLogAPI::OnListenerRemoved(const EventListenerInfo& details) {
79  // TODO(felt): Only observe activity_log_ events when we have a customer.
80}
81
82void ActivityLogAPI::OnExtensionActivity(scoped_refptr<Action> activity) {
83  scoped_ptr<base::ListValue> value(new base::ListValue());
84  scoped_ptr<ExtensionActivity> activity_arg =
85      activity->ConvertToExtensionActivity();
86  value->Append(activity_arg->ToValue().release());
87  scoped_ptr<Event> event(new Event(kNewActivityEventName, value.Pass()));
88  event->restrict_to_profile = profile_;
89  ExtensionSystem::Get(profile_)->event_router()->BroadcastEvent(event.Pass());
90}
91
92bool ActivityLogPrivateGetExtensionActivitiesFunction::RunImpl() {
93  return true;
94}
95
96}  // namespace extensions
97