| OLD | NEW | 
|---|
| 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 "chrome/browser/extensions/activity_log/activity_actions.h" | 5 #include "chrome/browser/extensions/activity_log/activity_actions.h" | 
| 6 | 6 | 
| 7 #include <string> | 7 #include <string> | 
| 8 | 8 | 
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" | 
| 10 #include "base/json/json_string_value_serializer.h" | 10 #include "base/json/json_string_value_serializer.h" | 
| 11 #include "base/logging.h" | 11 #include "base/logging.h" | 
| 12 #include "base/memory/singleton.h" | 12 #include "base/memory/singleton.h" | 
| 13 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" | 
|  | 14 #include "base/strings/string_util.h" | 
| 14 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" | 
| 15 #include "chrome/browser/extensions/activity_log/activity_action_constants.h" | 16 #include "chrome/browser/extensions/activity_log/activity_action_constants.h" | 
| 16 #include "chrome/browser/extensions/activity_log/api_name_constants.h" |  | 
| 17 #include "chrome/browser/extensions/activity_log/fullstream_ui_policy.h" | 17 #include "chrome/browser/extensions/activity_log/fullstream_ui_policy.h" | 
| 18 #include "chrome/browser/ui/browser.h" | 18 #include "chrome/browser/ui/browser.h" | 
| 19 #include "chrome/common/chrome_switches.h" | 19 #include "chrome/common/chrome_switches.h" | 
| 20 #include "content/public/browser/browser_thread.h" | 20 #include "content/public/browser/browser_thread.h" | 
| 21 #include "content/public/browser/web_contents.h" | 21 #include "content/public/browser/web_contents.h" | 
| 22 #include "sql/statement.h" | 22 #include "sql/statement.h" | 
| 23 | 23 | 
| 24 namespace constants = activity_log_constants; | 24 namespace constants = activity_log_constants; | 
| 25 | 25 | 
| 26 namespace { | 26 namespace { | 
| 27 | 27 | 
| 28 std::string Serialize(const base::Value* value) { | 28 std::string Serialize(const base::Value* value) { | 
| 29   std::string value_as_text; | 29   std::string value_as_text; | 
| 30   if (!value) { | 30   if (!value) { | 
| 31     value_as_text = "null"; | 31     value_as_text = "null"; | 
| 32   } else { | 32   } else { | 
| 33     JSONStringValueSerializer serializer(&value_as_text); | 33     JSONStringValueSerializer serializer(&value_as_text); | 
| 34     serializer.SerializeAndOmitBinaryValues(*value); | 34     serializer.SerializeAndOmitBinaryValues(*value); | 
| 35   } | 35   } | 
| 36   return value_as_text; | 36   return value_as_text; | 
| 37 } | 37 } | 
| 38 | 38 | 
| 39 // Sets up the hashmap for mapping extension strings to "ints". The hashmap is |  | 
| 40 // only set up once because it's quite long; the value is then cached. |  | 
| 41 class APINameMap { |  | 
| 42  public: |  | 
| 43   APINameMap() { |  | 
| 44     SetupMap(); |  | 
| 45   } |  | 
| 46 |  | 
| 47   // activity_log_api_name_constants.h lists all known API calls as of 5/17. |  | 
| 48   // This code maps each of those API calls (and events) to short strings |  | 
| 49   // (integers converted to strings). They're all strings because (1) sqlite |  | 
| 50   // databases are all strings underneath anyway and (2) the Lookup function |  | 
| 51   // will simply return the original api_call string if we don't have it in our |  | 
| 52   // lookup table. |  | 
| 53   void SetupMap() { |  | 
| 54     for (size_t i = 0; |  | 
| 55          i < arraysize(activity_log_api_name_constants::kNames); |  | 
| 56          ++i) { |  | 
| 57       std::string name = |  | 
| 58           std::string(activity_log_api_name_constants::kNames[i]); |  | 
| 59       std::string num = base::IntToString(i); |  | 
| 60       names_to_nums_[name] = num; |  | 
| 61       nums_to_names_[num] = name; |  | 
| 62     } |  | 
| 63   } |  | 
| 64 |  | 
| 65   static APINameMap* GetInstance() { |  | 
| 66     return Singleton<APINameMap>::get(); |  | 
| 67   } |  | 
| 68 |  | 
| 69   // This matches an api call to a number, if it's in the lookup table. If not, |  | 
| 70   // it returns the original api call. |  | 
| 71   const std::string& ApiToShortname(const std::string& api_call) { |  | 
| 72     std::map<std::string, std::string>::iterator it = |  | 
| 73         names_to_nums_.find(api_call); |  | 
| 74     if (it == names_to_nums_.end()) |  | 
| 75       return api_call; |  | 
| 76     else |  | 
| 77       return it->second; |  | 
| 78   } |  | 
| 79 |  | 
| 80   // This matches a number to an API call -- it's the opposite of |  | 
| 81   // ApiToShortname. |  | 
| 82   const std::string& ShortnameToApi(const std::string& shortname) { |  | 
| 83     std::map<std::string, std::string>::iterator it = |  | 
| 84         nums_to_names_.find(shortname); |  | 
| 85     if (it == nums_to_names_.end()) |  | 
| 86       return shortname; |  | 
| 87     else |  | 
| 88       return it->second; |  | 
| 89   } |  | 
| 90 |  | 
| 91  private: |  | 
| 92   std::map<std::string, std::string> names_to_nums_;  // <name, number label> |  | 
| 93   std::map<std::string, std::string> nums_to_names_;  // <number label, name> |  | 
| 94 }; |  | 
| 95 |  | 
| 96 }  // namespace | 39 }  // namespace | 
| 97 | 40 | 
| 98 namespace extensions { | 41 namespace extensions { | 
| 99 | 42 | 
| 100 using api::activity_log_private::BlockedChromeActivityDetail; | 43 using api::activity_log_private::BlockedChromeActivityDetail; | 
| 101 using api::activity_log_private::ChromeActivityDetail; | 44 using api::activity_log_private::ChromeActivityDetail; | 
| 102 using api::activity_log_private::DomActivityDetail; | 45 using api::activity_log_private::DomActivityDetail; | 
| 103 using api::activity_log_private::ExtensionActivity; | 46 using api::activity_log_private::ExtensionActivity; | 
| 104 | 47 | 
| 105 Action::Action(const std::string& extension_id, | 48 Action::Action(const std::string& extension_id, | 
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 156   other_.reset(other.release()); | 99   other_.reset(other.release()); | 
| 157 } | 100 } | 
| 158 | 101 | 
| 159 DictionaryValue* Action::mutable_other() { | 102 DictionaryValue* Action::mutable_other() { | 
| 160   if (!other_.get()) { | 103   if (!other_.get()) { | 
| 161     other_.reset(new DictionaryValue()); | 104     other_.reset(new DictionaryValue()); | 
| 162   } | 105   } | 
| 163   return other_.get(); | 106   return other_.get(); | 
| 164 } | 107 } | 
| 165 | 108 | 
|  | 109 std::string Action::SerializePageUrl() const { | 
|  | 110   return (page_incognito() ? constants::kIncognitoUrl : "") + page_url().spec(); | 
|  | 111 } | 
|  | 112 | 
|  | 113 void Action::ParsePageUrl(const std::string& url) { | 
|  | 114   set_page_incognito(StartsWithASCII(url, constants::kIncognitoUrl, true)); | 
|  | 115   if (page_incognito()) | 
|  | 116     set_page_url(GURL(url.substr(strlen(constants::kIncognitoUrl)))); | 
|  | 117   else | 
|  | 118     set_page_url(GURL(url)); | 
|  | 119 } | 
|  | 120 | 
|  | 121 std::string Action::SerializeArgUrl() const { | 
|  | 122   return (arg_incognito() ? constants::kIncognitoUrl : "") + arg_url().spec(); | 
|  | 123 } | 
|  | 124 | 
|  | 125 void Action::ParseArgUrl(const std::string& url) { | 
|  | 126   set_arg_incognito(StartsWithASCII(url, constants::kIncognitoUrl, true)); | 
|  | 127   if (arg_incognito()) | 
|  | 128     set_arg_url(GURL(url.substr(strlen(constants::kIncognitoUrl)))); | 
|  | 129   else | 
|  | 130     set_arg_url(GURL(url)); | 
|  | 131 } | 
|  | 132 | 
| 166 scoped_ptr<ExtensionActivity> Action::ConvertToExtensionActivity() { | 133 scoped_ptr<ExtensionActivity> Action::ConvertToExtensionActivity() { | 
| 167   scoped_ptr<ExtensionActivity> result(new ExtensionActivity); | 134   scoped_ptr<ExtensionActivity> result(new ExtensionActivity); | 
| 168 | 135 | 
| 169   result->extension_id.reset(new std::string(extension_id())); | 136   result->extension_id.reset(new std::string(extension_id())); | 
| 170   result->time.reset(new double(time().ToJsTime())); | 137   result->time.reset(new double(time().ToJsTime())); | 
| 171 | 138 | 
| 172   switch (action_type()) { | 139   switch (action_type()) { | 
| 173     case ACTION_API_CALL: | 140     case ACTION_API_CALL: | 
| 174     case ACTION_API_EVENT: { | 141     case ACTION_API_EVENT: { | 
| 175       ChromeActivityDetail* details = new ChromeActivityDetail; | 142       ChromeActivityDetail* details = new ChromeActivityDetail; | 
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 239     } | 206     } | 
| 240 | 207 | 
| 241     default: | 208     default: | 
| 242       LOG(WARNING) << "Bad activity log entry read from database (type=" | 209       LOG(WARNING) << "Bad activity log entry read from database (type=" | 
| 243                    << action_type_ << ")!"; | 210                    << action_type_ << ")!"; | 
| 244   } | 211   } | 
| 245 | 212 | 
| 246   return result.Pass(); | 213   return result.Pass(); | 
| 247 } | 214 } | 
| 248 | 215 | 
| 249 std::string Action::PrintForDebug() { | 216 std::string Action::PrintForDebug() const { | 
| 250   std::string result = "ID=" + extension_id() + " CATEGORY="; | 217   std::string result = "ID=" + extension_id() + " CATEGORY="; | 
| 251   switch (action_type_) { | 218   switch (action_type_) { | 
| 252     case ACTION_API_CALL: | 219     case ACTION_API_CALL: | 
| 253       result += "api_call"; | 220       result += "api_call"; | 
| 254       break; | 221       break; | 
| 255     case ACTION_API_EVENT: | 222     case ACTION_API_EVENT: | 
| 256       result += "api_event_callback"; | 223       result += "api_event_callback"; | 
| 257       break; | 224       break; | 
| 258     case ACTION_WEB_REQUEST: | 225     case ACTION_WEB_REQUEST: | 
| 259       result += "webrequest"; | 226       result += "webrequest"; | 
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 295       result += " ARG_URL=" + arg_url_.spec(); | 262       result += " ARG_URL=" + arg_url_.spec(); | 
| 296   } | 263   } | 
| 297   if (other_.get()) { | 264   if (other_.get()) { | 
| 298     result += " OTHER=" + Serialize(other_.get()); | 265     result += " OTHER=" + Serialize(other_.get()); | 
| 299   } | 266   } | 
| 300 | 267 | 
| 301   return result; | 268   return result; | 
| 302 } | 269 } | 
| 303 | 270 | 
| 304 }  // namespace extensions | 271 }  // namespace extensions | 
| OLD | NEW | 
|---|