Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / activity_log / activity_log.cc
index a9a598f..710e82b 100644 (file)
 #include "chrome/browser/extensions/activity_log/fullstream_ui_policy.h"
 #include "chrome/browser/extensions/api/activity_log_private/activity_log_private_api.h"
 #include "chrome/browser/extensions/extension_service.h"
-#include "chrome/browser/extensions/extension_system.h"
-#include "chrome/browser/extensions/extension_system_factory.h"
 #include "chrome/browser/extensions/extension_tab_util.h"
+#include "chrome/browser/extensions/install_tracker.h"
 #include "chrome/browser/extensions/install_tracker_factory.h"
 #include "chrome/browser/prefs/pref_service_syncable.h"
 #include "chrome/browser/prerender/prerender_manager.h"
 #include "chrome/browser/prerender/prerender_manager_factory.h"
-#include "chrome/browser/profiles/incognito_helpers.h"
+#include "chrome/browser/profiles/profile.h"
 #include "chrome/browser/ui/browser.h"
 #include "chrome/common/chrome_constants.h"
 #include "chrome/common/chrome_switches.h"
 #include "chrome/common/pref_names.h"
 #include "components/browser_context_keyed_service/browser_context_dependency_manager.h"
+#include "content/public/browser/browser_thread.h"
 #include "content/public/browser/web_contents.h"
+#include "extensions/browser/extension_system.h"
+#include "extensions/browser/extension_system_provider.h"
+#include "extensions/browser/extensions_browser_client.h"
 #include "extensions/common/extension.h"
 #include "third_party/re2/re2/re2.h"
 #include "url/gurl.h"
@@ -46,6 +49,7 @@ namespace {
 
 using extensions::Action;
 using constants::kArgUrlPlaceholder;
+using content::BrowserThread;
 
 // If DOM API methods start with this string, we flag them as being of type
 // DomActionType::XHR.
@@ -88,46 +92,64 @@ struct ApiInfo {
 };
 
 static const ApiInfo kApiInfoTable[] = {
-  // Tabs APIs that require tab ID translation
-  {Action::ACTION_API_CALL, "tabs.connect", 0, LOOKUP_TAB_ID, NULL},
-  {Action::ACTION_API_CALL, "tabs.detectLanguage", 0, LOOKUP_TAB_ID, NULL},
-  {Action::ACTION_API_CALL, "tabs.duplicate", 0, LOOKUP_TAB_ID, NULL},
-  {Action::ACTION_API_CALL, "tabs.executeScript", 0, LOOKUP_TAB_ID, NULL},
-  {Action::ACTION_API_CALL, "tabs.get", 0, LOOKUP_TAB_ID, NULL},
-  {Action::ACTION_API_CALL, "tabs.insertCSS", 0, LOOKUP_TAB_ID, NULL},
-  {Action::ACTION_API_CALL, "tabs.move", 0, LOOKUP_TAB_ID, NULL},
-  {Action::ACTION_API_CALL, "tabs.reload", 0, LOOKUP_TAB_ID, NULL},
-  {Action::ACTION_API_CALL, "tabs.remove", 0, LOOKUP_TAB_ID, NULL},
-  {Action::ACTION_API_CALL, "tabs.sendMessage", 0, LOOKUP_TAB_ID, NULL},
-  {Action::ACTION_API_CALL, "tabs.update", 0, LOOKUP_TAB_ID, NULL},
-
-  {Action::ACTION_API_EVENT, "tabs.onUpdated", 0, LOOKUP_TAB_ID, NULL},
-  {Action::ACTION_API_EVENT, "tabs.onMoved", 0, LOOKUP_TAB_ID, NULL},
-  {Action::ACTION_API_EVENT, "tabs.onDetached", 0, LOOKUP_TAB_ID, NULL},
-  {Action::ACTION_API_EVENT, "tabs.onAttached", 0, LOOKUP_TAB_ID, NULL},
-  {Action::ACTION_API_EVENT, "tabs.onRemoved", 0, LOOKUP_TAB_ID, NULL},
-  {Action::ACTION_API_EVENT, "tabs.onReplaced", 0, LOOKUP_TAB_ID, NULL},
-
-  // Other APIs that accept URLs as strings
-  {Action::ACTION_API_CALL, "bookmarks.create", 0, DICT_LOOKUP, "url"},
-  {Action::ACTION_API_CALL, "bookmarks.update", 1, DICT_LOOKUP, "url"},
-  {Action::ACTION_API_CALL, "cookies.get", 0, DICT_LOOKUP, "url"},
-  {Action::ACTION_API_CALL, "cookies.getAll", 0, DICT_LOOKUP, "url"},
-  {Action::ACTION_API_CALL, "cookies.remove", 0, DICT_LOOKUP, "url"},
-  {Action::ACTION_API_CALL, "cookies.set", 0, DICT_LOOKUP, "url"},
-  {Action::ACTION_API_CALL, "downloads.download", 0, DICT_LOOKUP, "url"},
-  {Action::ACTION_API_CALL, "history.addUrl", 0, DICT_LOOKUP, "url"},
-  {Action::ACTION_API_CALL, "history.deleteUrl", 0, DICT_LOOKUP, "url"},
-  {Action::ACTION_API_CALL, "history.getVisits", 0, DICT_LOOKUP, "url"},
-  {Action::ACTION_API_CALL, "webstore.install", 0, NONE, NULL},
-  {Action::ACTION_API_CALL, "windows.create", 0, DICT_LOOKUP, "url"},
-
-  {Action::ACTION_DOM_ACCESS, "Document.location", 0, NONE, NULL},
-  {Action::ACTION_DOM_ACCESS, "Location.assign", 0, NONE, NULL},
-  {Action::ACTION_DOM_ACCESS, "Location.replace", 0, NONE, NULL},
-  {Action::ACTION_DOM_ACCESS, "Window.location", 0, NONE, NULL},
-  {Action::ACTION_DOM_ACCESS, "XMLHttpRequest.open", 1, NONE, NULL},
-};
+    // Tabs APIs that require tab ID translation
+    {Action::ACTION_API_CALL, "tabs.connect", 0, LOOKUP_TAB_ID, NULL},
+    {Action::ACTION_API_CALL, "tabs.detectLanguage", 0, LOOKUP_TAB_ID, NULL},
+    {Action::ACTION_API_CALL, "tabs.duplicate", 0, LOOKUP_TAB_ID, NULL},
+    {Action::ACTION_API_CALL, "tabs.executeScript", 0, LOOKUP_TAB_ID, NULL},
+    {Action::ACTION_API_CALL, "tabs.get", 0, LOOKUP_TAB_ID, NULL},
+    {Action::ACTION_API_CALL, "tabs.insertCSS", 0, LOOKUP_TAB_ID, NULL},
+    {Action::ACTION_API_CALL, "tabs.move", 0, LOOKUP_TAB_ID, NULL},
+    {Action::ACTION_API_CALL, "tabs.reload", 0, LOOKUP_TAB_ID, NULL},
+    {Action::ACTION_API_CALL, "tabs.remove", 0, LOOKUP_TAB_ID, NULL},
+    {Action::ACTION_API_CALL, "tabs.sendMessage", 0, LOOKUP_TAB_ID, NULL},
+    {Action::ACTION_API_CALL, "tabs.update", 0, LOOKUP_TAB_ID, NULL},
+    {Action::ACTION_API_EVENT, "tabs.onUpdated", 0, LOOKUP_TAB_ID, NULL},
+    {Action::ACTION_API_EVENT, "tabs.onMoved", 0, LOOKUP_TAB_ID, NULL},
+    {Action::ACTION_API_EVENT, "tabs.onDetached", 0, LOOKUP_TAB_ID, NULL},
+    {Action::ACTION_API_EVENT, "tabs.onAttached", 0, LOOKUP_TAB_ID, NULL},
+    {Action::ACTION_API_EVENT, "tabs.onRemoved", 0, LOOKUP_TAB_ID, NULL},
+    {Action::ACTION_API_EVENT, "tabs.onReplaced", 0, LOOKUP_TAB_ID, NULL},
+
+    // Other APIs that accept URLs as strings
+    {Action::ACTION_API_CALL, "bookmarks.create", 0, DICT_LOOKUP, "url"},
+    {Action::ACTION_API_CALL, "bookmarks.update", 1, DICT_LOOKUP, "url"},
+    {Action::ACTION_API_CALL, "cookies.get", 0, DICT_LOOKUP, "url"},
+    {Action::ACTION_API_CALL, "cookies.getAll", 0, DICT_LOOKUP, "url"},
+    {Action::ACTION_API_CALL, "cookies.remove", 0, DICT_LOOKUP, "url"},
+    {Action::ACTION_API_CALL, "cookies.set", 0, DICT_LOOKUP, "url"},
+    {Action::ACTION_API_CALL, "downloads.download", 0, DICT_LOOKUP, "url"},
+    {Action::ACTION_API_CALL, "history.addUrl", 0, DICT_LOOKUP, "url"},
+    {Action::ACTION_API_CALL, "history.deleteUrl", 0, DICT_LOOKUP, "url"},
+    {Action::ACTION_API_CALL, "history.getVisits", 0, DICT_LOOKUP, "url"},
+    {Action::ACTION_API_CALL, "webstore.install", 0, NONE, NULL},
+    {Action::ACTION_API_CALL, "windows.create", 0, DICT_LOOKUP, "url"},
+    {Action::ACTION_DOM_ACCESS, "Document.location", 0, NONE, NULL},
+    {Action::ACTION_DOM_ACCESS, "HTMLButtonElement.formAction", 0, NONE, NULL},
+    {Action::ACTION_DOM_ACCESS, "HTMLEmbedElement.src", 0, NONE, NULL},
+    {Action::ACTION_DOM_ACCESS, "HTMLFormElement.action", 0, NONE, NULL},
+    {Action::ACTION_DOM_ACCESS, "HTMLFrameElement.src", 0, NONE, NULL},
+    {Action::ACTION_DOM_ACCESS, "HTMLHtmlElement.manifest", 0, NONE, NULL},
+    {Action::ACTION_DOM_ACCESS, "HTMLIFrameElement.src", 0, NONE, NULL},
+    {Action::ACTION_DOM_ACCESS, "HTMLImageElement.longDesc", 0, NONE, NULL},
+    {Action::ACTION_DOM_ACCESS, "HTMLImageElement.src", 0, NONE, NULL},
+    {Action::ACTION_DOM_ACCESS, "HTMLImageElement.lowsrc", 0, NONE, NULL},
+    {Action::ACTION_DOM_ACCESS, "HTMLInputElement.formAction", 0, NONE, NULL},
+    {Action::ACTION_DOM_ACCESS, "HTMLInputElement.src", 0, NONE, NULL},
+    {Action::ACTION_DOM_ACCESS, "HTMLLinkElement.href", 0, NONE, NULL},
+    {Action::ACTION_DOM_ACCESS, "HTMLMediaElement.src", 0, NONE, NULL},
+    {Action::ACTION_DOM_ACCESS, "HTMLMediaElement.currentSrc", 0, NONE, NULL},
+    {Action::ACTION_DOM_ACCESS, "HTMLModElement.cite", 0, NONE, NULL},
+    {Action::ACTION_DOM_ACCESS, "HTMLObjectElement.data", 0, NONE, NULL},
+    {Action::ACTION_DOM_ACCESS, "HTMLQuoteElement.cite", 0, NONE, NULL},
+    {Action::ACTION_DOM_ACCESS, "HTMLScriptElement.src", 0, NONE, NULL},
+    {Action::ACTION_DOM_ACCESS, "HTMLSourceElement.src", 0, NONE, NULL},
+    {Action::ACTION_DOM_ACCESS, "HTMLTrackElement.src", 0, NONE, NULL},
+    {Action::ACTION_DOM_ACCESS, "HTMLVideoElement.poster", 0, NONE, NULL},
+    {Action::ACTION_DOM_ACCESS, "Location.assign", 0, NONE, NULL},
+    {Action::ACTION_DOM_ACCESS, "Location.replace", 0, NONE, NULL},
+    {Action::ACTION_DOM_ACCESS, "Window.location", 0, NONE, NULL},
+    {Action::ACTION_DOM_ACCESS, "XMLHttpRequest.open", 1, NONE, NULL}};
 
 // A singleton class which provides lookups into the kApiInfoTable data
 // structure.  It inserts all data into a map on first lookup.
@@ -327,14 +349,14 @@ BrowserContextKeyedService* ActivityLogFactory::BuildServiceInstanceFor(
 
 content::BrowserContext* ActivityLogFactory::GetBrowserContextToUse(
     content::BrowserContext* context) const {
-  return chrome::GetBrowserContextRedirectedInIncognito(context);
+  return ExtensionsBrowserClient::Get()->GetOriginalContext(context);
 }
 
 ActivityLogFactory::ActivityLogFactory()
     : BrowserContextKeyedServiceFactory(
         "ActivityLog",
         BrowserContextDependencyManager::GetInstance()) {
-  DependsOn(ExtensionSystemFactory::GetInstance());
+  DependsOn(ExtensionsBrowserClient::Get()->GetExtensionSystemFactory());
   DependsOn(InstallTrackerFactory::GetInstance());
 }
 
@@ -393,10 +415,6 @@ ActivityLog::ActivityLog(Profile* profile)
       FROM_HERE,
       base::Bind(&ActivityLog::InitInstallTracker, base::Unretained(this)));
 
-  EventRouter* event_router = ExtensionSystem::Get(profile_)->event_router();
-  if (event_router)
-    event_router->SetEventDispatchObserver(this);
-
 // None of this should run on Android since the AL is behind ENABLE_EXTENSION
 // checks. However, UmaPolicy can't even compile on Android because it uses
 // BrowserList and related classes that aren't compiled for Android.
@@ -617,12 +635,27 @@ void ActivityLog::OnScriptsExecuted(
   }
 }
 
-void ActivityLog::OnWillDispatchEvent(scoped_ptr<EventDispatchInfo> details) {
-  scoped_refptr<Action> action = new Action(details->extension_id,
+void ActivityLog::OnApiEventDispatched(const std::string& extension_id,
+                                       const std::string& event_name,
+                                       scoped_ptr<base::ListValue> event_args) {
+  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
+  scoped_refptr<Action> action = new Action(extension_id,
                                             base::Time::Now(),
                                             Action::ACTION_API_EVENT,
-                                            details->event_name);
-  action->set_args(details->event_args.Pass());
+                                            event_name);
+  action->set_args(event_args.Pass());
+  LogAction(action);
+}
+
+void ActivityLog::OnApiFunctionCalled(const std::string& extension_id,
+                                      const std::string& api_name,
+                                      scoped_ptr<base::ListValue> args) {
+  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
+  scoped_refptr<Action> action = new Action(extension_id,
+                                            base::Time::Now(),
+                                            Action::ACTION_API_CALL,
+                                            api_name);
+  action->set_args(args.Pass());
   LogAction(action);
 }
 
@@ -645,6 +678,12 @@ void ActivityLog::GetFilteredActions(
 
 // DELETE ACTIONS. -------------------------------------------------------------
 
+void ActivityLog::RemoveActions(const std::vector<int64>& action_ids) {
+  if (!database_policy_)
+    return;
+  database_policy_->RemoveActions(action_ids);
+}
+
 void ActivityLog::RemoveURLs(const std::vector<GURL>& restrict_urls) {
   if (!database_policy_)
     return;