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

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: 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 bool WatchdogAction::Record(sql::Connection* db) {
felt 2013/07/16 06:44:03 Is Record used anywhere? could you just delete it
mvrable 2013/07/16 18:12:35 It needs to be defined since the function is pure
felt 2013/07/16 18:30:17 I see -- and you can't remove it from the parent c
mvrable 2013/07/16 18:38:39 Right. (Or possibly changed in the patch after th
42 } else { 54 // This methods isn't used and will go away entirely soon once database
43 // In case we ever want to add new fields, this initializes them to be 55 // writing moves to the policy objects.
44 // empty strings. 56 NOTREACHED();
45 for (int i = 0; i < num_content_fields; i++) {
46 if (!db->DoesColumnExist(table_name, content_fields[i])) {
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; 57 return true;
58 } 58 }
59 59
60 scoped_ptr<api::activity_log_private::ExtensionActivity>
61 WatchdogAction::ConvertToExtensionActivity() {
62 scoped_ptr<api::activity_log_private::ExtensionActivity> result;
63 return result.Pass();
64 }
65
66 std::string WatchdogAction::PrintForDebug() {
67 std::string result = "ID=" + extension_id() + " CATEGORY=";
68 switch (action_type_) {
69 case ACTION_API:
70 case ACTION_API_URL:
71 result += "api_call";
72 break;
73 case ACTION_API_EVENT:
74 result += "api_event_callback";
75 break;
76 case ACTION_WEB_REQUEST:
77 result += "webrequest";
78 break;
79 case ACTION_CONTENT_SCRIPT:
80 result += "content_script";
81 break;
82 case ACTION_API_BLOCKED:
83 result += "api_blocked";
84 break;
85 case ACTION_DOM_EVENT:
86 result += "dom_event";
87 break;
88 case ACTION_DOM_XHR:
89 result += "dom_xhr";
90 break;
91 case ACTION_DOM_OTHER:
92 result += "dom_other";
93 break;
94 default:
95 result += base::StringPrintf("type%d", static_cast<int>(action_type_));
96 }
97
98 result += " API=" + api_name_;
99 if (args_.get()) {
100 result += " ARGS=" + Serialize(args_.get());
101 }
102 if (page_url_.is_valid()) {
103 result += " PAGE_URL=" + page_url_.spec();
104 }
105 if (arg_url_.is_valid()) {
106 result += " ARG_URL=" + arg_url_.spec();
107 }
108 if (other_.get()) {
109 result += " OTHER=" + Serialize(other_.get());
110 }
111
112 return result;
113 }
114
60 } // namespace extensions 115 } // namespace extensions
61
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698