Home | History | Annotate | Download | only in bookmarks
      1 // Copyright (c) 2012 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_UI_BOOKMARKS_BOOKMARK_PROMPT_CONTROLLER_H_
      6 #define CHROME_BROWSER_UI_BOOKMARKS_BOOKMARK_PROMPT_CONTROLLER_H_
      7 
      8 #include "base/basictypes.h"
      9 #include "base/time/time.h"
     10 #include "chrome/browser/common/cancelable_request.h"
     11 #include "chrome/browser/history/history_types.h"
     12 #include "chrome/browser/ui/browser_list_observer.h"
     13 #include "chrome/browser/ui/tabs/tab_strip_model_observer.h"
     14 #include "content/public/browser/notification_observer.h"
     15 #include "content/public/browser/notification_registrar.h"
     16 #include "url/gurl.h"
     17 
     18 class Browser;
     19 class PrefService;
     20 
     21 namespace content {
     22 class WebContents;
     23 }
     24 
     25 // BookmarkPromptController is a kind of singleton object held by
     26 // BrowserProcessImpl, and controls showing bookmark prompt for frequently
     27 // visited URLs.
     28 class BookmarkPromptController : public chrome::BrowserListObserver,
     29                                  public content::NotificationObserver,
     30                                  public TabStripModelObserver {
     31  public:
     32   // When impression count is greater than |kMaxPromptImpressionCount|, we
     33   // don't display bookmark prompt anymore.
     34   static const int kMaxPromptImpressionCount;
     35 
     36   // When user visited the URL 10 times, we show the bookmark prompt.
     37   static const int kVisitCountForPermanentTrigger;
     38 
     39   // When user visited the URL 3 times last 24 hours, we show the bookmark
     40   // prompt.
     41   static const int kVisitCountForSessionTrigger;
     42 
     43   // An instance of BookmarkPromptController is constructed only if bookmark
     44   // feature enabled.
     45   BookmarkPromptController();
     46   virtual ~BookmarkPromptController();
     47 
     48   // Invoked when bookmark is added for |url| by clicking star icon in
     49   // |browser|. Note: Clicking bookmark menu item in action box is also
     50   // considered as clicking star icon.
     51   static void AddedBookmark(Browser* browser, const GURL& url);
     52 
     53   // Invoked when bookmark prompt is closing.
     54   static void ClosingBookmarkPrompt();
     55 
     56   // Disable bookmark prompt feature in a profile in |prefs|.
     57   static void DisableBookmarkPrompt(PrefService* prefs);
     58 
     59   // True if bookmark prompt feature is enabled, otherwise false.
     60   static bool IsEnabled();
     61 
     62  private:
     63   // TabStripModelObserver
     64   virtual void ActiveTabChanged(content::WebContents* old_contents,
     65                                 content::WebContents* new_contents,
     66                                 int index,
     67                                 int reason) OVERRIDE;
     68 
     69   void AddedBookmarkInternal(Browser* browser, const GURL& url);
     70   void ClosingBookmarkPromptInternal();
     71 
     72    // content::NotificationObserver:
     73   virtual void Observe(int type,
     74                        const content::NotificationSource& source,
     75                        const content::NotificationDetails& details) OVERRIDE;
     76 
     77   // chrome::BrowserListObserver
     78   virtual void OnBrowserRemoved(Browser* browser) OVERRIDE;
     79   virtual void OnBrowserSetLastActive(Browser* browser) OVERRIDE;
     80 
     81   // Callback for the HistoryService::QueryURL
     82   void OnDidQueryURL(CancelableRequestProvider::Handle handle,
     83                      bool success,
     84                      const history::URLRow* url_row,
     85                      history::VisitVector* visits);
     86 
     87   // Set current active browser cache to |browser|. |browser| can be null.
     88   void SetBrowser(Browser* browser);
     89 
     90   // Set current active WebContents cache from |web_contents|. |web_contents|
     91   // can be null.
     92   void SetWebContents(content::WebContents* web_contents);
     93 
     94   // Current active browser cache which we will display the prompt on it.
     95   Browser* browser_;
     96 
     97   // Current active WebContents cache which we will display the prompt for it.
     98   content::WebContents* web_contents_;
     99 
    100   // Remember last URL for recording metrics.
    101   GURL last_prompted_url_;
    102 
    103   // Last prompted time is used for measuring duration of prompt displaying
    104   // time.
    105   base::Time last_prompted_time_;
    106 
    107   // Start time of HistoryService::QueryURL.
    108   base::Time query_url_start_time_;
    109 
    110   CancelableRequestConsumer query_url_consumer_;
    111   content::NotificationRegistrar registrar_;
    112 
    113   DISALLOW_COPY_AND_ASSIGN(BookmarkPromptController);
    114 };
    115 
    116 #endif  // CHROME_BROWSER_UI_BOOKMARKS_BOOKMARK_PROMPT_CONTROLLER_H_
    117