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.
5 #include "chrome/browser/extensions/api/activity_log_private/activity_log_private_api.h"
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/common/extensions/api/activity_log_private.h"
15 #include "content/public/browser/browser_context.h"
16 #include "extensions/browser/extension_system_provider.h"
17 #include "extensions/browser/extensions_browser_client.h"
18 #include "extensions/common/features/feature.h"
19 #include "extensions/common/features/feature_provider.h"
21 namespace extensions
{
23 namespace activity_log_private
= api::activity_log_private
;
25 using api::activity_log_private::ActivityResultSet
;
26 using api::activity_log_private::ExtensionActivity
;
27 using api::activity_log_private::Filter
;
29 static base::LazyInstance
<BrowserContextKeyedAPIFactory
<ActivityLogAPI
> >
30 g_factory
= LAZY_INSTANCE_INITIALIZER
;
33 BrowserContextKeyedAPIFactory
<ActivityLogAPI
>*
34 ActivityLogAPI::GetFactoryInstance() {
35 return g_factory
.Pointer();
40 BrowserContextKeyedAPIFactory
<ActivityLogAPI
>::DeclareFactoryDependencies() {
41 DependsOn(ExtensionsBrowserClient::Get()->GetExtensionSystemFactory());
42 DependsOn(ActivityLog::GetFactoryInstance());
45 ActivityLogAPI::ActivityLogAPI(content::BrowserContext
* context
)
46 : browser_context_(context
), initialized_(false) {
47 if (!EventRouter::Get(browser_context_
)) { // Check for testing.
48 DVLOG(1) << "ExtensionSystem event_router does not exist.";
51 activity_log_
= extensions::ActivityLog::GetInstance(browser_context_
);
52 DCHECK(activity_log_
);
53 EventRouter::Get(browser_context_
)->RegisterObserver(
54 this, activity_log_private::OnExtensionActivity::kEventName
);
55 activity_log_
->AddObserver(this);
59 ActivityLogAPI::~ActivityLogAPI() {
62 void ActivityLogAPI::Shutdown() {
63 if (!initialized_
) { // Check for testing.
64 DVLOG(1) << "ExtensionSystem event_router does not exist.";
67 EventRouter::Get(browser_context_
)->UnregisterObserver(this);
68 activity_log_
->RemoveObserver(this);
72 bool ActivityLogAPI::IsExtensionWhitelisted(const std::string
& extension_id
) {
73 return FeatureProvider::GetPermissionFeatures()->
74 GetFeature("activityLogPrivate")->IsIdInWhitelist(extension_id
);
77 void ActivityLogAPI::OnListenerAdded(const EventListenerInfo
& details
) {
78 // TODO(felt): Only observe activity_log_ events when we have a customer.
81 void ActivityLogAPI::OnListenerRemoved(const EventListenerInfo
& details
) {
82 // TODO(felt): Only observe activity_log_ events when we have a customer.
85 void ActivityLogAPI::OnExtensionActivity(scoped_refptr
<Action
> activity
) {
86 scoped_ptr
<base::ListValue
> value(new base::ListValue());
87 scoped_ptr
<ExtensionActivity
> activity_arg
=
88 activity
->ConvertToExtensionActivity();
89 value
->Append(activity_arg
->ToValue().release());
90 scoped_ptr
<Event
> event(
91 new Event(activity_log_private::OnExtensionActivity::kEventName
,
93 event
->restrict_to_browser_context
= browser_context_
;
94 EventRouter::Get(browser_context_
)->BroadcastEvent(event
.Pass());
97 bool ActivityLogPrivateGetExtensionActivitiesFunction::RunAsync() {
98 scoped_ptr
<activity_log_private::GetExtensionActivities::Params
> params(
99 activity_log_private::GetExtensionActivities::Params::Create(*args_
));
100 EXTENSION_FUNCTION_VALIDATE(params
.get());
102 // Get the arguments in the right format.
103 scoped_ptr
<Filter
> filter
;
104 filter
.reset(¶ms
.release()->filter
);
105 Action::ActionType action_type
= Action::ACTION_API_CALL
;
106 switch (filter
->activity_type
) {
107 case Filter::ACTIVITY_TYPE_API_CALL
:
108 action_type
= Action::ACTION_API_CALL
;
110 case Filter::ACTIVITY_TYPE_API_EVENT
:
111 action_type
= Action::ACTION_API_EVENT
;
113 case Filter::ACTIVITY_TYPE_CONTENT_SCRIPT
:
114 action_type
= Action::ACTION_CONTENT_SCRIPT
;
116 case Filter::ACTIVITY_TYPE_DOM_ACCESS
:
117 action_type
= Action::ACTION_DOM_ACCESS
;
119 case Filter::ACTIVITY_TYPE_DOM_EVENT
:
120 action_type
= Action::ACTION_DOM_EVENT
;
122 case Filter::ACTIVITY_TYPE_WEB_REQUEST
:
123 action_type
= Action::ACTION_WEB_REQUEST
;
125 case Filter::ACTIVITY_TYPE_ANY
:
127 action_type
= Action::ACTION_ANY
;
129 std::string extension_id
=
130 filter
->extension_id
.get() ? *filter
->extension_id
: std::string();
131 std::string api_call
=
132 filter
->api_call
.get() ? *filter
->api_call
: std::string();
133 std::string page_url
=
134 filter
->page_url
.get() ? *filter
->page_url
: std::string();
135 std::string arg_url
=
136 filter
->arg_url
.get() ? *filter
->arg_url
: std::string();
138 if (filter
->days_ago
.get())
139 days_ago
= *filter
->days_ago
;
141 // Call the ActivityLog.
142 ActivityLog
* activity_log
= ActivityLog::GetInstance(GetProfile());
143 DCHECK(activity_log
);
144 activity_log
->GetFilteredActions(
152 &ActivityLogPrivateGetExtensionActivitiesFunction::OnLookupCompleted
,
158 void ActivityLogPrivateGetExtensionActivitiesFunction::OnLookupCompleted(
159 scoped_ptr
<std::vector
<scoped_refptr
<Action
> > > activities
) {
160 // Convert Actions to ExtensionActivities.
161 std::vector
<linked_ptr
<ExtensionActivity
> > result_arr
;
162 for (std::vector
<scoped_refptr
<Action
> >::iterator it
= activities
->begin();
163 it
!= activities
->end();
165 result_arr
.push_back(
166 make_linked_ptr(it
->get()->ConvertToExtensionActivity().release()));
169 // Populate the return object.
170 scoped_ptr
<ActivityResultSet
> result_set(new ActivityResultSet
);
171 result_set
->activities
= result_arr
;
172 results_
= activity_log_private::GetExtensionActivities::Results::Create(
178 bool ActivityLogPrivateDeleteActivitiesFunction::RunAsync() {
179 scoped_ptr
<activity_log_private::DeleteActivities::Params
> params(
180 activity_log_private::DeleteActivities::Params::Create(*args_
));
181 EXTENSION_FUNCTION_VALIDATE(params
.get());
183 // Put the arguments in the right format.
184 std::vector
<int64
> action_ids
;
186 for (size_t i
= 0; i
< params
->activity_ids
.size(); i
++) {
187 if (base::StringToInt64(params
->activity_ids
[i
], &value
))
188 action_ids
.push_back(value
);
191 ActivityLog
* activity_log
= ActivityLog::GetInstance(GetProfile());
192 DCHECK(activity_log
);
193 activity_log
->RemoveActions(action_ids
);
197 bool ActivityLogPrivateDeleteDatabaseFunction::RunAsync() {
198 ActivityLog
* activity_log
= ActivityLog::GetInstance(GetProfile());
199 DCHECK(activity_log
);
200 activity_log
->DeleteDatabase();
204 bool ActivityLogPrivateDeleteUrlsFunction::RunAsync() {
205 scoped_ptr
<activity_log_private::DeleteUrls::Params
> params(
206 activity_log_private::DeleteUrls::Params::Create(*args_
));
207 EXTENSION_FUNCTION_VALIDATE(params
.get());
209 // Put the arguments in the right format.
210 std::vector
<GURL
> gurls
;
211 std::vector
<std::string
> urls
= *params
->urls
.get();
212 for (std::vector
<std::string
>::iterator it
= urls
.begin();
215 gurls
.push_back(GURL(*it
));
218 ActivityLog
* activity_log
= ActivityLog::GetInstance(GetProfile());
219 DCHECK(activity_log
);
220 activity_log
->RemoveURLs(gurls
);
224 } // namespace extensions