app_list: Re-enable people search.
[chromium-blink-merge.git] / chrome / browser / extensions / api / activity_log_private / activity_log_private_api.cc
blobbb6fa76a6a2e343dec018d37d132523ebc6fdf32
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;
32 // static
33 BrowserContextKeyedAPIFactory<ActivityLogAPI>*
34 ActivityLogAPI::GetFactoryInstance() {
35 return g_factory.Pointer();
38 template <>
39 void
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.";
49 return;
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);
56 initialized_ = true;
59 ActivityLogAPI::~ActivityLogAPI() {
62 void ActivityLogAPI::Shutdown() {
63 if (!initialized_) { // Check for testing.
64 DVLOG(1) << "ExtensionSystem event_router does not exist.";
65 return;
67 EventRouter::Get(browser_context_)->UnregisterObserver(this);
68 activity_log_->RemoveObserver(this);
71 // static
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,
92 value.Pass()));
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(&params.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;
109 break;
110 case Filter::ACTIVITY_TYPE_API_EVENT:
111 action_type = Action::ACTION_API_EVENT;
112 break;
113 case Filter::ACTIVITY_TYPE_CONTENT_SCRIPT:
114 action_type = Action::ACTION_CONTENT_SCRIPT;
115 break;
116 case Filter::ACTIVITY_TYPE_DOM_ACCESS:
117 action_type = Action::ACTION_DOM_ACCESS;
118 break;
119 case Filter::ACTIVITY_TYPE_DOM_EVENT:
120 action_type = Action::ACTION_DOM_EVENT;
121 break;
122 case Filter::ACTIVITY_TYPE_WEB_REQUEST:
123 action_type = Action::ACTION_WEB_REQUEST;
124 break;
125 case Filter::ACTIVITY_TYPE_ANY:
126 default:
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();
137 int days_ago = -1;
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(
145 extension_id,
146 action_type,
147 api_call,
148 page_url,
149 arg_url,
150 days_ago,
151 base::Bind(
152 &ActivityLogPrivateGetExtensionActivitiesFunction::OnLookupCompleted,
153 this));
155 return true;
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();
164 ++it) {
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(
173 *result_set);
175 SendResponse(true);
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;
185 int64 value;
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);
194 return true;
197 bool ActivityLogPrivateDeleteDatabaseFunction::RunAsync() {
198 ActivityLog* activity_log = ActivityLog::GetInstance(GetProfile());
199 DCHECK(activity_log);
200 activity_log->DeleteDatabase();
201 return true;
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();
213 it != urls.end();
214 ++it) {
215 gurls.push_back(GURL(*it));
218 ActivityLog* activity_log = ActivityLog::GetInstance(GetProfile());
219 DCHECK(activity_log);
220 activity_log->RemoveURLs(gurls);
221 return true;
224 } // namespace extensions