Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(159)

Side by Side Diff: chrome/browser/extensions/activity_log/activity_log.cc

Issue 23545012: [Activity log] Rework extraction of URLs from argument lists (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Delete dead code Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/extensions/activity_log/activity_log.h" 5 #include "chrome/browser/extensions/activity_log/activity_log.h"
6 6
7 #include <set> 7 #include <set>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/command_line.h" 10 #include "base/command_line.h"
(...skipping 22 matching lines...) Expand all
33 #include "chrome/common/pref_names.h" 33 #include "chrome/common/pref_names.h"
34 #include "components/browser_context_keyed_service/browser_context_dependency_ma nager.h" 34 #include "components/browser_context_keyed_service/browser_context_dependency_ma nager.h"
35 #include "content/public/browser/web_contents.h" 35 #include "content/public/browser/web_contents.h"
36 #include "third_party/re2/re2/re2.h" 36 #include "third_party/re2/re2/re2.h"
37 #include "url/gurl.h" 37 #include "url/gurl.h"
38 38
39 namespace constants = activity_log_constants; 39 namespace constants = activity_log_constants;
40 40
41 namespace { 41 namespace {
42 42
43 // Concatenate arguments. 43 using extensions::Action;
44 std::string MakeArgList(const base::ListValue* args) { 44 using constants::kArgUrlPlaceholder;
45 std::string call_signature; 45
46 base::ListValue::const_iterator it = args->begin(); 46 // Specifies a possible action to take to get an extracted URL in the ApiInfo
47 for (; it != args->end(); ++it) { 47 // structure below.
48 std::string arg; 48 enum Transformation {
49 JSONStringValueSerializer serializer(&arg); 49 NONE,
50 if (serializer.SerializeAndOmitBinaryValues(**it)) { 50 DICT_LOOKUP,
51 if (it != args->begin()) 51 LOOKUP_TAB_ID,
52 call_signature += ", "; 52 };
53 call_signature += arg; 53
54 // Information about specific Chrome and DOM APIs, such as which contain
55 // arguments that should be extracted into the arg_url field of an Action.
56 struct ApiInfo {
57 // The lookup key consists of the action_type and api_name in the Action
58 // object.
59 Action::ActionType action_type;
60 const char* api_name;
61
62 // If non-negative, an index into args might contain a URL to be extracted
63 // into arg_url.
64 int arg_url_index;
65
66 // A transformation to apply to the data found at index arg_url_index in the
67 // argument list.
68 //
69 // If NONE, the data is expected to be a string which is treated as a URL.
70 //
71 // If LOOKUP_TAB_ID, the data is either an integer which is treated as a tab
72 // ID and translated (in the context of a provided Profile), or a list of tab
73 // IDs which are translated.
74 //
75 // If DICT_LOOKUP, the data is expected to be a dictionary, and
76 // arg_url_dict_path is a path (list of keys delimited by ".") where a URL
77 // string is to be found.
78 Transformation arg_url_transform;
79 const char* arg_url_dict_path;
80 };
81
82 static const ApiInfo kApiInfoTable[] = {
83 // Tabs APIs that require tab ID translation
84 {Action::ACTION_API_CALL, "tabs.connect", 0, LOOKUP_TAB_ID, NULL},
85 {Action::ACTION_API_CALL, "tabs.detectLanguage", 0, LOOKUP_TAB_ID, NULL},
86 {Action::ACTION_API_CALL, "tabs.duplicate", 0, LOOKUP_TAB_ID, NULL},
87 {Action::ACTION_API_CALL, "tabs.executeScript", 0, LOOKUP_TAB_ID, NULL},
88 {Action::ACTION_API_CALL, "tabs.get", 0, LOOKUP_TAB_ID, NULL},
89 {Action::ACTION_API_CALL, "tabs.insertCSS", 0, LOOKUP_TAB_ID, NULL},
90 {Action::ACTION_API_CALL, "tabs.move", 0, LOOKUP_TAB_ID, NULL},
91 {Action::ACTION_API_CALL, "tabs.reload", 0, LOOKUP_TAB_ID, NULL},
92 {Action::ACTION_API_CALL, "tabs.remove", 0, LOOKUP_TAB_ID, NULL},
93 {Action::ACTION_API_CALL, "tabs.sendMessage", 0, LOOKUP_TAB_ID, NULL},
94 {Action::ACTION_API_CALL, "tabs.update", 0, LOOKUP_TAB_ID, NULL},
95
96 {Action::ACTION_API_EVENT, "tabs.onUpdated", 0, LOOKUP_TAB_ID, NULL},
97 {Action::ACTION_API_EVENT, "tabs.onMoved", 0, LOOKUP_TAB_ID, NULL},
98 {Action::ACTION_API_EVENT, "tabs.onDetached", 0, LOOKUP_TAB_ID, NULL},
99 {Action::ACTION_API_EVENT, "tabs.onAttached", 0, LOOKUP_TAB_ID, NULL},
100 {Action::ACTION_API_EVENT, "tabs.onRemoved", 0, LOOKUP_TAB_ID, NULL},
101 {Action::ACTION_API_EVENT, "tabs.onReplaced", 0, LOOKUP_TAB_ID, NULL},
102
103 // Other APIs that accept URLs as strings
104 {Action::ACTION_API_CALL, "windows.create", 0, DICT_LOOKUP, "url"},
105
106 {Action::ACTION_DOM_ACCESS, "Location.assign", 0, NONE, NULL},
107 {Action::ACTION_DOM_ACCESS, "Location.replace", 0, NONE, NULL},
108 {Action::ACTION_DOM_ACCESS, "XMLHttpRequest.open", 1, NONE, NULL},
109 };
110
111 // A singleton class which provides lookups into the kApiInfoTable data
112 // structure. It inserts all data into a map on first lookup.
113 class ApiInfoDatabase {
114 public:
115 static ApiInfoDatabase* GetInstance() {
116 return Singleton<ApiInfoDatabase>::get();
117 }
118
119 // Retrieves an ApiInfo record for the given Action type. Returns either a
120 // pointer to the record, or NULL if no such record was found.
121 const ApiInfo* Lookup(Action::ActionType action_type,
122 const std::string& api_name) const {
123 std::map<std::string, const ApiInfo*>::const_iterator i =
124 api_database_.find(api_name);
125 if (i == api_database_.end())
126 return NULL;
127 if (i->second->action_type != action_type)
128 return NULL;
129 return i->second;
130 }
131
132 private:
133 ApiInfoDatabase() {
134 for (size_t i = 0; i < arraysize(kApiInfoTable); i++) {
135 const ApiInfo* info = &kApiInfoTable[i];
136 api_database_[info->api_name] = info;
54 } 137 }
55 } 138 }
56 return call_signature; 139 virtual ~ApiInfoDatabase() {}
57 }
58 140
59 // Gets the URL for a given tab ID. Helper method for LookupTabId. Returns 141 // The map is keyed by API name only, since API names aren't be repeated
142 // across multiple action types in kApiInfoTable. However, the action type
143 // should still be checked before returning a positive match.
144 std::map<std::string, const ApiInfo*> api_database_;
145
146 friend struct DefaultSingletonTraits<ApiInfoDatabase>;
147 DISALLOW_COPY_AND_ASSIGN(ApiInfoDatabase);
148 };
149
150 // Gets the URL for a given tab ID. Helper method for ExtractUrls. Returns
60 // true if able to perform the lookup. The URL is stored to *url, and 151 // true if able to perform the lookup. The URL is stored to *url, and
61 // *is_incognito is set to indicate whether the URL is for an incognito tab. 152 // *is_incognito is set to indicate whether the URL is for an incognito tab.
62 bool GetUrlForTabId(int tab_id, 153 bool GetUrlForTabId(int tab_id,
63 Profile* profile, 154 Profile* profile,
64 GURL* url, 155 GURL* url,
65 bool* is_incognito) { 156 bool* is_incognito) {
66 content::WebContents* contents = NULL; 157 content::WebContents* contents = NULL;
67 Browser* browser = NULL; 158 Browser* browser = NULL;
68 bool found = ExtensionTabUtil::GetTabById(tab_id, 159 bool found = ExtensionTabUtil::GetTabById(tab_id,
69 profile, 160 profile,
70 true, // search incognito tabs too 161 true, // search incognito tabs too
71 &browser, 162 &browser,
72 NULL, 163 NULL,
73 &contents, 164 &contents,
74 NULL); 165 NULL);
75 if (found) { 166 if (found) {
76 *url = contents->GetURL(); 167 *url = contents->GetURL();
77 *is_incognito = browser->profile()->IsOffTheRecord(); 168 *is_incognito = browser->profile()->IsOffTheRecord();
78 return true; 169 return true;
79 } else { 170 } else {
80 return false; 171 return false;
81 } 172 }
82 } 173 }
83 174
84 // Translate tab IDs to URLs in tabs API calls. Mutates the Action object in 175 // Performs processing of the Action object to extract URLs from the argument
85 // place. There is a small chance that the URL translation could be wrong, if 176 // list and translate tab IDs to URLs, according to the API call metadata in
86 // the tab has already been navigated by the time of invocation. 177 // kApiInfoTable. Mutates the Action object in place. There is a small chance
178 // that the tab id->URL translation could be wrong, if the tab has already been
179 // navigated by the time of invocation.
87 // 180 //
88 // If a single tab ID is translated to a URL, the URL is stored into arg_url 181 // Any extracted URL is stored into the arg_url field of the action, and the
89 // where it can more easily be searched for in the database. For APIs that 182 // URL in the argument list is replaced with the marker value "<arg_url>". For
90 // take a list of tab IDs, replace each tab ID with the URL in the argument 183 // APIs that take a list of tab IDs, extracts the first valid URL into arg_url
91 // list; we can only extract a single URL into arg_url so arbitrarily pull out 184 // and overwrites the other tab IDs in the argument list with the translated
92 // the first one. 185 // URL.
93 void LookupTabIds(scoped_refptr<extensions::Action> action, Profile* profile) { 186 void ExtractUrls(scoped_refptr<Action> action, Profile* profile) {
94 const std::string& api_call = action->api_name(); 187 const ApiInfo* api_info = ApiInfoDatabase::GetInstance()->Lookup(
95 if (api_call == "tabs.get" || // api calls, ID as int 188 action->action_type(), action->api_name());
96 api_call == "tabs.connect" || 189 if (api_info == NULL)
97 api_call == "tabs.sendMessage" || 190 return;
98 api_call == "tabs.duplicate" || 191
99 api_call == "tabs.update" || 192 int url_index = api_info->arg_url_index;
100 api_call == "tabs.reload" || 193
101 api_call == "tabs.detectLanguage" || 194 if (!action->args() || url_index < 0 ||
not at google - send to devlin 2013/09/06 22:05:05 it doesn't seem that url_index can ever be < 0.
mvrable 2013/09/06 22:32:49 Not currently. I was leaving that as an option (d
102 api_call == "tabs.executeScript" || 195 static_cast<size_t>(url_index) >= action->args()->GetSize())
103 api_call == "tabs.insertCSS" || 196 return;
104 api_call == "tabs.move" || // api calls, IDs in array 197
105 api_call == "tabs.remove" || 198 // Do not overwrite an existing arg_url value in the Action, so that callers
106 api_call == "tabs.onUpdated" || // events, ID as int 199 // have the option of doing custom arg_url extraction.
107 api_call == "tabs.onMoved" || 200 if (action->arg_url().is_valid())
108 api_call == "tabs.onDetached" || 201 return;
109 api_call == "tabs.onAttached" || 202
110 api_call == "tabs.onRemoved" || 203 GURL arg_url;
111 api_call == "tabs.onReplaced") { 204 bool arg_incognito = action->page_incognito();
205
206 if (api_info->arg_url_transform == NONE) {
not at google - send to devlin 2013/09/06 22:05:05 use switch
mvrable 2013/09/06 22:32:49 Done.
207 // No translation needed; just extract the URL directly from a raw string
208 // or from a dictionary.
209 std::string url_string;
210 if (action->args()->GetString(url_index, &url_string)) {
211 arg_url = GURL(url_string);
212 if (arg_url.is_valid()) {
213 action->mutable_args()->Set(url_index,
214 new StringValue(kArgUrlPlaceholder));
215 }
216 }
217 } else if (api_info->arg_url_transform == DICT_LOOKUP) {
218 // Look up the URL from a dictionary at the specified location.
219 DictionaryValue* dict = NULL;
220 std::string url_string;
221 CHECK(api_info->arg_url_dict_path);
222
223 if (action->mutable_args()->GetDictionary(url_index, &dict) &&
224 dict->GetString(api_info->arg_url_dict_path, &url_string)) {
225 arg_url = GURL(url_string);
226 if (arg_url.is_valid())
227 dict->SetString(api_info->arg_url_dict_path, kArgUrlPlaceholder);
228 }
229 } else if (api_info->arg_url_transform == LOOKUP_TAB_ID) {
230 // Translation of tab IDs to URLs has been requested. There are two cases
231 // to consider: either a single integer or a list of integers (when
232 // multiple tabs are manipulated).
112 int tab_id; 233 int tab_id;
113 base::ListValue* id_list; 234 base::ListValue* tab_list = NULL;
114 base::ListValue* args = action->mutable_args(); 235 if (action->args()->GetInteger(url_index, &tab_id)) {
115 if (args->GetInteger(0, &tab_id)) {
116 // Single tab ID to translate. 236 // Single tab ID to translate.
117 GURL url; 237 GetUrlForTabId(tab_id, profile, &arg_url, &arg_incognito);
118 bool is_incognito; 238 if (arg_url.is_valid()) {
119 if (GetUrlForTabId(tab_id, profile, &url, &is_incognito)) { 239 action->mutable_args()->Set(url_index,
120 action->set_arg_url(url); 240 new StringValue(kArgUrlPlaceholder));
121 action->set_arg_incognito(is_incognito);
122 } 241 }
123 } else if ((api_call == "tabs.move" || api_call == "tabs.remove") && 242 } else if (action->mutable_args()->GetList(url_index, &tab_list)) {
124 args->GetList(0, &id_list)) { 243 // A list of possible IDs to translate. Work through in reverse order
125 // Array of tab IDs to translate. 244 // so the last one translated is left in arg_url.
126 for (int i = 0; i < static_cast<int>(id_list->GetSize()); ++i) { 245 int extracted_index = -1; // Which item in the list is stored to arg_url?
127 if (id_list->GetInteger(i, &tab_id)) { 246 for (int i = tab_list->GetSize() - 1; i >= 0; --i) {
128 GURL url; 247 if (tab_list->GetInteger(i, &tab_id) &&
129 bool is_incognito; 248 GetUrlForTabId(tab_id, profile, &arg_url, &arg_incognito)) {
130 if (GetUrlForTabId(tab_id, profile, &url, &is_incognito) && 249 if (!arg_incognito)
131 !is_incognito) { 250 tab_list->Set(i, new base::StringValue(arg_url.spec()));
132 id_list->Set(i, new base::StringValue(url.spec())); 251 extracted_index = i;
133 if (i == 0) {
134 action->set_arg_url(url);
135 action->set_arg_incognito(is_incognito);
136 }
137 }
138 } else {
139 LOG(ERROR) << "The tab ID array is malformed at index " << i;
140 } 252 }
141 } 253 }
254 if (extracted_index >= 0)
255 tab_list->Set(extracted_index, new StringValue(kArgUrlPlaceholder));
142 } 256 }
143 } 257 }
258
259 if (arg_url.is_valid()) {
260 action->set_arg_incognito(arg_incognito);
261 action->set_arg_url(arg_url);
262 }
144 } 263 }
145 264
146 } // namespace 265 } // namespace
147 266
148 namespace extensions { 267 namespace extensions {
149 268
150 // ActivityLogFactory 269 // ActivityLogFactory
151 270
152 ActivityLogFactory* ActivityLogFactory::GetInstance() { 271 ActivityLogFactory* ActivityLogFactory::GetInstance() {
153 return Singleton<ActivityLogFactory>::get(); 272 return Singleton<ActivityLogFactory>::get();
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after
345 } 464 }
346 465
347 // LOG ACTIONS. ---------------------------------------------------------------- 466 // LOG ACTIONS. ----------------------------------------------------------------
348 467
349 void ActivityLog::LogAction(scoped_refptr<Action> action) { 468 void ActivityLog::LogAction(scoped_refptr<Action> action) {
350 if (ActivityLogAPI::IsExtensionWhitelisted(action->extension_id())) 469 if (ActivityLogAPI::IsExtensionWhitelisted(action->extension_id()))
351 return; 470 return;
352 471
353 // Perform some preprocessing of the Action data: convert tab IDs to URLs and 472 // Perform some preprocessing of the Action data: convert tab IDs to URLs and
354 // mask out incognito URLs if appropriate. 473 // mask out incognito URLs if appropriate.
355 if ((action->action_type() == Action::ACTION_API_CALL || 474 ExtractUrls(action, profile_);
356 action->action_type() == Action::ACTION_API_EVENT) &&
357 StartsWithASCII(action->api_name(), "tabs.", true)) {
358 LookupTabIds(action, profile_);
359 }
360 475
361 if (IsDatabaseEnabled() && policy_) 476 if (IsDatabaseEnabled() && policy_)
362 policy_->ProcessAction(action); 477 policy_->ProcessAction(action);
363 if (IsWatchdogAppActive()) 478 if (IsWatchdogAppActive())
364 observers_->Notify(&Observer::OnExtensionActivity, action); 479 observers_->Notify(&Observer::OnExtensionActivity, action);
365 if (testing_mode_) 480 if (testing_mode_)
366 LOG(INFO) << action->PrintForDebug(); 481 LOG(INFO) << action->PrintForDebug();
367 } 482 }
368 483
369 void ActivityLog::OnScriptsExecuted( 484 void ActivityLog::OnScriptsExecuted(
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
466 RemoveURLs(urls); 581 RemoveURLs(urls);
467 } 582 }
468 583
469 void ActivityLog::DeleteDatabase() { 584 void ActivityLog::DeleteDatabase() {
470 if (!policy_) 585 if (!policy_)
471 return; 586 return;
472 policy_->DeleteDatabase(); 587 policy_->DeleteDatabase();
473 } 588 }
474 589
475 } // namespace extensions 590 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698