Home | History | Annotate | Download | only in activity_log
      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
      3 // found in the LICENSE file.
      4 
      5 #ifndef CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_ACTIVITY_ACTIONS_H_
      6 #define CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_ACTIVITY_ACTIONS_H_
      7 
      8 #include <string>
      9 #include <vector>
     10 
     11 #include "base/memory/ref_counted_memory.h"
     12 #include "base/time/time.h"
     13 #include "base/values.h"
     14 #include "chrome/browser/profiles/profile.h"
     15 #include "chrome/common/extensions/api/activity_log_private.h"
     16 #include "sql/connection.h"
     17 #include "sql/statement.h"
     18 #include "sql/transaction.h"
     19 #include "url/gurl.h"
     20 
     21 namespace extensions {
     22 
     23 // This is the interface for extension actions that are to be recorded in
     24 // the activity log.
     25 class Action : public base::RefCountedThreadSafe<Action> {
     26  public:
     27   // Types of log entries that can be stored.  The numeric values are stored in
     28   // the database, so keep them stable.  Append values only.
     29   enum ActionType {
     30     ACTION_API_CALL = 0,
     31     ACTION_API_EVENT = 1,
     32     ACTION_API_BLOCKED = 2,
     33     ACTION_CONTENT_SCRIPT = 3,
     34     ACTION_DOM_ACCESS = 4,
     35     ACTION_DOM_EVENT = 5,
     36     ACTION_WEB_REQUEST = 6,
     37   };
     38 
     39   // A useful shorthand for methods that take or return collections of Action
     40   // objects.
     41   typedef std::vector<scoped_refptr<Action> > ActionVector;
     42 
     43   // Creates a new activity log Action object.  The extension_id, time, and
     44   // type are immutable.  All other fields can be filled in with the
     45   // accessors/mutators below.
     46   Action(const std::string& extension_id,
     47          const base::Time& time,
     48          const ActionType action_type,
     49          const std::string& api_name);
     50 
     51   // Creates and returns a mutable copy of an Action.
     52   scoped_refptr<Action> Clone() const;
     53 
     54   // The extension which caused this record to be generated.
     55   const std::string& extension_id() const { return extension_id_; }
     56 
     57   // The time the record was generated (or some approximation).
     58   const base::Time& time() const { return time_; }
     59 
     60   // The ActionType distinguishes different classes of actions that can be
     61   // logged, and determines which other fields are expected to be filled in.
     62   ActionType action_type() const { return action_type_; }
     63 
     64   // The specific API call used or accessed, for example "chrome.tabs.get".
     65   const std::string& api_name() const { return api_name_; }
     66   void set_api_name(const std::string api_name) { api_name_ = api_name; }
     67 
     68   // Any applicable arguments.  This might be null to indicate no data
     69   // available (a distinct condition from an empty argument list).
     70   // mutable_args() returns a pointer to the list stored in the Action which
     71   // can be modified in place; if the list was null an empty list is created
     72   // first.
     73   const ListValue* args() const { return args_.get(); }
     74   void set_args(scoped_ptr<ListValue> args);
     75   ListValue* mutable_args();
     76 
     77   // The URL of the page which was modified or accessed.
     78   const GURL& page_url() const { return page_url_; }
     79   void set_page_url(const GURL& page_url);
     80 
     81   // The title of the above page if available.
     82   const std::string& page_title() const { return page_title_; }
     83   void set_page_title(const std::string& title) { page_title_ = title; }
     84 
     85   // A URL which appears in the arguments of the API call, if present.
     86   const GURL& arg_url() const { return arg_url_; }
     87   void set_arg_url(const GURL& arg_url);
     88 
     89   // Get or set a flag indicating whether the page or argument values above
     90   // refer to incognito pages.
     91   bool page_incognito() const { return page_incognito_; }
     92   void set_page_incognito(bool incognito) { page_incognito_ = incognito; }
     93   bool arg_incognito() const { return arg_incognito_; }
     94   void set_arg_incognito(bool incognito) { arg_incognito_ = incognito; }
     95 
     96   // A dictionary where any additional data can be stored.
     97   const DictionaryValue* other() const { return other_.get(); }
     98   void set_other(scoped_ptr<DictionaryValue> other);
     99   DictionaryValue* mutable_other();
    100 
    101   // Helper methods for serializing and deserializing URLs into strings.  If
    102   // the URL is marked as incognito, then the string is prefixed with
    103   // kIncognitoUrl ("<incognito>").
    104   std::string SerializePageUrl() const;
    105   void ParsePageUrl(const std::string& url);
    106   std::string SerializeArgUrl() const;
    107   void ParseArgUrl(const std::string& url);
    108 
    109   // Flatten the activity's type-specific fields into an ExtensionActivity.
    110   scoped_ptr<api::activity_log_private::ExtensionActivity>
    111       ConvertToExtensionActivity();
    112 
    113   // Print an action as a regular string for debugging purposes.
    114   virtual std::string PrintForDebug() const;
    115 
    116  protected:
    117   virtual ~Action();
    118 
    119  private:
    120   friend class base::RefCountedThreadSafe<Action>;
    121 
    122   std::string extension_id_;
    123   base::Time time_;
    124   api::activity_log_private::ExtensionActivity::ActivityType activity_type_;
    125   ActionType action_type_;
    126   std::string api_name_;
    127   scoped_ptr<ListValue> args_;
    128   GURL page_url_;
    129   std::string page_title_;
    130   bool page_incognito_;
    131   GURL arg_url_;
    132   bool arg_incognito_;
    133   scoped_ptr<DictionaryValue> other_;
    134 
    135   DISALLOW_COPY_AND_ASSIGN(Action);
    136 };
    137 
    138 }  // namespace extensions
    139 
    140 #endif  // CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_ACTIVITY_ACTIONS_H_
    141