1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
// Copyright (c) 2012 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.
#include "chrome/browser/ui/webui/instant_ui.h"
#include "base/bind.h"
#include "chrome/browser/prefs/pref_service.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/webui/chrome_web_ui_data_source.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/url_constants.h"
#include "content/public/browser/web_ui.h"
#include "content/public/browser/web_ui_message_handler.h"
#include "grit/browser_resources.h"
namespace {
ChromeWebUIDataSource* CreateInstantHTMLSource() {
ChromeWebUIDataSource* source =
new ChromeWebUIDataSource(chrome::kChromeUIInstantHost);
source->set_json_path("strings.js");
source->add_resource_path("instant.js", IDR_INSTANT_JS);
source->add_resource_path("instant.css", IDR_INSTANT_CSS);
source->set_default_resource(IDR_INSTANT_HTML);
return source;
}
// This class receives JavaScript messages from the renderer.
// Note that the WebUI infrastructure runs on the UI thread, therefore all of
// this class's methods are expected to run on the UI thread.
class InstantUIMessageHandler
: public content::WebUIMessageHandler,
public base::SupportsWeakPtr<InstantUIMessageHandler> {
public:
InstantUIMessageHandler();
virtual ~InstantUIMessageHandler();
// WebUIMessageHandler implementation.
virtual void RegisterMessages() OVERRIDE;
static int slow_animation_scale_factor() {
return slow_animation_scale_factor_;
}
static bool show_search_provider_logo() {
return show_search_provider_logo_;
}
private:
void GetPreferenceValue(const base::ListValue* args);
void SetPreferenceValue(const base::ListValue* args);
// Slows down Instant animations by a time factor.
static int slow_animation_scale_factor_;
// True if search provider logo should be shown.
static bool show_search_provider_logo_;
DISALLOW_COPY_AND_ASSIGN(InstantUIMessageHandler);
};
// static
int InstantUIMessageHandler::slow_animation_scale_factor_ = 1;
bool InstantUIMessageHandler::show_search_provider_logo_ = false;
InstantUIMessageHandler::InstantUIMessageHandler() {}
InstantUIMessageHandler::~InstantUIMessageHandler() {}
void InstantUIMessageHandler::RegisterMessages() {
web_ui()->RegisterMessageCallback(
"getPreferenceValue",
base::Bind(&InstantUIMessageHandler::GetPreferenceValue,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"setPreferenceValue",
base::Bind(&InstantUIMessageHandler::SetPreferenceValue,
base::Unretained(this)));
}
void InstantUIMessageHandler::GetPreferenceValue(const base::ListValue* args) {
std::string pref_name;
if (!args->GetString(0, &pref_name)) return;
base::StringValue pref_name_value(pref_name);
if (pref_name == prefs::kInstantAnimationScaleFactor) {
double value = 0.0;
#if defined(TOOLKIT_VIEWS)
value = slow_animation_scale_factor_;
#endif
base::FundamentalValue arg(value);
web_ui()->CallJavascriptFunction(
"instantConfig.getPreferenceValueResult", pref_name_value, arg);
} else if (pref_name == prefs::kInstantShowSearchProviderLogo) {
base::FundamentalValue arg(show_search_provider_logo_);
web_ui()->CallJavascriptFunction(
"instantConfig.getPreferenceValueResult", pref_name_value, arg);
} else if (pref_name == prefs::kExperimentalZeroSuggestUrlPrefix) {
PrefService* prefs = Profile::FromWebUI(web_ui())->GetPrefs();
base::StringValue arg(prefs->GetString(pref_name.c_str()));
web_ui()->CallJavascriptFunction(
"instantConfig.getPreferenceValueResult", pref_name_value, arg);
}
}
void InstantUIMessageHandler::SetPreferenceValue(const base::ListValue* args) {
std::string pref_name;
if (!args->GetString(0, &pref_name)) return;
if (pref_name == prefs::kInstantAnimationScaleFactor) {
double value;
if (!args->GetDouble(1, &value))
return;
#if defined(TOOLKIT_VIEWS)
// Clamp to something reasonable.
value = std::max(0.1, std::min(value, 20.0));
slow_animation_scale_factor_ = static_cast<int>(value);
#else
NOTIMPLEMENTED();
#endif // defined(TOOLKIT_VIEWS)
} else if (pref_name == prefs::kInstantShowSearchProviderLogo) {
bool value;
if (!args->GetBoolean(1, &value))
return;
show_search_provider_logo_ = value;
} else if (pref_name == prefs::kExperimentalZeroSuggestUrlPrefix) {
std::string value;
if (!args->GetString(1, &value))
return;
PrefService* prefs = Profile::FromWebUI(web_ui())->GetPrefs();
prefs->SetString(pref_name.c_str(), value);
}
}
} // namespace
////////////////////////////////////////////////////////////////////////////////
// InstantUI
InstantUI::InstantUI(content::WebUI* web_ui) : WebUIController(web_ui) {
web_ui->AddMessageHandler(new InstantUIMessageHandler());
// Set up the chrome://instant/ source.
Profile* profile = Profile::FromWebUI(web_ui);
ChromeURLDataManager::AddDataSource(profile, CreateInstantHTMLSource());
}
// static
int InstantUI::GetSlowAnimationScaleFactor() {
return InstantUIMessageHandler::slow_animation_scale_factor();
}
// static
bool InstantUI::ShouldShowSearchProviderLogo() {
return InstantUIMessageHandler::show_search_provider_logo();
}
|