Skip to content

Commit 8bb8848

Browse files
committed
Firefox magic
1 parent 900aead commit 8bb8848

File tree

10 files changed

+316
-237
lines changed

10 files changed

+316
-237
lines changed

bt/app/browser.cpp

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#include "browser.h"
2-
#include "browser.h"
32
#include "match_rule.h"
4-
#include "browser.h"
53
#include <filesystem>
64
#include <algorithm>
75
#include "win32/shell.h"
@@ -10,8 +8,6 @@
108
#include "win32/user.h"
119
#include "str.h"
1210
#include <fmt/core.h>
13-
#include "discovery.h"
14-
#include "strings.h"
1511

1612
namespace fs = std::filesystem;
1713
using namespace std;
@@ -23,15 +19,10 @@ namespace bt {
2319
browser::browser(
2420
const std::string& id,
2521
const std::string& name,
26-
const std::string& open_cmd,
27-
bool is_system)
28-
: id{id}, name{ name }, open_cmd{ open_cmd },
29-
is_chromium{ discovery::is_chromium_id(id) }, is_firefox{ discovery::is_firefox_id(id) },
30-
is_system{ is_system },
31-
supports_frameless_windows{is_chromium}
22+
const std::string& open_cmd)
23+
: id{id}, name{ name }, open_cmd{ open_cmd }
3224
{
3325
str::trim(this->name);
34-
this->open_cmd = unmangle_open_cmd(this->open_cmd);
3526
}
3627

3728
bool operator==(const browser& b1, const browser& b2) {
@@ -50,7 +41,7 @@ namespace bt {
5041

5142
if(!icon_path.empty()) return icon_path;
5243

53-
if(!is_system) {
44+
if(!is_autodiscovered) {
5445
if(!instances.empty()) {
5546
return instances[0]->get_best_icon_path();
5647
}
@@ -148,10 +139,10 @@ namespace bt {
148139
vector<shared_ptr<browser>> r;
149140

150141
for (shared_ptr<browser> b_new : new_set) {
151-
// find corresponding browser
142+
// find corresponding browser by open_cmd
152143
auto b_old_it = std::find_if(
153144
old_set.begin(), old_set.end(),
154-
[b_new](shared_ptr<browser> el) { return el->id == b_new->id; });
145+
[b_new](shared_ptr<browser> el) { return el->open_cmd == b_new->open_cmd; });
155146

156147
if (b_old_it != old_set.end()) {
157148
shared_ptr<browser> b_old = *b_old_it;
@@ -187,7 +178,7 @@ namespace bt {
187178

188179
// add user browsers from the old set
189180
for(shared_ptr<browser> b_custom : old_set) {
190-
if(b_custom->is_system) continue;
181+
if(b_custom->is_autodiscovered) continue;
191182

192183
r.push_back(b_custom);
193184
}
@@ -230,23 +221,6 @@ namespace bt {
230221
return fs::path{open_cmd}.filename().replace_extension().string();
231222
}
232223

233-
std::string browser::unmangle_open_cmd(const std::string& open_cmd) {
234-
235-
string r = open_cmd;
236-
237-
// if open_cmd starts with quote ("), remove it, and substring up to first next quote
238-
if(r.starts_with("\"")) {
239-
r = r.substr(1);
240-
241-
size_t pos = r.find("\"");
242-
if(pos != string::npos) {
243-
r = r.substr(0, pos);
244-
}
245-
}
246-
247-
return r;
248-
}
249-
250224
browser_instance::browser_instance(
251225
shared_ptr<browser> b,
252226
const std::string& id,
@@ -344,7 +318,7 @@ namespace bt {
344318
std::string browser_instance::get_best_display_name() const {
345319
if(is_incognito) return fmt::format("Private {}", b->name);
346320

347-
if(b->is_system && is_singular()) return b->name;
321+
if(b->is_autodiscovered && is_singular()) return b->name;
348322

349323
return name;
350324
}

bt/app/browser.h

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ namespace bt {
1010
class browser_instance;
1111
class browser_match_result;
1212

13+
enum class browser_engine {
14+
unknown,
15+
chromium,
16+
gecko
17+
};
18+
1319
class browser {
1420
public:
1521

@@ -18,20 +24,18 @@ namespace bt {
1824
browser(
1925
const std::string& id,
2026
const std::string& name,
21-
const std::string& open_cmd,
22-
bool is_system = true);
27+
const std::string& open_cmd);
2328

2429
std::string id;
2530
std::string name;
2631
std::string open_cmd;
27-
bool is_chromium;
28-
bool is_firefox;
32+
browser_engine engine{browser_engine::unknown};
2933
int sort_order{0};
3034

3135
/**
3236
* @brief when true, this browser is part of the system i.e not a user defined one.
3337
*/
34-
bool is_system;
38+
bool is_autodiscovered{false};
3539

3640
/**
3741
* @brief Whether to hide this browser from UI.
@@ -50,18 +54,20 @@ namespace bt {
5054

5155
std::vector<std::shared_ptr<browser_instance>> instances;
5256

53-
// instance id, non persistent, used in discovery process
54-
std::string disco_instance_id;
57+
/**
58+
* @brief Instance ID, used by Firefox. Not persisted as it's not required after discovery.
59+
*/
60+
std::string instance_id;
5561

5662
// UI helper properties
5763
bool ui_test_url_matches{false};
5864
float ui_icon_size_anim{0.0f};
5965

6066
size_t get_total_rule_count() const;
6167

62-
bool get_supports_frameless_windows() const { return supports_frameless_windows; }
68+
bool get_supports_frameless_windows() const { return engine == browser_engine::chromium; }
6369

64-
bool is_wellknown() const { return is_chromium || is_firefox; }
70+
bool is_wellknown() const { return engine != browser_engine::unknown; }
6571

6672
bool is_msstore() const { return open_cmd.starts_with(UwpCmdPrefix); }
6773

@@ -111,11 +117,7 @@ namespace bt {
111117

112118
private:
113119

114-
const bool supports_frameless_windows;
115-
116120
static std::string get_image_name(const std::string& open_cmd);
117-
118-
static std::string unmangle_open_cmd(const std::string& open_cmd);
119121
};
120122

121123
class browser_instance {

bt/app/config.cpp

Lines changed: 71 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
#include "config.h"
2-
#include "config.h"
3-
#include "config.h"
42
#include "../globals.h"
53
#include "win32/reg.h"
64
#include "win32/ole32.h"
@@ -9,6 +7,7 @@
97
#include <fmt/core.h>
108
#include <filesystem>
119
#include "fss.h"
10+
#include "hashing.h"
1211

1312
using namespace std;
1413
namespace fs = std::filesystem;
@@ -28,11 +27,15 @@ namespace bt {
2827
#define ToastVisibleSecsKey "toast_visible_secs"
2928
#define ToastBorderWidthKey "toast_border_width"
3029
#define IconOverlayKey "icon_overlay"
30+
#define BrowserEngine "engine"
31+
#define IsAutodiscovered "auto"
32+
#define IsIncognito "incognito"
3133
#define LogRuleHitsKey "log_rule_hits"
3234
#define LogAppKey "log_app"
3335
#define PersistPopularityKey "persist_popularity"
3436
#define ShowHiddenBrowsersKey "browsers_show_hidden"
3537
#define DiscoverFirefoxContainersKey "firefox_containers"
38+
#define DiscoverClassicFirefoxProfilesKey "firefox_classic_profiles"
3639
#define UnshortEnabledKey "unshort_enabled"
3740
#define PickerSectionName "picker"
3841
#define PickerOnKeyCS "on_key_cs"
@@ -127,6 +130,47 @@ namespace bt {
127130
cfg.set_value("rule", rules, section);
128131
}
129132

133+
for(auto ssn : section_names) {
134+
if(ssn.starts_with("browser:")) {
135+
vector<string> parts = str::split(ssn, ":");
136+
137+
if(parts.size() == 2) { // browser
138+
139+
string browser_id = parts[1];
140+
141+
// browser subtype (5.5.0)
142+
string subtype = cfg.get_value("subtype", ssn);
143+
if(!subtype.empty()) {
144+
is_dirty = true;
145+
cfg.delete_key("subtype", ssn);
146+
if(subtype == "firefox") {
147+
cfg.set_value("engine", "gecko", ssn);
148+
cfg.set_value("auto", true, ssn);
149+
} else if(subtype == "chromium") {
150+
cfg.set_value("engine", "chromium", ssn);
151+
cfg.set_value("auto", true, ssn);
152+
} else if(subtype == "user") {
153+
// no keys to add
154+
}
155+
}
156+
157+
} else if(parts.size() == 3) { // profile
158+
159+
string profile_id = parts[2];
160+
161+
// profile subtype and incognito (5.5.0)
162+
string subtype = cfg.get_value("subtype", ssn);
163+
if(!subtype.empty()) {
164+
is_dirty = true;
165+
cfg.delete_key("subtype", ssn);
166+
if(subtype == "incognito") {
167+
cfg.set_value("incognito", true, ssn);
168+
}
169+
}
170+
}
171+
}
172+
}
173+
130174
if(is_dirty) {
131175
cfg.commit();
132176
}
@@ -136,6 +180,7 @@ namespace bt {
136180
string v;
137181

138182
show_hidden_browsers = cfg.get_bool_value(ShowHiddenBrowsersKey, true);
183+
discover_classic_firefox_profiles = cfg.get_bool_value(DiscoverClassicFirefoxProfilesKey, false);
139184
discover_firefox_containers = cfg.get_bool_value(DiscoverFirefoxContainersKey, false);
140185

141186
theme_id = cfg.get_value("theme");
@@ -182,6 +227,7 @@ namespace bt {
182227

183228
void config::commit() {
184229
cfg.set_value(ShowHiddenBrowsersKey, show_hidden_browsers);
230+
cfg.set_value(DiscoverClassicFirefoxProfilesKey, discover_classic_firefox_profiles);
185231
cfg.set_value(DiscoverFirefoxContainersKey, discover_firefox_containers);
186232
cfg.set_value("theme", theme_id == "follow_os" ? "" : theme_id);
187233
cfg.set_value(LogRuleHitsKey, log_rule_hits);
@@ -245,18 +291,11 @@ namespace bt {
245291
cfg.set_value(Icon, b->icon_path, section);
246292
cfg.set_value(ItemSortOrder, b->sort_order, section);
247293
cfg.set_value(DataPath, b->data_path, section);
248-
249-
string subtype;
250-
if(b->is_system) {
251-
if(b->is_firefox) subtype = "firefox";
252-
else if(b->is_chromium) subtype = "chromium";
253-
} else {
254-
subtype = "user";
255-
}
256-
cfg.set_value("subtype", subtype, section);
294+
cfg.set_value(BrowserEngine, browser_engine_to_string(b->engine), section);
295+
cfg.set_value(IsAutodiscovered, b->is_autodiscovered, section);
257296

258297
// singular user instance
259-
if(!b->is_system && b->instances.size() == 1) {
298+
if(!b->is_autodiscovered && b->instances.size() == 1) {
260299
auto instance = b->instances[0];
261300
cfg.set_value("arg", instance->launch_arg, section);
262301
cfg.set_value("rule", instance->get_rules_as_text_clean(), section);
@@ -273,7 +312,7 @@ namespace bt {
273312
cfg.set_value("user_arg", bi->user_arg, section);
274313
cfg.set_value("icon", bi->icon_path, section);
275314
cfg.set_value("user_icon", bi->user_icon_path, section);
276-
cfg.set_value("subtype", bi->is_incognito ? "incognito" : "", section);
315+
cfg.set_value(IsIncognito, bi->is_incognito, section);
277316
cfg.set_value(IsHidden, bi->is_hidden, section);
278317
cfg.set_value("rule", bi->get_rules_as_text_clean(), section);
279318
cfg.set_value(ItemSortOrder, bi->sort_order, section);
@@ -291,24 +330,22 @@ namespace bt {
291330
vector<string> parts = str::split(bsn, ":");
292331
if(parts[0] != BrowserPrefix || parts.size() != 2) continue;
293332

294-
string subtype = cfg.get_value("subtype", bsn);
295333
string b_id = parts[1];
296334

297335
auto b = make_shared<browser>(
298336
b_id,
299337
cfg.get_value("name", bsn),
300-
cfg.get_value("cmd", bsn),
301-
subtype != "user"
338+
cfg.get_value("cmd", bsn)
302339
);
303340

304-
b->is_firefox = subtype == "firefox";
305-
b->is_chromium = subtype == "chromium";
341+
b->engine = to_browser_engine(cfg.get_value(BrowserEngine, bsn));
306342
b->is_hidden = cfg.get_bool_value(IsHidden, false, bsn);
307343
b->icon_path = cfg.get_value(Icon, bsn);
308344
b->sort_order = cfg.get_int_value(ItemSortOrder, 0, bsn);
309345
b->data_path = cfg.get_value(DataPath, bsn);
346+
b->is_autodiscovered = cfg.get_bool_value(IsAutodiscovered, false, bsn);
310347

311-
if(b->is_system) {
348+
if(b->is_autodiscovered) {
312349

313350
// profiles, if any
314351
string profile_prefix = fmt::format("{}:{}", BrowserPrefix, b_id);
@@ -317,7 +354,6 @@ namespace bt {
317354
if(parts[0] != BrowserPrefix || parts.size() != 3 || parts[1] != b_id) continue;
318355

319356
string p_sys_name = parts[2];
320-
string p_subtype = cfg.get_value("subtype", ssn);
321357

322358
auto bi = make_shared<browser_instance>(
323359
b,
@@ -328,7 +364,7 @@ namespace bt {
328364

329365
bi->user_icon_path = cfg.get_value("user_icon", ssn);
330366
bi->user_arg = cfg.get_value("user_arg", ssn);
331-
bi->is_incognito = p_subtype == "incognito";
367+
bi->is_incognito = cfg.get_bool_value(IsIncognito, false, ssn);
332368
bi->is_hidden = cfg.get_bool_value(IsHidden, false, ssn);
333369
bi->sort_order = cfg.get_int_value(ItemSortOrder, 0, ssn);
334370

@@ -382,4 +418,18 @@ namespace bt {
382418
return icon_overlay_mode::profile_on_browser;
383419
}
384420

421+
std::string config::browser_engine_to_string(browser_engine engine) {
422+
switch(engine) {
423+
case browser_engine::chromium: return "chromium";
424+
case browser_engine::gecko: return "gecko";
425+
default: return "";
426+
}
427+
}
428+
429+
browser_engine config::to_browser_engine(const std::string& name) {
430+
if(name == "chromium") return browser_engine::chromium;
431+
if(name == "gecko") return browser_engine::gecko;
432+
return browser_engine::unknown;
433+
}
434+
385435
}

bt/app/config.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ namespace bt {
2626
public:
2727
// whether to show hidden browsers in the configuration list
2828
bool show_hidden_browsers{true};
29+
bool discover_classic_firefox_profiles{false};
2930
bool discover_firefox_containers{false};
3031
std::string theme_id;
3132
bool log_rule_hits{true};
@@ -86,6 +87,8 @@ namespace bt {
8687
// conversion functions
8788
static std::string icon_overlay_mode_to_string(icon_overlay_mode mode);
8889
static icon_overlay_mode to_icon_overlay_mode(const std::string& name);
90+
static std::string browser_engine_to_string(browser_engine engine);
91+
static browser_engine to_browser_engine(const std::string& name);
8992

9093
static std::string get_data_file_path(const std::string& name);
9194

0 commit comments

Comments
 (0)