Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(109)

Side by Side Diff: chrome/browser/ui/webui/instant_ui.cc

Issue 12386019: Instant: Use only one hidden WebContents per profile. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/ui/webui/instant_ui.h" 5 #include "chrome/browser/ui/webui/instant_ui.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/prefs/pref_service.h"
9 #include "base/stringprintf.h" 8 #include "base/stringprintf.h"
10 #include "base/time.h" 9 #include "base/values.h"
11 #include "chrome/browser/instant/instant_controller.h" 10 #include "chrome/browser/instant/instant_service.h"
11 #include "chrome/browser/instant/instant_service_factory.h"
12 #include "chrome/browser/profiles/profile.h" 12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/ui/browser.h"
14 #include "chrome/browser/ui/browser_finder.h"
15 #include "chrome/browser/ui/browser_instant_controller.h"
16 #include "chrome/common/pref_names.h"
17 #include "chrome/common/url_constants.h" 13 #include "chrome/common/url_constants.h"
18 #include "components/user_prefs/pref_registry_syncable.h"
19 #include "content/public/browser/web_ui.h" 14 #include "content/public/browser/web_ui.h"
20 #include "content/public/browser/web_ui_data_source.h" 15 #include "content/public/browser/web_ui_data_source.h"
21 #include "content/public/browser/web_ui_message_handler.h" 16 #include "content/public/browser/web_ui_message_handler.h"
22 #include "grit/browser_resources.h" 17 #include "grit/browser_resources.h"
23 18
24 namespace { 19 namespace {
25 20
26 content::WebUIDataSource* CreateInstantHTMLSource() { 21 content::WebUIDataSource* CreateInstantHTMLSource() {
27 content::WebUIDataSource* source = 22 content::WebUIDataSource* source =
28 content::WebUIDataSource::Create(chrome::kChromeUIInstantHost); 23 content::WebUIDataSource::Create(chrome::kChromeUIInstantHost);
29 source->SetJsonPath("strings.js");
30 source->AddResourcePath("instant.js", IDR_INSTANT_JS); 24 source->AddResourcePath("instant.js", IDR_INSTANT_JS);
31 source->AddResourcePath("instant.css", IDR_INSTANT_CSS); 25 source->AddResourcePath("instant.css", IDR_INSTANT_CSS);
32 source->SetDefaultResource(IDR_INSTANT_HTML); 26 source->SetDefaultResource(IDR_INSTANT_HTML);
33 return source; 27 return source;
34 } 28 }
35 29
36 std::string FormatTime(int64 time) { 30 std::string FormatTime(int64 time) {
37 base::Time::Exploded exploded; 31 base::Time::Exploded exploded;
38 base::Time::FromInternalValue(time).UTCExplode(&exploded); 32 base::Time::FromInternalValue(time).UTCExplode(&exploded);
39 return base::StringPrintf("%04d-%02d-%02d %02d:%02d:%02d.%03d", 33 return base::StringPrintf("%04d-%02d-%02d %02d:%02d:%02d.%03d",
40 exploded.year, exploded.month, exploded.day_of_month, 34 exploded.year, exploded.month, exploded.day_of_month,
41 exploded.hour, exploded.minute, exploded.second, exploded.millisecond); 35 exploded.hour, exploded.minute, exploded.second, exploded.millisecond);
42 } 36 }
43 37
44 // This class receives JavaScript messages from the renderer. 38 // This class receives JavaScript messages from the renderer. Note that the
45 // Note that the WebUI infrastructure runs on the UI thread, therefore all of 39 // WebUI infrastructure runs on the UI thread, therefore all of this class's
46 // this class's methods are expected to run on the UI thread. 40 // methods are expected to run on the UI thread.
47 class InstantUIMessageHandler 41 class InstantUIMessageHandler : public content::WebUIMessageHandler {
48 : public content::WebUIMessageHandler,
49 public base::SupportsWeakPtr<InstantUIMessageHandler> {
50 public: 42 public:
51 InstantUIMessageHandler(); 43 InstantUIMessageHandler();
52 virtual ~InstantUIMessageHandler(); 44 virtual ~InstantUIMessageHandler();
53 45
54 // WebUIMessageHandler implementation. 46 private:
47 // Overridden from content::WebUIMessageHandler:
55 virtual void RegisterMessages() OVERRIDE; 48 virtual void RegisterMessages() OVERRIDE;
56 49
57 private: 50 void GetDebugEvents(const base::ListValue* value);
58 void GetPreferenceValue(const base::ListValue* args); 51 void ClearDebugEvents(const base::ListValue* value);
59 void SetPreferenceValue(const base::ListValue* args);
60 void GetDebugInfo(const base::ListValue* value);
61 void ClearDebugInfo(const base::ListValue* value);
62 52
63 DISALLOW_COPY_AND_ASSIGN(InstantUIMessageHandler); 53 DISALLOW_COPY_AND_ASSIGN(InstantUIMessageHandler);
64 }; 54 };
65 55
66 InstantUIMessageHandler::InstantUIMessageHandler() {} 56 InstantUIMessageHandler::InstantUIMessageHandler() {
57 }
67 58
68 InstantUIMessageHandler::~InstantUIMessageHandler() {} 59 InstantUIMessageHandler::~InstantUIMessageHandler() {
60 }
69 61
70 void InstantUIMessageHandler::RegisterMessages() { 62 void InstantUIMessageHandler::RegisterMessages() {
71 web_ui()->RegisterMessageCallback( 63 web_ui()->RegisterMessageCallback(
72 "getPreferenceValue", 64 "getDebugEvents",
73 base::Bind(&InstantUIMessageHandler::GetPreferenceValue, 65 base::Bind(&InstantUIMessageHandler::GetDebugEvents,
74 base::Unretained(this))); 66 base::Unretained(this)));
75 web_ui()->RegisterMessageCallback( 67 web_ui()->RegisterMessageCallback(
76 "setPreferenceValue", 68 "clearDebugEvents",
77 base::Bind(&InstantUIMessageHandler::SetPreferenceValue, 69 base::Bind(&InstantUIMessageHandler::ClearDebugEvents,
78 base::Unretained(this)));
79 web_ui()->RegisterMessageCallback(
80 "getDebugInfo",
81 base::Bind(&InstantUIMessageHandler::GetDebugInfo,
82 base::Unretained(this)));
83 web_ui()->RegisterMessageCallback(
84 "clearDebugInfo",
85 base::Bind(&InstantUIMessageHandler::ClearDebugInfo,
86 base::Unretained(this))); 70 base::Unretained(this)));
87 } 71 }
88 72
89 void InstantUIMessageHandler::GetPreferenceValue(const base::ListValue* args) { 73 void InstantUIMessageHandler::GetDebugEvents(
90 std::string pref_name; 74 const base::ListValue* /* args */) {
91 if (!args->GetString(0, &pref_name)) return; 75 InstantService* service =
76 InstantServiceFactory::GetForProfile(Profile::FromWebUI(web_ui()));
77 if (!service)
78 return;
92 79
93 base::StringValue pref_name_value(pref_name); 80 base::ListValue events;
94 if (pref_name == prefs::kInstantUIZeroSuggestUrlPrefix) { 81 const InstantService::DebugEventsList& source = service->debug_events();
95 PrefService* prefs = Profile::FromWebUI(web_ui())->GetPrefs(); 82
96 base::StringValue arg(prefs->GetString(pref_name.c_str())); 83 for (InstantService::DebugEventsList::const_iterator i = source.begin();
97 web_ui()->CallJavascriptFunction( 84 i != source.end(); ++i) {
98 "instantConfig.getPreferenceValueResult", pref_name_value, arg); 85 scoped_ptr<base::DictionaryValue> event(new base::DictionaryValue());
86 event->SetString("time", FormatTime(i->first));
87 event->SetString("text", i->second);
88 events.Append(event.release());
99 } 89 }
90
91 web_ui()->CallJavascriptFunction("instantConfig.setDebugEvents", events);
100 } 92 }
101 93
102 void InstantUIMessageHandler::SetPreferenceValue(const base::ListValue* args) { 94 void InstantUIMessageHandler::ClearDebugEvents(
103 std::string pref_name; 95 const base::ListValue* /* args */) {
104 if (!args->GetString(0, &pref_name)) return; 96 InstantService* service =
105 97 InstantServiceFactory::GetForProfile(Profile::FromWebUI(web_ui()));
106 if (pref_name == prefs::kInstantUIZeroSuggestUrlPrefix) { 98 if (service)
107 std::string value; 99 service->ClearDebugEvents();
108 if (!args->GetString(1, &value))
109 return;
110 PrefService* prefs = Profile::FromWebUI(web_ui())->GetPrefs();
111 prefs->SetString(pref_name.c_str(), value);
112 }
113 }
114
115 void InstantUIMessageHandler::GetDebugInfo(const base::ListValue* args) {
116 #if !defined(OS_ANDROID)
117 typedef std::pair<int64, std::string> DebugEvent;
118
119 if (!web_ui()->GetWebContents())
120 return;
121 Browser* browser = chrome::FindBrowserWithWebContents(
122 web_ui()->GetWebContents());
123 if (!browser || !browser->instant_controller())
124 return;
125
126 InstantController* instant = browser->instant_controller()->instant();
127 const std::list<DebugEvent>& events = instant->debug_events();
128
129 base::DictionaryValue data;
130 base::ListValue* entries = new base::ListValue();
131 for (std::list<DebugEvent>::const_iterator it = events.begin();
132 it != events.end(); ++it) {
133 base::DictionaryValue* entry = new base::DictionaryValue();
134 entry->SetString("time", FormatTime(it->first));
135 entry->SetString("text", it->second);
136 entries->Append(entry);
137 }
138 data.Set("entries", entries);
139
140 web_ui()->CallJavascriptFunction("instantConfig.getDebugInfoResult", data);
141 #endif
142 }
143
144 void InstantUIMessageHandler::ClearDebugInfo(const base::ListValue* args) {
145 #if !defined(OS_ANDROID)
146 if (!web_ui()->GetWebContents())
147 return;
148 Browser* browser = chrome::FindBrowserWithWebContents(
149 web_ui()->GetWebContents());
150 if (!browser || !browser->instant_controller())
151 return;
152
153 browser->instant_controller()->instant()->ClearDebugEvents();
154 #endif
155 } 100 }
156 101
157 } // namespace 102 } // namespace
158 103
159 //////////////////////////////////////////////////////////////////////////////// 104 ////////////////////////////////////////////////////////////////////////////////
160 // InstantUI 105 // InstantUI
161 106
162 InstantUI::InstantUI(content::WebUI* web_ui) : WebUIController(web_ui) { 107 InstantUI::InstantUI(content::WebUI* web_ui) : WebUIController(web_ui) {
163 web_ui->AddMessageHandler(new InstantUIMessageHandler()); 108 web_ui->AddMessageHandler(new InstantUIMessageHandler());
164 109
165 // Set up the chrome://instant/ source. 110 // Set up the chrome://instant/ source.
166 Profile* profile = Profile::FromWebUI(web_ui); 111 Profile* profile = Profile::FromWebUI(web_ui);
167 content::WebUIDataSource::Add(profile, CreateInstantHTMLSource()); 112 content::WebUIDataSource::Add(profile, CreateInstantHTMLSource());
168 } 113 }
169 114
170 // static 115 InstantUI::~InstantUI() {
171 void InstantUI::RegisterUserPrefs(PrefRegistrySyncable* registry) {
172 registry->RegisterStringPref(prefs::kInstantUIZeroSuggestUrlPrefix, "",
173 PrefRegistrySyncable::UNSYNCABLE_PREF);
174 } 116 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698