Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / webui / ntp / new_tab_ui.h
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_WEBUI_NTP_NEW_TAB_UI_H_
6 #define CHROME_BROWSER_UI_WEBUI_NTP_NEW_TAB_UI_H_
7
8 #include <string>
9
10 #include "base/gtest_prod_util.h"
11 #include "base/prefs/pref_change_registrar.h"
12 #include "base/time/time.h"
13 #include "base/timer/timer.h"
14 #include "content/public/browser/notification_observer.h"
15 #include "content/public/browser/notification_registrar.h"
16 #include "content/public/browser/url_data_source.h"
17 #include "content/public/browser/web_contents.h"
18 #include "content/public/browser/web_contents_observer.h"
19 #include "content/public/browser/web_ui_controller.h"
20
21 class GURL;
22 class Profile;
23
24 namespace base {
25 class DictionaryValue;
26 }
27
28 namespace user_prefs {
29 class PrefRegistrySyncable;
30 }
31
32 // The WebUIController used for the New Tab page.
33 class NewTabUI : public content::WebUIController,
34                  public content::WebContentsObserver,
35                  public content::NotificationObserver {
36  public:
37   explicit NewTabUI(content::WebUI* web_ui);
38   ~NewTabUI() override;
39
40   static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
41
42   // Returns whether or not to show apps pages.
43   static bool ShouldShowApps();
44
45   // Returns whether or not "Discovery" in the NTP is Enabled.
46   static bool IsDiscoveryInNTPEnabled();
47
48   // Adds "url", "title", and "direction" keys on incoming dictionary, setting
49   // title as the url as a fallback on empty title.
50   static void SetUrlTitleAndDirection(base::DictionaryValue* dictionary,
51                                       const base::string16& title,
52                                       const GURL& gurl);
53
54   // Adds "full_name" and "full_name_direction" keys on incoming dictionary.
55   static void SetFullNameAndDirection(const base::string16& full_name,
56                                       base::DictionaryValue* dictionary);
57
58   // Returns a pointer to a NewTabUI if the WebUIController object is a new tab
59   // page.
60   static NewTabUI* FromWebUIController(content::WebUIController* ui);
61
62   // The current preference version.
63   static int current_pref_version() { return current_pref_version_; }
64
65   // WebUIController implementation:
66   void RenderViewCreated(content::RenderViewHost* render_view_host) override;
67   void RenderViewReused(content::RenderViewHost* render_view_host) override;
68
69   // WebContentsObserver implementation:
70   void WasHidden() override;
71
72   bool showing_sync_bubble() { return showing_sync_bubble_; }
73   void set_showing_sync_bubble(bool showing) { showing_sync_bubble_ = showing; }
74
75   class NewTabHTMLSource : public content::URLDataSource {
76    public:
77     explicit NewTabHTMLSource(Profile* profile);
78     ~NewTabHTMLSource() override;
79
80     // content::URLDataSource implementation.
81     std::string GetSource() const override;
82     void StartDataRequest(
83         const std::string& path,
84         int render_process_id,
85         int render_frame_id,
86         const content::URLDataSource::GotDataCallback& callback) override;
87     std::string GetMimeType(const std::string&) const override;
88     bool ShouldReplaceExistingSource() const override;
89     bool ShouldAddContentSecurityPolicy() const override;
90
91     // Adds |resource| to the source. |resource_id| is resource id or 0,
92     // which means return empty data set. |mime_type| is mime type of the
93     // resource.
94     void AddResource(const char* resource,
95                      const char* mime_type,
96                      int resource_id);
97
98    private:
99     // Pointer back to the original profile.
100     Profile* profile_;
101
102     // Maps resource files to mime types an resource ids.
103     std::map<std::string, std::pair<std::string, int> > resource_map_;
104
105     DISALLOW_COPY_AND_ASSIGN(NewTabHTMLSource);
106   };
107
108  private:
109   FRIEND_TEST_ALL_PREFIXES(NewTabUITest, UpdateUserPrefsVersion);
110
111   // content::NotificationObserver implementation.
112   void Observe(int type,
113                const content::NotificationSource& source,
114                const content::NotificationDetails& details) override;
115
116   // If |web_contents| has an NTP URL, emits a number of NTP statistics (like
117   // mouseovers counts) associated with |web_contents|, to be logged in UMA
118   // histograms.
119   void EmitNtpStatistics();
120
121   void OnShowBookmarkBarChanged();
122
123   void StartTimingPaint(content::RenderViewHost* render_view_host);
124   void PaintTimeout();
125
126   Profile* GetProfile() const;
127
128   content::NotificationRegistrar registrar_;
129
130   // The time when we started benchmarking.
131   base::TimeTicks start_;
132   // The last time we got a paint notification.
133   base::TimeTicks last_paint_;
134   // Scoping so we can be sure our timeouts don't outlive us.
135   base::OneShotTimer<NewTabUI> timer_;
136   // The preference version. This used for migrating prefs of the NTP.
137   static const int current_pref_version_ = 3;
138
139   // If the sync promo NTP bubble is being shown.
140   bool showing_sync_bubble_;
141
142   PrefChangeRegistrar pref_change_registrar_;
143
144   DISALLOW_COPY_AND_ASSIGN(NewTabUI);
145 };
146
147 #endif  // CHROME_BROWSER_UI_WEBUI_NTP_NEW_TAB_UI_H_