<commit_before>// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_HISTORY_API_H_
#define CHROME_BROWSER_EXTENSIONS_EXTENSION_HISTORY_API_H_

#include <map>
#include <string>

#include "base/singleton.h"
#include "chrome/browser/history/history.h"
#include "chrome/browser/history/history_notifications.h"
#include "chrome/browser/extensions/extension_function.h"
#include "chrome/common/notification_registrar.h"

// Observes History service and routes the notifications as events to the
// extension system.
class ExtensionHistoryEventRouter : public NotificationObserver {
 public:
  // Single instance of the event router.
  static ExtensionHistoryEventRouter* GetInstance();

  // Safe to call multiple times.
  void ObserveProfile(Profile* profile);

 private:
  friend struct DefaultSingletonTraits<ExtensionHistoryEventRouter>;

  ExtensionHistoryEventRouter() {}
  virtual ~ExtensionHistoryEventRouter() {}

  // NotificationObserver::Observe.
  virtual void Observe(NotificationType type,
                       const NotificationSource& source,
                       const NotificationDetails& details);

  void HistoryUrlVisited(Profile* profile,
                         const history::URLVisitedDetails* details);

  void HistoryUrlsRemoved(Profile* profile,
                          const history::URLsDeletedDetails* details);

  void DispatchEvent(Profile* profile,
                     const char* event_name,
                     const std::string& json_args);

  // Used for tracking registrations to history service notifications.
  NotificationRegistrar registrar_;

  // Registered profiles.
  typedef std::map<uintptr_t, Profile*> ProfileMap;
  ProfileMap profiles_;

  DISALLOW_COPY_AND_ASSIGN(ExtensionHistoryEventRouter);
};


// Base class for history function APIs.
class HistoryFunction : public AsyncExtensionFunction {
 public:
  virtual void Run();
  virtual bool RunImpl() = 0;

  bool GetUrlFromValue(Value* value, GURL* url);
  bool GetTimeFromValue(Value* value, base::Time* time);
};

// Base class for history funciton APIs which require async interaction with
// chrome services and the extension thread.
class HistoryFunctionWithCallback : public HistoryFunction {
 public:
  // Return true if the async call was completed, false otherwise.
  virtual bool RunAsyncImpl() = 0;

  // Call this method to report the results of the async method to the caller.
  // This method calls Release().
  virtual void SendAsyncResponse();

  // Override HistoryFunction::RunImpl.
  virtual bool RunImpl();

 protected:
  // The consumer for the HistoryService callbacks.
  CancelableRequestConsumer cancelable_consumer_;

 private:
  // The actual call to SendReposne.  This is required since the semantics for
  // CancelableRequestConsumerT require it to be accessed after the call.
  void SendResponseToCallback();

  friend class NewRunnableMethod;
};

class GetVisitsHistoryFunction : public HistoryFunctionWithCallback {
 public:
  // Override HistoryFunction.
  virtual bool RunAsyncImpl();
  DECLARE_EXTENSION_FUNCTION_NAME("history.getVisits");

  // Callback for the history function to provide results.
  void QueryComplete(HistoryService::Handle request_service,
                     bool success,
                     const history::URLRow* url_row,
                     history::VisitVector* visits);
};

class SearchHistoryFunction : public HistoryFunctionWithCallback {
 public:
  virtual bool RunAsyncImpl();
  DECLARE_EXTENSION_FUNCTION_NAME("history.search");

  // Callback for the history function to provide results.
  void SearchComplete(HistoryService::Handle request_handle,<commit_msg>Remove needless friend declaration.<commit_after>// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_HISTORY_API_H_
#define CHROME_BROWSER_EXTENSIONS_EXTENSION_HISTORY_API_H_

#include <map>
#include <string>

#include "base/singleton.h"
#include "chrome/browser/history/history.h"
#include "chrome/browser/history/history_notifications.h"
#include "chrome/browser/extensions/extension_function.h"
#include "chrome/common/notification_registrar.h"

// Observes History service and routes the notifications as events to the
// extension system.
class ExtensionHistoryEventRouter : public NotificationObserver {
 public:
  // Single instance of the event router.
  static ExtensionHistoryEventRouter* GetInstance();

  // Safe to call multiple times.
  void ObserveProfile(Profile* profile);

 private:
  friend struct DefaultSingletonTraits<ExtensionHistoryEventRouter>;

  ExtensionHistoryEventRouter() {}
  virtual ~ExtensionHistoryEventRouter() {}

  // NotificationObserver::Observe.
  virtual void Observe(NotificationType type,
                       const NotificationSource& source,
                       const NotificationDetails& details);

  void HistoryUrlVisited(Profile* profile,
                         const history::URLVisitedDetails* details);

  void HistoryUrlsRemoved(Profile* profile,
                          const history::URLsDeletedDetails* details);

  void DispatchEvent(Profile* profile,
                     const char* event_name,
                     const std::string& json_args);

  // Used for tracking registrations to history service notifications.
  NotificationRegistrar registrar_;

  // Registered profiles.
  typedef std::map<uintptr_t, Profile*> ProfileMap;
  ProfileMap profiles_;

  DISALLOW_COPY_AND_ASSIGN(ExtensionHistoryEventRouter);
};


// Base class for history function APIs.
class HistoryFunction : public AsyncExtensionFunction {
 public:
  virtual void Run();
  virtual bool RunImpl() = 0;

  bool GetUrlFromValue(Value* value, GURL* url);
  bool GetTimeFromValue(Value* value, base::Time* time);
};

// Base class for history funciton APIs which require async interaction with
// chrome services and the extension thread.
class HistoryFunctionWithCallback : public HistoryFunction {
 public:
  // Return true if the async call was completed, false otherwise.
  virtual bool RunAsyncImpl() = 0;

  // Call this method to report the results of the async method to the caller.
  // This method calls Release().
  virtual void SendAsyncResponse();

  // Override HistoryFunction::RunImpl.
  virtual bool RunImpl();

 protected:
  // The consumer for the HistoryService callbacks.
  CancelableRequestConsumer cancelable_consumer_;

 private:
  // The actual call to SendResponse.  This is required since the semantics for
  // CancelableRequestConsumerT require it to be accessed after the call.
  void SendResponseToCallback();
};

class GetVisitsHistoryFunction : public HistoryFunctionWithCallback {
 public:
  // Override HistoryFunction.
  virtual bool RunAsyncImpl();
  DECLARE_EXTENSION_FUNCTION_NAME("history.getVisits");

  // Callback for the history function to provide results.
  void QueryComplete(HistoryService::Handle request_service,
                     bool success,
                     const history::URLRow* url_row,
                     history::VisitVector* visits);
};

class SearchHistoryFunction : public HistoryFunctionWithCallback {
 public:
  virtual bool RunAsyncImpl();
  DECLARE_EXTENSION_FUNCTION_NAME("history.search");

  // Callback for the history function to provide results.
  void SearchComplete(HistoryService::Handle request_handle,
{'fraction_non_alphanumeric': 0.07453252567816697, 'fraction_numerical': 0.0015801948907031868, 'mean_word_length': 3.954337899543379, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6432828', 'n_tokens_mistral': 2000, 'n_tokens_neox': 1883, 'n_words': 629}