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

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

Issue 19234003: Extension activity log database refactoring (step 2) (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Address some reviewer comments Created 7 years, 5 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) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 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 <string> 5 #include <string>
6 #include "base/json/json_string_value_serializer.h"
6 #include "base/logging.h" 7 #include "base/logging.h"
7 #include "base/strings/stringprintf.h" 8 #include "base/strings/stringprintf.h"
8 #include "chrome/browser/extensions/activity_log/activity_actions.h" 9 #include "chrome/browser/extensions/activity_log/activity_actions.h"
9 10
11 namespace {
12
13 std::string Serialize(const base::Value* value) {
14 std::string value_as_text;
15 if (!value) {
16 value_as_text = "null";
17 } else {
18 JSONStringValueSerializer serializer(&value_as_text);
19 serializer.SerializeAndOmitBinaryValues(*value);
20 }
21 return value_as_text;
22 }
23
24 } // namespace
25
10 namespace extensions { 26 namespace extensions {
11 27
12 using api::activity_log_private::ExtensionActivity; 28 using api::activity_log_private::ExtensionActivity;
13 29
14 const char* Action::kTableBasicFields =
15 "extension_id LONGVARCHAR NOT NULL, "
16 "time INTEGER NOT NULL";
17
18 Action::Action(const std::string& extension_id, 30 Action::Action(const std::string& extension_id,
19 const base::Time& time, 31 const base::Time& time,
20 ExtensionActivity::ActivityType activity_type) 32 ExtensionActivity::ActivityType activity_type)
21 : extension_id_(extension_id), 33 : extension_id_(extension_id),
22 time_(time), 34 time_(time),
23 activity_type_(activity_type) {} 35 activity_type_(activity_type) {}
24 36
25 // static 37 WatchdogAction::WatchdogAction(const std::string& extension_id,
26 bool Action::InitializeTableInternal(sql::Connection* db, 38 const base::Time& time,
27 const char* table_name, 39 const ActionType action_type,
28 const char* content_fields[], 40 const std::string& api_name,
29 const char* field_types[], 41 scoped_ptr<ListValue> args,
30 const int num_content_fields) { 42 const GURL& page_url,
31 if (!db->DoesTableExist(table_name)) { 43 const GURL& arg_url,
32 std::string table_creator = base::StringPrintf( 44 scoped_ptr<DictionaryValue> other)
33 "CREATE TABLE %s (%s", table_name, kTableBasicFields); 45 : Action(extension_id, time, ExtensionActivity::ACTIVITY_TYPE_CHROME),
34 for (int i = 0; i < num_content_fields; i++) { 46 action_type_(action_type),
35 table_creator += base::StringPrintf(", %s %s", 47 api_name_(api_name),
36 content_fields[i], 48 args_(args.Pass()),
37 field_types[i]); 49 page_url_(page_url),
38 } 50 arg_url_(arg_url),
39 table_creator += ")"; 51 other_(other.Pass()) {}
40 if (!db->Execute(table_creator.c_str())) 52
41 return false; 53 WatchdogAction::~WatchdogAction() {}
42 } else { 54
43 // In case we ever want to add new fields, this initializes them to be 55 bool WatchdogAction::Record(sql::Connection* db) {
44 // empty strings. 56 // This methods isn't used and will go away entirely soon once database
45 for (int i = 0; i < num_content_fields; i++) { 57 // writing moves to the policy objects.
46 if (!db->DoesColumnExist(table_name, content_fields[i])) { 58 NOTREACHED();
47 std::string table_updater = base::StringPrintf(
48 "ALTER TABLE %s ADD COLUMN %s %s; ",
49 table_name,
50 content_fields[i],
51 field_types[i]);
52 if (!db->Execute(table_updater.c_str()))
53 return false;
54 }
55 }
56 }
57 return true; 59 return true;
58 } 60 }
59 61
62 scoped_ptr<api::activity_log_private::ExtensionActivity>
63 WatchdogAction::ConvertToExtensionActivity() {
64 scoped_ptr<api::activity_log_private::ExtensionActivity> result;
65 return result.Pass();
66 }
67
68 std::string WatchdogAction::PrintForDebug() {
69 std::string result = "ID=" + extension_id() + " CATEGORY=";
70 switch (action_type_) {
71 case ACTION_API_CALL:
72 result += "api_call";
73 break;
74 case ACTION_API_EVENT:
75 result += "api_event_callback";
76 break;
77 case ACTION_WEB_REQUEST:
78 result += "webrequest";
79 break;
80 case ACTION_CONTENT_SCRIPT:
81 result += "content_script";
82 break;
83 case ACTION_API_BLOCKED:
84 result += "api_blocked";
85 break;
86 case ACTION_DOM_EVENT:
87 result += "dom_event";
88 break;
89 case ACTION_DOM_XHR:
90 result += "dom_xhr";
91 break;
92 case ACTION_DOM_ACCESS:
93 result += "dom_access";
94 break;
95 default:
96 result += base::StringPrintf("type%d", static_cast<int>(action_type_));
97 }
98
99 result += " API=" + api_name_;
100 if (args_.get()) {
101 result += " ARGS=" + Serialize(args_.get());
102 }
103 if (page_url_.is_valid()) {
104 result += " PAGE_URL=" + page_url_.spec();
105 }
106 if (arg_url_.is_valid()) {
107 result += " ARG_URL=" + arg_url_.spec();
108 }
109 if (other_.get()) {
110 result += " OTHER=" + Serialize(other_.get());
111 }
112
113 return result;
114 }
115
60 } // namespace extensions 116 } // namespace extensions
61
OLDNEW
« no previous file with comments | « chrome/browser/extensions/activity_log/activity_actions.h ('k') | chrome/browser/extensions/activity_log/activity_database.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698